root / lib / rapi / baserlib.py @ c41eea6e
History | View | Annotate | Download (4 kB)
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 |
from ganeti import luxi |
30 |
|
31 |
|
32 |
def BuildUriList(ids, uri_format, uri_fields=("name", "uri")): |
33 |
"""Builds a URI list as used by index resources.
|
34 |
|
35 |
@param ids: list of ids as strings
|
36 |
@param uri_format: format to be applied for URI
|
37 |
@param uri_fields: optional parameter for field IDs
|
38 |
|
39 |
"""
|
40 |
(field_id, field_uri) = uri_fields |
41 |
|
42 |
def _MapId(m_id): |
43 |
return { field_id: m_id, field_uri: uri_format % m_id, }
|
44 |
|
45 |
# Make sure the result is sorted, makes it nicer to look at and simplifies
|
46 |
# unittests.
|
47 |
ids.sort() |
48 |
|
49 |
return map(_MapId, ids) |
50 |
|
51 |
|
52 |
def ExtractField(sequence, index): |
53 |
"""Creates a list containing one column out of a list of lists.
|
54 |
|
55 |
@param sequence: sequence of lists
|
56 |
@param 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 |
Example::
|
66 |
>>> MapFields(["a", "b"], ["foo", 123])
|
67 |
{'a': 'foo', 'b': 123}
|
68 |
|
69 |
@param names: field names (list of strings)
|
70 |
@param data: field data (list)
|
71 |
|
72 |
"""
|
73 |
if len(names) != len(data): |
74 |
raise AttributeError("Names and data must have the same length") |
75 |
return dict(zip(names, data)) |
76 |
|
77 |
|
78 |
def _Tags_GET(kind, name=""): |
79 |
"""Helper function to retrieve tags.
|
80 |
|
81 |
"""
|
82 |
op = ganeti.opcodes.OpGetTags(kind=kind, name=name) |
83 |
tags = ganeti.cli.SubmitOpCode(op) |
84 |
return list(tags) |
85 |
|
86 |
|
87 |
def _Tags_PUT(kind, tags, name=""): |
88 |
"""Helper function to set tags.
|
89 |
|
90 |
"""
|
91 |
cl = luxi.Client() |
92 |
return cl.SubmitJob([ganeti.opcodes.OpAddTags(kind=kind, name=name,
|
93 |
tags=tags)]) |
94 |
|
95 |
|
96 |
def _Tags_DELETE(kind, tags, name=""): |
97 |
"""Helper function to delete tags.
|
98 |
|
99 |
"""
|
100 |
cl = luxi.Client() |
101 |
return cl.SubmitJob([ganeti.opcodes.OpDelTags(kind=kind, name=name,
|
102 |
tags=tags)]) |
103 |
|
104 |
|
105 |
def MapBulkFields(itemslist, fields): |
106 |
"""Map value to field name in to one dictionary.
|
107 |
|
108 |
@param itemslist: a list of items values
|
109 |
@param fields: a list of items names
|
110 |
|
111 |
@return: a list of mapped dictionaries
|
112 |
|
113 |
"""
|
114 |
items_details = [] |
115 |
for item in itemslist: |
116 |
mapped = MapFields(fields, item) |
117 |
items_details.append(mapped) |
118 |
return items_details
|
119 |
|
120 |
|
121 |
def MakeParamsDict(opts, params): |
122 |
"""Makes params dictionary out of a option set.
|
123 |
|
124 |
This function returns a dictionary needed for hv or be parameters. But only
|
125 |
those fields which provided in the option set. Takes parameters frozensets
|
126 |
from constants.
|
127 |
|
128 |
@type opts: dict
|
129 |
@param opts: selected options
|
130 |
@type params: frozenset
|
131 |
@param params: subset of options
|
132 |
@rtype: dict
|
133 |
@return: dictionary of options, filtered by given subset.
|
134 |
|
135 |
"""
|
136 |
result = {} |
137 |
|
138 |
for p in params: |
139 |
try:
|
140 |
value = opts[p] |
141 |
except KeyError: |
142 |
continue
|
143 |
result[p] = value |
144 |
|
145 |
return result
|
146 |
|
147 |
|
148 |
class R_Generic(object): |
149 |
"""Generic class for resources.
|
150 |
|
151 |
"""
|
152 |
def __init__(self, items, queryargs, req): |
153 |
"""Generic resource constructor.
|
154 |
|
155 |
@param items: a list with variables encoded in the URL
|
156 |
@param queryargs: a dictionary with additional options from URL
|
157 |
|
158 |
"""
|
159 |
self.items = items
|
160 |
self.queryargs = queryargs
|
161 |
self.req = req
|
162 |
self.sn = None |
163 |
|
164 |
def getSerialNumber(self): |
165 |
"""Get Serial Number.
|
166 |
|
167 |
"""
|
168 |
return self.sn |