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
32 def BuildUriList(ids, uri_format, uri_fields=("name", "uri")):
33 """Builds a URI list as used by index resources.
36 - ids: List of ids as strings
37 - uri_format: Format to be applied for URI
38 - 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.
57 - sequence: Sequence of lists
58 - index: Index of field
61 return map(lambda item: item[index], sequence)
64 def MapFields(names, data):
65 """Maps two lists into one dictionary.
68 - names: Field names (list of strings)
69 - data: Field data (list)
72 >>> MapFields(["a", "b"], ["foo", 123])
73 {'a': 'foo', 'b': 123}
76 if len(names) != len(data):
77 raise AttributeError("Names and data must have the same length")
78 return dict(zip(names, data))
81 def _Tags_GET(kind, name=None):
82 """Helper function to retrieve tags.
86 # Do not cause "missing parameter" error, which happens if a parameter
89 op = ganeti.opcodes.OpGetTags(kind=kind, name=name)
90 tags = ganeti.cli.SubmitOpCode(op)
94 def _Tags_POST(kind, tags, name=None):
95 """Helper function to set tags.
99 # Do not cause "missing parameter" error, which happens if a parameter
103 return cl.SubmitJob([ganeti.opcodes.OpAddTags(kind=kind, name=name,
107 def MapBulkFields(itemslist, fields):
108 """Map value to field name in to one dictionary.
111 - itemslist: A list of items values
112 - instance: A list of items names
115 A list of mapped dictionaries
118 for item in itemslist:
119 mapped = MapFields(fields, item)
120 items_details.append(mapped)
124 class R_Generic(object):
125 """Generic class for resources.
128 def __init__(self, request, items, queryargs, post_data):
129 """Generic resource constructor.
132 request: HTTPRequestHandler object
133 items: a list with variables encoded in the URL
134 queryargs: a dictionary with additional options from URL
137 self.request = request
139 self.queryargs = queryargs
140 self.post_data = post_data