root / lib / rapi / baserlib.py @ 51ee2f49
History | View | Annotate | Download (3 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 |
|
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(zip(names, data)) |
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 |
def MapBulkFields(itemslist, fields): |
93 |
"""Map value to field name in to one dictionary.
|
94 |
|
95 |
Args:
|
96 |
- itemslist: A list of items values
|
97 |
- instance: A list of items names
|
98 |
|
99 |
Returns:
|
100 |
A list of mapped dictionaries
|
101 |
"""
|
102 |
items_details = [] |
103 |
for item in itemslist: |
104 |
mapped = MapFields(fields, item) |
105 |
items_details.append(mapped) |
106 |
return items_details
|
107 |
|
108 |
|
109 |
class R_Generic(object): |
110 |
"""Generic class for resources.
|
111 |
|
112 |
"""
|
113 |
def __init__(self, request, items, queryargs): |
114 |
"""Generic resource constructor.
|
115 |
|
116 |
Args:
|
117 |
request: HTTPRequestHandler object
|
118 |
items: a list with variables encoded in the URL
|
119 |
queryargs: a dictionary with additional options from URL
|
120 |
|
121 |
"""
|
122 |
self.request = request
|
123 |
self.items = items
|
124 |
self.queryargs = queryargs
|