Revision f87ec53f
b/lib/rapi/baserlib.py | ||
---|---|---|
32 | 32 |
from ganeti import luxi |
33 | 33 |
from ganeti import rapi |
34 | 34 |
from ganeti import http |
35 |
from ganeti import ssconf |
|
36 |
from ganeti import constants |
|
37 |
from ganeti import opcodes |
|
38 | 35 |
from ganeti import errors |
39 | 36 |
|
40 | 37 |
|
... | ... | |
91 | 88 |
return dict(zip(names, data)) |
92 | 89 |
|
93 | 90 |
|
94 |
def _Tags_GET(kind, name): |
|
95 |
"""Helper function to retrieve tags. |
|
96 |
|
|
97 |
""" |
|
98 |
if kind in (constants.TAG_INSTANCE, |
|
99 |
constants.TAG_NODEGROUP, |
|
100 |
constants.TAG_NODE): |
|
101 |
if not name: |
|
102 |
raise http.HttpBadRequest("Missing name on tag request") |
|
103 |
cl = GetClient() |
|
104 |
if kind == constants.TAG_INSTANCE: |
|
105 |
fn = cl.QueryInstances |
|
106 |
elif kind == constants.TAG_NODEGROUP: |
|
107 |
fn = cl.QueryGroups |
|
108 |
else: |
|
109 |
fn = cl.QueryNodes |
|
110 |
result = fn(names=[name], fields=["tags"], use_locking=False) |
|
111 |
if not result or not result[0]: |
|
112 |
raise http.HttpBadGateway("Invalid response from tag query") |
|
113 |
tags = result[0][0] |
|
114 |
elif kind == constants.TAG_CLUSTER: |
|
115 |
ssc = ssconf.SimpleStore() |
|
116 |
tags = ssc.GetClusterTags() |
|
117 |
|
|
118 |
return list(tags) |
|
119 |
|
|
120 |
|
|
121 |
def _Tags_PUT(kind, tags, name, dry_run): |
|
122 |
"""Helper function to set tags. |
|
123 |
|
|
124 |
""" |
|
125 |
return SubmitJob([opcodes.OpTagsSet(kind=kind, name=name, |
|
126 |
tags=tags, dry_run=dry_run)]) |
|
127 |
|
|
128 |
|
|
129 |
def _Tags_DELETE(kind, tags, name, dry_run): |
|
130 |
"""Helper function to delete tags. |
|
131 |
|
|
132 |
""" |
|
133 |
return SubmitJob([opcodes.OpTagsDel(kind=kind, name=name, |
|
134 |
tags=tags, dry_run=dry_run)]) |
|
135 |
|
|
136 |
|
|
137 | 91 |
def MapBulkFields(itemslist, fields): |
138 | 92 |
"""Map value to field name in to one dictionary. |
139 | 93 |
|
b/lib/rapi/rlib2.py | ||
---|---|---|
1380 | 1380 |
Example: ["tag1", "tag2", "tag3"] |
1381 | 1381 |
|
1382 | 1382 |
""" |
1383 |
# pylint: disable-msg=W0212 |
|
1384 |
return baserlib._Tags_GET(self.TAG_LEVEL, name=self.name) |
|
1383 |
kind = self.TAG_LEVEL |
|
1384 |
|
|
1385 |
if kind in (constants.TAG_INSTANCE, |
|
1386 |
constants.TAG_NODEGROUP, |
|
1387 |
constants.TAG_NODE): |
|
1388 |
if not self.name: |
|
1389 |
raise http.HttpBadRequest("Missing name on tag request") |
|
1390 |
|
|
1391 |
cl = baserlib.GetClient() |
|
1392 |
if kind == constants.TAG_INSTANCE: |
|
1393 |
fn = cl.QueryInstances |
|
1394 |
elif kind == constants.TAG_NODEGROUP: |
|
1395 |
fn = cl.QueryGroups |
|
1396 |
else: |
|
1397 |
fn = cl.QueryNodes |
|
1398 |
result = fn(names=[self.name], fields=["tags"], use_locking=False) |
|
1399 |
if not result or not result[0]: |
|
1400 |
raise http.HttpBadGateway("Invalid response from tag query") |
|
1401 |
tags = result[0][0] |
|
1402 |
|
|
1403 |
elif kind == constants.TAG_CLUSTER: |
|
1404 |
assert not self.name |
|
1405 |
# TODO: Use query API? |
|
1406 |
ssc = ssconf.SimpleStore() |
|
1407 |
tags = ssc.GetClusterTags() |
|
1408 |
|
|
1409 |
return list(tags) |
|
1385 | 1410 |
|
1386 | 1411 |
def PUT(self): |
1387 | 1412 |
"""Add a set of tags. |
... | ... | |
1394 | 1419 |
if "tag" not in self.queryargs: |
1395 | 1420 |
raise http.HttpBadRequest("Please specify tag(s) to add using the" |
1396 | 1421 |
" the 'tag' parameter") |
1397 |
return baserlib._Tags_PUT(self.TAG_LEVEL,
|
|
1398 |
self.queryargs["tag"], name=self.name,
|
|
1399 |
dry_run=bool(self.dryRun()))
|
|
1422 |
op = opcodes.OpTagsSet(kind=self.TAG_LEVEL, name=self.name,
|
|
1423 |
tags=self.queryargs["tag"], dry_run=self.dryRun())
|
|
1424 |
return baserlib.SubmitJob([op])
|
|
1400 | 1425 |
|
1401 | 1426 |
def DELETE(self): |
1402 | 1427 |
"""Delete a tag. |
... | ... | |
1411 | 1436 |
# no we not gonna delete all tags |
1412 | 1437 |
raise http.HttpBadRequest("Cannot delete all tags - please specify" |
1413 | 1438 |
" tag(s) using the 'tag' parameter") |
1414 |
return baserlib._Tags_DELETE(self.TAG_LEVEL, |
|
1415 |
self.queryargs["tag"], |
|
1416 |
name=self.name, |
|
1417 |
dry_run=bool(self.dryRun())) |
|
1439 |
op = opcodes.OpTagsDel(kind=self.TAG_LEVEL, name=self.name, |
|
1440 |
tags=self.queryargs["tag"], dry_run=self.dryRun()) |
|
1441 |
return baserlib.SubmitJob([op]) |
|
1418 | 1442 |
|
1419 | 1443 |
|
1420 | 1444 |
class R_2_instances_name_tags(_R_Tags): |
Also available in: Unified diff