Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / rlib1.py @ 028c6b76

History | View | Annotate | Download (5.4 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", "admin_state", "disk_template",
37
            "ip", "mac", "bridge", "sda_size", "sdb_size", "beparams",
38
            "oper_state", "status", "tags"]
39

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

    
44

    
45
class R_version(baserlib.R_Generic):
46
  """/version resource.
47

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

51
  """
52
  DOC_URI = "/version"
53

    
54
  def GET(self):
55
    """Returns the remote API version.
56

57
    """
58
    return constants.RAPI_VERSION
59

    
60

    
61
class R_tags(baserlib.R_Generic):
62
  """/tags resource.
63

64
  Manages cluster tags.
65

66
  """
67
  DOC_URI = "/tags"
68

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

72
    Example: ["tag1", "tag2", "tag3"]
73

74
    """
75
    return baserlib._Tags_GET(constants.TAG_CLUSTER)
76

    
77

    
78
class R_info(baserlib.R_Generic):
79
  """Cluster info.
80

81
  """
82
  DOC_URI = "/info"
83

    
84
  def GET(self):
85
    """Returns cluster information.
86

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

102
    """
103
    op = ganeti.opcodes.OpQueryClusterInfo()
104
    return ganeti.cli.SubmitOpCode(op)
105

    
106

    
107
class R_nodes_name(baserlib.R_Generic):
108
  """/nodes/[node_name] resources.
109

110
  """
111
  DOC_URI = "/nodes/[node_name]"
112

    
113
  def GET(self):
114
    """Send information about a node.
115

116
    """
117
    node_name = self.items[0]
118
    op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
119
                                     names=[node_name])
120
    result = ganeti.cli.SubmitOpCode(op)
121

    
122
    return baserlib.MapFields(N_FIELDS, result[0])
123

    
124

    
125
class R_nodes_name_tags(baserlib.R_Generic):
126
  """/nodes/[node_name]/tags resource.
127

128
  Manages per-node tags.
129

130
  """
131
  DOC_URI = "/nodes/[node_name]/tags"
132

    
133
  def GET(self):
134
    """Returns a list of node tags.
135

136
    Example: ["tag1", "tag2", "tag3"]
137

138
    """
139
    return baserlib._Tags_GET(constants.TAG_NODE, name=self.items[0])
140

    
141

    
142
class R_instances_name(baserlib.R_Generic):
143
  """/instances/[instance_name] resources.
144

145
  """
146
  DOC_URI = "/instances/[instance_name]"
147

    
148
  def GET(self):
149
    """Send information about an instance.
150

151
    """
152
    instance_name = self.items[0]
153
    op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
154
                                         names=[instance_name])
155
    result = ganeti.cli.SubmitOpCode(op)
156

    
157
    return baserlib.MapFields(I_FIELDS, result[0])
158

    
159
  def DELETE(self):
160
    """Removes an instance.
161

162
    """
163
    instance_name = self.items[0]
164
    op = ganeti.opcodes.OpRemoveInstance(instance_name=instance_name,
165
                                         ignore_failures=True)
166
    job_id = ganeti.cli.SendJob([op])
167

    
168
    return job_id
169

    
170
  def PUT(self):
171
    """Modify an instance.
172

173
    """
174
    instance_name = self.items[0]
175
    opts = {}
176

    
177
    for key in self.queryargs:
178
      opts[key] = self.queryargs[key][0]
179

    
180
    beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
181
    hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
182

    
183
    op = ganeti.opcodes.OpSetInstanceParams(
184
        instance_name=instance_name,
185
        ip=opts.get('ip', None),
186
        bridge=opts.get('bridge', None),
187
        mac=opts.get('mac', None),
188
        hvparams=hvparams,
189
        beparams=beparams,
190
        force=opts.get('force', None))
191

    
192
    job_id = ganeti.cli.SendJob([op])
193

    
194
    return job_id
195

    
196

    
197
class R_instances_name_tags(baserlib.R_Generic):
198
  """/instances/[instance_name]/tags resource.
199

200
  Manages per-instance tags.
201

202
  """
203
  DOC_URI = "/instances/[instance_name]/tags"
204

    
205
  def GET(self):
206
    """Returns a list of instance tags.
207

208
    Example: ["tag1", "tag2", "tag3"]
209

210
    """
211
    return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
212

    
213

    
214
class R_os(baserlib.R_Generic):
215
  """/os resource.
216

217
  """
218
  DOC_URI = "/os"
219

    
220
  def GET(self):
221
    """Return a list of all OSes.
222

223
    Can return error 500 in case of a problem.
224

225
    Example: ["debian-etch"]
226

227
    """
228
    op = ganeti.opcodes.OpDiagnoseOS(output_fields=["name", "valid"],
229
                                     names=[])
230
    diagnose_data = ganeti.cli.SubmitOpCode(op)
231

    
232
    if not isinstance(diagnose_data, list):
233
      raise http.HTTPInternalError(message="Can't get OS list")
234

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