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 I_FIELDS = ["name", "os", "pnode", "snodes",
40 "admin_state", "admin_ram",
41 "disk_template", "ip", "mac", "bridge",
42 "sda_size", "sdb_size", "vcpus",
43 "oper_state", "status", "tags"]
45 N_FIELDS = ["name","dtotal", "dfree",
46 "mtotal", "mnode", "mfree",
47 "pinst_cnt", "sinst_cnt", "tags"]
50 class R_version(baserlib.R_Generic):
53 This resource should be used to determine the remote API version and to adapt
60 """Returns the remote API version.
63 return constants.RAPI_VERSION
66 class R_tags(baserlib.R_Generic):
75 """Returns a list of all cluster tags.
77 Example: ["tag1", "tag2", "tag3"]
80 return baserlib._Tags_GET(constants.TAG_CLUSTER)
83 class R_info(baserlib.R_Generic):
90 """Returns cluster information.
94 "name": "cluster1.example.com",
95 "software_version": "1.2.4",
98 "master": "node1.example.com",
103 "hypervisor_type": "xen-3.0",
104 "protocol_version": 12
108 op = ganeti.opcodes.OpQueryClusterInfo()
109 return ganeti.cli.SubmitOpCode(op)
112 class R_nodes(baserlib.R_Generic):
119 """Returns a list of all nodes.
122 A dictionary with 'name' and 'uri' keys for each of them.
126 "name": "node1.example.com",
127 "uri": "\/instances\/node1.example.com"
130 "name": "node2.example.com",
131 "uri": "\/instances\/node2.example.com"
134 If the optional 'bulk' argument is provided and set to 'true'
135 value (i.e '?bulk=1'), the output contains detailed
136 information about nodes as a list.
143 "name": "www.example.com",
154 op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
155 nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
157 if 'bulk' in self.queryargs:
158 op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
160 result = ganeti.cli.SubmitOpCode(op)
161 return baserlib.MapBulkFields(result, N_FIELDS)
163 return baserlib.BuildUriList(nodeslist, "/nodes/%s")
166 class R_nodes_name(baserlib.R_Generic):
167 """/nodes/[node_name] resources.
170 DOC_URI = "/nodes/[node_name]"
173 """Send information about a node.
176 node_name = self.items[0]
177 op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
179 result = ganeti.cli.SubmitOpCode(op)
181 return baserlib.MapFields(N_FIELDS, result[0])
184 class R_nodes_name_tags(baserlib.R_Generic):
185 """/nodes/[node_name]/tags resource.
187 Manages per-node tags.
190 DOC_URI = "/nodes/[node_name]/tags"
193 """Returns a list of node tags.
195 Example: ["tag1", "tag2", "tag3"]
198 return baserlib._Tags_GET(constants.TAG_NODE, name=self.items[0])
201 class R_instances(baserlib.R_Generic):
202 """/instances resource.
205 DOC_URI = "/instances"
209 """Returns a list of all available instances.
212 A dictionary with 'name' and 'uri' keys for each of them.
216 "name": "web.example.com",
217 "uri": "\/instances\/web.example.com"
220 "name": "mail.example.com",
221 "uri": "\/instances\/mail.example.com"
224 If the optional 'bulk' argument is provided and set to 'true'
225 value (i.e '?bulk=1'), the output contains detailed
226 information about instances as a list.
232 "name": "web.example.com",
233 "tags": ["tag1", "tag2"],
236 "pnode": "node1.example.com",
237 "mac": "01:23:45:67:89:01",
239 "snodes": ["node2.example.com"],
240 "disk_template": "drbd",
251 op = ganeti.opcodes.OpQueryInstances(output_fields=["name"], names=[])
252 instanceslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
254 if 'bulk' in self.queryargs:
255 op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
257 result = ganeti.cli.SubmitOpCode(op)
258 return baserlib.MapBulkFields(result, I_FIELDS)
262 return baserlib.BuildUriList(instanceslist, "/instances/%s")
265 class R_instances_name(baserlib.R_Generic):
266 """/instances/[instance_name] resources.
269 DOC_URI = "/instances/[instance_name]"
272 """Send information about an instance.
275 instance_name = self.items[0]
276 op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
277 names=[instance_name])
278 result = ganeti.cli.SubmitOpCode(op)
280 return baserlib.MapFields(I_FIELDS, result[0])
283 class R_instances_name_tags(baserlib.R_Generic):
284 """/instances/[instance_name]/tags resource.
286 Manages per-instance tags.
289 DOC_URI = "/instances/[instance_name]/tags"
292 """Returns a list of instance tags.
294 Example: ["tag1", "tag2", "tag3"]
297 return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
300 class R_os(baserlib.R_Generic):
307 """Return a list of all OSes.
309 Can return error 500 in case of a problem.
311 Example: ["debian-etch"]
314 op = ganeti.opcodes.OpDiagnoseOS(output_fields=["name", "valid"],
316 diagnose_data = ganeti.cli.SubmitOpCode(op)
318 if not isinstance(diagnose_data, list):
319 raise httperror.HTTPInternalError(message="Can't get OS list")
321 return [row[0] for row in diagnose_data if row[1]]