baserlib.py 7.39 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#
#

# Copyright (C) 2006, 2007, 2008 Google Inc.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.


"""Remote API base resources library.

"""

Iustin Pop's avatar
Iustin Pop committed
26
27
28
29
# pylint: disable-msg=C0103

# C0103: Invalid name, since the R_* names are not conforming

Iustin Pop's avatar
Iustin Pop committed
30
31
import logging

32
from ganeti import luxi
33
from ganeti import rapi
34
from ganeti import http
35
36
from ganeti import ssconf
from ganeti import constants
Iustin Pop's avatar
Iustin Pop committed
37
38
from ganeti import opcodes
from ganeti import errors
39

40
41
42
43

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

Iustin Pop's avatar
Iustin Pop committed
44
45
46
  @param ids: list of ids as strings
  @param uri_format: format to be applied for URI
  @param uri_fields: optional parameter for field IDs
47
48
49

  """
  (field_id, field_uri) = uri_fields
50

51
52
53
54
55
56
57
58
59
60
61
62
63
  def _MapId(m_id):
    return { field_id: m_id, field_uri: uri_format % m_id, }

  # Make sure the result is sorted, makes it nicer to look at and simplifies
  # unittests.
  ids.sort()

  return map(_MapId, ids)


def ExtractField(sequence, index):
  """Creates a list containing one column out of a list of lists.

Iustin Pop's avatar
Iustin Pop committed
64
65
  @param sequence: sequence of lists
  @param index: index of field
66
67
68
69
70
71
72
73

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


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

Iustin Pop's avatar
Iustin Pop committed
74
75
76
  Example::
      >>> MapFields(["a", "b"], ["foo", 123])
      {'a': 'foo', 'b': 123}
77

Iustin Pop's avatar
Iustin Pop committed
78
79
  @param names: field names (list of strings)
  @param data: field data (list)
80
81
82
83

  """
  if len(names) != len(data):
    raise AttributeError("Names and data must have the same length")
84
  return dict(zip(names, data))
85
86


87
def _Tags_GET(kind, name=""):
88
89
90
  """Helper function to retrieve tags.

  """
91
92
  if kind == constants.TAG_INSTANCE or kind == constants.TAG_NODE:
    if not name:
Iustin Pop's avatar
Iustin Pop committed
93
94
      raise http.HttpBadRequest("Missing name on tag request")
    cl = GetClient()
95
96
97
98
99
100
101
102
103
104
105
106
    if kind == constants.TAG_INSTANCE:
      fn = cl.QueryInstances
    else:
      fn = cl.QueryNodes
    result = fn(names=[name], fields=["tags"], use_locking=False)
    if not result or not result[0]:
      raise http.HttpBadGateway("Invalid response from tag query")
    tags = result[0][0]
  elif kind == constants.TAG_CLUSTER:
    ssc = ssconf.SimpleStore()
    tags = ssc.GetClusterTags()

107
108
109
  return list(tags)


Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
110
def _Tags_PUT(kind, tags, name=""):
111
112
113
  """Helper function to set tags.

  """
Iustin Pop's avatar
Iustin Pop committed
114
  return SubmitJob([opcodes.OpAddTags(kind=kind, name=name, tags=tags)])
115
116


117
118
119
120
def _Tags_DELETE(kind, tags, name=""):
  """Helper function to delete tags.

  """
Iustin Pop's avatar
Iustin Pop committed
121
  return SubmitJob([opcodes.OpDelTags(kind=kind, name=name, tags=tags)])
122
123


124
125
126
def MapBulkFields(itemslist, fields):
  """Map value to field name in to one dictionary.

Iustin Pop's avatar
Iustin Pop committed
127
128
129
130
  @param itemslist: a list of items values
  @param fields: a list of items names

  @return: a list of mapped dictionaries
131
132
133
134
135
136
137
138
139

  """
  items_details = []
  for item in itemslist:
    mapped = MapFields(fields, item)
    items_details.append(mapped)
  return items_details


Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
140
def MakeParamsDict(opts, params):
Iustin Pop's avatar
Iustin Pop committed
141
  """Makes params dictionary out of a option set.
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166

  This function returns a dictionary needed for hv or be parameters. But only
  those fields which provided in the option set. Takes parameters frozensets
  from constants.

  @type opts: dict
  @param opts: selected options
  @type params: frozenset
  @param params: subset of options
  @rtype: dict
  @return: dictionary of options, filtered by given subset.

  """
  result = {}

  for p in params:
    try:
      value = opts[p]
    except KeyError:
      continue
    result[p] = value

  return result


Iustin Pop's avatar
Iustin Pop committed
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
def SubmitJob(op, cl=None):
  """Generic wrapper for submit job, for better http compatibility.

  @type op: list
  @param op: the list of opcodes for the job
  @type cl: None or luxi.Client
  @param cl: optional luxi client to use
  @rtype: string
  @return: the job ID

  """
  try:
    if cl is None:
      cl = GetClient()
    return cl.SubmitJob(op)
  except errors.JobQueueFull:
    raise http.HttpServiceUnavailable("Job queue is full, needs archiving")
  except errors.JobQueueDrainError:
    raise http.HttpServiceUnavailable("Job queue is drained, cannot submit")
  except luxi.NoMasterError, err:
    raise http.HttpBadGateway("Master seems to unreachable: %s" % str(err))
  except luxi.TimeoutError, err:
    raise http.HttpGatewayTimeout("Timeout while talking to the master"
                                  " daemon. Error: %s" % str(err))

def GetClient():
  """Geric wrapper for luxi.Client(), for better http compatiblity.

  """
  try:
    return luxi.Client()
  except luxi.NoMasterError, err:
    raise http.HttpBadGateway("Master seems to unreachable: %s" % str(err))


def FeedbackFn(ts, log_type, log_msg):
  """Feedback logging function for http case.

  We don't have a stdout for printing log messages, so log them to the
  http log at least.

  """
  logging.info("%s: %s", log_type, log_msg)


212
213
214
215
class R_Generic(object):
  """Generic class for resources.

  """
216
217
218
219
220
221
  # Default permission requirements
  GET_ACCESS = []
  PUT_ACCESS = [rapi.RAPI_ACCESS_WRITE]
  POST_ACCESS = [rapi.RAPI_ACCESS_WRITE]
  DELETE_ACCESS = [rapi.RAPI_ACCESS_WRITE]

222
  def __init__(self, items, queryargs, req):
223
224
    """Generic resource constructor.

Iustin Pop's avatar
Iustin Pop committed
225
226
    @param items: a list with variables encoded in the URL
    @param queryargs: a dictionary with additional options from URL
227
228
229
230

    """
    self.items = items
    self.queryargs = queryargs
231
    self.req = req
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
232
233
234
235
236
237
238
    self.sn = None

  def getSerialNumber(self):
    """Get Serial Number.

    """
    return self.sn
239
240
241
242
243
244
245
246
247
248
249
250
251

  def _checkIntVariable(self, name):
    """Return the parsed value of an int argument.

    """
    val = self.queryargs.get(name, 0)
    if isinstance(val, list):
      if val:
        val = val[0]
      else:
        val = 0
    try:
      val = int(val)
Michael Hanselmann's avatar
Michael Hanselmann committed
252
    except (ValueError, TypeError):
Iustin Pop's avatar
Iustin Pop committed
253
      raise http.HttpBadRequest("Invalid value for the"
254
255
256
                                " '%s' parameter" % (name,))
    return val

257
258
259
260
261
262
263
264
265
266
267
268
  def _checkStringVariable(self, name, default=None):
    """Return the parsed value of an int argument.

    """
    val = self.queryargs.get(name, default)
    if isinstance(val, list):
      if val:
        val = val[0]
      else:
        val = default
    return val

Iustin Pop's avatar
Iustin Pop committed
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
  def getBodyParameter(self, name, *args):
    """Check and return the value for a given parameter.

    If a second parameter is not given, an error will be returned,
    otherwise this parameter specifies the default value.

    @param name: the required parameter

    """
    if name in self.req.request_body:
      return self.req.request_body[name]
    elif args:
      return args[0]
    else:
      raise http.HttpBadRequest("Required parameter '%s' is missing" %
                                name)

286
287
288
289
290
291
292
293
294
295
296
  def useLocking(self):
    """Check if the request specifies locking.

    """
    return self._checkIntVariable('lock')

  def useBulk(self):
    """Check if the request specifies bulk querying.

    """
    return self._checkIntVariable('bulk')