1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31 """Verification helpers for the configuration object."""
32
33 from ganeti import constants
34 from ganeti import errors
35 from ganeti import objects
36 from ganeti import utils
37
38
51
52
53 -def VerifyType(owner, attr, value, template, callback):
54 """Checks if an attribute has correct form.
55
56 @type owner: str
57 @param owner: name of the object containing the attribute
58 @type attr: str
59 @param attr: name of the attribute
60 @type value: dict
61 @param value: actual value of the attribute
62 @type template: dict
63 @param template: expected types of the keys
64 @type callback: callable
65 @param callback: will be called if there is an error
66
67 """
68 try:
69 utils.ForceDictType(value, template)
70 except errors.GenericError, err:
71 return callback("%s has invalid %s: %s" % (owner, attr, err))
72
73
75 """Checks if a NIC has correct form.
76
77 @type owner: str
78 @param owner: name of the object containing the attribute
79 @type params: dict
80 @param params: actual value of the NIC parameters
81 @type callback: callable
82 @param callback: will be called if there is an error
83
84 """
85 try:
86 objects.NIC.CheckParameterSyntax(params)
87 except errors.ConfigurationError, err:
88 callback("%s has invalid nicparams: %s" % (owner, err))
89
90
92 """Checks if an ipolicy has correct form.
93
94 @type owner: str
95 @param owner: name of the object containing the attribute
96 @type ipolicy: dict
97 @param ipolicy: actual value of the ipolicy parameters
98 @type iscluster: bool
99 @param iscluster: True iff the owner is the cluster
100 @type callback: callable
101 @param callback: will be called if there is an error
102
103 """
104 try:
105 objects.InstancePolicy.CheckParameterSyntax(ipolicy, iscluster)
106 except errors.ConfigurationError, err:
107 callback("%s has invalid instance policy: %s" % (owner, err))
108 for key, value in ipolicy.items():
109 if key == constants.ISPECS_MINMAX:
110 for k in range(len(value)):
111 VerifyIspecs(owner, "ipolicy/%s[%s]" % (key, k), value[k], callback)
112 elif key == constants.ISPECS_STD:
113 VerifyType(owner, "ipolicy/" + key, value,
114 constants.ISPECS_PARAMETER_TYPES, callback)
115 else:
116
117 if key in constants.IPOLICY_PARAMETERS:
118 exp_type = float
119
120 convertible_types = [int]
121 else:
122 exp_type = list
123 convertible_types = []
124
125 if any(isinstance(value, ct) for ct in convertible_types):
126 try:
127 value = exp_type(value)
128 ipolicy[key] = value
129 except ValueError:
130 pass
131 if not isinstance(value, exp_type):
132 callback("%s has invalid instance policy: for %s,"
133 " expecting %s, got %s" %
134 (owner, key, exp_type.__name__, type(value)))
135
136
138 """Checks if an ispec has correct form.
139
140 @type owner: str
141 @param owner: name of the object containing the attribute
142 @type parentkey: str
143 @param parentkey: the root name of the key
144 @type params: dict
145 @param params: actual value of the ispec parameters
146 @type callback: callable
147 @param callback: will be called if there is an error
148
149 """
150 for (key, value) in params.items():
151 fullkey = "/".join([parentkey, key])
152 VerifyType(owner, fullkey, value, constants.ISPECS_PARAMETER_TYPES,
153 callback)
154