Revision a268af8d
b/lib/rapi/client.py | ||
---|---|---|
1371 | 1371 |
return self._SendRequest(HTTP_DELETE, |
1372 | 1372 |
("/%s/nodes/%s/tags" % |
1373 | 1373 |
(GANETI_RAPI_VERSION, node)), query, None) |
1374 |
|
|
1375 |
def GetGroups(self, bulk=False): |
|
1376 |
"""Gets all node groups in the cluster. |
|
1377 |
|
|
1378 |
@type bulk: bool |
|
1379 |
@param bulk: whether to return all information about the groups |
|
1380 |
|
|
1381 |
@rtype: list of dict or str |
|
1382 |
@return: if bulk is true, a list of dictionaries with info about all node |
|
1383 |
groups in the cluster, else a list of names of those node groups |
|
1384 |
|
|
1385 |
""" |
|
1386 |
query = [] |
|
1387 |
if bulk: |
|
1388 |
query.append(("bulk", 1)) |
|
1389 |
|
|
1390 |
groups = self._SendRequest(HTTP_GET, "/%s/groups" % GANETI_RAPI_VERSION, |
|
1391 |
query, None) |
|
1392 |
if bulk: |
|
1393 |
return groups |
|
1394 |
else: |
|
1395 |
return [g["name"] for g in groups] |
|
1396 |
|
|
1397 |
def GetGroup(self, group): |
|
1398 |
"""Gets information about a node group. |
|
1399 |
|
|
1400 |
@type group: str |
|
1401 |
@param group: name of the node group whose info to return |
|
1402 |
|
|
1403 |
@rtype: dict |
|
1404 |
@return: info about the node group |
|
1405 |
|
|
1406 |
""" |
|
1407 |
return self._SendRequest(HTTP_GET, |
|
1408 |
"/%s/groups/%s" % (GANETI_RAPI_VERSION, group), |
|
1409 |
None, None) |
|
1410 |
|
|
1411 |
def CreateGroup(self, name, dry_run=False): |
|
1412 |
"""Creates a new node group. |
|
1413 |
|
|
1414 |
@type name: str |
|
1415 |
@param name: the name of node group to create |
|
1416 |
@type dry_run: bool |
|
1417 |
@param dry_run: whether to peform a dry run |
|
1418 |
|
|
1419 |
@rtype: int |
|
1420 |
@return: job id |
|
1421 |
|
|
1422 |
""" |
|
1423 |
query = [] |
|
1424 |
if dry_run: |
|
1425 |
query.append(("dry-run", 1)) |
|
1426 |
|
|
1427 |
body = { |
|
1428 |
"name": name, |
|
1429 |
} |
|
1430 |
|
|
1431 |
return self._SendRequest(HTTP_POST, "/%s/groups" % GANETI_RAPI_VERSION, |
|
1432 |
query, body) |
|
1433 |
|
|
1434 |
def DeleteGroup(self, group, dry_run=False): |
|
1435 |
"""Deletes a node group. |
|
1436 |
|
|
1437 |
@type group: str |
|
1438 |
@param group: the node group to delete |
|
1439 |
@type dry_run: bool |
|
1440 |
@param dry_run: whether to peform a dry run |
|
1441 |
|
|
1442 |
@rtype: int |
|
1443 |
@return: job id |
|
1444 |
|
|
1445 |
""" |
|
1446 |
query = [] |
|
1447 |
if dry_run: |
|
1448 |
query.append(("dry-run", 1)) |
|
1449 |
|
|
1450 |
return self._SendRequest(HTTP_DELETE, |
|
1451 |
("/%s/groups/%s" % |
|
1452 |
(GANETI_RAPI_VERSION, group)), query, None) |
|
1453 |
|
|
1454 |
def RenameGroup(self, group, new_name): |
|
1455 |
"""Changes the name of a node group. |
|
1456 |
|
|
1457 |
@type group: string |
|
1458 |
@param group: Node group name |
|
1459 |
@type new_name: string |
|
1460 |
@param new_name: New node group name |
|
1461 |
|
|
1462 |
@rtype: int |
|
1463 |
@return: job id |
|
1464 |
|
|
1465 |
""" |
|
1466 |
body = { |
|
1467 |
"new_name": new_name, |
|
1468 |
} |
|
1469 |
|
|
1470 |
return self._SendRequest(HTTP_PUT, |
|
1471 |
("/%s/groups/%s/rename" % |
|
1472 |
(GANETI_RAPI_VERSION, group)), None, body) |
b/test/ganeti.rapi.client_unittest.py | ||
---|---|---|
976 | 976 |
self.assertDryRun() |
977 | 977 |
self.assertQuery("tag", ["awesome"]) |
978 | 978 |
|
979 |
def testGetGroups(self): |
|
980 |
groups = [{"name": "group1", |
|
981 |
"uri": "/2/groups/group1", |
|
982 |
}, |
|
983 |
{"name": "group2", |
|
984 |
"uri": "/2/groups/group2", |
|
985 |
}, |
|
986 |
] |
|
987 |
self.rapi.AddResponse(serializer.DumpJson(groups)) |
|
988 |
self.assertEqual(["group1", "group2"], self.client.GetGroups()) |
|
989 |
self.assertHandler(rlib2.R_2_groups) |
|
990 |
|
|
991 |
def testGetGroupsBulk(self): |
|
992 |
groups = [{"name": "group1", |
|
993 |
"uri": "/2/groups/group1", |
|
994 |
"node_cnt": 2, |
|
995 |
"node_list": ["gnt1.test", |
|
996 |
"gnt2.test", |
|
997 |
], |
|
998 |
}, |
|
999 |
{"name": "group2", |
|
1000 |
"uri": "/2/groups/group2", |
|
1001 |
"node_cnt": 1, |
|
1002 |
"node_list": ["gnt3.test", |
|
1003 |
], |
|
1004 |
}, |
|
1005 |
] |
|
1006 |
self.rapi.AddResponse(serializer.DumpJson(groups)) |
|
1007 |
|
|
1008 |
self.assertEqual(groups, self.client.GetGroups(bulk=True)) |
|
1009 |
self.assertHandler(rlib2.R_2_groups) |
|
1010 |
self.assertBulk() |
|
1011 |
|
|
1012 |
def testGetGroup(self): |
|
1013 |
group = {"ctime": None, |
|
1014 |
"name": "default", |
|
1015 |
} |
|
1016 |
self.rapi.AddResponse(serializer.DumpJson(group)) |
|
1017 |
self.assertEqual({"ctime": None, "name": "default"}, |
|
1018 |
self.client.GetGroup("default")) |
|
1019 |
self.assertHandler(rlib2.R_2_groups_name) |
|
1020 |
self.assertItems(["default"]) |
|
1021 |
|
|
1022 |
def testCreateGroup(self): |
|
1023 |
self.rapi.AddResponse("12345") |
|
1024 |
job_id = self.client.CreateGroup("newgroup", dry_run=True) |
|
1025 |
self.assertEqual(job_id, 12345) |
|
1026 |
self.assertHandler(rlib2.R_2_groups) |
|
1027 |
self.assertDryRun() |
|
1028 |
|
|
1029 |
def testDeleteGroup(self): |
|
1030 |
self.rapi.AddResponse("12346") |
|
1031 |
job_id = self.client.DeleteGroup("newgroup", dry_run=True) |
|
1032 |
self.assertEqual(job_id, 12346) |
|
1033 |
self.assertHandler(rlib2.R_2_groups_name) |
|
1034 |
self.assertDryRun() |
|
1035 |
|
|
1036 |
def testRenameGroup(self): |
|
1037 |
self.rapi.AddResponse("12347") |
|
1038 |
job_id = self.client.RenameGroup("oldname", "newname") |
|
1039 |
self.assertEqual(job_id, 12347) |
|
1040 |
self.assertHandler(rlib2.R_2_groups_name_rename) |
|
1041 |
|
|
979 | 1042 |
|
980 | 1043 |
if __name__ == '__main__': |
981 | 1044 |
client.UsesRapiClient(testutils.GanetiTestProgram)() |
Also available in: Unified diff