Statistics
| Branch: | Tag: | Revision:

root / lib / rapi / rlib2.py @ a2f92677

History | View | Annotate | Download (6.4 kB)

1 10b207d4 Oleksiy Mishchenko
#
2 10b207d4 Oleksiy Mishchenko
#
3 10b207d4 Oleksiy Mishchenko
4 10b207d4 Oleksiy Mishchenko
# Copyright (C) 2006, 2007, 2008 Google Inc.
5 10b207d4 Oleksiy Mishchenko
#
6 10b207d4 Oleksiy Mishchenko
# This program is free software; you can redistribute it and/or modify
7 10b207d4 Oleksiy Mishchenko
# it under the terms of the GNU General Public License as published by
8 10b207d4 Oleksiy Mishchenko
# the Free Software Foundation; either version 2 of the License, or
9 10b207d4 Oleksiy Mishchenko
# (at your option) any later version.
10 10b207d4 Oleksiy Mishchenko
#
11 10b207d4 Oleksiy Mishchenko
# This program is distributed in the hope that it will be useful, but
12 10b207d4 Oleksiy Mishchenko
# WITHOUT ANY WARRANTY; without even the implied warranty of
13 10b207d4 Oleksiy Mishchenko
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14 10b207d4 Oleksiy Mishchenko
# General Public License for more details.
15 10b207d4 Oleksiy Mishchenko
#
16 10b207d4 Oleksiy Mishchenko
# You should have received a copy of the GNU General Public License
17 10b207d4 Oleksiy Mishchenko
# along with this program; if not, write to the Free Software
18 10b207d4 Oleksiy Mishchenko
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
19 10b207d4 Oleksiy Mishchenko
# 02110-1301, USA.
20 10b207d4 Oleksiy Mishchenko
21 10b207d4 Oleksiy Mishchenko
22 10b207d4 Oleksiy Mishchenko
"""Remote API version 2 baserlib.library.
23 10b207d4 Oleksiy Mishchenko

24 10b207d4 Oleksiy Mishchenko
"""
25 10b207d4 Oleksiy Mishchenko
26 10b207d4 Oleksiy Mishchenko
import ganeti.opcodes
27 15fd9fd5 Oleksiy Mishchenko
from ganeti import http
28 10b207d4 Oleksiy Mishchenko
from ganeti import luxi
29 15fd9fd5 Oleksiy Mishchenko
from ganeti import constants
30 38206f3c Iustin Pop
from ganeti.rapi import baserlib
31 10b207d4 Oleksiy Mishchenko
32 51ee2f49 Oleksiy Mishchenko
from ganeti.rapi.rlib1 import I_FIELDS, N_FIELDS
33 51ee2f49 Oleksiy Mishchenko
34 10b207d4 Oleksiy Mishchenko
35 10b207d4 Oleksiy Mishchenko
class R_2_jobs(baserlib.R_Generic):
36 10b207d4 Oleksiy Mishchenko
  """/2/jobs resource.
37 10b207d4 Oleksiy Mishchenko

38 10b207d4 Oleksiy Mishchenko
  """
39 10b207d4 Oleksiy Mishchenko
  DOC_URI = "/2/jobs"
40 10b207d4 Oleksiy Mishchenko
41 10b207d4 Oleksiy Mishchenko
  def GET(self):
42 10b207d4 Oleksiy Mishchenko
    """Returns a dictionary of jobs.
43 10b207d4 Oleksiy Mishchenko

44 10b207d4 Oleksiy Mishchenko
    Returns:
45 10b207d4 Oleksiy Mishchenko
      A dictionary with jobs id and uri.
46 38206f3c Iustin Pop

47 10b207d4 Oleksiy Mishchenko
    """
48 10b207d4 Oleksiy Mishchenko
    fields = ["id"]
49 10b207d4 Oleksiy Mishchenko
    # Convert the list of lists to the list of ids
50 10b207d4 Oleksiy Mishchenko
    result = [job_id for [job_id] in luxi.Client().QueryJobs(None, fields)]
51 10b207d4 Oleksiy Mishchenko
    return baserlib.BuildUriList(result, "/2/jobs/%s", uri_fields=("id", "uri"))
52 10b207d4 Oleksiy Mishchenko
53 10b207d4 Oleksiy Mishchenko
54 10b207d4 Oleksiy Mishchenko
class R_2_jobs_id(baserlib.R_Generic):
55 10b207d4 Oleksiy Mishchenko
  """/2/jobs/[job_id] resource.
56 10b207d4 Oleksiy Mishchenko

57 10b207d4 Oleksiy Mishchenko
  """
58 10b207d4 Oleksiy Mishchenko
  DOC_URI = "/2/jobs/[job_id]"
59 10b207d4 Oleksiy Mishchenko
60 10b207d4 Oleksiy Mishchenko
  def GET(self):
61 10b207d4 Oleksiy Mishchenko
    """Returns a job status.
62 10b207d4 Oleksiy Mishchenko

63 38206f3c Iustin Pop
    Returns:
64 10b207d4 Oleksiy Mishchenko
      A dictionary with job parameters.
65 10b207d4 Oleksiy Mishchenko

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

74 10b207d4 Oleksiy Mishchenko
    """
75 10b207d4 Oleksiy Mishchenko
    fields = ["id", "ops", "status", "opstatus", "opresult"]
76 10b207d4 Oleksiy Mishchenko
    job_id = self.items[0]
77 38206f3c Iustin Pop
    result = luxi.Client().QueryJobs([job_id, ], fields)[0]
78 10b207d4 Oleksiy Mishchenko
    return baserlib.MapFields(fields, result)
79 10b207d4 Oleksiy Mishchenko
80 10b207d4 Oleksiy Mishchenko
81 10b207d4 Oleksiy Mishchenko
class R_2_nodes(baserlib.R_Generic):
82 10b207d4 Oleksiy Mishchenko
  """/2/nodes resource.
83 10b207d4 Oleksiy Mishchenko

84 10b207d4 Oleksiy Mishchenko
  """
85 10b207d4 Oleksiy Mishchenko
  DOC_URI = "/2/nodes"
86 38206f3c Iustin Pop
87 10b207d4 Oleksiy Mishchenko
  def GET(self):
88 10b207d4 Oleksiy Mishchenko
    """Returns a list of all nodes.
89 38206f3c Iustin Pop

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

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

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

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

122 10b207d4 Oleksiy Mishchenko
    """
123 10b207d4 Oleksiy Mishchenko
    op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
124 10b207d4 Oleksiy Mishchenko
    nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
125 38206f3c Iustin Pop
126 10b207d4 Oleksiy Mishchenko
    if 'bulk' in self.queryargs:
127 51ee2f49 Oleksiy Mishchenko
      op = ganeti.opcodes.OpQueryNodes(output_fields=N_FIELDS,
128 51ee2f49 Oleksiy Mishchenko
                                       names=nodeslist)
129 51ee2f49 Oleksiy Mishchenko
      result = ganeti.cli.SubmitOpCode(op)
130 51ee2f49 Oleksiy Mishchenko
      return baserlib.MapBulkFields(result, N_FIELDS)
131 10b207d4 Oleksiy Mishchenko
132 441e7cfd Oleksiy Mishchenko
    return baserlib.BuildUriList(nodeslist, "/2/nodes/%s",
133 38206f3c Iustin Pop
                                 uri_fields=("id", "uri"))
134 441e7cfd Oleksiy Mishchenko
135 441e7cfd Oleksiy Mishchenko
136 441e7cfd Oleksiy Mishchenko
class R_2_instances(baserlib.R_Generic):
137 441e7cfd Oleksiy Mishchenko
  """/2/instances resource.
138 441e7cfd Oleksiy Mishchenko

139 441e7cfd Oleksiy Mishchenko
  """
140 441e7cfd Oleksiy Mishchenko
  DOC_URI = "/2/instances"
141 441e7cfd Oleksiy Mishchenko
142 441e7cfd Oleksiy Mishchenko
143 441e7cfd Oleksiy Mishchenko
  def GET(self):
144 441e7cfd Oleksiy Mishchenko
    """Returns a list of all available instances.
145 441e7cfd Oleksiy Mishchenko

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

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

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

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

185 441e7cfd Oleksiy Mishchenko
    """
186 441e7cfd Oleksiy Mishchenko
    op = ganeti.opcodes.OpQueryInstances(output_fields=["name"], names=[])
187 441e7cfd Oleksiy Mishchenko
    instanceslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
188 441e7cfd Oleksiy Mishchenko
189 441e7cfd Oleksiy Mishchenko
    if 'bulk' in self.queryargs:
190 441e7cfd Oleksiy Mishchenko
      op = ganeti.opcodes.OpQueryInstances(output_fields=I_FIELDS,
191 441e7cfd Oleksiy Mishchenko
                                           names=instanceslist)
192 441e7cfd Oleksiy Mishchenko
      result = ganeti.cli.SubmitOpCode(op)
193 441e7cfd Oleksiy Mishchenko
      return baserlib.MapBulkFields(result, I_FIELDS)
194 441e7cfd Oleksiy Mishchenko
195 441e7cfd Oleksiy Mishchenko
196 441e7cfd Oleksiy Mishchenko
    else:
197 441e7cfd Oleksiy Mishchenko
      return baserlib.BuildUriList(instanceslist, "/2/instances/%s",
198 441e7cfd Oleksiy Mishchenko
                                   uri_fields=("id", "uri"))
199 441e7cfd Oleksiy Mishchenko
200 441e7cfd Oleksiy Mishchenko
201 441e7cfd Oleksiy Mishchenko
class R_2_instances_name_tags(baserlib.R_Generic):
202 441e7cfd Oleksiy Mishchenko
  """/2/instances/[instance_name]/tags resource.
203 441e7cfd Oleksiy Mishchenko

204 441e7cfd Oleksiy Mishchenko
  Manages per-instance tags.
205 441e7cfd Oleksiy Mishchenko

206 441e7cfd Oleksiy Mishchenko
  """
207 441e7cfd Oleksiy Mishchenko
  DOC_URI = "/2/instances/[instance_name]/tags"
208 441e7cfd Oleksiy Mishchenko
209 441e7cfd Oleksiy Mishchenko
  def GET(self):
210 441e7cfd Oleksiy Mishchenko
    """Returns a list of instance tags.
211 441e7cfd Oleksiy Mishchenko

212 441e7cfd Oleksiy Mishchenko
    Example: ["tag1", "tag2", "tag3"]
213 441e7cfd Oleksiy Mishchenko

214 441e7cfd Oleksiy Mishchenko
    """
215 441e7cfd Oleksiy Mishchenko
    return baserlib._Tags_GET(constants.TAG_INSTANCE, name=self.items[0])
216 441e7cfd Oleksiy Mishchenko
217 441e7cfd Oleksiy Mishchenko
  def POST(self):
218 441e7cfd Oleksiy Mishchenko
    """Add a set of tags to the instance.
219 441e7cfd Oleksiy Mishchenko

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

223 441e7cfd Oleksiy Mishchenko
    """
224 441e7cfd Oleksiy Mishchenko
    return baserlib._Tags_POST(constants.TAG_INSTANCE,
225 441e7cfd Oleksiy Mishchenko
                               self.post_data, name=self.items[0])
226 15fd9fd5 Oleksiy Mishchenko
227 15fd9fd5 Oleksiy Mishchenko
  def DELETE(self):
228 15fd9fd5 Oleksiy Mishchenko
    """Delete a tag.
229 15fd9fd5 Oleksiy Mishchenko

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

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