4 # Copyright (C) 2006, 2007, 2008 Google Inc.
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.
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.
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
22 """Remote API version 1 resources library.
30 from ganeti import constants
31 from ganeti import http
33 from ganeti.rapi import baserlib
36 I_FIELDS = ["name", "os", "pnode", "snodes", "admin_state", "disk_template",
37 "ip", "mac", "bridge", "sda_size", "sdb_size", "beparams",
38 "oper_state", "status", "tags"]
40 N_FIELDS = ["name", "dtotal", "dfree",
41 "mtotal", "mnode", "mfree",
42 "pinst_cnt", "sinst_cnt", "tags"]
45 class R_version(baserlib.R_Generic):
48 This resource should be used to determine the remote API version and to adapt
55 """Returns the remote API version.
58 return constants.RAPI_VERSION
61 class R_tags(baserlib.R_Generic):
70 """Returns a list of all cluster tags.
72 Example: ["tag1", "tag2", "tag3"]
75 return baserlib._Tags_GET(constants.TAG_CLUSTER)
78 class R_info(baserlib.R_Generic):
85 """Returns cluster information.
89 "name": "cluster1.example.com",
90 "software_version": "1.2.4",
93 "master": "node1.example.com",
98 "hypervisor_type": "xen-pvm",
99 "protocol_version": 12
103 op = ganeti.opcodes.OpQueryClusterInfo()
104 return ganeti.cli.SubmitOpCode(op)
107 class R_nodes(baserlib.R_Generic):
114 """Returns a list of all nodes.
117 A dictionary with 'name' and 'uri' keys for each of them.
121 "name": "node1.example.com",
122 "uri": "\/instances\/node1.example.com"
125 "name": "node2.example.com",
126 "uri": "\/instances\/node2.example.com"
129 If the optional 'bulk' argument is provided and set to 'true'
130 value (i.e '?bulk=1'), the output contains detailed
131 information about nodes as a list.
138 "name": "www.example.com",
149 op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
150 nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
152 if 'bulk' in self.queryargs:
153 op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
155 result = ganeti.cli.SubmitOpCode(op)
156 return baserlib.MapBulkFields(result, N_FIELDS)
158 return baserlib.BuildUriList(nodeslist, "/nodes/%s")
161 class R_nodes_name(baserlib.R_Generic):
162 """/nodes/[node_name] resources.
165 DOC_URI = "/nodes/[node_name]"
168 """Send information about a node.
171 node_name = self.items[0]
172 op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
174 result = ganeti.cli.SubmitOpCode(op)
176 return baserlib.MapFields(N_FIELDS, result[0])
179 class R_nodes_name_tags(baserlib.R_Generic):
180 """/nodes/[node_name]/tags resource.
182 Manages per-node tags.
185 DOC_URI = "/nodes/[node_name]/tags"
188 """Returns a list of node tags.
190 Example: ["tag1", "tag2", "tag3"]
193 return baserlib._Tags_GET(constants.TAG_NODE, name=self.items[0])
196 class R_instances(baserlib.R_Generic):
197 """/instances resource.
200 DOC_URI = "/instances"
204 """Returns a list of all available instances.
207 A dictionary with 'name' and 'uri' keys for each of them.
211 "name": "web.example.com",
212 "uri": "\/instances\/web.example.com"
215 "name": "mail.example.com",
216 "uri": "\/instances\/mail.example.com"
219 If the optional 'bulk' argument is provided and set to 'true'
220 value (i.e '?bulk=1'), the output contains detailed
221 information about instances as a list.
227 "name": "web.example.com",
228 "tags": ["tag1", "tag2"],
231 "pnode": "node1.example.com",
232 "mac": "01:23:45:67:89:01",
234 "snodes": ["node2.example.com"],
235 "disk_template": "drbd",
246 op = ganeti.opcodes.OpQueryInstances(output_fields=["name"], names=[])
247 instanceslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
249 if 'bulk' in self.queryargs:
250 op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
252 result = ganeti.cli.SubmitOpCode(op)
253 return baserlib.MapBulkFields(result, I_FIELDS)
257 return baserlib.BuildUriList(instanceslist, "/instances/%s")
260 class R_instances_name(baserlib.R_Generic):
261 """/instances/[instance_name] resources.
264 DOC_URI = "/instances/[instance_name]"
267 """Send information about an instance.
270 instance_name = self.items[0]
271 op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
272 names=[instance_name])
273 result = ganeti.cli.SubmitOpCode(op)
275 return baserlib.MapFields(I_FIELDS, result[0])
278 """Removes an instance.
281 instance_name = self.items[0]
282 op = ganeti.opcodes.OpRemoveInstance(instance_name=instance_name,
283 ignore_failures=True)
284 job_id = ganeti.cli.SendJob([op])
289 class R_instances_name_tags(baserlib.R_Generic):
290 """/instances/[instance_name]/tags resource.
292 Manages per-instance tags.
295 DOC_URI = "/instances/[instance_name]/tags"
298 """Returns a list of instance tags.
300 Example: ["tag1", "tag2", "tag3"]
303 return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
306 class R_os(baserlib.R_Generic):
313 """Return a list of all OSes.
315 Can return error 500 in case of a problem.
317 Example: ["debian-etch"]
320 op = ganeti.opcodes.OpDiagnoseOS(output_fields=["name", "valid"],
322 diagnose_data = ganeti.cli.SubmitOpCode(op)
324 if not isinstance(diagnose_data, list):
325 raise http.HTTPInternalError(message="Can't get OS list")
327 return [row[0] for row in diagnose_data if row[1]]