diff --git a/lib/ht.py b/lib/ht.py index 85d3b82d8ef9312a2a1401f06426b206a00efcd3..eba209ac35e306de70ebb5dcff8caccbdaa645aa 100644 --- a/lib/ht.py +++ b/lib/ht.py @@ -187,6 +187,14 @@ def TNone(val): return val is None +@WithDesc("ValueNone") +def TValueNone(val): + """Checks if the given value is L{constants.VALUE_NONE}. + + """ + return val == constants.VALUE_NONE + + @WithDesc("Boolean") def TBool(val): """Checks if the given value is a boolean. @@ -319,6 +327,13 @@ def TMaybe(test): return TOr(TNone, test) +def TMaybeValueNone(test): + """Used for unsetting values. + + """ + return TMaybe(TOr(TValueNone, test)) + + # Type aliases #: a non-empty string diff --git a/lib/opcodes.py b/lib/opcodes.py index 959d56b0e212c1d8568f32797fd9652d1a4e015a..0721eab60adb3d1e9c1a49fcca4e0462d8d7e2b4 100644 --- a/lib/opcodes.py +++ b/lib/opcodes.py @@ -348,10 +348,6 @@ _PStorageType = ("storage_type", ht.NoDefault, _CheckStorageType, _CheckNetworkType = ht.TElemOf(constants.NETWORK_VALID_TYPES) -#: Network type parameter -_PNetworkType = ("network_type", None, ht.TMaybe(_CheckNetworkType), - "Network type") - @ht.WithDesc("IPv4 network") def _CheckCIDRNetNotation(value): @@ -2024,7 +2020,7 @@ class OpNetworkAdd(OpCode): OP_DSC_FIELD = "network_name" OP_PARAMS = [ _PNetworkName, - _PNetworkType, + ("network_type", None, ht.TMaybe(_CheckNetworkType), "Network type"), ("network", ht.NoDefault, _TIpNetwork4, "IPv4 subnet"), ("gateway", None, ht.TMaybe(_TIpAddress4), "IPv4 gateway"), ("network6", None, ht.TMaybe(_TIpNetwork6), "IPv6 subnet"), @@ -2056,11 +2052,12 @@ class OpNetworkSetParams(OpCode): OP_DSC_FIELD = "network_name" OP_PARAMS = [ _PNetworkName, - _PNetworkType, - ("gateway", None, ht.TMaybe(_TIpAddress4), "IPv4 gateway"), - ("network6", None, ht.TMaybe(_TIpNetwork6), "IPv6 subnet"), - ("gateway6", None, ht.TMaybe(_TIpAddress6), "IPv6 gateway"), - ("mac_prefix", None, ht.TMaybeString, + ("network_type", None, ht.TMaybeValueNone(_CheckNetworkType), + "Network type"), + ("gateway", None, ht.TMaybeValueNone(_TIpAddress4), "IPv4 gateway"), + ("network6", None, ht.TMaybeValueNone(_TIpNetwork6), "IPv6 subnet"), + ("gateway6", None, ht.TMaybeValueNone(_TIpAddress6), "IPv6 gateway"), + ("mac_prefix", None, ht.TMaybeValueNone(ht.TString), "MAC address prefix that overrides cluster one"), ("add_reserved_ips", None, _TMaybeAddr4List, "Which external IP addresses to reserve"), diff --git a/test/ganeti.ht_unittest.py b/test/ganeti.ht_unittest.py index 8e7b910f101838b1a1067cb47f18d6b041501812..e99a810296f4f5467aa6408ef8b7b908ce136cfe 100755 --- a/test/ganeti.ht_unittest.py +++ b/test/ganeti.ht_unittest.py @@ -23,6 +23,7 @@ import unittest +from ganeti import constants from ganeti import ht import testutils @@ -282,6 +283,17 @@ class TestTypeChecks(unittest.TestCase): self.assertFalse(fn(None)) + def testMaybeValueNone(self): + fn = ht.TMaybeValueNone(ht.TInt) + + self.assertTrue(fn(None)) + self.assertTrue(fn(0)) + self.assertTrue(fn(constants.VALUE_NONE)) + + self.assertFalse(fn("")) + self.assertFalse(fn([])) + self.assertFalse(fn(constants.VALUE_DEFAULT)) + if __name__ == "__main__": testutils.GanetiTestProgram()