Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / rlib1.py @ c41eea6e

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
49
  to adapt 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

89
      {
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_name(baserlib.R_Generic):
110
  """/nodes/[node_name] resources.
111

112
  """
113
  DOC_URI = "/nodes/[node_name]"
114

    
115
  def GET(self):
116
    """Send information about a node.
117

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

    
124
    return baserlib.MapFields(N_FIELDS, result[0])
125

    
126

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

130
  Manages per-node tags.
131

132
  """
133
  DOC_URI = "/nodes/[node_name]/tags"
134

    
135
  def GET(self):
136
    """Returns a list of node tags.
137

138
    Example: ["tag1", "tag2", "tag3"]
139

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

    
143

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

147
  """
148
  DOC_URI = "/instances/[instance_name]"
149

    
150
  def GET(self):
151
    """Send information about an instance.
152

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

    
159
    return baserlib.MapFields(I_FIELDS, result[0])
160

    
161
  def DELETE(self):
162
    """Removes an instance.
163

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

    
170
    return job_id
171

    
172
  def PUT(self):
173
    """Modify an instance.
174

175
    """
176
    instance_name = self.items[0]
177
    opts = {}
178

    
179
    for key in self.queryargs:
180
      opts[key] = self.queryargs[key][0]
181

    
182
    beparams = baserlib.MakeParamsDict(opts, constants.BES_PARAMETERS)
183
    hvparams = baserlib.MakeParamsDict(opts, constants.HVS_PARAMETERS)
184

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

    
194
    job_id = ganeti.cli.SendJob([op])
195

    
196
    return job_id
197

    
198

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

202
  Manages per-instance tags.
203

204
  """
205
  DOC_URI = "/instances/[instance_name]/tags"
206

    
207
  def GET(self):
208
    """Returns a list of instance tags.
209

210
    Example: ["tag1", "tag2", "tag3"]
211

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

    
215

    
216
class R_os(baserlib.R_Generic):
217
  """/os resource.
218

219
  """
220
  DOC_URI = "/os"
221

    
222
  def GET(self):
223
    """Return a list of all OSes.
224

225
    Can return error 500 in case of a problem.
226

227
    Example: ["debian-etch"]
228

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

    
234
    if not isinstance(diagnose_data, list):
235
      raise http.HttpInternalError(message="Can't get OS list")
236

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