Commit 94c42103 authored by Klaus Aehlig's avatar Klaus Aehlig
Browse files

Add VTypeFloat



...in order not to have to declare floating point
values as VTypeInt and rely on the sloppiness of
the JSON specification to not distinguish between
integers and floating point numbers.
Signed-off-by: default avatarKlaus Aehlig <aehlig@google.com>
Reviewed-by: default avatarPetr Pudlak <pudlak@google.com>
parent 946c0e2b
...@@ -353,6 +353,7 @@ _QFT_NAMES = { ...@@ -353,6 +353,7 @@ _QFT_NAMES = {
constants.QFT_TEXT: "Text", constants.QFT_TEXT: "Text",
constants.QFT_BOOL: "Boolean", constants.QFT_BOOL: "Boolean",
constants.QFT_NUMBER: "Number", constants.QFT_NUMBER: "Number",
constants.QFT_NUMBER_FLOAT: "Floating-point number",
constants.QFT_UNIT: "Storage size", constants.QFT_UNIT: "Storage size",
constants.QFT_TIMESTAMP: "Timestamp", constants.QFT_TIMESTAMP: "Timestamp",
constants.QFT_OTHER: "Custom", constants.QFT_OTHER: "Custom",
......
...@@ -68,8 +68,8 @@ from ganeti import jstore ...@@ -68,8 +68,8 @@ from ganeti import jstore
from ganeti.hypervisor import hv_base from ganeti.hypervisor import hv_base
from ganeti.constants import (QFT_UNKNOWN, QFT_TEXT, QFT_BOOL, QFT_NUMBER, from ganeti.constants import (QFT_UNKNOWN, QFT_TEXT, QFT_BOOL, QFT_NUMBER,
QFT_UNIT, QFT_TIMESTAMP, QFT_OTHER, QFT_NUMBER_FLOAT, QFT_UNIT, QFT_TIMESTAMP,
RS_NORMAL, RS_UNKNOWN, RS_NODATA, QFT_OTHER, RS_NORMAL, RS_UNKNOWN, RS_NODATA,
RS_UNAVAIL, RS_OFFLINE) RS_UNAVAIL, RS_OFFLINE)
(NETQ_CONFIG, (NETQ_CONFIG,
...@@ -127,6 +127,7 @@ _VERIFY_FN = { ...@@ -127,6 +127,7 @@ _VERIFY_FN = {
QFT_TEXT: ht.TString, QFT_TEXT: ht.TString,
QFT_BOOL: ht.TBool, QFT_BOOL: ht.TBool,
QFT_NUMBER: ht.TInt, QFT_NUMBER: ht.TInt,
QFT_NUMBER_FLOAT: ht.TFloat,
QFT_UNIT: ht.TInt, QFT_UNIT: ht.TInt,
QFT_TIMESTAMP: ht.TNumber, QFT_TIMESTAMP: ht.TNumber,
QFT_OTHER: lambda _: True, QFT_OTHER: lambda _: True,
...@@ -154,6 +155,7 @@ _VTToQFT = { ...@@ -154,6 +155,7 @@ _VTToQFT = {
constants.VTYPE_BOOL: QFT_BOOL, constants.VTYPE_BOOL: QFT_BOOL,
constants.VTYPE_SIZE: QFT_UNIT, constants.VTYPE_SIZE: QFT_UNIT,
constants.VTYPE_INT: QFT_NUMBER, constants.VTYPE_INT: QFT_NUMBER,
constants.VTYPE_FLOAT: QFT_NUMBER_FLOAT,
} }
_SERIAL_NO_DOC = "%s object serial number, incremented on each modification" _SERIAL_NO_DOC = "%s object serial number, incremented on each modification"
......
...@@ -134,6 +134,12 @@ def ForceDictType(target, key_types, allowed_values=None): ...@@ -134,6 +134,12 @@ def ForceDictType(target, key_types, allowed_values=None):
except (ValueError, TypeError): except (ValueError, TypeError):
msg = "'%s' (value %s) is not a valid integer" % (key, target[key]) msg = "'%s' (value %s) is not a valid integer" % (key, target[key])
raise errors.TypeEnforcementError(msg) raise errors.TypeEnforcementError(msg)
elif ktype == constants.VTYPE_FLOAT:
try:
target[key] = float(target[key])
except (ValueError, TypeError):
msg = "'%s' (value %s) is not a valid float" % (key, target[key])
raise errors.TypeEnforcementError(msg)
def ValidateServiceName(name): def ValidateServiceName(name):
......
...@@ -1435,6 +1435,9 @@ vtypeBool = VTypeBool ...@@ -1435,6 +1435,9 @@ vtypeBool = VTypeBool
vtypeInt :: VType vtypeInt :: VType
vtypeInt = VTypeInt vtypeInt = VTypeInt
vtypeFloat :: VType
vtypeFloat = VTypeFloat
vtypeMaybeString :: VType vtypeMaybeString :: VType
vtypeMaybeString = VTypeMaybeString vtypeMaybeString = VTypeMaybeString
...@@ -3476,6 +3479,9 @@ qftBool = "bool" ...@@ -3476,6 +3479,9 @@ qftBool = "bool"
qftNumber :: String qftNumber :: String
qftNumber = "number" qftNumber = "number"
qftNumberFloat :: String
qftNumberFloat = "float"
qftOther :: String qftOther :: String
qftOther = "other" qftOther = "other"
...@@ -3495,6 +3501,7 @@ qftAll :: FrozenSet String ...@@ -3495,6 +3501,7 @@ qftAll :: FrozenSet String
qftAll = qftAll =
ConstantUtils.mkSet [qftBool, ConstantUtils.mkSet [qftBool,
qftNumber, qftNumber,
qftNumberFloat,
qftOther, qftOther,
qftText, qftText,
qftTimestamp, qftTimestamp,
......
...@@ -75,6 +75,7 @@ vTypeToQFT VTypeMaybeString = QFTOther ...@@ -75,6 +75,7 @@ vTypeToQFT VTypeMaybeString = QFTOther
vTypeToQFT VTypeBool = QFTBool vTypeToQFT VTypeBool = QFTBool
vTypeToQFT VTypeSize = QFTUnit vTypeToQFT VTypeSize = QFTUnit
vTypeToQFT VTypeInt = QFTNumber vTypeToQFT VTypeInt = QFTNumber
vTypeToQFT VTypeFloat = QFTNumberFloat
-- * Result helpers -- * Result helpers
......
...@@ -99,6 +99,7 @@ $(declareSADT "FieldType" ...@@ -99,6 +99,7 @@ $(declareSADT "FieldType"
, ("QFTText", 'C.qftText ) , ("QFTText", 'C.qftText )
, ("QFTBool", 'C.qftBool ) , ("QFTBool", 'C.qftBool )
, ("QFTNumber", 'C.qftNumber ) , ("QFTNumber", 'C.qftNumber )
, ("QFTNumberFloat", 'C.qftNumberFloat )
, ("QFTUnit", 'C.qftUnit ) , ("QFTUnit", 'C.qftUnit )
, ("QFTTimestamp", 'C.qftTimestamp ) , ("QFTTimestamp", 'C.qftTimestamp )
, ("QFTOther", 'C.qftOther ) , ("QFTOther", 'C.qftOther )
......
...@@ -768,6 +768,7 @@ $(THH.declareLADT ''String "VType" ...@@ -768,6 +768,7 @@ $(THH.declareLADT ''String "VType"
, ("VTypeBool", "bool") , ("VTypeBool", "bool")
, ("VTypeSize", "size") , ("VTypeSize", "size")
, ("VTypeInt", "int") , ("VTypeInt", "int")
, ("VTypeFloat", "float")
]) ])
$(THH.makeJSONInstance ''VType) $(THH.makeJSONInstance ''VType)
......
...@@ -122,6 +122,7 @@ checkResultType fdef (ResultEntry RSNormal (Just v)) = ...@@ -122,6 +122,7 @@ checkResultType fdef (ResultEntry RSNormal (Just v)) =
(QFTText , JSString {}) -> passTest (QFTText , JSString {}) -> passTest
(QFTBool , JSBool {}) -> passTest (QFTBool , JSBool {}) -> passTest
(QFTNumber , JSRational {}) -> passTest (QFTNumber , JSRational {}) -> passTest
(QFTNumberFloat , JSRational {}) -> passTest
(QFTTimestamp , JSRational {}) -> passTest (QFTTimestamp , JSRational {}) -> passTest
(QFTUnit , JSRational {}) -> passTest (QFTUnit , JSRational {}) -> passTest
(QFTOther , _) -> passTest -- meh, QFT not precise... (QFTOther , _) -> passTest -- meh, QFT not precise...
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment