Make argument to CleanCacheUnlocked mandatory
[ganeti-local] / lib / rapi / rlib2.py
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 re
27
28 import ganeti.opcodes
29
30 from ganeti import constants
31 from ganeti import luxi
32
33 from ganeti.rapi import baserlib 
34
35
36 class R_2_jobs(baserlib.R_Generic):
37   """/2/jobs resource.
38
39   """
40   DOC_URI = "/2/jobs"
41
42   def GET(self):
43     """Returns a dictionary of jobs.
44
45     Returns:
46       A dictionary with jobs id and uri.
47     
48     """
49     fields = ["id"]
50     # Convert the list of lists to the list of ids
51     result = [job_id for [job_id] in luxi.Client().QueryJobs(None, fields)]
52     return baserlib.BuildUriList(result, "/2/jobs/%s", uri_fields=("id", "uri"))
53
54
55 class R_2_jobs_id(baserlib.R_Generic):
56   """/2/jobs/[job_id] resource.
57
58   """
59   DOC_URI = "/2/jobs/[job_id]"
60
61   def GET(self):
62     """Returns a job status.
63
64     Returns: 
65       A dictionary with job parameters.
66
67     The result includes:
68       id - job ID as a number
69       status - current job status as a string
70       ops - involved OpCodes as a list of dictionaries for each opcodes in 
71         the job
72       opstatus - OpCodes status as a list
73       opresult - OpCodes results as a list of lists
74     
75     """
76     fields = ["id", "ops", "status", "opstatus", "opresult"]
77     job_id = self.items[0]
78     result = luxi.Client().QueryJobs([job_id,], fields)[0]
79     return baserlib.MapFields(fields, result)
80
81
82 class R_2_nodes(baserlib.R_Generic):
83   """/2/nodes resource.
84
85   """
86   DOC_URI = "/2/nodes"
87  
88   def _GetDetails(self, nodeslist):
89     """Returns detailed instance data for bulk output.
90
91     Args:
92       instance: A list of nodes names.
93
94     Returns:
95       A list of nodes properties
96
97     """
98     fields = ["name","dtotal", "dfree",
99               "mtotal", "mnode", "mfree",
100               "pinst_cnt", "sinst_cnt", "tags"]
101
102     op = ganeti.opcodes.OpQueryNodes(output_fields=fields,
103                                      names=nodeslist)
104     result = ganeti.cli.SubmitOpCode(op)
105
106     nodes_details = []
107     for node in result:
108       mapped = baserlib.MapFields(fields, node)
109       nodes_details.append(mapped)
110     return nodes_details
111  
112   def GET(self):
113     """Returns a list of all nodes.
114     
115     Returns:
116       A dictionary with 'name' and 'uri' keys for each of them.
117
118     Example: [
119         {
120           "id": "node1.example.com",
121           "uri": "\/instances\/node1.example.com"
122         },
123         {
124           "id": "node2.example.com",
125           "uri": "\/instances\/node2.example.com"
126         }]
127
128     If the optional 'bulk' argument is provided and set to 'true' 
129     value (i.e '?bulk=1'), the output contains detailed
130     information about nodes as a list.
131
132     Example: [
133         {
134           "pinst_cnt": 1,
135           "mfree": 31280,
136           "mtotal": 32763,
137           "name": "www.example.com",
138           "tags": [],
139           "mnode": 512,
140           "dtotal": 5246208,
141           "sinst_cnt": 2,
142           "dfree": 5171712
143         },
144         ...
145     ]
146
147     """
148     op = ganeti.opcodes.OpQueryNodes(output_fields=["name"], names=[])
149     nodeslist = baserlib.ExtractField(ganeti.cli.SubmitOpCode(op), 0)
150     
151     if 'bulk' in self.queryargs:
152       return self._GetDetails(nodeslist)
153
154     return baserlib.BuildUriList(nodeslist, "/nodes/%s", uri_fields=("id", "uri"))