Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / rlib2.py @ 15fd9fd5

History | View | Annotate | Download (6.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 2 baserlib.library.
23

24
"""
25

    
26
import ganeti.opcodes
27
from ganeti import http
28
from ganeti import luxi
29
from ganeti import constants
30
from ganeti.rapi import baserlib
31

    
32
from ganeti.rapi.rlib1 import I_FIELDS, N_FIELDS
33

    
34

    
35
class R_2_jobs(baserlib.R_Generic):
36
  """/2/jobs resource.
37

38
  """
39
  DOC_URI = "/2/jobs"
40

    
41
  def GET(self):
42
    """Returns a dictionary of jobs.
43

44
    Returns:
45
      A dictionary with jobs id and uri.
46

47
    """
48
    fields = ["id"]
49
    # Convert the list of lists to the list of ids
50
    result = [job_id for [job_id] in luxi.Client().QueryJobs(None, fields)]
51
    return baserlib.BuildUriList(result, "/2/jobs/%s", uri_fields=("id", "uri"))
52

    
53

    
54
class R_2_jobs_id(baserlib.R_Generic):
55
  """/2/jobs/[job_id] resource.
56

57
  """
58
  DOC_URI = "/2/jobs/[job_id]"
59

    
60
  def GET(self):
61
    """Returns a job status.
62

63
    Returns:
64
      A dictionary with job parameters.
65

66
    The result includes:
67
      id - job ID as a number
68
      status - current job status as a string
69
      ops - involved OpCodes as a list of dictionaries for each opcodes in
70
        the job
71
      opstatus - OpCodes status as a list
72
      opresult - OpCodes results as a list of lists
73

74
    """
75
    fields = ["id", "ops", "status", "opstatus", "opresult"]
76
    job_id = self.items[0]
77
    result = luxi.Client().QueryJobs([job_id, ], fields)[0]
78
    return baserlib.MapFields(fields, result)
79

    
80

    
81
class R_2_nodes(baserlib.R_Generic):
82
  """/2/nodes resource.
83

84
  """
85
  DOC_URI = "/2/nodes"
86

    
87
  def GET(self):
88
    """Returns a list of all nodes.
89

90
    Returns:
91
      A dictionary with 'name' and 'uri' keys for each of them.
92

93
    Example: [
94
        {
95
          "id": "node1.example.com",
96
          "uri": "\/instances\/node1.example.com"
97
        },
98
        {
99
          "id": "node2.example.com",
100
          "uri": "\/instances\/node2.example.com"
101
        }]
102

103
    If the optional 'bulk' argument is provided and set to 'true'
104
    value (i.e '?bulk=1'), the output contains detailed
105
    information about nodes as a list.
106

107
    Example: [
108
        {
109
          "pinst_cnt": 1,
110
          "mfree": 31280,
111
          "mtotal": 32763,
112
          "name": "www.example.com",
113
          "tags": [],
114
          "mnode": 512,
115
          "dtotal": 5246208,
116
          "sinst_cnt": 2,
117
          "dfree": 5171712
118
        },
119
        ...
120
    ]
121

122
    """
123
    op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
124
    nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
125

    
126
    if 'bulk' in self.queryargs:
127
      op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
128
                                       names=nodeslist)
129
      result = ganeti.cli.SubmitOpCode(op)
130
      return baserlib.MapBulkFields(result, N_FIELDS)
131

    
132
    return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
133
                                 uri_fields=("id", "uri"))
134

    
135

    
136
class R_2_instances(baserlib.R_Generic):
137
  """/2/instances resource.
138

139
  """
140
  DOC_URI = "/2/instances"
141

    
142

    
143
  def GET(self):
144
    """Returns a list of all available instances.
145

146
    Returns:
147
       A dictionary with 'name' and 'uri' keys for each of them.
148

149
    Example: [
150
        {
151
          "name": "web.example.com",
152
          "uri": "\/instances\/web.example.com"
153
        },
154
        {
155
          "name": "mail.example.com",
156
          "uri": "\/instances\/mail.example.com"
157
        }]
158

159
    If the optional 'bulk' argument is provided and set to 'true'
160
    value (i.e '?bulk=1'), the output contains detailed
161
    information about instances as a list.
162

163
    Example: [
164
        {
165
           "status": "running",
166
           "bridge": "xen-br0",
167
           "name": "web.example.com",
168
           "tags": ["tag1", "tag2"],
169
           "admin_ram": 512,
170
           "sda_size": 20480,
171
           "pnode": "node1.example.com",
172
           "mac": "01:23:45:67:89:01",
173
           "sdb_size": 4096,
174
           "snodes": ["node2.example.com"],
175
           "disk_template": "drbd",
176
           "ip": null,
177
           "admin_state": true,
178
           "os": "debian-etch",
179
           "vcpus": 2,
180
           "oper_state": true
181
        },
182
        ...
183
    ]
184

185
    """
186
    op = ganeti.opcodes.OpQueryInstances(output_fields=["name"], names=[])
187
    instanceslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
188

    
189
    if 'bulk' in self.queryargs:
190
      op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
191
                                           names=instanceslist)
192
      result = ganeti.cli.SubmitOpCode(op)
193
      return baserlib.MapBulkFields(result, I_FIELDS)
194

    
195

    
196
    else:
197
      return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
198
                                   uri_fields=("id", "uri"))
199

    
200

    
201
class R_2_instances_name_tags(baserlib.R_Generic):
202
  """/2/instances/[instance_name]/tags resource.
203

204
  Manages per-instance tags.
205

206
  """
207
  DOC_URI = "/2/instances/[instance_name]/tags"
208

    
209
  def GET(self):
210
    """Returns a list of instance tags.
211

212
    Example: ["tag1", "tag2", "tag3"]
213

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

    
217
  def POST(self):
218
    """Add a set of tags to the instance.
219

220
    The reqest as a list of strings should be POST to this URI. And you'll have
221
    back a job id.
222

223
    """
224
    return baserlib._Tags_POST(constants.TAG_INSTANCE,
225
                               self.post_data, name=self.items[0])
226

    
227
  def DELETE(self):
228
    """Delete a tag.
229

230
    In order to delete a set of tags from a instance, DELETE request should be
231
    addressed to URI like: /2/instances/[instance_name]/tags?tag=[tag]&tag=[tag]
232

233
    """
234
    if 'tag' not in self.queryargs:
235
      # no we not gonna delete all tags from an instance
236
      raise http.HTTPNotImplemented
237
    return baserlib._Tags_DELETE(constants.TAG_INSTANCE,
238
                                 self.queryargs['tag'],
239
                                 name=self.items[0])