Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / baserlib.py @ 441e7cfd

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=None):
82
  """Helper function to retrieve tags.
83

84
  """
85
  if name is None:
86
    # Do not cause "missing parameter" error, which happens if a parameter
87
    # is None.
88
    name = ""
89
  op = ganeti.opcodes.OpGetTags(kind=kind, name=name)
90
  tags = ganeti.cli.SubmitOpCode(op)
91
  return list(tags)
92

    
93

    
94
def _Tags_POST(kind, tags, name=None):
95
  """Helper function to set tags.
96

97
  """
98
  if name is None:
99
    # Do not cause "missing parameter" error, which happens if a parameter
100
    # is None.
101
    name = ""
102
  cl = luxi.Client()
103
  return cl.SubmitJob([ganeti.opcodes.OpAddTags(kind=kind, name=name,
104
                                                tags=tags)])
105

    
106

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

110
  Args:
111
  - itemslist: A list of items values
112
  - instance: A list of items names
113

114
  Returns:
115
    A list of mapped dictionaries
116
  """
117
  items_details = []
118
  for item in itemslist:
119
    mapped = MapFields(fields, item)
120
    items_details.append(mapped)
121
  return items_details
122

    
123

    
124
class R_Generic(object):
125
  """Generic class for resources.
126

127
  """
128
  def __init__(self, request, items, queryargs, post_data):
129
    """Generic resource constructor.
130

131
    Args:
132
      request: HTTPRequestHandler object
133
      items: a list with variables encoded in the URL
134
      queryargs: a dictionary with additional options from URL
135

136
    """
137
    self.request = request
138
    self.items = items
139
    self.queryargs = queryargs
140
    self.post_data = post_data