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", "be/vcpus",
38 "be/memory", "be/auto_balance",
39 "oper_state", "status", "tags"]
41 N_FIELDS = ["name", "dtotal", "dfree",
42 "mtotal", "mnode", "mfree",
43 "pinst_cnt", "sinst_cnt", "tags"]
46 class R_version(baserlib.R_Generic):
49 This resource should be used to determine the remote API version and to adapt
56 """Returns the remote API version.
59 return constants.RAPI_VERSION
62 class R_tags(baserlib.R_Generic):
71 """Returns a list of all cluster tags.
73 Example: ["tag1", "tag2", "tag3"]
76 return baserlib._Tags_GET(constants.TAG_CLUSTER)
79 class R_info(baserlib.R_Generic):
86 """Returns cluster information.
90 "name": "cluster1.example.com",
91 "software_version": "1.2.4",
94 "master": "node1.example.com",
99 "hypervisor_type": "xen-pvm",
100 "protocol_version": 12
104 op = ganeti.opcodes.OpQueryClusterInfo()
105 return ganeti.cli.SubmitOpCode(op)
108 class R_nodes(baserlib.R_Generic):
115 """Returns a list of all nodes.
118 A dictionary with 'name' and 'uri' keys for each of them.
122 "name": "node1.example.com",
123 "uri": "\/instances\/node1.example.com"
126 "name": "node2.example.com",
127 "uri": "\/instances\/node2.example.com"
130 If the optional 'bulk' argument is provided and set to 'true'
131 value (i.e '?bulk=1'), the output contains detailed
132 information about nodes as a list.
139 "name": "www.example.com",
150 op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
151 nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
153 if 'bulk' in self.queryargs:
154 op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
156 result = ganeti.cli.SubmitOpCode(op)
157 return baserlib.MapBulkFields(result, N_FIELDS)
159 return baserlib.BuildUriList(nodeslist, "/nodes/%s")
162 class R_nodes_name(baserlib.R_Generic):
163 """/nodes/[node_name] resources.
166 DOC_URI = "/nodes/[node_name]"
169 """Send information about a node.
172 node_name = self.items[0]
173 op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
175 result = ganeti.cli.SubmitOpCode(op)
177 return baserlib.MapFields(N_FIELDS, result[0])
180 class R_nodes_name_tags(baserlib.R_Generic):
181 """/nodes/[node_name]/tags resource.
183 Manages per-node tags.
186 DOC_URI = "/nodes/[node_name]/tags"
189 """Returns a list of node tags.
191 Example: ["tag1", "tag2", "tag3"]
194 return baserlib._Tags_GET(constants.TAG_NODE, name=self.items[0])
197 class R_instances(baserlib.R_Generic):
198 """/instances resource.
201 DOC_URI = "/instances"
205 """Returns a list of all available instances.
208 A dictionary with 'name' and 'uri' keys for each of them.
212 "name": "web.example.com",
213 "uri": "\/instances\/web.example.com"
216 "name": "mail.example.com",
217 "uri": "\/instances\/mail.example.com"
220 If the optional 'bulk' argument is provided and set to 'true'
221 value (i.e '?bulk=1'), the output contains detailed
222 information about instances as a list.
228 "name": "web.example.com",
229 "tags": ["tag1", "tag2"],
232 "pnode": "node1.example.com",
233 "mac": "01:23:45:67:89:01",
235 "snodes": ["node2.example.com"],
236 "disk_template": "drbd",
247 op = ganeti.opcodes.OpQueryInstances(output_fields=["name"], names=[])
248 instanceslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
250 if 'bulk' in self.queryargs:
251 op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
253 result = ganeti.cli.SubmitOpCode(op)
254 return baserlib.MapBulkFields(result, I_FIELDS)
258 return baserlib.BuildUriList(instanceslist, "/instances/%s")
261 class R_instances_name(baserlib.R_Generic):
262 """/instances/[instance_name] resources.
265 DOC_URI = "/instances/[instance_name]"
268 """Send information about an instance.
271 instance_name = self.items[0]
272 op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
273 names=[instance_name])
274 result = ganeti.cli.SubmitOpCode(op)
276 return baserlib.MapFields(I_FIELDS, result[0])
279 """Removes an instance.
282 instance_name = self.items[0]
283 op = ganeti.opcodes.OpRemoveInstance(instance_name=instance_name,
284 ignore_failures=True)
285 job_id = ganeti.cli.SendJob([op])
290 class R_instances_name_tags(baserlib.R_Generic):
291 """/instances/[instance_name]/tags resource.
293 Manages per-instance tags.
296 DOC_URI = "/instances/[instance_name]/tags"
299 """Returns a list of instance tags.
301 Example: ["tag1", "tag2", "tag3"]
304 return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
307 class R_os(baserlib.R_Generic):
314 """Return a list of all OSes.
316 Can return error 500 in case of a problem.
318 Example: ["debian-etch"]
321 op = ganeti.opcodes.OpDiagnoseOS(output_fields=["name", "valid"],
323 diagnose_data = ganeti.cli.SubmitOpCode(op)
325 if not isinstance(diagnose_data, list):
326 raise http.HTTPInternalError(message="Can't get OS list")
328 return [row[0] for row in diagnose_data if row[1]]