Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / baserlib.py @ b5b67ef9

History | View | Annotate | Download (4.2 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
from ganeti import rapi
31

    
32

    
33
def BuildUriList(ids, uri_format, uri_fields=("name", "uri")):
34
  """Builds a URI list as used by index resources.
35

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
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
  @param sequence: sequence of lists
57
  @param index: index of field
58

59
  """
60
  return map(lambda item: item[index], sequence)
61

    
62

    
63
def MapFields(names, data):
64
  """Maps two lists into one dictionary.
65

66
  Example::
67
      >>> MapFields(["a", "b"], ["foo", 123])
68
      {'a': 'foo', 'b': 123}
69

70
  @param names: field names (list of strings)
71
  @param data: field data (list)
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=""):
80
  """Helper function to retrieve tags.
81

82
  """
83
  op = ganeti.opcodes.OpGetTags(kind=kind, name=name)
84
  tags = ganeti.cli.SubmitOpCode(op)
85
  return list(tags)
86

    
87

    
88
def _Tags_PUT(kind, tags, name=""):
89
  """Helper function to set tags.
90

91
  """
92
  cl = luxi.Client()
93
  return cl.SubmitJob([ganeti.opcodes.OpAddTags(kind=kind, name=name,
94
                                                tags=tags)])
95

    
96

    
97
def _Tags_DELETE(kind, tags, name=""):
98
  """Helper function to delete tags.
99

100
  """
101
  cl = luxi.Client()
102
  return cl.SubmitJob([ganeti.opcodes.OpDelTags(kind=kind, name=name,
103
                                                tags=tags)])
104

    
105

    
106
def MapBulkFields(itemslist, fields):
107
  """Map value to field name in to one dictionary.
108

109
  @param itemslist: a list of items values
110
  @param fields: a list of items names
111

112
  @return: a list of mapped dictionaries
113

114
  """
115
  items_details = []
116
  for item in itemslist:
117
    mapped = MapFields(fields, item)
118
    items_details.append(mapped)
119
  return items_details
120

    
121

    
122
def MakeParamsDict(opts, params):
123
  """Makes params dictionary out of a option set.
124

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
127
  from constants.
128

129
  @type opts: dict
130
  @param opts: selected options
131
  @type params: frozenset
132
  @param params: subset of options
133
  @rtype: dict
134
  @return: dictionary of options, filtered by given subset.
135

136
  """
137
  result = {}
138

    
139
  for p in params:
140
    try:
141
      value = opts[p]
142
    except KeyError:
143
      continue
144
    result[p] = value
145

    
146
  return result
147

    
148

    
149
class R_Generic(object):
150
  """Generic class for resources.
151

152
  """
153
  # Default permission requirements
154
  GET_ACCESS = []
155
  PUT_ACCESS = [rapi.RAPI_ACCESS_WRITE]
156
  POST_ACCESS = [rapi.RAPI_ACCESS_WRITE]
157
  DELETE_ACCESS = [rapi.RAPI_ACCESS_WRITE]
158

    
159
  def __init__(self, items, queryargs, req):
160
    """Generic resource constructor.
161

162
    @param items: a list with variables encoded in the URL
163
    @param queryargs: a dictionary with additional options from URL
164

165
    """
166
    self.items = items
167
    self.queryargs = queryargs
168
    self.req = req
169
    self.sn = None
170

    
171
  def getSerialNumber(self):
172
    """Get Serial Number.
173

174
    """
175
    return self.sn