root / lib / rapi / baserlib.py @ 6c00d19a
History | View | Annotate | Download (5.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 | c2dca9af | Oleksiy Mishchenko | import ganeti.cli |
27 | c2dca9af | Oleksiy Mishchenko | import ganeti.opcodes |
28 | c2dca9af | Oleksiy Mishchenko | |
29 | 441e7cfd | Oleksiy Mishchenko | from ganeti import luxi |
30 | b5b67ef9 | Michael Hanselmann | from ganeti import rapi |
31 | 3d103742 | Iustin Pop | from ganeti import http |
32 | 441e7cfd | Oleksiy Mishchenko | |
33 | c2dca9af | Oleksiy Mishchenko | |
34 | c2dca9af | Oleksiy Mishchenko | def BuildUriList(ids, uri_format, uri_fields=("name", "uri")): |
35 | c2dca9af | Oleksiy Mishchenko | """Builds a URI list as used by index resources.
|
36 | c2dca9af | Oleksiy Mishchenko |
|
37 | c41eea6e | Iustin Pop | @param ids: list of ids as strings
|
38 | c41eea6e | Iustin Pop | @param uri_format: format to be applied for URI
|
39 | c41eea6e | Iustin Pop | @param uri_fields: optional parameter for field IDs
|
40 | c2dca9af | Oleksiy Mishchenko |
|
41 | c2dca9af | Oleksiy Mishchenko | """
|
42 | c2dca9af | Oleksiy Mishchenko | (field_id, field_uri) = uri_fields |
43 | dca1764e | Iustin Pop | |
44 | c2dca9af | Oleksiy Mishchenko | def _MapId(m_id): |
45 | c2dca9af | Oleksiy Mishchenko | return { field_id: m_id, field_uri: uri_format % m_id, }
|
46 | c2dca9af | Oleksiy Mishchenko | |
47 | c2dca9af | Oleksiy Mishchenko | # Make sure the result is sorted, makes it nicer to look at and simplifies
|
48 | c2dca9af | Oleksiy Mishchenko | # unittests.
|
49 | c2dca9af | Oleksiy Mishchenko | ids.sort() |
50 | c2dca9af | Oleksiy Mishchenko | |
51 | c2dca9af | Oleksiy Mishchenko | return map(_MapId, ids) |
52 | c2dca9af | Oleksiy Mishchenko | |
53 | c2dca9af | Oleksiy Mishchenko | |
54 | c2dca9af | Oleksiy Mishchenko | def ExtractField(sequence, index): |
55 | c2dca9af | Oleksiy Mishchenko | """Creates a list containing one column out of a list of lists.
|
56 | c2dca9af | Oleksiy Mishchenko |
|
57 | c41eea6e | Iustin Pop | @param sequence: sequence of lists
|
58 | c41eea6e | Iustin Pop | @param index: index of field
|
59 | c2dca9af | Oleksiy Mishchenko |
|
60 | c2dca9af | Oleksiy Mishchenko | """
|
61 | c2dca9af | Oleksiy Mishchenko | return map(lambda item: item[index], sequence) |
62 | c2dca9af | Oleksiy Mishchenko | |
63 | c2dca9af | Oleksiy Mishchenko | |
64 | c2dca9af | Oleksiy Mishchenko | def MapFields(names, data): |
65 | c2dca9af | Oleksiy Mishchenko | """Maps two lists into one dictionary.
|
66 | c2dca9af | Oleksiy Mishchenko |
|
67 | c41eea6e | Iustin Pop | Example::
|
68 | c41eea6e | Iustin Pop | >>> MapFields(["a", "b"], ["foo", 123])
|
69 | c41eea6e | Iustin Pop | {'a': 'foo', 'b': 123}
|
70 | c2dca9af | Oleksiy Mishchenko |
|
71 | c41eea6e | Iustin Pop | @param names: field names (list of strings)
|
72 | c41eea6e | Iustin Pop | @param data: field data (list)
|
73 | c2dca9af | Oleksiy Mishchenko |
|
74 | c2dca9af | Oleksiy Mishchenko | """
|
75 | c2dca9af | Oleksiy Mishchenko | if len(names) != len(data): |
76 | c2dca9af | Oleksiy Mishchenko | raise AttributeError("Names and data must have the same length") |
77 | dca1764e | Iustin Pop | return dict(zip(names, data)) |
78 | c2dca9af | Oleksiy Mishchenko | |
79 | c2dca9af | Oleksiy Mishchenko | |
80 | 15fd9fd5 | Oleksiy Mishchenko | def _Tags_GET(kind, name=""): |
81 | c2dca9af | Oleksiy Mishchenko | """Helper function to retrieve tags.
|
82 | c2dca9af | Oleksiy Mishchenko |
|
83 | c2dca9af | Oleksiy Mishchenko | """
|
84 | c2dca9af | Oleksiy Mishchenko | op = ganeti.opcodes.OpGetTags(kind=kind, name=name) |
85 | c2dca9af | Oleksiy Mishchenko | tags = ganeti.cli.SubmitOpCode(op) |
86 | c2dca9af | Oleksiy Mishchenko | return list(tags) |
87 | c2dca9af | Oleksiy Mishchenko | |
88 | c2dca9af | Oleksiy Mishchenko | |
89 | 21f04e5e | Oleksiy Mishchenko | def _Tags_PUT(kind, tags, name=""): |
90 | 441e7cfd | Oleksiy Mishchenko | """Helper function to set tags.
|
91 | 441e7cfd | Oleksiy Mishchenko |
|
92 | 441e7cfd | Oleksiy Mishchenko | """
|
93 | 441e7cfd | Oleksiy Mishchenko | cl = luxi.Client() |
94 | 441e7cfd | Oleksiy Mishchenko | return cl.SubmitJob([ganeti.opcodes.OpAddTags(kind=kind, name=name,
|
95 | 441e7cfd | Oleksiy Mishchenko | tags=tags)]) |
96 | 441e7cfd | Oleksiy Mishchenko | |
97 | 441e7cfd | Oleksiy Mishchenko | |
98 | 15fd9fd5 | Oleksiy Mishchenko | def _Tags_DELETE(kind, tags, name=""): |
99 | 15fd9fd5 | Oleksiy Mishchenko | """Helper function to delete tags.
|
100 | 15fd9fd5 | Oleksiy Mishchenko |
|
101 | 15fd9fd5 | Oleksiy Mishchenko | """
|
102 | 15fd9fd5 | Oleksiy Mishchenko | cl = luxi.Client() |
103 | 15fd9fd5 | Oleksiy Mishchenko | return cl.SubmitJob([ganeti.opcodes.OpDelTags(kind=kind, name=name,
|
104 | 15fd9fd5 | Oleksiy Mishchenko | tags=tags)]) |
105 | 15fd9fd5 | Oleksiy Mishchenko | |
106 | 15fd9fd5 | Oleksiy Mishchenko | |
107 | 51ee2f49 | Oleksiy Mishchenko | def MapBulkFields(itemslist, fields): |
108 | 51ee2f49 | Oleksiy Mishchenko | """Map value to field name in to one dictionary.
|
109 | 51ee2f49 | Oleksiy Mishchenko |
|
110 | c41eea6e | Iustin Pop | @param itemslist: a list of items values
|
111 | c41eea6e | Iustin Pop | @param fields: a list of items names
|
112 | c41eea6e | Iustin Pop |
|
113 | c41eea6e | Iustin Pop | @return: a list of mapped dictionaries
|
114 | 51ee2f49 | Oleksiy Mishchenko |
|
115 | 51ee2f49 | Oleksiy Mishchenko | """
|
116 | 51ee2f49 | Oleksiy Mishchenko | items_details = [] |
117 | 51ee2f49 | Oleksiy Mishchenko | for item in itemslist: |
118 | 51ee2f49 | Oleksiy Mishchenko | mapped = MapFields(fields, item) |
119 | 51ee2f49 | Oleksiy Mishchenko | items_details.append(mapped) |
120 | 51ee2f49 | Oleksiy Mishchenko | return items_details
|
121 | 51ee2f49 | Oleksiy Mishchenko | |
122 | 51ee2f49 | Oleksiy Mishchenko | |
123 | d50b3059 | Oleksiy Mishchenko | def MakeParamsDict(opts, params): |
124 | c41eea6e | Iustin Pop | """Makes params dictionary out of a option set.
|
125 | d50b3059 | Oleksiy Mishchenko |
|
126 | d50b3059 | Oleksiy Mishchenko | This function returns a dictionary needed for hv or be parameters. But only
|
127 | d50b3059 | Oleksiy Mishchenko | those fields which provided in the option set. Takes parameters frozensets
|
128 | d50b3059 | Oleksiy Mishchenko | from constants.
|
129 | d50b3059 | Oleksiy Mishchenko |
|
130 | d50b3059 | Oleksiy Mishchenko | @type opts: dict
|
131 | d50b3059 | Oleksiy Mishchenko | @param opts: selected options
|
132 | d50b3059 | Oleksiy Mishchenko | @type params: frozenset
|
133 | d50b3059 | Oleksiy Mishchenko | @param params: subset of options
|
134 | d50b3059 | Oleksiy Mishchenko | @rtype: dict
|
135 | d50b3059 | Oleksiy Mishchenko | @return: dictionary of options, filtered by given subset.
|
136 | d50b3059 | Oleksiy Mishchenko |
|
137 | d50b3059 | Oleksiy Mishchenko | """
|
138 | d50b3059 | Oleksiy Mishchenko | result = {} |
139 | d50b3059 | Oleksiy Mishchenko | |
140 | d50b3059 | Oleksiy Mishchenko | for p in params: |
141 | d50b3059 | Oleksiy Mishchenko | try:
|
142 | d50b3059 | Oleksiy Mishchenko | value = opts[p] |
143 | d50b3059 | Oleksiy Mishchenko | except KeyError: |
144 | d50b3059 | Oleksiy Mishchenko | continue
|
145 | d50b3059 | Oleksiy Mishchenko | result[p] = value |
146 | d50b3059 | Oleksiy Mishchenko | |
147 | d50b3059 | Oleksiy Mishchenko | return result
|
148 | d50b3059 | Oleksiy Mishchenko | |
149 | d50b3059 | Oleksiy Mishchenko | |
150 | c2dca9af | Oleksiy Mishchenko | class R_Generic(object): |
151 | c2dca9af | Oleksiy Mishchenko | """Generic class for resources.
|
152 | c2dca9af | Oleksiy Mishchenko |
|
153 | c2dca9af | Oleksiy Mishchenko | """
|
154 | b5b67ef9 | Michael Hanselmann | # Default permission requirements
|
155 | b5b67ef9 | Michael Hanselmann | GET_ACCESS = [] |
156 | b5b67ef9 | Michael Hanselmann | PUT_ACCESS = [rapi.RAPI_ACCESS_WRITE] |
157 | b5b67ef9 | Michael Hanselmann | POST_ACCESS = [rapi.RAPI_ACCESS_WRITE] |
158 | b5b67ef9 | Michael Hanselmann | DELETE_ACCESS = [rapi.RAPI_ACCESS_WRITE] |
159 | b5b67ef9 | Michael Hanselmann | |
160 | 7a8f64da | Oleksiy Mishchenko | def __init__(self, items, queryargs, req): |
161 | c2dca9af | Oleksiy Mishchenko | """Generic resource constructor.
|
162 | c2dca9af | Oleksiy Mishchenko |
|
163 | c41eea6e | Iustin Pop | @param items: a list with variables encoded in the URL
|
164 | c41eea6e | Iustin Pop | @param queryargs: a dictionary with additional options from URL
|
165 | c2dca9af | Oleksiy Mishchenko |
|
166 | c2dca9af | Oleksiy Mishchenko | """
|
167 | c2dca9af | Oleksiy Mishchenko | self.items = items
|
168 | c2dca9af | Oleksiy Mishchenko | self.queryargs = queryargs
|
169 | 7a8f64da | Oleksiy Mishchenko | self.req = req
|
170 | 713faea6 | Oleksiy Mishchenko | self.sn = None |
171 | 713faea6 | Oleksiy Mishchenko | |
172 | 713faea6 | Oleksiy Mishchenko | def getSerialNumber(self): |
173 | 713faea6 | Oleksiy Mishchenko | """Get Serial Number.
|
174 | 713faea6 | Oleksiy Mishchenko |
|
175 | 713faea6 | Oleksiy Mishchenko | """
|
176 | 713faea6 | Oleksiy Mishchenko | return self.sn |
177 | 3d103742 | Iustin Pop | |
178 | 3d103742 | Iustin Pop | def _checkIntVariable(self, name): |
179 | 3d103742 | Iustin Pop | """Return the parsed value of an int argument.
|
180 | 3d103742 | Iustin Pop |
|
181 | 3d103742 | Iustin Pop | """
|
182 | 3d103742 | Iustin Pop | val = self.queryargs.get(name, 0) |
183 | 3d103742 | Iustin Pop | if isinstance(val, list): |
184 | 3d103742 | Iustin Pop | if val:
|
185 | 3d103742 | Iustin Pop | val = val[0]
|
186 | 3d103742 | Iustin Pop | else:
|
187 | 3d103742 | Iustin Pop | val = 0
|
188 | 3d103742 | Iustin Pop | try:
|
189 | 3d103742 | Iustin Pop | val = int(val)
|
190 | 3d103742 | Iustin Pop | except (ValueError, TypeError), err: |
191 | 6e99c5a0 | Iustin Pop | raise http.HttpBadRequest("Invalid value for the" |
192 | 3d103742 | Iustin Pop | " '%s' parameter" % (name,))
|
193 | 3d103742 | Iustin Pop | return val
|
194 | 3d103742 | Iustin Pop | |
195 | 6e99c5a0 | Iustin Pop | def getBodyParameter(self, name, *args): |
196 | 6e99c5a0 | Iustin Pop | """Check and return the value for a given parameter.
|
197 | 6e99c5a0 | Iustin Pop |
|
198 | 6e99c5a0 | Iustin Pop | If a second parameter is not given, an error will be returned,
|
199 | 6e99c5a0 | Iustin Pop | otherwise this parameter specifies the default value.
|
200 | 6e99c5a0 | Iustin Pop |
|
201 | 6e99c5a0 | Iustin Pop | @param name: the required parameter
|
202 | 6e99c5a0 | Iustin Pop |
|
203 | 6e99c5a0 | Iustin Pop | """
|
204 | 6e99c5a0 | Iustin Pop | if name in self.req.request_body: |
205 | 6e99c5a0 | Iustin Pop | return self.req.request_body[name] |
206 | 6e99c5a0 | Iustin Pop | elif args:
|
207 | 6e99c5a0 | Iustin Pop | return args[0] |
208 | 6e99c5a0 | Iustin Pop | else:
|
209 | 6e99c5a0 | Iustin Pop | raise http.HttpBadRequest("Required parameter '%s' is missing" % |
210 | 6e99c5a0 | Iustin Pop | name) |
211 | 6e99c5a0 | Iustin Pop | |
212 | 3d103742 | Iustin Pop | def useLocking(self): |
213 | 3d103742 | Iustin Pop | """Check if the request specifies locking.
|
214 | 3d103742 | Iustin Pop |
|
215 | 3d103742 | Iustin Pop | """
|
216 | 3d103742 | Iustin Pop | return self._checkIntVariable('lock') |
217 | 3d103742 | Iustin Pop | |
218 | 3d103742 | Iustin Pop | def useBulk(self): |
219 | 3d103742 | Iustin Pop | """Check if the request specifies bulk querying.
|
220 | 3d103742 | Iustin Pop |
|
221 | 3d103742 | Iustin Pop | """
|
222 | 3d103742 | Iustin Pop | return self._checkIntVariable('bulk') |