Revision c2dca9af
b/lib/rapi/baserlib.py | ||
---|---|---|
1 |
# |
|
2 |
# |
|
3 |
|
|
4 |
# Copyright (C) 2006, 2007, 2008 Google Inc. |
|
5 |
# |
|
6 |
# This program is free software; you can redistribute it and/or modify |
|
7 |
# it under the terms of the GNU General Public License as published by |
|
8 |
# the Free Software Foundation; either version 2 of the License, or |
|
9 |
# (at your option) any later version. |
|
10 |
# |
|
11 |
# This program is distributed in the hope that it will be useful, but |
|
12 |
# WITHOUT ANY WARRANTY; without even the implied warranty of |
|
13 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
|
14 |
# General Public License for more details. |
|
15 |
# |
|
16 |
# You should have received a copy of the GNU General Public License |
|
17 |
# along with this program; if not, write to the Free Software |
|
18 |
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
|
19 |
# 02110-1301, USA. |
|
20 |
|
|
21 |
|
|
22 |
"""Remote API base resources library. |
|
23 |
|
|
24 |
""" |
|
25 |
|
|
26 |
import ganeti.cli |
|
27 |
import ganeti.opcodes |
|
28 |
|
|
29 |
|
|
30 |
def BuildUriList(ids, uri_format, uri_fields=("name", "uri")): |
|
31 |
"""Builds a URI list as used by index resources. |
|
32 |
|
|
33 |
Args: |
|
34 |
- ids: List of ids as strings |
|
35 |
- uri_format: Format to be applied for URI |
|
36 |
- uri_fields: Optional parameter for field ids |
|
37 |
|
|
38 |
""" |
|
39 |
(field_id, field_uri) = uri_fields |
|
40 |
|
|
41 |
def _MapId(m_id): |
|
42 |
return { field_id: m_id, field_uri: uri_format % m_id, } |
|
43 |
|
|
44 |
# Make sure the result is sorted, makes it nicer to look at and simplifies |
|
45 |
# unittests. |
|
46 |
ids.sort() |
|
47 |
|
|
48 |
return map(_MapId, ids) |
|
49 |
|
|
50 |
|
|
51 |
def ExtractField(sequence, index): |
|
52 |
"""Creates a list containing one column out of a list of lists. |
|
53 |
|
|
54 |
Args: |
|
55 |
- sequence: Sequence of lists |
|
56 |
- index: Index of field |
|
57 |
|
|
58 |
""" |
|
59 |
return map(lambda item: item[index], sequence) |
|
60 |
|
|
61 |
|
|
62 |
def MapFields(names, data): |
|
63 |
"""Maps two lists into one dictionary. |
|
64 |
|
|
65 |
Args: |
|
66 |
- names: Field names (list of strings) |
|
67 |
- data: Field data (list) |
|
68 |
|
|
69 |
Example: |
|
70 |
>>> MapFields(["a", "b"], ["foo", 123]) |
|
71 |
{'a': 'foo', 'b': 123} |
|
72 |
|
|
73 |
""" |
|
74 |
if len(names) != len(data): |
|
75 |
raise AttributeError("Names and data must have the same length") |
|
76 |
return dict([(names[i], data[i]) for i in range(len(names))]) |
|
77 |
|
|
78 |
|
|
79 |
def _Tags_GET(kind, name=None): |
|
80 |
"""Helper function to retrieve tags. |
|
81 |
|
|
82 |
""" |
|
83 |
if name is None: |
|
84 |
# Do not cause "missing parameter" error, which happens if a parameter |
|
85 |
# is None. |
|
86 |
name = "" |
|
87 |
op = ganeti.opcodes.OpGetTags(kind=kind, name=name) |
|
88 |
tags = ganeti.cli.SubmitOpCode(op) |
|
89 |
return list(tags) |
|
90 |
|
|
91 |
|
|
92 |
class R_Generic(object): |
|
93 |
"""Generic class for resources. |
|
94 |
|
|
95 |
""" |
|
96 |
def __init__(self, request, items, queryargs): |
|
97 |
"""Generic resource constructor. |
|
98 |
|
|
99 |
Args: |
|
100 |
request: HTTPRequestHandler object |
|
101 |
items: a list with variables encoded in the URL |
|
102 |
queryargs: a dictionary with additional options from URL |
|
103 |
|
|
104 |
""" |
|
105 |
self.request = request |
|
106 |
self.items = items |
|
107 |
self.queryargs = queryargs |
Also available in: Unified diff