baserlib.py 7.85 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


Iustin Pop's avatar
Iustin Pop committed
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)


Iustin Pop's avatar
Iustin Pop committed
110
def _Tags_PUT(kind, tags, name, dry_run):
111
112
113
  """Helper function to set tags.

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


Iustin Pop's avatar
Iustin Pop committed
118
def _Tags_DELETE(kind, tags, name, dry_run):
119
120
121
  """Helper function to delete tags.

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


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

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

  @return: a list of mapped dictionaries
133
134
135
136
137
138
139
140
141

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


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

  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
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
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))


204
def FeedbackFn(ts, log_type, log_msg): # pylint: disable-msg=W0613
Iustin Pop's avatar
Iustin Pop committed
205
206
207
208
209
  """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.

210
211
  @param ts: the timestamp (unused)

Iustin Pop's avatar
Iustin Pop committed
212
213
214
215
  """
  logging.info("%s: %s", log_type, log_msg)


216
217
218
219
class R_Generic(object):
  """Generic class for resources.

  """
220
221
222
223
224
225
  # Default permission requirements
  GET_ACCESS = []
  PUT_ACCESS = [rapi.RAPI_ACCESS_WRITE]
  POST_ACCESS = [rapi.RAPI_ACCESS_WRITE]
  DELETE_ACCESS = [rapi.RAPI_ACCESS_WRITE]

226
  def __init__(self, items, queryargs, req):
227
228
    """Generic resource constructor.

Iustin Pop's avatar
Iustin Pop committed
229
230
    @param items: a list with variables encoded in the URL
    @param queryargs: a dictionary with additional options from URL
231
232
233
234

    """
    self.items = items
    self.queryargs = queryargs
235
    self.req = req
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
236
237
238
239
240
241
242
    self.sn = None

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

    """
    return self.sn
243

244
  def _checkIntVariable(self, name, default=0):
245
246
247
    """Return the parsed value of an int argument.

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

261
262
263
264
265
266
267
268
269
270
271
  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
272

Iustin Pop's avatar
Iustin Pop committed
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
  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)

290
291
292
293
294
295
296
297
298
299
300
  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')
Iustin Pop's avatar
Iustin Pop committed
301

302
303
304
305
306
307
  def useForce(self):
    """Check if the request specifies a forced operation.

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

Iustin Pop's avatar
Iustin Pop committed
308
309
310
311
312
  def dryRun(self):
    """Check if the request specifies dry-run mode.

    """
    return self._checkIntVariable('dry-run')