Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / rlib1.py @ 8b3fd458

History | View | Annotate | Download (7.5 kB)

1
#
2
#
3

    
4
# Copyright (C) 2006, 2007, 2008 Google Inc.
5
#
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.
10
#
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.
15
#
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
19
# 02110-1301, USA.
20

    
21

    
22
"""Remote API version 1 resources library.
23

24
"""
25

    
26
import ganeti.cli
27
import ganeti.errors
28
import ganeti.opcodes
29

    
30
from ganeti import constants
31
from ganeti import http
32

    
33
from ganeti.rapi import baserlib
34

    
35

    
36
I_FIELDS = ["name", "os", "pnode", "snodes",
37
            "admin_state", "admin_ram",
38
            "disk_template", "ip", "mac", "bridge",
39
            "sda_size", "sdb_size", "vcpus",
40
            "oper_state", "status", "tags"]
41

    
42
N_FIELDS = ["name", "dtotal", "dfree",
43
            "mtotal", "mnode", "mfree",
44
            "pinst_cnt", "sinst_cnt", "tags"]
45

    
46

    
47
class R_version(baserlib.R_Generic):
48
  """/version resource.
49

50
  This resource should be used to determine the remote API version and to adapt
51
  clients accordingly.
52

53
  """
54
  DOC_URI = "/version"
55

    
56
  def GET(self):
57
    """Returns the remote API version.
58

59
    """
60
    return constants.RAPI_VERSION
61

    
62

    
63
class R_tags(baserlib.R_Generic):
64
  """/tags resource.
65

66
  Manages cluster tags.
67

68
  """
69
  DOC_URI = "/tags"
70

    
71
  def GET(self):
72
    """Returns a list of all cluster tags.
73

74
    Example: ["tag1", "tag2", "tag3"]
75

76
    """
77
    return baserlib._Tags_GET(constants.TAG_CLUSTER)
78

    
79

    
80
class R_info(baserlib.R_Generic):
81
  """Cluster info.
82

83
  """
84
  DOC_URI = "/info"
85

    
86
  def GET(self):
87
    """Returns cluster information.
88

89
    Example: {
90
      "config_version": 3,
91
      "name": "cluster1.example.com",
92
      "software_version": "1.2.4",
93
      "os_api_version": 5,
94
      "export_version": 0,
95
      "master": "node1.example.com",
96
      "architecture": [
97
        "64bit",
98
        "x86_64"
99
      ],
100
      "hypervisor_type": "xen-pvm",
101
      "protocol_version": 12
102
    }
103

104
    """
105
    op = ganeti.opcodes.OpQueryClusterInfo()
106
    return ganeti.cli.SubmitOpCode(op)
107

    
108

    
109
class R_nodes(baserlib.R_Generic):
110
  """/nodes resource.
111

112
  """
113
  DOC_URI = "/nodes"
114

    
115
  def GET(self):
116
    """Returns a list of all nodes.
117

118
    Returns:
119
      A dictionary with 'name' and 'uri' keys for each of them.
120

121
    Example: [
122
        {
123
          "name": "node1.example.com",
124
          "uri": "\/instances\/node1.example.com"
125
        },
126
        {
127
          "name": "node2.example.com",
128
          "uri": "\/instances\/node2.example.com"
129
        }]
130

131
    If the optional 'bulk' argument is provided and set to 'true'
132
    value (i.e '?bulk=1'), the output contains detailed
133
    information about nodes as a list.
134

135
    Example: [
136
        {
137
          "pinst_cnt": 1,
138
          "mfree": 31280,
139
          "mtotal": 32763,
140
          "name": "www.example.com",
141
          "tags": [],
142
          "mnode": 512,
143
          "dtotal": 5246208,
144
          "sinst_cnt": 2,
145
          "dfree": 5171712
146
        },
147
        ...
148
    ]
149

150
    """
151
    op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
152
    nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
153

    
154
    if 'bulk' in self.queryargs:
155
      op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
156
                                       names=nodeslist)
157
      result = ganeti.cli.SubmitOpCode(op)
158
      return baserlib.MapBulkFields(result, N_FIELDS)
159

    
160
    return baserlib.BuildUriList(nodeslist, "/nodes/%s")
161

    
162

    
163
class R_nodes_name(baserlib.R_Generic):
164
  """/nodes/[node_name] resources.
165

166
  """
167
  DOC_URI = "/nodes/[node_name]"
168

    
169
  def GET(self):
170
    """Send information about a node.
171

172
    """
173
    node_name = self.items[0]
174
    op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
175
                                     names=[node_name])
176
    result = ganeti.cli.SubmitOpCode(op)
177

    
178
    return baserlib.MapFields(N_FIELDS, result[0])
179

    
180

    
181
class R_nodes_name_tags(baserlib.R_Generic):
182
  """/nodes/[node_name]/tags resource.
183

184
  Manages per-node tags.
185

186
  """
187
  DOC_URI = "/nodes/[node_name]/tags"
188

    
189
  def GET(self):
190
    """Returns a list of node tags.
191

192
    Example: ["tag1", "tag2", "tag3"]
193

194
    """
195
    return baserlib._Tags_GET(constants.TAG_NODE, name=self.items[0])
196

    
197

    
198
class R_instances(baserlib.R_Generic):
199
  """/instances resource.
200

201
  """
202
  DOC_URI = "/instances"
203

    
204

    
205
  def GET(self):
206
    """Returns a list of all available instances.
207

208
    Returns:
209
       A dictionary with 'name' and 'uri' keys for each of them.
210

211
    Example: [
212
        {
213
          "name": "web.example.com",
214
          "uri": "\/instances\/web.example.com"
215
        },
216
        {
217
          "name": "mail.example.com",
218
          "uri": "\/instances\/mail.example.com"
219
        }]
220

221
    If the optional 'bulk' argument is provided and set to 'true'
222
    value (i.e '?bulk=1'), the output contains detailed
223
    information about instances as a list.
224

225
    Example: [
226
        {
227
           "status": "running",
228
           "bridge": "xen-br0",
229
           "name": "web.example.com",
230
           "tags": ["tag1", "tag2"],
231
           "admin_ram": 512,
232
           "sda_size": 20480,
233
           "pnode": "node1.example.com",
234
           "mac": "01:23:45:67:89:01",
235
           "sdb_size": 4096,
236
           "snodes": ["node2.example.com"],
237
           "disk_template": "drbd",
238
           "ip": null,
239
           "admin_state": true,
240
           "os": "debian-etch",
241
           "vcpus": 2,
242
           "oper_state": true
243
        },
244
        ...
245
    ]
246

247
    """
248
    op = ganeti.opcodes.OpQueryInstances(output_fields=["name"], names=[])
249
    instanceslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
250

    
251
    if 'bulk' in self.queryargs:
252
      op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
253
                                           names=instanceslist)
254
      result = ganeti.cli.SubmitOpCode(op)
255
      return baserlib.MapBulkFields(result, I_FIELDS)
256

    
257

    
258
    else:
259
      return baserlib.BuildUriList(instanceslist, "/instances/%s")
260

    
261

    
262
class R_instances_name(baserlib.R_Generic):
263
  """/instances/[instance_name] resources.
264

265
  """
266
  DOC_URI = "/instances/[instance_name]"
267

    
268
  def GET(self):
269
    """Send information about an instance.
270

271
    """
272
    instance_name = self.items[0]
273
    op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
274
                                         names=[instance_name])
275
    result = ganeti.cli.SubmitOpCode(op)
276

    
277
    return baserlib.MapFields(I_FIELDS, result[0])
278

    
279

    
280
class R_instances_name_tags(baserlib.R_Generic):
281
  """/instances/[instance_name]/tags resource.
282

283
  Manages per-instance tags.
284

285
  """
286
  DOC_URI = "/instances/[instance_name]/tags"
287

    
288
  def GET(self):
289
    """Returns a list of instance tags.
290

291
    Example: ["tag1", "tag2", "tag3"]
292

293
    """
294
    return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
295

    
296

    
297
class R_os(baserlib.R_Generic):
298
  """/os resource.
299

300
  """
301
  DOC_URI = "/os"
302

    
303
  def GET(self):
304
    """Return a list of all OSes.
305

306
    Can return error 500 in case of a problem.
307

308
    Example: ["debian-etch"]
309

310
    """
311
    op = ganeti.opcodes.OpDiagnoseOS(output_fields=["name", "valid"],
312
                                     names=[])
313
    diagnose_data = ganeti.cli.SubmitOpCode(op)
314

    
315
    if not isinstance(diagnose_data, list):
316
      raise http.HTTPInternalError(message="Can't get OS list")
317

    
318
    return [row[0] for row in diagnose_data if row[1]]