Revision 4c864b55 lib/rapi/client.py
b/lib/rapi/client.py | ||
---|---|---|
108 | 108 |
"hypervisor", "file_storage_dir", "file_driver", "dry_run", |
109 | 109 |
]) |
110 | 110 |
_INST_CREATE_V0_DPARAMS = frozenset(["beparams", "hvparams"]) |
111 |
_QPARAM_DRY_RUN = "dry-run" |
|
112 |
_QPARAM_FORCE = "force" |
|
111 | 113 |
|
112 | 114 |
# Older pycURL versions don't have all error constants |
113 | 115 |
try: |
... | ... | |
146 | 148 |
self.code = code |
147 | 149 |
|
148 | 150 |
|
151 |
def _AppendIf(container, condition, value): |
|
152 |
"""Appends to a list if a condition evaluates to truth. |
|
153 |
|
|
154 |
""" |
|
155 |
if condition: |
|
156 |
container.append(value) |
|
157 |
|
|
158 |
return condition |
|
159 |
|
|
160 |
|
|
161 |
def _AppendDryRunIf(container, condition): |
|
162 |
"""Appends a "dry-run" parameter if a condition evaluates to truth. |
|
163 |
|
|
164 |
""" |
|
165 |
return _AppendIf(container, condition, (_QPARAM_DRY_RUN, 1)) |
|
166 |
|
|
167 |
|
|
168 |
def _AppendForceIf(container, condition): |
|
169 |
"""Appends a "force" parameter if a condition evaluates to truth. |
|
170 |
|
|
171 |
""" |
|
172 |
return _AppendIf(container, condition, (_QPARAM_FORCE, 1)) |
|
173 |
|
|
174 |
|
|
149 | 175 |
def UsesRapiClient(fn): |
150 | 176 |
"""Decorator for code using RAPI client to initialize pycURL. |
151 | 177 |
|
... | ... | |
558 | 584 |
|
559 | 585 |
""" |
560 | 586 |
query = [("tag", t) for t in tags] |
561 |
if dry_run: |
|
562 |
query.append(("dry-run", 1)) |
|
587 |
_AppendDryRunIf(query, dry_run) |
|
563 | 588 |
|
564 | 589 |
return self._SendRequest(HTTP_PUT, "/%s/tags" % GANETI_RAPI_VERSION, |
565 | 590 |
query, None) |
... | ... | |
576 | 601 |
|
577 | 602 |
""" |
578 | 603 |
query = [("tag", t) for t in tags] |
579 |
if dry_run: |
|
580 |
query.append(("dry-run", 1)) |
|
604 |
_AppendDryRunIf(query, dry_run) |
|
581 | 605 |
|
582 | 606 |
return self._SendRequest(HTTP_DELETE, "/%s/tags" % GANETI_RAPI_VERSION, |
583 | 607 |
query, None) |
... | ... | |
593 | 617 |
|
594 | 618 |
""" |
595 | 619 |
query = [] |
596 |
if bulk: |
|
597 |
query.append(("bulk", 1)) |
|
620 |
_AppendIf(query, bulk, ("bulk", 1)) |
|
598 | 621 |
|
599 | 622 |
instances = self._SendRequest(HTTP_GET, |
600 | 623 |
"/%s/instances" % GANETI_RAPI_VERSION, |
... | ... | |
662 | 685 |
""" |
663 | 686 |
query = [] |
664 | 687 |
|
665 |
if kwargs.get("dry_run"): |
|
666 |
query.append(("dry-run", 1)) |
|
688 |
_AppendDryRunIf(query, kwargs.get("dry_run")) |
|
667 | 689 |
|
668 | 690 |
if _INST_CREATE_REQV1 in self.GetFeatures(): |
669 | 691 |
# All required fields for request data version 1 |
... | ... | |
701 | 723 |
|
702 | 724 |
""" |
703 | 725 |
query = [] |
704 |
if dry_run: |
|
705 |
query.append(("dry-run", 1)) |
|
726 |
_AppendDryRunIf(query, dry_run) |
|
706 | 727 |
|
707 | 728 |
return self._SendRequest(HTTP_DELETE, |
708 | 729 |
("/%s/instances/%s" % |
... | ... | |
737 | 758 |
|
738 | 759 |
""" |
739 | 760 |
query = [] |
740 |
if ignore_size: |
|
741 |
query.append(("ignore_size", 1)) |
|
761 |
_AppendIf(query, ignore_size, ("ignore_size", 1)) |
|
742 | 762 |
|
743 | 763 |
return self._SendRequest(HTTP_PUT, |
744 | 764 |
("/%s/instances/%s/activate-disks" % |
... | ... | |
840 | 860 |
|
841 | 861 |
""" |
842 | 862 |
query = [("tag", t) for t in tags] |
843 |
if dry_run: |
|
844 |
query.append(("dry-run", 1)) |
|
863 |
_AppendDryRunIf(query, dry_run) |
|
845 | 864 |
|
846 | 865 |
return self._SendRequest(HTTP_PUT, |
847 | 866 |
("/%s/instances/%s/tags" % |
... | ... | |
861 | 880 |
|
862 | 881 |
""" |
863 | 882 |
query = [("tag", t) for t in tags] |
864 |
if dry_run: |
|
865 |
query.append(("dry-run", 1)) |
|
883 |
_AppendDryRunIf(query, dry_run) |
|
866 | 884 |
|
867 | 885 |
return self._SendRequest(HTTP_DELETE, |
868 | 886 |
("/%s/instances/%s/tags" % |
... | ... | |
886 | 904 |
|
887 | 905 |
""" |
888 | 906 |
query = [] |
889 |
if reboot_type: |
|
890 |
query.append(("type", reboot_type)) |
|
891 |
if ignore_secondaries is not None: |
|
892 |
query.append(("ignore_secondaries", ignore_secondaries)) |
|
893 |
if dry_run: |
|
894 |
query.append(("dry-run", 1)) |
|
907 |
_AppendDryRunIf(query, dry_run) |
|
908 |
_AppendIf(query, reboot_type, ("type", reboot_type)) |
|
909 |
_AppendIf(query, ignore_secondaries is not None, |
|
910 |
("ignore_secondaries", ignore_secondaries)) |
|
895 | 911 |
|
896 | 912 |
return self._SendRequest(HTTP_POST, |
897 | 913 |
("/%s/instances/%s/reboot" % |
... | ... | |
911 | 927 |
|
912 | 928 |
""" |
913 | 929 |
query = [] |
914 |
if dry_run: |
|
915 |
query.append(("dry-run", 1)) |
|
916 |
if no_remember: |
|
917 |
query.append(("no-remember", 1)) |
|
930 |
_AppendDryRunIf(query, dry_run) |
|
931 |
_AppendIf(query, no_remember, ("no-remember", 1)) |
|
918 | 932 |
|
919 | 933 |
return self._SendRequest(HTTP_PUT, |
920 | 934 |
("/%s/instances/%s/shutdown" % |
... | ... | |
934 | 948 |
|
935 | 949 |
""" |
936 | 950 |
query = [] |
937 |
if dry_run: |
|
938 |
query.append(("dry-run", 1)) |
|
939 |
if no_remember: |
|
940 |
query.append(("no-remember", 1)) |
|
951 |
_AppendDryRunIf(query, dry_run) |
|
952 |
_AppendIf(query, no_remember, ("no-remember", 1)) |
|
941 | 953 |
|
942 | 954 |
return self._SendRequest(HTTP_PUT, |
943 | 955 |
("/%s/instances/%s/startup" % |
... | ... | |
976 | 988 |
" for instance reinstallation") |
977 | 989 |
|
978 | 990 |
query = [] |
979 |
if os: |
|
980 |
query.append(("os", os)) |
|
981 |
if no_startup: |
|
982 |
query.append(("nostartup", 1)) |
|
991 |
_AppendIf(query, os, ("os", os)) |
|
992 |
_AppendIf(query, no_startup, ("nostartup", 1)) |
|
993 |
|
|
983 | 994 |
return self._SendRequest(HTTP_POST, |
984 | 995 |
("/%s/instances/%s/reinstall" % |
985 | 996 |
(GANETI_RAPI_VERSION, instance)), query, None) |
... | ... | |
1012 | 1023 |
# TODO: Convert to body parameters |
1013 | 1024 |
|
1014 | 1025 |
if disks is not None: |
1015 |
query.append(("disks", ",".join(str(idx) for idx in disks))) |
|
1026 |
_AppendIf(query, True, |
|
1027 |
("disks", ",".join(str(idx) for idx in disks))) |
|
1016 | 1028 |
|
1017 |
if remote_node is not None: |
|
1018 |
query.append(("remote_node", remote_node)) |
|
1019 |
|
|
1020 |
if iallocator is not None: |
|
1021 |
query.append(("iallocator", iallocator)) |
|
1029 |
_AppendIf(query, remote_node is not None, ("remote_node", remote_node)) |
|
1030 |
_AppendIf(query, iallocator is not None, ("iallocator", iallocator)) |
|
1022 | 1031 |
|
1023 | 1032 |
return self._SendRequest(HTTP_POST, |
1024 | 1033 |
("/%s/instances/%s/replace-disks" % |
... | ... | |
1268 | 1277 |
|
1269 | 1278 |
""" |
1270 | 1279 |
query = [] |
1271 |
if dry_run: |
|
1272 |
query.append(("dry-run", 1)) |
|
1280 |
_AppendDryRunIf(query, dry_run) |
|
1273 | 1281 |
|
1274 | 1282 |
return self._SendRequest(HTTP_DELETE, |
1275 | 1283 |
"/%s/jobs/%s" % (GANETI_RAPI_VERSION, job_id), |
... | ... | |
1287 | 1295 |
|
1288 | 1296 |
""" |
1289 | 1297 |
query = [] |
1290 |
if bulk: |
|
1291 |
query.append(("bulk", 1)) |
|
1298 |
_AppendIf(query, bulk, ("bulk", 1)) |
|
1292 | 1299 |
|
1293 | 1300 |
nodes = self._SendRequest(HTTP_GET, "/%s/nodes" % GANETI_RAPI_VERSION, |
1294 | 1301 |
query, None) |
... | ... | |
1346 | 1353 |
raise GanetiApiError("Only one of iallocator or remote_node can be used") |
1347 | 1354 |
|
1348 | 1355 |
query = [] |
1349 |
if dry_run: |
|
1350 |
query.append(("dry-run", 1)) |
|
1356 |
_AppendDryRunIf(query, dry_run) |
|
1351 | 1357 |
|
1352 | 1358 |
if _NODE_EVAC_RES1 in self.GetFeatures(): |
1353 | 1359 |
# Server supports body parameters |
... | ... | |
1374 | 1380 |
if mode is not None and mode != NODE_EVAC_SEC: |
1375 | 1381 |
raise GanetiApiError("Server can only evacuate secondary instances") |
1376 | 1382 |
|
1377 |
if iallocator: |
|
1378 |
query.append(("iallocator", iallocator)) |
|
1379 |
if remote_node: |
|
1380 |
query.append(("remote_node", remote_node)) |
|
1381 |
if early_release: |
|
1382 |
query.append(("early_release", 1)) |
|
1383 |
_AppendIf(query, iallocator, ("iallocator", iallocator)) |
|
1384 |
_AppendIf(query, remote_node, ("remote_node", remote_node)) |
|
1385 |
_AppendIf(query, early_release, ("early_release", 1)) |
|
1383 | 1386 |
|
1384 | 1387 |
return self._SendRequest(HTTP_POST, |
1385 | 1388 |
("/%s/nodes/%s/evacuate" % |
... | ... | |
1406 | 1409 |
|
1407 | 1410 |
""" |
1408 | 1411 |
query = [] |
1409 |
if dry_run: |
|
1410 |
query.append(("dry-run", 1)) |
|
1412 |
_AppendDryRunIf(query, dry_run) |
|
1411 | 1413 |
|
1412 | 1414 |
if _NODE_MIGRATE_REQV1 in self.GetFeatures(): |
1413 | 1415 |
body = {} |
... | ... | |
1430 | 1432 |
raise GanetiApiError("Server does not support specifying target node" |
1431 | 1433 |
" for node migration") |
1432 | 1434 |
|
1433 |
if mode is not None: |
|
1434 |
query.append(("mode", mode)) |
|
1435 |
_AppendIf(query, mode is not None, ("mode", mode)) |
|
1435 | 1436 |
|
1436 | 1437 |
return self._SendRequest(HTTP_POST, |
1437 | 1438 |
("/%s/nodes/%s/migrate" % |
... | ... | |
1468 | 1469 |
@return: job id |
1469 | 1470 |
|
1470 | 1471 |
""" |
1471 |
query = [ |
|
1472 |
("force", force), |
|
1473 |
] |
|
1474 |
|
|
1475 |
if auto_promote is not None: |
|
1476 |
query.append(("auto-promote", auto_promote)) |
|
1472 |
query = [] |
|
1473 |
_AppendForceIf(query, force) |
|
1474 |
_AppendIf(query, auto_promote is not None, ("auto-promote", auto_promote)) |
|
1477 | 1475 |
|
1478 | 1476 |
return self._SendRequest(HTTP_PUT, |
1479 | 1477 |
("/%s/nodes/%s/role" % |
... | ... | |
1490 | 1488 |
@return: job id |
1491 | 1489 |
|
1492 | 1490 |
""" |
1493 |
query = [ |
|
1494 |
("force", force), |
|
1495 |
] |
|
1491 |
query = [] |
|
1492 |
_AppendForceIf(query, force) |
|
1496 | 1493 |
|
1497 | 1494 |
return self._SendRequest(HTTP_POST, |
1498 | 1495 |
("/%s/nodes/%s/powercycle" % |
... | ... | |
1558 | 1555 |
("name", name), |
1559 | 1556 |
] |
1560 | 1557 |
|
1561 |
if allocatable is not None: |
|
1562 |
query.append(("allocatable", allocatable)) |
|
1558 |
_AppendIf(query, allocatable is not None, ("allocatable", allocatable)) |
|
1563 | 1559 |
|
1564 | 1560 |
return self._SendRequest(HTTP_PUT, |
1565 | 1561 |
("/%s/nodes/%s/storage/modify" % |
... | ... | |
1617 | 1613 |
|
1618 | 1614 |
""" |
1619 | 1615 |
query = [("tag", t) for t in tags] |
1620 |
if dry_run: |
|
1621 |
query.append(("dry-run", 1)) |
|
1616 |
_AppendDryRunIf(query, dry_run) |
|
1622 | 1617 |
|
1623 | 1618 |
return self._SendRequest(HTTP_PUT, |
1624 | 1619 |
("/%s/nodes/%s/tags" % |
... | ... | |
1639 | 1634 |
|
1640 | 1635 |
""" |
1641 | 1636 |
query = [("tag", t) for t in tags] |
1642 |
if dry_run: |
|
1643 |
query.append(("dry-run", 1)) |
|
1637 |
_AppendDryRunIf(query, dry_run) |
|
1644 | 1638 |
|
1645 | 1639 |
return self._SendRequest(HTTP_DELETE, |
1646 | 1640 |
("/%s/nodes/%s/tags" % |
... | ... | |
1658 | 1652 |
|
1659 | 1653 |
""" |
1660 | 1654 |
query = [] |
1661 |
if bulk: |
|
1662 |
query.append(("bulk", 1)) |
|
1655 |
_AppendIf(query, bulk, ("bulk", 1)) |
|
1663 | 1656 |
|
1664 | 1657 |
groups = self._SendRequest(HTTP_GET, "/%s/groups" % GANETI_RAPI_VERSION, |
1665 | 1658 |
query, None) |
... | ... | |
1697 | 1690 |
|
1698 | 1691 |
""" |
1699 | 1692 |
query = [] |
1700 |
if dry_run: |
|
1701 |
query.append(("dry-run", 1)) |
|
1693 |
_AppendDryRunIf(query, dry_run) |
|
1702 | 1694 |
|
1703 | 1695 |
body = { |
1704 | 1696 |
"name": name, |
... | ... | |
1736 | 1728 |
|
1737 | 1729 |
""" |
1738 | 1730 |
query = [] |
1739 |
if dry_run: |
|
1740 |
query.append(("dry-run", 1)) |
|
1731 |
_AppendDryRunIf(query, dry_run) |
|
1741 | 1732 |
|
1742 | 1733 |
return self._SendRequest(HTTP_DELETE, |
1743 | 1734 |
("/%s/groups/%s" % |
... | ... | |
1776 | 1767 |
|
1777 | 1768 |
""" |
1778 | 1769 |
query = [] |
1779 |
|
|
1780 |
if force: |
|
1781 |
query.append(("force", 1)) |
|
1782 |
|
|
1783 |
if dry_run: |
|
1784 |
query.append(("dry-run", 1)) |
|
1770 |
_AppendForceIf(query, force) |
|
1771 |
_AppendDryRunIf(query, dry_run) |
|
1785 | 1772 |
|
1786 | 1773 |
body = { |
1787 | 1774 |
"nodes": nodes, |
... | ... | |
1820 | 1807 |
|
1821 | 1808 |
""" |
1822 | 1809 |
query = [("tag", t) for t in tags] |
1823 |
if dry_run: |
|
1824 |
query.append(("dry-run", 1)) |
|
1810 |
_AppendDryRunIf(query, dry_run) |
|
1825 | 1811 |
|
1826 | 1812 |
return self._SendRequest(HTTP_PUT, |
1827 | 1813 |
("/%s/groups/%s/tags" % |
... | ... | |
1841 | 1827 |
|
1842 | 1828 |
""" |
1843 | 1829 |
query = [("tag", t) for t in tags] |
1844 |
if dry_run: |
|
1845 |
query.append(("dry-run", 1)) |
|
1830 |
_AppendDryRunIf(query, dry_run) |
|
1846 | 1831 |
|
1847 | 1832 |
return self._SendRequest(HTTP_DELETE, |
1848 | 1833 |
("/%s/groups/%s/tags" % |
... | ... | |
1890 | 1875 |
query = [] |
1891 | 1876 |
|
1892 | 1877 |
if fields is not None: |
1893 |
query.append(("fields", ",".join(fields)))
|
|
1878 |
_AppendIf(query, True, ("fields", ",".join(fields)))
|
|
1894 | 1879 |
|
1895 | 1880 |
return self._SendRequest(HTTP_GET, |
1896 | 1881 |
("/%s/query/%s/fields" % |
Also available in: Unified diff