root / lib / rapi / baserlib.py @ 15fd9fd5
History | View | Annotate | Download (3.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 |
Args:
|
36 |
- ids: List of ids as strings
|
37 |
- uri_format: Format to be applied for URI
|
38 |
- uri_fields: Optional parameter for field ids
|
39 |
|
40 |
"""
|
41 |
(field_id, field_uri) = uri_fields |
42 |
|
43 |
def _MapId(m_id): |
44 |
return { field_id: m_id, field_uri: uri_format % m_id, }
|
45 |
|
46 |
# Make sure the result is sorted, makes it nicer to look at and simplifies
|
47 |
# unittests.
|
48 |
ids.sort() |
49 |
|
50 |
return map(_MapId, ids) |
51 |
|
52 |
|
53 |
def ExtractField(sequence, index): |
54 |
"""Creates a list containing one column out of a list of lists.
|
55 |
|
56 |
Args:
|
57 |
- sequence: Sequence of lists
|
58 |
- index: Index of field
|
59 |
|
60 |
"""
|
61 |
return map(lambda item: item[index], sequence) |
62 |
|
63 |
|
64 |
def MapFields(names, data): |
65 |
"""Maps two lists into one dictionary.
|
66 |
|
67 |
Args:
|
68 |
- names: Field names (list of strings)
|
69 |
- data: Field data (list)
|
70 |
|
71 |
Example:
|
72 |
>>> MapFields(["a", "b"], ["foo", 123])
|
73 |
{'a': 'foo', 'b': 123}
|
74 |
|
75 |
"""
|
76 |
if len(names) != len(data): |
77 |
raise AttributeError("Names and data must have the same length") |
78 |
return dict(zip(names, data)) |
79 |
|
80 |
|
81 |
def _Tags_GET(kind, name=""): |
82 |
"""Helper function to retrieve tags.
|
83 |
|
84 |
"""
|
85 |
op = ganeti.opcodes.OpGetTags(kind=kind, name=name) |
86 |
tags = ganeti.cli.SubmitOpCode(op) |
87 |
return list(tags) |
88 |
|
89 |
|
90 |
def _Tags_POST(kind, tags, name=""): |
91 |
"""Helper function to set tags.
|
92 |
|
93 |
"""
|
94 |
cl = luxi.Client() |
95 |
return cl.SubmitJob([ganeti.opcodes.OpAddTags(kind=kind, name=name,
|
96 |
tags=tags)]) |
97 |
|
98 |
|
99 |
def _Tags_DELETE(kind, tags, name=""): |
100 |
"""Helper function to delete tags.
|
101 |
|
102 |
"""
|
103 |
cl = luxi.Client() |
104 |
return cl.SubmitJob([ganeti.opcodes.OpDelTags(kind=kind, name=name,
|
105 |
tags=tags)]) |
106 |
|
107 |
|
108 |
def MapBulkFields(itemslist, fields): |
109 |
"""Map value to field name in to one dictionary.
|
110 |
|
111 |
Args:
|
112 |
- itemslist: A list of items values
|
113 |
- instance: A list of items names
|
114 |
|
115 |
Returns:
|
116 |
A list of mapped dictionaries
|
117 |
"""
|
118 |
items_details = [] |
119 |
for item in itemslist: |
120 |
mapped = MapFields(fields, item) |
121 |
items_details.append(mapped) |
122 |
return items_details
|
123 |
|
124 |
|
125 |
class R_Generic(object): |
126 |
"""Generic class for resources.
|
127 |
|
128 |
"""
|
129 |
def __init__(self, request, items, queryargs, post_data): |
130 |
"""Generic resource constructor.
|
131 |
|
132 |
Args:
|
133 |
request: HTTPRequestHandler object
|
134 |
items: a list with variables encoded in the URL
|
135 |
queryargs: a dictionary with additional options from URL
|
136 |
|
137 |
"""
|
138 |
self.request = request
|
139 |
self.items = items
|
140 |
self.queryargs = queryargs
|
141 |
self.post_data = post_data
|