root / lib / rapi / connector.py @ d1908b41
History | View | Annotate | Download (4.3 kB)
1 | 10b207d4 | Oleksiy Mishchenko | #
|
---|---|---|---|
2 | 10b207d4 | Oleksiy Mishchenko | #
|
3 | 10b207d4 | Oleksiy Mishchenko | |
4 | 10b207d4 | Oleksiy Mishchenko | # Copyright (C) 2006, 2007, 2008 Google Inc.
|
5 | 10b207d4 | Oleksiy Mishchenko | #
|
6 | 10b207d4 | Oleksiy Mishchenko | # This program is free software; you can redistribute it and/or modify
|
7 | 10b207d4 | Oleksiy Mishchenko | # it under the terms of the GNU General Public License as published by
|
8 | 10b207d4 | Oleksiy Mishchenko | # the Free Software Foundation; either version 2 of the License, or
|
9 | 10b207d4 | Oleksiy Mishchenko | # (at your option) any later version.
|
10 | 10b207d4 | Oleksiy Mishchenko | #
|
11 | 10b207d4 | Oleksiy Mishchenko | # This program is distributed in the hope that it will be useful, but
|
12 | 10b207d4 | Oleksiy Mishchenko | # WITHOUT ANY WARRANTY; without even the implied warranty of
|
13 | 10b207d4 | Oleksiy Mishchenko | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
14 | 10b207d4 | Oleksiy Mishchenko | # General Public License for more details.
|
15 | 10b207d4 | Oleksiy Mishchenko | #
|
16 | 10b207d4 | Oleksiy Mishchenko | # You should have received a copy of the GNU General Public License
|
17 | 10b207d4 | Oleksiy Mishchenko | # along with this program; if not, write to the Free Software
|
18 | 10b207d4 | Oleksiy Mishchenko | # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
|
19 | 10b207d4 | Oleksiy Mishchenko | # 02110-1301, USA.
|
20 | 10b207d4 | Oleksiy Mishchenko | |
21 | 10b207d4 | Oleksiy Mishchenko | """Remote API connection map.
|
22 | 10b207d4 | Oleksiy Mishchenko |
|
23 | 10b207d4 | Oleksiy Mishchenko | """
|
24 | 10b207d4 | Oleksiy Mishchenko | |
25 | 10b207d4 | Oleksiy Mishchenko | import cgi |
26 | 10b207d4 | Oleksiy Mishchenko | import re |
27 | 10b207d4 | Oleksiy Mishchenko | |
28 | a2f92677 | Michael Hanselmann | from ganeti import constants |
29 | a2f92677 | Michael Hanselmann | from ganeti import http |
30 | 10b207d4 | Oleksiy Mishchenko | |
31 | a2f92677 | Michael Hanselmann | from ganeti.rapi import baserlib |
32 | 10b207d4 | Oleksiy Mishchenko | from ganeti.rapi import rlib2 |
33 | 10b207d4 | Oleksiy Mishchenko | |
34 | a2f92677 | Michael Hanselmann | # the connection map is created at the end of this file
|
35 | 10b207d4 | Oleksiy Mishchenko | CONNECTOR = {} |
36 | 10b207d4 | Oleksiy Mishchenko | |
37 | 10b207d4 | Oleksiy Mishchenko | |
38 | 10b207d4 | Oleksiy Mishchenko | class Mapper: |
39 | 10b207d4 | Oleksiy Mishchenko | """Map resource to method.
|
40 | 10b207d4 | Oleksiy Mishchenko |
|
41 | 10b207d4 | Oleksiy Mishchenko | """
|
42 | 10b207d4 | Oleksiy Mishchenko | def __init__(self, connector=CONNECTOR): |
43 | 10b207d4 | Oleksiy Mishchenko | """Resource mapper constructor.
|
44 | 10b207d4 | Oleksiy Mishchenko |
|
45 | c41eea6e | Iustin Pop | @param connector: a dictionary, mapping method name with URL path regexp
|
46 | 10b207d4 | Oleksiy Mishchenko |
|
47 | 10b207d4 | Oleksiy Mishchenko | """
|
48 | 10b207d4 | Oleksiy Mishchenko | self._connector = connector
|
49 | 10b207d4 | Oleksiy Mishchenko | |
50 | 10b207d4 | Oleksiy Mishchenko | def getController(self, uri): |
51 | 10b207d4 | Oleksiy Mishchenko | """Find method for a given URI.
|
52 | 10b207d4 | Oleksiy Mishchenko |
|
53 | c41eea6e | Iustin Pop | @param uri: string with URI
|
54 | 10b207d4 | Oleksiy Mishchenko |
|
55 | c41eea6e | Iustin Pop | @return: None if no method is found or a tuple containing
|
56 | c41eea6e | Iustin Pop | the following fields:
|
57 | c41eea6e | Iustin Pop | - method: name of method mapped to URI
|
58 | c41eea6e | Iustin Pop | - items: a list of variable intems in the path
|
59 | c41eea6e | Iustin Pop | - args: a dictionary with additional parameters from URL
|
60 | 10b207d4 | Oleksiy Mishchenko |
|
61 | 10b207d4 | Oleksiy Mishchenko | """
|
62 | 10b207d4 | Oleksiy Mishchenko | if '?' in uri: |
63 | 10b207d4 | Oleksiy Mishchenko | (path, query) = uri.split('?', 1) |
64 | 10b207d4 | Oleksiy Mishchenko | args = cgi.parse_qs(query) |
65 | 10b207d4 | Oleksiy Mishchenko | else:
|
66 | 10b207d4 | Oleksiy Mishchenko | path = uri |
67 | 10b207d4 | Oleksiy Mishchenko | query = None
|
68 | 10b207d4 | Oleksiy Mishchenko | args = {} |
69 | 10b207d4 | Oleksiy Mishchenko | |
70 | 10b207d4 | Oleksiy Mishchenko | result = None
|
71 | 10b207d4 | Oleksiy Mishchenko | |
72 | 10b207d4 | Oleksiy Mishchenko | for key, handler in self._connector.iteritems(): |
73 | 10b207d4 | Oleksiy Mishchenko | # Regex objects
|
74 | 10b207d4 | Oleksiy Mishchenko | if hasattr(key, "match"): |
75 | 10b207d4 | Oleksiy Mishchenko | m = key.match(path) |
76 | 10b207d4 | Oleksiy Mishchenko | if m:
|
77 | 10b207d4 | Oleksiy Mishchenko | result = (handler, list(m.groups()), args)
|
78 | 10b207d4 | Oleksiy Mishchenko | break
|
79 | 10b207d4 | Oleksiy Mishchenko | |
80 | 10b207d4 | Oleksiy Mishchenko | # String objects
|
81 | 10b207d4 | Oleksiy Mishchenko | elif key == path:
|
82 | 10b207d4 | Oleksiy Mishchenko | result = (handler, [], args) |
83 | 10b207d4 | Oleksiy Mishchenko | break
|
84 | 10b207d4 | Oleksiy Mishchenko | |
85 | a0638838 | Oleksiy Mishchenko | if result:
|
86 | 10b207d4 | Oleksiy Mishchenko | return result
|
87 | 10b207d4 | Oleksiy Mishchenko | else:
|
88 | 84f2756e | Michael Hanselmann | raise http.HttpNotFound()
|
89 | 10b207d4 | Oleksiy Mishchenko | |
90 | 10b207d4 | Oleksiy Mishchenko | |
91 | 10b207d4 | Oleksiy Mishchenko | class R_root(baserlib.R_Generic): |
92 | 10b207d4 | Oleksiy Mishchenko | """/ resource.
|
93 | 10b207d4 | Oleksiy Mishchenko |
|
94 | 10b207d4 | Oleksiy Mishchenko | """
|
95 | 10b207d4 | Oleksiy Mishchenko | DOC_URI = "/"
|
96 | 10b207d4 | Oleksiy Mishchenko | |
97 | 10b207d4 | Oleksiy Mishchenko | def GET(self): |
98 | 10b207d4 | Oleksiy Mishchenko | """Show the list of mapped resources.
|
99 | a2f92677 | Michael Hanselmann |
|
100 | c41eea6e | Iustin Pop | @return: a dictionary with 'name' and 'uri' keys for each of them.
|
101 | 10b207d4 | Oleksiy Mishchenko |
|
102 | 10b207d4 | Oleksiy Mishchenko | """
|
103 | 10b207d4 | Oleksiy Mishchenko | root_pattern = re.compile('^R_([a-zA-Z0-9]+)$')
|
104 | 10b207d4 | Oleksiy Mishchenko | |
105 | 10b207d4 | Oleksiy Mishchenko | rootlist = [] |
106 | 10b207d4 | Oleksiy Mishchenko | for handler in CONNECTOR.values(): |
107 | 10b207d4 | Oleksiy Mishchenko | m = root_pattern.match(handler.__name__) |
108 | 10b207d4 | Oleksiy Mishchenko | if m:
|
109 | 10b207d4 | Oleksiy Mishchenko | name = m.group(1)
|
110 | 10b207d4 | Oleksiy Mishchenko | if name != 'root': |
111 | 10b207d4 | Oleksiy Mishchenko | rootlist.append(name) |
112 | 10b207d4 | Oleksiy Mishchenko | |
113 | 10b207d4 | Oleksiy Mishchenko | return baserlib.BuildUriList(rootlist, "/%s") |
114 | 10b207d4 | Oleksiy Mishchenko | |
115 | 10b207d4 | Oleksiy Mishchenko | |
116 | fc72a3a3 | Oleksiy Mishchenko | def _getResources(id): |
117 | fc72a3a3 | Oleksiy Mishchenko | """Return a list of resources underneath given id.
|
118 | fc72a3a3 | Oleksiy Mishchenko |
|
119 | fc72a3a3 | Oleksiy Mishchenko | This is to generalize querying of version resources lists.
|
120 | fc72a3a3 | Oleksiy Mishchenko |
|
121 | fc72a3a3 | Oleksiy Mishchenko | @return: a list of resources names.
|
122 | fc72a3a3 | Oleksiy Mishchenko |
|
123 | fc72a3a3 | Oleksiy Mishchenko | """
|
124 | fc72a3a3 | Oleksiy Mishchenko | r_pattern = re.compile('^R_%s_([a-zA-Z0-9]+)$' % id) |
125 | fc72a3a3 | Oleksiy Mishchenko | |
126 | fc72a3a3 | Oleksiy Mishchenko | rlist = [] |
127 | fc72a3a3 | Oleksiy Mishchenko | for handler in CONNECTOR.values(): |
128 | fc72a3a3 | Oleksiy Mishchenko | m = r_pattern.match(handler.__name__) |
129 | fc72a3a3 | Oleksiy Mishchenko | if m:
|
130 | fc72a3a3 | Oleksiy Mishchenko | name = m.group(1)
|
131 | fc72a3a3 | Oleksiy Mishchenko | rlist.append(name) |
132 | fc72a3a3 | Oleksiy Mishchenko | |
133 | fc72a3a3 | Oleksiy Mishchenko | return rlist
|
134 | fc72a3a3 | Oleksiy Mishchenko | |
135 | fc72a3a3 | Oleksiy Mishchenko | |
136 | fc72a3a3 | Oleksiy Mishchenko | class R_2(baserlib.R_Generic): |
137 | bf4a90af | Iustin Pop | """ /2 resource, the root of the version 2 API.
|
138 | fc72a3a3 | Oleksiy Mishchenko |
|
139 | fc72a3a3 | Oleksiy Mishchenko | """
|
140 | fc72a3a3 | Oleksiy Mishchenko | DOC_URI = "/2"
|
141 | fc72a3a3 | Oleksiy Mishchenko | |
142 | fc72a3a3 | Oleksiy Mishchenko | def GET(self): |
143 | fc72a3a3 | Oleksiy Mishchenko | """Show the list of mapped resources.
|
144 | fc72a3a3 | Oleksiy Mishchenko |
|
145 | fc72a3a3 | Oleksiy Mishchenko | @return: a dictionary with 'name' and 'uri' keys for each of them.
|
146 | fc72a3a3 | Oleksiy Mishchenko |
|
147 | fc72a3a3 | Oleksiy Mishchenko | """
|
148 | fc72a3a3 | Oleksiy Mishchenko | return baserlib.BuildUriList(_getResources("2"), "/2/%s") |
149 | fc72a3a3 | Oleksiy Mishchenko | |
150 | fc72a3a3 | Oleksiy Mishchenko | |
151 | 10b207d4 | Oleksiy Mishchenko | CONNECTOR.update({ |
152 | 10b207d4 | Oleksiy Mishchenko | "/": R_root,
|
153 | 10b207d4 | Oleksiy Mishchenko | |
154 | 4e5a68f8 | Oleksiy Mishchenko | "/version": rlib2.R_version,
|
155 | 10b207d4 | Oleksiy Mishchenko | |
156 | fc72a3a3 | Oleksiy Mishchenko | "/2": R_2,
|
157 | 10b207d4 | Oleksiy Mishchenko | "/2/jobs": rlib2.R_2_jobs,
|
158 | 10b207d4 | Oleksiy Mishchenko | "/2/nodes": rlib2.R_2_nodes,
|
159 | 4e5a68f8 | Oleksiy Mishchenko | re.compile(r'^/2/nodes/([\w\._-]+)$'): rlib2.R_2_nodes_name,
|
160 | 18cb43a2 | Oleksiy Mishchenko | re.compile(r'^/2/nodes/([\w\._-]+)/tags$'): rlib2.R_2_nodes_name_tags,
|
161 | 441e7cfd | Oleksiy Mishchenko | "/2/instances": rlib2.R_2_instances,
|
162 | 4e5a68f8 | Oleksiy Mishchenko | re.compile(r'^/2/instances/([\w\._-]+)$'): rlib2.R_2_instances_name,
|
163 | 441e7cfd | Oleksiy Mishchenko | re.compile(r'^/2/instances/([\w\._-]+)/tags$'): rlib2.R_2_instances_name_tags,
|
164 | 2276aa29 | Oleksiy Mishchenko | re.compile(r'^/2/instances/([\w\._-]+)/reboot$'):
|
165 | 2276aa29 | Oleksiy Mishchenko | rlib2.R_2_instances_name_reboot, |
166 | 0c55c24b | Oleksiy Mishchenko | re.compile(r'^/2/instances/([\w\._-]+)/shutdown$'):
|
167 | 0c55c24b | Oleksiy Mishchenko | rlib2.R_2_instances_name_shutdown, |
168 | 0c55c24b | Oleksiy Mishchenko | re.compile(r'^/2/instances/([\w\._-]+)/startup$'):
|
169 | 0c55c24b | Oleksiy Mishchenko | rlib2.R_2_instances_name_startup, |
170 | 10b207d4 | Oleksiy Mishchenko | re.compile(r'/2/jobs/(%s)$' % constants.JOB_ID_TEMPLATE): rlib2.R_2_jobs_id,
|
171 | 4e5a68f8 | Oleksiy Mishchenko | "/2/tags": rlib2.R_2_tags,
|
172 | 4e5a68f8 | Oleksiy Mishchenko | "/2/info": rlib2.R_2_info,
|
173 | 4e5a68f8 | Oleksiy Mishchenko | "/2/os": rlib2.R_2_os,
|
174 | 10b207d4 | Oleksiy Mishchenko | }) |