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.
32 from ganeti import constants
33 from ganeti import utils
35 from ganeti.rapi import baserlib
36 from ganeti.rapi import httperror
39 class R_version(baserlib.R_Generic):
42 This resource should be used to determine the remote API version and to adapt
49 """Returns the remote API version.
52 return constants.RAPI_VERSION
55 class R_tags(baserlib.R_Generic):
64 """Returns a list of all cluster tags.
66 Example: ["tag1", "tag2", "tag3"]
69 return baserlib._Tags_GET(constants.TAG_CLUSTER)
72 class R_info(baserlib.R_Generic):
79 """Returns cluster information.
83 "name": "cluster1.example.com",
84 "software_version": "1.2.4",
87 "master": "node1.example.com",
92 "hypervisor_type": "xen-3.0",
93 "protocol_version": 12
97 op = ganeti.opcodes.OpQueryClusterInfo()
98 return ganeti.cli.SubmitOpCode(op)
101 class R_nodes(baserlib.R_Generic):
107 def _GetDetails(self, nodeslist):
108 """Returns detailed instance data for bulk output.
111 instance: A list of nodes names.
114 A list of nodes properties
117 fields = ["name","dtotal", "dfree",
118 "mtotal", "mnode", "mfree",
119 "pinst_cnt", "sinst_cnt", "tags"]
121 op = ganeti.opcodes.OpQueryNodes(output_fields=fields,
123 result = ganeti.cli.SubmitOpCode(op)
127 mapped = baserlib.MapFields(fields, node)
128 nodes_details.append(mapped)
132 """Returns a list of all nodes.
135 A dictionary with 'name' and 'uri' keys for each of them.
139 "name": "node1.example.com",
140 "uri": "\/instances\/node1.example.com"
143 "name": "node2.example.com",
144 "uri": "\/instances\/node2.example.com"
147 If the optional 'bulk' argument is provided and set to 'true'
148 value (i.e '?bulk=1'), the output contains detailed
149 information about nodes as a list.
156 "name": "www.example.com",
167 op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
168 nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
170 if 'bulk' in self.queryargs:
171 return self._GetDetails(nodeslist)
173 return baserlib.BuildUriList(nodeslist, "/nodes/%s")
176 class R_nodes_name(baserlib.R_Generic):
177 """/nodes/[node_name] resources.
180 DOC_URI = "/nodes/[node_name]"
183 """Send information about a node.
186 node_name = self.items[0]
187 fields = ["name","dtotal", "dfree",
188 "mtotal", "mnode", "mfree",
189 "pinst_cnt", "sinst_cnt", "tags"]
191 op = ganeti.opcodes.OpQueryNodes(output_fields=fields,
193 result = ganeti.cli.SubmitOpCode(op)
195 return baserlib.MapFields(fields, result[0])
198 class R_nodes_name_tags(baserlib.R_Generic):
199 """/nodes/[node_name]/tags resource.
201 Manages per-node tags.
204 DOC_URI = "/nodes/[node_name]/tags"
207 """Returns a list of node tags.
209 Example: ["tag1", "tag2", "tag3"]
212 return baserlib._Tags_GET(constants.TAG_NODE, name=self.items[0])
215 class R_instances(baserlib.R_Generic):
216 """/instances resource.
219 DOC_URI = "/instances"
221 def _GetDetails(self, instanceslist):
222 """Returns detailed instance data for bulk output.
225 instance: A list of instances names.
228 A list with instances properties.
231 fields = ["name", "os", "pnode", "snodes",
232 "admin_state", "admin_ram",
233 "disk_template", "ip", "mac", "bridge",
234 "sda_size", "sdb_size", "vcpus",
235 "oper_state", "status", "tags"]
237 op = ganeti.opcodes.OpQueryInstances(output_fields=fields,
239 result = ganeti.cli.SubmitOpCode(op)
241 instances_details = []
242 for instance in result:
243 mapped = baserlib.MapFields(fields, instance)
244 instances_details.append(mapped)
245 return instances_details
248 """Returns a list of all available instances.
251 A dictionary with 'name' and 'uri' keys for each of them.
255 "name": "web.example.com",
256 "uri": "\/instances\/web.example.com"
259 "name": "mail.example.com",
260 "uri": "\/instances\/mail.example.com"
263 If the optional 'bulk' argument is provided and set to 'true'
264 value (i.e '?bulk=1'), the output contains detailed
265 information about instances as a list.
271 "name": "web.example.com",
272 "tags": ["tag1", "tag2"],
275 "pnode": "node1.example.com",
276 "mac": "01:23:45:67:89:01",
278 "snodes": ["node2.example.com"],
279 "disk_template": "drbd",
290 op = ganeti.opcodes.OpQueryInstances(output_fields=["name"], names=[])
291 instanceslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
293 if 'bulk' in self.queryargs:
294 return self._GetDetails(instanceslist)
297 return baserlib.BuildUriList(instanceslist, "/instances/%s")
300 class R_instances_name(baserlib.R_Generic):
301 """/instances/[instance_name] resources.
304 DOC_URI = "/instances/[instance_name]"
307 """Send information about an instance.
310 instance_name = self.items[0]
311 fields = ["name", "os", "pnode", "snodes",
312 "admin_state", "admin_ram",
313 "disk_template", "ip", "mac", "bridge",
314 "sda_size", "sdb_size", "vcpus",
315 "oper_state", "status", "tags"]
317 op = ganeti.opcodes.OpQueryInstances(output_fields=fields,
318 names=[instance_name])
319 result = ganeti.cli.SubmitOpCode(op)
321 return baserlib.MapFields(fields, result[0])
324 class R_instances_name_tags(baserlib.R_Generic):
325 """/instances/[instance_name]/tags resource.
327 Manages per-instance tags.
330 DOC_URI = "/instances/[instance_name]/tags"
333 """Returns a list of instance tags.
335 Example: ["tag1", "tag2", "tag3"]
338 return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
341 class R_os(baserlib.R_Generic):
348 """Return a list of all OSes.
350 Can return error 500 in case of a problem.
352 Example: ["debian-etch"]
355 op = ganeti.opcodes.OpDiagnoseOS(output_fields=["name", "valid"],
357 diagnose_data = ganeti.cli.SubmitOpCode(op)
359 if not isinstance(diagnose_data, list):
360 raise httperror.HTTPInternalError(message="Can't get OS list")
362 return [row[0] for row in diagnose_data if row[1]]