root / lib / rapi / baserlib.py @ acd65a16
History | View | Annotate | Download (9.4 kB)
1 | c2dca9af | Oleksiy Mishchenko | #
|
---|---|---|---|
2 | c2dca9af | Oleksiy Mishchenko | #
|
3 | c2dca9af | Oleksiy Mishchenko | |
4 | c2dca9af | Oleksiy Mishchenko | # Copyright (C) 2006, 2007, 2008 Google Inc.
|
5 | c2dca9af | Oleksiy Mishchenko | #
|
6 | c2dca9af | Oleksiy Mishchenko | # This program is free software; you can redistribute it and/or modify
|
7 | c2dca9af | Oleksiy Mishchenko | # it under the terms of the GNU General Public License as published by
|
8 | c2dca9af | Oleksiy Mishchenko | # the Free Software Foundation; either version 2 of the License, or
|
9 | c2dca9af | Oleksiy Mishchenko | # (at your option) any later version.
|
10 | c2dca9af | Oleksiy Mishchenko | #
|
11 | c2dca9af | Oleksiy Mishchenko | # This program is distributed in the hope that it will be useful, but
|
12 | c2dca9af | Oleksiy Mishchenko | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | c2dca9af | Oleksiy Mishchenko | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | c2dca9af | Oleksiy Mishchenko | # General Public License for more details.
|
15 | c2dca9af | Oleksiy Mishchenko | #
|
16 | c2dca9af | Oleksiy Mishchenko | # You should have received a copy of the GNU General Public License
|
17 | c2dca9af | Oleksiy Mishchenko | # along with this program; if not, write to the Free Software
|
18 | c2dca9af | Oleksiy Mishchenko | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | c2dca9af | Oleksiy Mishchenko | # 02110-1301, USA.
|
20 | c2dca9af | Oleksiy Mishchenko | |
21 | c2dca9af | Oleksiy Mishchenko | |
22 | c2dca9af | Oleksiy Mishchenko | """Remote API base resources library.
|
23 | c2dca9af | Oleksiy Mishchenko |
|
24 | c2dca9af | Oleksiy Mishchenko | """
|
25 | c2dca9af | Oleksiy Mishchenko | |
26 | fe267188 | Iustin Pop | # pylint: disable-msg=C0103
|
27 | fe267188 | Iustin Pop | |
28 | fe267188 | Iustin Pop | # C0103: Invalid name, since the R_* names are not conforming
|
29 | fe267188 | Iustin Pop | |
30 | 59b4eeef | Iustin Pop | import logging |
31 | 59b4eeef | Iustin Pop | |
32 | 441e7cfd | Oleksiy Mishchenko | from ganeti import luxi |
33 | b5b67ef9 | Michael Hanselmann | from ganeti import rapi |
34 | 3d103742 | Iustin Pop | from ganeti import http |
35 | 25e39bfa | Iustin Pop | from ganeti import ssconf |
36 | 25e39bfa | Iustin Pop | from ganeti import constants |
37 | 59b4eeef | Iustin Pop | from ganeti import opcodes |
38 | 59b4eeef | Iustin Pop | from ganeti import errors |
39 | 441e7cfd | Oleksiy Mishchenko | |
40 | c2dca9af | Oleksiy Mishchenko | |
41 | af6433c6 | Michael Hanselmann | # Dummy value to detect unchanged parameters
|
42 | af6433c6 | Michael Hanselmann | _DEFAULT = object()
|
43 | af6433c6 | Michael Hanselmann | |
44 | af6433c6 | Michael Hanselmann | |
45 | c2dca9af | Oleksiy Mishchenko | def BuildUriList(ids, uri_format, uri_fields=("name", "uri")): |
46 | c2dca9af | Oleksiy Mishchenko | """Builds a URI list as used by index resources.
|
47 | c2dca9af | Oleksiy Mishchenko |
|
48 | c41eea6e | Iustin Pop | @param ids: list of ids as strings
|
49 | c41eea6e | Iustin Pop | @param uri_format: format to be applied for URI
|
50 | c41eea6e | Iustin Pop | @param uri_fields: optional parameter for field IDs
|
51 | c2dca9af | Oleksiy Mishchenko |
|
52 | c2dca9af | Oleksiy Mishchenko | """
|
53 | c2dca9af | Oleksiy Mishchenko | (field_id, field_uri) = uri_fields |
54 | dca1764e | Iustin Pop | |
55 | c2dca9af | Oleksiy Mishchenko | def _MapId(m_id): |
56 | c2dca9af | Oleksiy Mishchenko | return { field_id: m_id, field_uri: uri_format % m_id, }
|
57 | c2dca9af | Oleksiy Mishchenko | |
58 | c2dca9af | Oleksiy Mishchenko | # Make sure the result is sorted, makes it nicer to look at and simplifies
|
59 | c2dca9af | Oleksiy Mishchenko | # unittests.
|
60 | c2dca9af | Oleksiy Mishchenko | ids.sort() |
61 | c2dca9af | Oleksiy Mishchenko | |
62 | c2dca9af | Oleksiy Mishchenko | return map(_MapId, ids) |
63 | c2dca9af | Oleksiy Mishchenko | |
64 | c2dca9af | Oleksiy Mishchenko | |
65 | c2dca9af | Oleksiy Mishchenko | def ExtractField(sequence, index): |
66 | c2dca9af | Oleksiy Mishchenko | """Creates a list containing one column out of a list of lists.
|
67 | c2dca9af | Oleksiy Mishchenko |
|
68 | c41eea6e | Iustin Pop | @param sequence: sequence of lists
|
69 | c41eea6e | Iustin Pop | @param index: index of field
|
70 | c2dca9af | Oleksiy Mishchenko |
|
71 | c2dca9af | Oleksiy Mishchenko | """
|
72 | c2dca9af | Oleksiy Mishchenko | return map(lambda item: item[index], sequence) |
73 | c2dca9af | Oleksiy Mishchenko | |
74 | c2dca9af | Oleksiy Mishchenko | |
75 | c2dca9af | Oleksiy Mishchenko | def MapFields(names, data): |
76 | c2dca9af | Oleksiy Mishchenko | """Maps two lists into one dictionary.
|
77 | c2dca9af | Oleksiy Mishchenko |
|
78 | c41eea6e | Iustin Pop | Example::
|
79 | c41eea6e | Iustin Pop | >>> MapFields(["a", "b"], ["foo", 123])
|
80 | c41eea6e | Iustin Pop | {'a': 'foo', 'b': 123}
|
81 | c2dca9af | Oleksiy Mishchenko |
|
82 | c41eea6e | Iustin Pop | @param names: field names (list of strings)
|
83 | c41eea6e | Iustin Pop | @param data: field data (list)
|
84 | c2dca9af | Oleksiy Mishchenko |
|
85 | c2dca9af | Oleksiy Mishchenko | """
|
86 | c2dca9af | Oleksiy Mishchenko | if len(names) != len(data): |
87 | c2dca9af | Oleksiy Mishchenko | raise AttributeError("Names and data must have the same length") |
88 | dca1764e | Iustin Pop | return dict(zip(names, data)) |
89 | c2dca9af | Oleksiy Mishchenko | |
90 | c2dca9af | Oleksiy Mishchenko | |
91 | 64246438 | Iustin Pop | def _Tags_GET(kind, name): |
92 | c2dca9af | Oleksiy Mishchenko | """Helper function to retrieve tags.
|
93 | c2dca9af | Oleksiy Mishchenko |
|
94 | c2dca9af | Oleksiy Mishchenko | """
|
95 | 25e39bfa | Iustin Pop | if kind == constants.TAG_INSTANCE or kind == constants.TAG_NODE: |
96 | 25e39bfa | Iustin Pop | if not name: |
97 | 59b4eeef | Iustin Pop | raise http.HttpBadRequest("Missing name on tag request") |
98 | 59b4eeef | Iustin Pop | cl = GetClient() |
99 | 25e39bfa | Iustin Pop | if kind == constants.TAG_INSTANCE:
|
100 | 25e39bfa | Iustin Pop | fn = cl.QueryInstances |
101 | 25e39bfa | Iustin Pop | else:
|
102 | 25e39bfa | Iustin Pop | fn = cl.QueryNodes |
103 | 25e39bfa | Iustin Pop | result = fn(names=[name], fields=["tags"], use_locking=False) |
104 | 25e39bfa | Iustin Pop | if not result or not result[0]: |
105 | 25e39bfa | Iustin Pop | raise http.HttpBadGateway("Invalid response from tag query") |
106 | 25e39bfa | Iustin Pop | tags = result[0][0] |
107 | 25e39bfa | Iustin Pop | elif kind == constants.TAG_CLUSTER:
|
108 | 25e39bfa | Iustin Pop | ssc = ssconf.SimpleStore() |
109 | 25e39bfa | Iustin Pop | tags = ssc.GetClusterTags() |
110 | 25e39bfa | Iustin Pop | |
111 | c2dca9af | Oleksiy Mishchenko | return list(tags) |
112 | c2dca9af | Oleksiy Mishchenko | |
113 | c2dca9af | Oleksiy Mishchenko | |
114 | 64246438 | Iustin Pop | def _Tags_PUT(kind, tags, name, dry_run): |
115 | 441e7cfd | Oleksiy Mishchenko | """Helper function to set tags.
|
116 | 441e7cfd | Oleksiy Mishchenko |
|
117 | 441e7cfd | Oleksiy Mishchenko | """
|
118 | 64246438 | Iustin Pop | return SubmitJob([opcodes.OpAddTags(kind=kind, name=name,
|
119 | 64246438 | Iustin Pop | tags=tags, dry_run=dry_run)]) |
120 | 441e7cfd | Oleksiy Mishchenko | |
121 | 441e7cfd | Oleksiy Mishchenko | |
122 | 64246438 | Iustin Pop | def _Tags_DELETE(kind, tags, name, dry_run): |
123 | 15fd9fd5 | Oleksiy Mishchenko | """Helper function to delete tags.
|
124 | 15fd9fd5 | Oleksiy Mishchenko |
|
125 | 15fd9fd5 | Oleksiy Mishchenko | """
|
126 | 64246438 | Iustin Pop | return SubmitJob([opcodes.OpDelTags(kind=kind, name=name,
|
127 | 64246438 | Iustin Pop | tags=tags, dry_run=dry_run)]) |
128 | 15fd9fd5 | Oleksiy Mishchenko | |
129 | 15fd9fd5 | Oleksiy Mishchenko | |
130 | 51ee2f49 | Oleksiy Mishchenko | def MapBulkFields(itemslist, fields): |
131 | 51ee2f49 | Oleksiy Mishchenko | """Map value to field name in to one dictionary.
|
132 | 51ee2f49 | Oleksiy Mishchenko |
|
133 | c41eea6e | Iustin Pop | @param itemslist: a list of items values
|
134 | c41eea6e | Iustin Pop | @param fields: a list of items names
|
135 | c41eea6e | Iustin Pop |
|
136 | c41eea6e | Iustin Pop | @return: a list of mapped dictionaries
|
137 | 51ee2f49 | Oleksiy Mishchenko |
|
138 | 51ee2f49 | Oleksiy Mishchenko | """
|
139 | 51ee2f49 | Oleksiy Mishchenko | items_details = [] |
140 | 51ee2f49 | Oleksiy Mishchenko | for item in itemslist: |
141 | 51ee2f49 | Oleksiy Mishchenko | mapped = MapFields(fields, item) |
142 | 51ee2f49 | Oleksiy Mishchenko | items_details.append(mapped) |
143 | 51ee2f49 | Oleksiy Mishchenko | return items_details
|
144 | 51ee2f49 | Oleksiy Mishchenko | |
145 | 51ee2f49 | Oleksiy Mishchenko | |
146 | d50b3059 | Oleksiy Mishchenko | def MakeParamsDict(opts, params): |
147 | c41eea6e | Iustin Pop | """Makes params dictionary out of a option set.
|
148 | d50b3059 | Oleksiy Mishchenko |
|
149 | d50b3059 | Oleksiy Mishchenko | This function returns a dictionary needed for hv or be parameters. But only
|
150 | d50b3059 | Oleksiy Mishchenko | those fields which provided in the option set. Takes parameters frozensets
|
151 | d50b3059 | Oleksiy Mishchenko | from constants.
|
152 | d50b3059 | Oleksiy Mishchenko |
|
153 | d50b3059 | Oleksiy Mishchenko | @type opts: dict
|
154 | d50b3059 | Oleksiy Mishchenko | @param opts: selected options
|
155 | d50b3059 | Oleksiy Mishchenko | @type params: frozenset
|
156 | d50b3059 | Oleksiy Mishchenko | @param params: subset of options
|
157 | d50b3059 | Oleksiy Mishchenko | @rtype: dict
|
158 | d50b3059 | Oleksiy Mishchenko | @return: dictionary of options, filtered by given subset.
|
159 | d50b3059 | Oleksiy Mishchenko |
|
160 | d50b3059 | Oleksiy Mishchenko | """
|
161 | d50b3059 | Oleksiy Mishchenko | result = {} |
162 | d50b3059 | Oleksiy Mishchenko | |
163 | d50b3059 | Oleksiy Mishchenko | for p in params: |
164 | d50b3059 | Oleksiy Mishchenko | try:
|
165 | d50b3059 | Oleksiy Mishchenko | value = opts[p] |
166 | d50b3059 | Oleksiy Mishchenko | except KeyError: |
167 | d50b3059 | Oleksiy Mishchenko | continue
|
168 | d50b3059 | Oleksiy Mishchenko | result[p] = value |
169 | d50b3059 | Oleksiy Mishchenko | |
170 | d50b3059 | Oleksiy Mishchenko | return result
|
171 | d50b3059 | Oleksiy Mishchenko | |
172 | d50b3059 | Oleksiy Mishchenko | |
173 | 59b4eeef | Iustin Pop | def SubmitJob(op, cl=None): |
174 | 59b4eeef | Iustin Pop | """Generic wrapper for submit job, for better http compatibility.
|
175 | 59b4eeef | Iustin Pop |
|
176 | 59b4eeef | Iustin Pop | @type op: list
|
177 | 59b4eeef | Iustin Pop | @param op: the list of opcodes for the job
|
178 | 59b4eeef | Iustin Pop | @type cl: None or luxi.Client
|
179 | 59b4eeef | Iustin Pop | @param cl: optional luxi client to use
|
180 | 59b4eeef | Iustin Pop | @rtype: string
|
181 | 59b4eeef | Iustin Pop | @return: the job ID
|
182 | 59b4eeef | Iustin Pop |
|
183 | 59b4eeef | Iustin Pop | """
|
184 | 59b4eeef | Iustin Pop | try:
|
185 | 59b4eeef | Iustin Pop | if cl is None: |
186 | 59b4eeef | Iustin Pop | cl = GetClient() |
187 | 59b4eeef | Iustin Pop | return cl.SubmitJob(op)
|
188 | 59b4eeef | Iustin Pop | except errors.JobQueueFull:
|
189 | 59b4eeef | Iustin Pop | raise http.HttpServiceUnavailable("Job queue is full, needs archiving") |
190 | 59b4eeef | Iustin Pop | except errors.JobQueueDrainError:
|
191 | 59b4eeef | Iustin Pop | raise http.HttpServiceUnavailable("Job queue is drained, cannot submit") |
192 | 59b4eeef | Iustin Pop | except luxi.NoMasterError, err:
|
193 | 59b4eeef | Iustin Pop | raise http.HttpBadGateway("Master seems to unreachable: %s" % str(err)) |
194 | 59b4eeef | Iustin Pop | except luxi.TimeoutError, err:
|
195 | 59b4eeef | Iustin Pop | raise http.HttpGatewayTimeout("Timeout while talking to the master" |
196 | 59b4eeef | Iustin Pop | " daemon. Error: %s" % str(err)) |
197 | 59b4eeef | Iustin Pop | |
198 | e8ebbd2b | Michael Hanselmann | |
199 | e8ebbd2b | Michael Hanselmann | def HandleItemQueryErrors(fn, *args, **kwargs): |
200 | e8ebbd2b | Michael Hanselmann | """Converts errors when querying a single item.
|
201 | e8ebbd2b | Michael Hanselmann |
|
202 | e8ebbd2b | Michael Hanselmann | """
|
203 | e8ebbd2b | Michael Hanselmann | try:
|
204 | e8ebbd2b | Michael Hanselmann | return fn(*args, **kwargs)
|
205 | e8ebbd2b | Michael Hanselmann | except errors.OpPrereqError, err:
|
206 | e8ebbd2b | Michael Hanselmann | if len(err.args) == 2 and err.args[1] == errors.ECODE_NOENT: |
207 | e8ebbd2b | Michael Hanselmann | raise http.HttpNotFound()
|
208 | e8ebbd2b | Michael Hanselmann | |
209 | e8ebbd2b | Michael Hanselmann | raise
|
210 | e8ebbd2b | Michael Hanselmann | |
211 | e8ebbd2b | Michael Hanselmann | |
212 | 59b4eeef | Iustin Pop | def GetClient(): |
213 | 59b4eeef | Iustin Pop | """Geric wrapper for luxi.Client(), for better http compatiblity.
|
214 | 59b4eeef | Iustin Pop |
|
215 | 59b4eeef | Iustin Pop | """
|
216 | 59b4eeef | Iustin Pop | try:
|
217 | 59b4eeef | Iustin Pop | return luxi.Client()
|
218 | 59b4eeef | Iustin Pop | except luxi.NoMasterError, err:
|
219 | 59b4eeef | Iustin Pop | raise http.HttpBadGateway("Master seems to unreachable: %s" % str(err)) |
220 | 59b4eeef | Iustin Pop | |
221 | 59b4eeef | Iustin Pop | |
222 | 2d54e29c | Iustin Pop | def FeedbackFn(ts, log_type, log_msg): # pylint: disable-msg=W0613 |
223 | 59b4eeef | Iustin Pop | """Feedback logging function for http case.
|
224 | 59b4eeef | Iustin Pop |
|
225 | 59b4eeef | Iustin Pop | We don't have a stdout for printing log messages, so log them to the
|
226 | 59b4eeef | Iustin Pop | http log at least.
|
227 | 59b4eeef | Iustin Pop |
|
228 | 2d54e29c | Iustin Pop | @param ts: the timestamp (unused)
|
229 | 2d54e29c | Iustin Pop |
|
230 | 59b4eeef | Iustin Pop | """
|
231 | 59b4eeef | Iustin Pop | logging.info("%s: %s", log_type, log_msg)
|
232 | 59b4eeef | Iustin Pop | |
233 | 59b4eeef | Iustin Pop | |
234 | af6433c6 | Michael Hanselmann | def CheckType(value, exptype, descr): |
235 | af6433c6 | Michael Hanselmann | """Abort request if value type doesn't match expected type.
|
236 | af6433c6 | Michael Hanselmann |
|
237 | af6433c6 | Michael Hanselmann | @param value: Value
|
238 | af6433c6 | Michael Hanselmann | @type exptype: type
|
239 | af6433c6 | Michael Hanselmann | @param exptype: Expected type
|
240 | af6433c6 | Michael Hanselmann | @type descr: string
|
241 | af6433c6 | Michael Hanselmann | @param descr: Description of value
|
242 | af6433c6 | Michael Hanselmann | @return: Value (allows inline usage)
|
243 | af6433c6 | Michael Hanselmann |
|
244 | af6433c6 | Michael Hanselmann | """
|
245 | af6433c6 | Michael Hanselmann | if not isinstance(value, exptype): |
246 | af6433c6 | Michael Hanselmann | raise http.HttpBadRequest("%s: Type is '%s', but '%s' is expected" % |
247 | af6433c6 | Michael Hanselmann | (descr, type(value).__name__, exptype.__name__))
|
248 | af6433c6 | Michael Hanselmann | |
249 | af6433c6 | Michael Hanselmann | return value
|
250 | af6433c6 | Michael Hanselmann | |
251 | af6433c6 | Michael Hanselmann | |
252 | af6433c6 | Michael Hanselmann | def CheckParameter(data, name, default=_DEFAULT, exptype=_DEFAULT): |
253 | af6433c6 | Michael Hanselmann | """Check and return the value for a given parameter.
|
254 | af6433c6 | Michael Hanselmann |
|
255 | af6433c6 | Michael Hanselmann | If no default value was given and the parameter doesn't exist in the input
|
256 | af6433c6 | Michael Hanselmann | data, an error is raise.
|
257 | af6433c6 | Michael Hanselmann |
|
258 | af6433c6 | Michael Hanselmann | @type data: dict
|
259 | af6433c6 | Michael Hanselmann | @param data: Dictionary containing input data
|
260 | af6433c6 | Michael Hanselmann | @type name: string
|
261 | af6433c6 | Michael Hanselmann | @param name: Parameter name
|
262 | af6433c6 | Michael Hanselmann | @param default: Default value (can be None)
|
263 | af6433c6 | Michael Hanselmann | @param exptype: Expected type (can be None)
|
264 | af6433c6 | Michael Hanselmann |
|
265 | af6433c6 | Michael Hanselmann | """
|
266 | af6433c6 | Michael Hanselmann | try:
|
267 | af6433c6 | Michael Hanselmann | value = data[name] |
268 | af6433c6 | Michael Hanselmann | except KeyError: |
269 | af6433c6 | Michael Hanselmann | if default is not _DEFAULT: |
270 | af6433c6 | Michael Hanselmann | return default
|
271 | af6433c6 | Michael Hanselmann | |
272 | af6433c6 | Michael Hanselmann | raise http.HttpBadRequest("Required parameter '%s' is missing" % |
273 | af6433c6 | Michael Hanselmann | name) |
274 | af6433c6 | Michael Hanselmann | |
275 | af6433c6 | Michael Hanselmann | if exptype is _DEFAULT: |
276 | af6433c6 | Michael Hanselmann | return value
|
277 | af6433c6 | Michael Hanselmann | |
278 | af6433c6 | Michael Hanselmann | return CheckType(value, exptype, "'%s' parameter" % name) |
279 | af6433c6 | Michael Hanselmann | |
280 | af6433c6 | Michael Hanselmann | |
281 | c2dca9af | Oleksiy Mishchenko | class R_Generic(object): |
282 | c2dca9af | Oleksiy Mishchenko | """Generic class for resources.
|
283 | c2dca9af | Oleksiy Mishchenko |
|
284 | c2dca9af | Oleksiy Mishchenko | """
|
285 | b5b67ef9 | Michael Hanselmann | # Default permission requirements
|
286 | b5b67ef9 | Michael Hanselmann | GET_ACCESS = [] |
287 | b5b67ef9 | Michael Hanselmann | PUT_ACCESS = [rapi.RAPI_ACCESS_WRITE] |
288 | b5b67ef9 | Michael Hanselmann | POST_ACCESS = [rapi.RAPI_ACCESS_WRITE] |
289 | b5b67ef9 | Michael Hanselmann | DELETE_ACCESS = [rapi.RAPI_ACCESS_WRITE] |
290 | b5b67ef9 | Michael Hanselmann | |
291 | 7a8f64da | Oleksiy Mishchenko | def __init__(self, items, queryargs, req): |
292 | c2dca9af | Oleksiy Mishchenko | """Generic resource constructor.
|
293 | c2dca9af | Oleksiy Mishchenko |
|
294 | c41eea6e | Iustin Pop | @param items: a list with variables encoded in the URL
|
295 | c41eea6e | Iustin Pop | @param queryargs: a dictionary with additional options from URL
|
296 | c2dca9af | Oleksiy Mishchenko |
|
297 | c2dca9af | Oleksiy Mishchenko | """
|
298 | c2dca9af | Oleksiy Mishchenko | self.items = items
|
299 | c2dca9af | Oleksiy Mishchenko | self.queryargs = queryargs
|
300 | 627ad739 | Michael Hanselmann | self._req = req
|
301 | 627ad739 | Michael Hanselmann | |
302 | 2feecf12 | Michael Hanselmann | def _GetRequestBody(self): |
303 | 2feecf12 | Michael Hanselmann | """Returns the body data.
|
304 | 2feecf12 | Michael Hanselmann |
|
305 | 2feecf12 | Michael Hanselmann | """
|
306 | 2feecf12 | Michael Hanselmann | return self._req.private.body_data |
307 | 2feecf12 | Michael Hanselmann | |
308 | 2feecf12 | Michael Hanselmann | request_body = property(fget=_GetRequestBody)
|
309 | 3d103742 | Iustin Pop | |
310 | 3fb8680a | Michael Hanselmann | def _checkIntVariable(self, name, default=0): |
311 | 3d103742 | Iustin Pop | """Return the parsed value of an int argument.
|
312 | 3d103742 | Iustin Pop |
|
313 | 3d103742 | Iustin Pop | """
|
314 | 3fb8680a | Michael Hanselmann | val = self.queryargs.get(name, default)
|
315 | 3d103742 | Iustin Pop | if isinstance(val, list): |
316 | 3d103742 | Iustin Pop | if val:
|
317 | 3d103742 | Iustin Pop | val = val[0]
|
318 | 3d103742 | Iustin Pop | else:
|
319 | 3fb8680a | Michael Hanselmann | val = default |
320 | 3d103742 | Iustin Pop | try:
|
321 | 3d103742 | Iustin Pop | val = int(val)
|
322 | 7c4d6c7b | Michael Hanselmann | except (ValueError, TypeError): |
323 | 6e99c5a0 | Iustin Pop | raise http.HttpBadRequest("Invalid value for the" |
324 | 3d103742 | Iustin Pop | " '%s' parameter" % (name,))
|
325 | 3d103742 | Iustin Pop | return val
|
326 | 3d103742 | Iustin Pop | |
327 | e5b7c4ca | Iustin Pop | def _checkStringVariable(self, name, default=None): |
328 | e5b7c4ca | Iustin Pop | """Return the parsed value of an int argument.
|
329 | e5b7c4ca | Iustin Pop |
|
330 | e5b7c4ca | Iustin Pop | """
|
331 | e5b7c4ca | Iustin Pop | val = self.queryargs.get(name, default)
|
332 | e5b7c4ca | Iustin Pop | if isinstance(val, list): |
333 | e5b7c4ca | Iustin Pop | if val:
|
334 | e5b7c4ca | Iustin Pop | val = val[0]
|
335 | e5b7c4ca | Iustin Pop | else:
|
336 | e5b7c4ca | Iustin Pop | val = default |
337 | e5b7c4ca | Iustin Pop | return val
|
338 | 3d103742 | Iustin Pop | |
339 | 6e99c5a0 | Iustin Pop | def getBodyParameter(self, name, *args): |
340 | 6e99c5a0 | Iustin Pop | """Check and return the value for a given parameter.
|
341 | 6e99c5a0 | Iustin Pop |
|
342 | 6e99c5a0 | Iustin Pop | If a second parameter is not given, an error will be returned,
|
343 | 6e99c5a0 | Iustin Pop | otherwise this parameter specifies the default value.
|
344 | 6e99c5a0 | Iustin Pop |
|
345 | 6e99c5a0 | Iustin Pop | @param name: the required parameter
|
346 | 6e99c5a0 | Iustin Pop |
|
347 | 6e99c5a0 | Iustin Pop | """
|
348 | af6433c6 | Michael Hanselmann | if args:
|
349 | 1c54156d | Luca Bigliardi | return CheckParameter(self.request_body, name, default=args[0]) |
350 | ab221ddf | Michael Hanselmann | |
351 | 1c54156d | Luca Bigliardi | return CheckParameter(self.request_body, name) |
352 | 6e99c5a0 | Iustin Pop | |
353 | 3d103742 | Iustin Pop | def useLocking(self): |
354 | 3d103742 | Iustin Pop | """Check if the request specifies locking.
|
355 | 3d103742 | Iustin Pop |
|
356 | 3d103742 | Iustin Pop | """
|
357 | 3d103742 | Iustin Pop | return self._checkIntVariable('lock') |
358 | 3d103742 | Iustin Pop | |
359 | 3d103742 | Iustin Pop | def useBulk(self): |
360 | 3d103742 | Iustin Pop | """Check if the request specifies bulk querying.
|
361 | 3d103742 | Iustin Pop |
|
362 | 3d103742 | Iustin Pop | """
|
363 | 3d103742 | Iustin Pop | return self._checkIntVariable('bulk') |
364 | 6f59b964 | Iustin Pop | |
365 | 3427d34f | Michael Hanselmann | def useForce(self): |
366 | 3427d34f | Michael Hanselmann | """Check if the request specifies a forced operation.
|
367 | 3427d34f | Michael Hanselmann |
|
368 | 3427d34f | Michael Hanselmann | """
|
369 | 3427d34f | Michael Hanselmann | return self._checkIntVariable('force') |
370 | 3427d34f | Michael Hanselmann | |
371 | 6f59b964 | Iustin Pop | def dryRun(self): |
372 | 6f59b964 | Iustin Pop | """Check if the request specifies dry-run mode.
|
373 | 6f59b964 | Iustin Pop |
|
374 | 6f59b964 | Iustin Pop | """
|
375 | 6f59b964 | Iustin Pop | return self._checkIntVariable('dry-run') |