4 # Copyright (C) 2006, 2007, 2008 Google Inc.
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.
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.
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
22 """Remote API base resources library.
29 from ganeti import luxi
30 from ganeti import rapi
33 def BuildUriList(ids, uri_format, uri_fields=("name", "uri")):
34 """Builds a URI list as used by index resources.
36 @param ids: list of ids as strings
37 @param uri_format: format to be applied for URI
38 @param uri_fields: optional parameter for field IDs
41 (field_id, field_uri) = uri_fields
44 return { field_id: m_id, field_uri: uri_format % m_id, }
46 # Make sure the result is sorted, makes it nicer to look at and simplifies
50 return map(_MapId, ids)
53 def ExtractField(sequence, index):
54 """Creates a list containing one column out of a list of lists.
56 @param sequence: sequence of lists
57 @param index: index of field
60 return map(lambda item: item[index], sequence)
63 def MapFields(names, data):
64 """Maps two lists into one dictionary.
67 >>> MapFields(["a", "b"], ["foo", 123])
68 {'a': 'foo', 'b': 123}
70 @param names: field names (list of strings)
71 @param data: field data (list)
74 if len(names) != len(data):
75 raise AttributeError("Names and data must have the same length")
76 return dict(zip(names, data))
79 def _Tags_GET(kind, name=""):
80 """Helper function to retrieve tags.
83 op = ganeti.opcodes.OpGetTags(kind=kind, name=name)
84 tags = ganeti.cli.SubmitOpCode(op)
88 def _Tags_PUT(kind, tags, name=""):
89 """Helper function to set tags.
93 return cl.SubmitJob([ganeti.opcodes.OpAddTags(kind=kind, name=name,
97 def _Tags_DELETE(kind, tags, name=""):
98 """Helper function to delete tags.
102 return cl.SubmitJob([ganeti.opcodes.OpDelTags(kind=kind, name=name,
106 def MapBulkFields(itemslist, fields):
107 """Map value to field name in to one dictionary.
109 @param itemslist: a list of items values
110 @param fields: a list of items names
112 @return: a list of mapped dictionaries
116 for item in itemslist:
117 mapped = MapFields(fields, item)
118 items_details.append(mapped)
122 def MakeParamsDict(opts, params):
123 """Makes params dictionary out of a option set.
125 This function returns a dictionary needed for hv or be parameters. But only
126 those fields which provided in the option set. Takes parameters frozensets
130 @param opts: selected options
131 @type params: frozenset
132 @param params: subset of options
134 @return: dictionary of options, filtered by given subset.
149 class R_Generic(object):
150 """Generic class for resources.
153 # Default permission requirements
155 PUT_ACCESS = [rapi.RAPI_ACCESS_WRITE]
156 POST_ACCESS = [rapi.RAPI_ACCESS_WRITE]
157 DELETE_ACCESS = [rapi.RAPI_ACCESS_WRITE]
159 def __init__(self, items, queryargs, req):
160 """Generic resource constructor.
162 @param items: a list with variables encoded in the URL
163 @param queryargs: a dictionary with additional options from URL
167 self.queryargs = queryargs
171 def getSerialNumber(self):
172 """Get Serial Number.