connector.py 4.3 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
26
27
#
#

# 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 connection map.

"""

import cgi
import re

Michael Hanselmann's avatar
Michael Hanselmann committed
28
29
from ganeti import constants
from ganeti import http
30

Michael Hanselmann's avatar
Michael Hanselmann committed
31
from ganeti.rapi import baserlib
32
33
from ganeti.rapi import rlib2

Michael Hanselmann's avatar
Michael Hanselmann committed
34
# the connection map is created at the end of this file
35
36
37
38
39
40
41
42
43
44
CONNECTOR = {}


class Mapper:
  """Map resource to method.

  """
  def __init__(self, connector=CONNECTOR):
    """Resource mapper constructor.

Iustin Pop's avatar
Iustin Pop committed
45
    @param connector: a dictionary, mapping method name with URL path regexp
46
47
48
49
50
51
52

    """
    self._connector = connector

  def getController(self, uri):
    """Find method for a given URI.

Iustin Pop's avatar
Iustin Pop committed
53
    @param uri: string with URI
54

Iustin Pop's avatar
Iustin Pop committed
55
56
57
58
59
    @return: None if no method is found or a tuple containing
        the following fields:
            - method: name of method mapped to URI
            - items: a list of variable intems in the path
            - args: a dictionary with additional parameters from URL
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84

    """
    if '?' in uri:
      (path, query) = uri.split('?', 1)
      args = cgi.parse_qs(query)
    else:
      path = uri
      query = None
      args = {}

    result = None

    for key, handler in self._connector.iteritems():
      # Regex objects
      if hasattr(key, "match"):
        m = key.match(path)
        if m:
          result = (handler, list(m.groups()), args)
          break

      # String objects
      elif key == path:
        result = (handler, [], args)
        break

85
    if result:
86
87
      return result
    else:
88
      raise http.HttpNotFound()
89
90
91
92
93
94
95
96


class R_root(baserlib.R_Generic):
  """/ resource.

  """
  def GET(self):
    """Show the list of mapped resources.
Michael Hanselmann's avatar
Michael Hanselmann committed
97

Iustin Pop's avatar
Iustin Pop committed
98
    @return: a dictionary with 'name' and 'uri' keys for each of them.
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

    """
    root_pattern = re.compile('^R_([a-zA-Z0-9]+)$')

    rootlist = []
    for handler in CONNECTOR.values():
      m = root_pattern.match(handler.__name__)
      if m:
        name = m.group(1)
        if name != 'root':
          rootlist.append(name)

    return baserlib.BuildUriList(rootlist, "/%s")


114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def _getResources(id):
  """Return a list of resources underneath given id.

  This is to generalize querying of version resources lists.

  @return: a list of resources names.

  """
  r_pattern = re.compile('^R_%s_([a-zA-Z0-9]+)$' % id)

  rlist = []
  for handler in CONNECTOR.values():
    m = r_pattern.match(handler.__name__)
    if m:
      name = m.group(1)
      rlist.append(name)

  return rlist


class R_2(baserlib.R_Generic):
Iustin Pop's avatar
Iustin Pop committed
135
  """ /2 resource, the root of the version 2 API.
136
137
138
139
140
141
142
143
144
145
146

  """
  def GET(self):
    """Show the list of mapped resources.

    @return: a dictionary with 'name' and 'uri' keys for each of them.

    """
    return baserlib.BuildUriList(_getResources("2"), "/2/%s")


147
148
149
CONNECTOR.update({
  "/": R_root,

Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
150
  "/version": rlib2.R_version,
151

152
  "/2": R_2,
153
154
  "/2/jobs": rlib2.R_2_jobs,
  "/2/nodes": rlib2.R_2_nodes,
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
155
  re.compile(r'^/2/nodes/([\w\._-]+)$'): rlib2.R_2_nodes_name,
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
156
  re.compile(r'^/2/nodes/([\w\._-]+)/tags$'): rlib2.R_2_nodes_name_tags,
157
  re.compile(r'^/2/nodes/([\w\._-]+)/role$'): rlib2.R_2_nodes_name_role,
158
  "/2/instances": rlib2.R_2_instances,
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
159
  re.compile(r'^/2/instances/([\w\._-]+)$'): rlib2.R_2_instances_name,
160
  re.compile(r'^/2/instances/([\w\._-]+)/tags$'): rlib2.R_2_instances_name_tags,
161
162
  re.compile(r'^/2/instances/([\w\._-]+)/reboot$'):
      rlib2.R_2_instances_name_reboot,
163
164
165
166
  re.compile(r'^/2/instances/([\w\._-]+)/shutdown$'):
      rlib2.R_2_instances_name_shutdown,
  re.compile(r'^/2/instances/([\w\._-]+)/startup$'):
      rlib2.R_2_instances_name_startup,
167
  re.compile(r'/2/jobs/(%s)$' % constants.JOB_ID_TEMPLATE): rlib2.R_2_jobs_id,
Oleksiy Mishchenko's avatar
Oleksiy Mishchenko committed
168
169
170
  "/2/tags": rlib2.R_2_tags,
  "/2/info": rlib2.R_2_info,
  "/2/os": rlib2.R_2_os,
171
  })