1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 1738 1739 1740 1741 1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 1753 1754 1755 1756 1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796
|
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import typing as ty
import warnings
from openstack.block_storage.v3 import volume as _volume
from openstack.compute.v2 import aggregate as _aggregate
from openstack.compute.v2 import availability_zone
from openstack.compute.v2 import extension
from openstack.compute.v2 import flavor as _flavor
from openstack.compute.v2 import hypervisor as _hypervisor
from openstack.compute.v2 import image as _image
from openstack.compute.v2 import keypair as _keypair
from openstack.compute.v2 import limits
from openstack.compute.v2 import migration as _migration
from openstack.compute.v2 import quota_class_set as _quota_class_set
from openstack.compute.v2 import quota_set as _quota_set
from openstack.compute.v2 import server as _server
from openstack.compute.v2 import server_action as _server_action
from openstack.compute.v2 import server_diagnostics as _server_diagnostics
from openstack.compute.v2 import server_group as _server_group
from openstack.compute.v2 import server_interface as _server_interface
from openstack.compute.v2 import server_ip
from openstack.compute.v2 import server_migration as _server_migration
from openstack.compute.v2 import server_remote_console as _src
from openstack.compute.v2 import service as _service
from openstack.compute.v2 import usage as _usage
from openstack.compute.v2 import volume_attachment as _volume_attachment
from openstack import exceptions
from openstack.identity.v3 import project as _project
from openstack.identity.v3 import user as _user
from openstack.network.v2 import security_group as _sg
from openstack import proxy
from openstack import resource
from openstack import utils
from openstack import warnings as os_warnings
class Proxy(proxy.Proxy):
_resource_registry = {
"aggregate": _aggregate.Aggregate,
"availability_zone": availability_zone.AvailabilityZone,
"extension": extension.Extension,
"flavor": _flavor.Flavor,
"hypervisor": _hypervisor.Hypervisor,
"image": _image.Image,
"keypair": _keypair.Keypair,
"limits": limits.Limits,
"migration": _migration.Migration,
"quota_class_set": _quota_class_set.QuotaClassSet,
"quota_set": _quota_set.QuotaSet,
"server": _server.Server,
"server_action": _server_action.ServerAction,
"server_diagnostics": _server_diagnostics.ServerDiagnostics,
"server_group": _server_group.ServerGroup,
"server_interface": _server_interface.ServerInterface,
"server_ip": server_ip.ServerIP,
"server_migration": _server_migration.ServerMigration,
"server_remote_console": _src.ServerRemoteConsole,
"service": _service.Service,
"usage": _usage.Usage,
"volume_attachment": _volume_attachment.VolumeAttachment,
}
# ========== Extensions ==========
def find_extension(self, name_or_id, ignore_missing=True):
"""Find a single extension
:param name_or_id: The name or ID of an extension.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.extension.Extension` or
None
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
:raises: :class:`~openstack.exceptions.DuplicateResource` when multiple
resources are found.
"""
return self._find(
extension.Extension,
name_or_id,
ignore_missing=ignore_missing,
)
def extensions(self):
"""Retrieve a generator of extensions
:returns: A generator of extension instances.
:rtype: :class:`~openstack.compute.v2.extension.Extension`
"""
return self._list(extension.Extension)
# ========== Flavors ==========
# TODO(stephenfin): Drop 'query' parameter or apply it consistently
def find_flavor(
self,
name_or_id,
ignore_missing=True,
*,
get_extra_specs=False,
**query,
):
"""Find a single flavor
:param name_or_id: The name or ID of a flavor.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the resource does not exist. When set to ``True``, None will be
returned when attempting to find a nonexistent resource.
:param bool get_extra_specs: When set to ``True`` and extra_specs not
present in the response will invoke additional API call to fetch
extra_specs.
:param kwargs query: Optional query parameters to be sent to limit
the flavors being returned.
:returns: One :class:`~openstack.compute.v2.flavor.Flavor` or None
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
:raises: :class:`~openstack.exceptions.DuplicateResource` when multiple
resources are found.
"""
flavor = self._find(
_flavor.Flavor,
name_or_id,
ignore_missing=ignore_missing,
**query,
)
if flavor and get_extra_specs and not flavor.extra_specs:
flavor = flavor.fetch_extra_specs(self)
return flavor
def create_flavor(self, **attrs):
"""Create a new flavor from attributes
:param dict attrs: Keyword arguments which will be used to create
a :class:`~openstack.compute.v2.flavor.Flavor`,
comprised of the properties on the Flavor class.
:returns: The results of flavor creation
:rtype: :class:`~openstack.compute.v2.flavor.Flavor`
"""
return self._create(_flavor.Flavor, **attrs)
def delete_flavor(self, flavor, ignore_missing=True):
"""Delete a flavor
:param flavor: The value can be either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be
raised when the flavor does not exist.
When set to ``True``, no exception will be set when
attempting to delete a nonexistent flavor.
:returns: ``None``
"""
self._delete(_flavor.Flavor, flavor, ignore_missing=ignore_missing)
def update_flavor(self, flavor, **attrs):
"""Update a flavor
:param flavor: Either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:param attrs: The attributes to update on the flavor represented
by ``flavor``.
:returns: The updated flavor
:rtype: :class:`~openstack.compute.v2.flavor.Flavor`
"""
return self._update(_flavor.Flavor, flavor, **attrs)
def get_flavor(self, flavor, get_extra_specs=False):
"""Get a single flavor
:param flavor: The value can be the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:param bool get_extra_specs: When set to ``True`` and extra_specs not
present in the response will invoke additional API call to fetch
extra_specs.
:returns: One :class:`~openstack.compute.v2.flavor.Flavor`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
flavor = self._get(_flavor.Flavor, flavor)
if get_extra_specs and not flavor.extra_specs:
flavor = flavor.fetch_extra_specs(self)
return flavor
def flavors(self, details=True, get_extra_specs=False, **query):
"""Return a generator of flavors
:param bool details: When ``True``, returns
:class:`~openstack.compute.v2.flavor.Flavor` objects,
with additional attributes filled.
:param bool get_extra_specs: When set to ``True`` and extra_specs not
present in the response will invoke additional API call to fetch
extra_specs.
:param kwargs query: Optional query parameters to be sent to limit
the flavors being returned.
:returns: A generator of flavor objects
"""
base_path = '/flavors/detail' if details else '/flavors'
for flv in self._list(_flavor.Flavor, base_path=base_path, **query):
if get_extra_specs and not flv.extra_specs:
flv = flv.fetch_extra_specs(self)
yield flv
def flavor_add_tenant_access(self, flavor, tenant):
"""Adds tenant/project access to flavor.
:param flavor: Either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:param str tenant: The UUID of the tenant.
:returns: One :class:`~openstack.compute.v2.flavor.Flavor`
"""
flavor = self._get_resource(_flavor.Flavor, flavor)
return flavor.add_tenant_access(self, tenant)
def flavor_remove_tenant_access(self, flavor, tenant):
"""Removes tenant/project access to flavor.
:param flavor: Either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:param str tenant: The UUID of the tenant.
:returns: One :class:`~openstack.compute.v2.flavor.Flavor`
"""
flavor = self._get_resource(_flavor.Flavor, flavor)
return flavor.remove_tenant_access(self, tenant)
def get_flavor_access(self, flavor):
"""Lists tenants who have access to private flavor
:param flavor: Either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:returns: List of dicts with flavor_id and tenant_id attributes.
"""
flavor = self._get_resource(_flavor.Flavor, flavor)
return flavor.get_access(self)
def fetch_flavor_extra_specs(self, flavor):
"""Lists Extra Specs of a flavor
:param flavor: Either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:returns: One :class:`~openstack.compute.v2.flavor.Flavor`
"""
flavor = self._get_resource(_flavor.Flavor, flavor)
return flavor.fetch_extra_specs(self)
def create_flavor_extra_specs(self, flavor, extra_specs):
"""Lists Extra Specs of a flavor
:param flavor: Either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:param dict extra_specs: dict of extra specs
:returns: One :class:`~openstack.compute.v2.flavor.Flavor`
"""
flavor = self._get_resource(_flavor.Flavor, flavor)
return flavor.create_extra_specs(self, specs=extra_specs)
def get_flavor_extra_specs_property(self, flavor, prop):
"""Get specific Extra Spec property of a flavor
:param flavor: Either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:param str prop: Property name.
:returns: String value of the requested property.
"""
flavor = self._get_resource(_flavor.Flavor, flavor)
return flavor.get_extra_specs_property(self, prop)
def update_flavor_extra_specs_property(self, flavor, prop, val):
"""Update specific Extra Spec property of a flavor
:param flavor: Either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:param str prop: Property name.
:param str val: Property value.
:returns: String value of the requested property.
"""
flavor = self._get_resource(_flavor.Flavor, flavor)
return flavor.update_extra_specs_property(self, prop, val)
def delete_flavor_extra_specs_property(self, flavor, prop):
"""Delete specific Extra Spec property of a flavor
:param flavor: Either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:param str prop: Property name.
:returns: None
"""
flavor = self._get_resource(_flavor.Flavor, flavor)
return flavor.delete_extra_specs_property(self, prop)
# ========== Aggregates ==========
def aggregates(self, **query):
"""Return a generator of aggregate
:param kwargs query: Optional query parameters to be sent to limit
the aggregates being returned.
:returns: A generator of aggregate
:rtype: class: `~openstack.compute.v2.aggregate.Aggregate`
"""
return self._list(_aggregate.Aggregate, **query)
def get_aggregate(self, aggregate):
"""Get a single host aggregate
:param aggregate: The value can be the ID of an aggregate or a
:class:`~openstack.compute.v2.aggregate.Aggregate` instance.
:returns: One :class:`~openstack.compute.v2.aggregate.Aggregate`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
return self._get(_aggregate.Aggregate, aggregate)
def find_aggregate(self, name_or_id, ignore_missing=True):
"""Find a single aggregate
:param name_or_id: The name or ID of an aggregate.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the resource does not exist. When set to ``True``, None will be
returned when attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.aggregate.Aggregate`
or None
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
:raises: :class:`~openstack.exceptions.DuplicateResource` when multiple
resources are found.
"""
return self._find(
_aggregate.Aggregate,
name_or_id,
ignore_missing=ignore_missing,
)
def create_aggregate(self, **attrs):
"""Create a new host aggregate from attributes
:param dict attrs: Keyword arguments which will be used to create a
:class:`~openstack.compute.v2.aggregate.Aggregate`,
comprised of the properties on the Aggregate class.
:returns: The results of aggregate creation
:rtype: :class:`~openstack.compute.v2.aggregate.Aggregate`
"""
return self._create(_aggregate.Aggregate, **attrs)
def update_aggregate(self, aggregate, **attrs):
"""Update a host aggregate
:param server: Either the ID of a host aggregate or a
:class:`~openstack.compute.v2.aggregate.Aggregate` instance.
:param attrs: The attributes to update on the aggregate represented
by ``aggregate``.
:returns: The updated aggregate
:rtype: :class:`~openstack.compute.v2.aggregate.Aggregate`
"""
return self._update(_aggregate.Aggregate, aggregate, **attrs)
def delete_aggregate(self, aggregate, ignore_missing=True):
"""Delete a host aggregate
:param keypair: The value can be either the ID of an aggregate or a
:class:`~openstack.compute.v2.aggregate.Aggregate`
instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be
raised when the aggregate does not exist.
When set to ``True``, no exception will be set when
attempting to delete a nonexistent aggregate.
:returns: ``None``
"""
self._delete(
_aggregate.Aggregate,
aggregate,
ignore_missing=ignore_missing,
)
def add_host_to_aggregate(self, aggregate, host):
"""Adds a host to an aggregate
:param aggregate: Either the ID of a aggregate or a
:class:`~openstack.compute.v2.aggregate.Aggregate`
instance.
:param str host: The host to add to the aggregate
:returns: One :class:`~openstack.compute.v2.aggregate.Aggregate`
"""
aggregate = self._get_resource(_aggregate.Aggregate, aggregate)
return aggregate.add_host(self, host)
def remove_host_from_aggregate(self, aggregate, host):
"""Removes a host from an aggregate
:param aggregate: Either the ID of a aggregate or a
:class:`~openstack.compute.v2.aggregate.Aggregate`
instance.
:param str host: The host to remove from the aggregate
:returns: One :class:`~openstack.compute.v2.aggregate.Aggregate`
"""
aggregate = self._get_resource(_aggregate.Aggregate, aggregate)
return aggregate.remove_host(self, host)
def set_aggregate_metadata(self, aggregate, metadata):
"""Creates or replaces metadata for an aggregate
:param aggregate: Either the ID of a aggregate or a
:class:`~openstack.compute.v2.aggregate.Aggregate`
instance.
:param dict metadata: Metadata key and value pairs. The maximum
size for each metadata key and value pair
is 255 bytes.
:returns: One :class:`~openstack.compute.v2.aggregate.Aggregate`
"""
aggregate = self._get_resource(_aggregate.Aggregate, aggregate)
return aggregate.set_metadata(self, metadata)
def aggregate_precache_images(self, aggregate, images):
"""Requests image precaching on an aggregate
:param aggregate: Either the ID of a aggregate or a
:class:`~openstack.compute.v2.aggregate.Aggregate` instance.
:param images: Single image id or list of image ids.
:returns: ``None``
"""
aggregate = self._get_resource(_aggregate.Aggregate, aggregate)
# We need to ensure we pass list of image IDs
if isinstance(images, str):
images = [images]
image_data = []
for img in images:
image_data.append({'id': img})
return aggregate.precache_images(self, image_data)
# ========== Images ==========
def delete_image(self, image, ignore_missing=True):
"""Delete an image
:param image: The value can be either the ID of an image or a
:class:`~openstack.compute.v2.image.Image` instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be
raised when the image does not exist.
When set to ``True``, no exception will be set when
attempting to delete a nonexistent image.
:returns: ``None``
"""
warnings.warn(
'This API is a proxy to the image service and has been '
'deprecated; use the image service proxy API instead',
os_warnings.OpenStackDeprecationWarning,
)
self._delete(_image.Image, image, ignore_missing=ignore_missing)
# NOTE(stephenfin): We haven't added 'details' support here since this
# method is deprecated
def find_image(self, name_or_id, ignore_missing=True):
"""Find a single image
:param name_or_id: The name or ID of a image.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:returns: One :class:`~openstack.compute.v2.image.Image` or None
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
:raises: :class:`~openstack.exceptions.DuplicateResource` when multiple
resources are found.
"""
warnings.warn(
'This API is a proxy to the image service and has been '
'deprecated; use the image service proxy API instead',
os_warnings.OpenStackDeprecationWarning,
)
return self._find(
_image.Image,
name_or_id,
ignore_missing=ignore_missing,
)
def get_image(self, image):
"""Get a single image
:param image: The value can be the ID of an image or a
:class:`~openstack.compute.v2.image.Image` instance.
:returns: One :class:`~openstack.compute.v2.image.Image`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
warnings.warn(
'This API is a proxy to the image service and has been '
'deprecated; use the image service proxy API instead',
os_warnings.OpenStackDeprecationWarning,
)
return self._get(_image.Image, image)
def images(self, details=True, **query):
"""Return a generator of images
:param bool details: When ``True``, returns
:class:`~openstack.compute.v2.image.Image` objects with all
available properties, otherwise only basic properties are returned.
*Default: ``True``*
:param kwargs query: Optional query parameters to be sent to limit
the resources being returned.
:returns: A generator of image objects
"""
warnings.warn(
'This API is a proxy to the image service and has been '
'deprecated; use the image service proxy API instead',
os_warnings.OpenStackDeprecationWarning,
)
base_path = '/images/detail' if details else None
return self._list(_image.Image, base_path=base_path, **query)
def _get_base_resource(self, res, base):
# Metadata calls for Image and Server can work for both those
# resources but also ImageDetail and ServerDetail. If we get
# either class, use it, otherwise create an instance of the base.
if isinstance(res, base):
return res
else:
return base(id=res)
def get_image_metadata(self, image):
"""Return a dictionary of metadata for an image
:param image: Either the ID of an image or a
:class:`~openstack.compute.v2.image.Image` instance.
:returns: A :class:`~openstack.compute.v2.image.Image` with only the
image's metadata. All keys and values are Unicode text.
:rtype: :class:`~openstack.compute.v2.image.Image`
"""
res = self._get_base_resource(image, _image.Image)
return res.fetch_metadata(self)
def set_image_metadata(self, image, **metadata):
"""Update metadata for an image
:param image: Either the ID of an image or a
:class:`~openstack.compute.v2.image.Image` instance.
:param kwargs metadata: Key/value pairs to be updated in the image's
metadata. No other metadata is modified
by this call. All keys and values are stored
as Unicode.
:returns: A :class:`~openstack.compute.v2.image.Image` with only the
image's metadata. All keys and values are Unicode text.
:rtype: :class:`~openstack.compute.v2.image.Image`
"""
res = self._get_base_resource(image, _image.Image)
return res.set_metadata(self, metadata=metadata)
def delete_image_metadata(self, image, keys=None):
"""Delete metadata for an image
Note: This method will do a HTTP DELETE request for every key in keys.
:param image: Either the ID of an image or a
:class:`~openstack.compute.v2.image.Image` instance.
:param list keys: The keys to delete. If left empty complete metadata
will be removed.
:rtype: ``None``
"""
res = self._get_base_resource(image, _image.Image)
if keys is not None:
# Create a set as a snapshot of keys to avoid "changed during
# iteration"
for key in set(keys):
res.delete_metadata_item(self, key)
else:
res.delete_metadata(self)
# ========== Keypairs ==========
def create_keypair(self, **attrs):
"""Create a new keypair from attributes
:param dict attrs: Keyword arguments which will be used to create
a :class:`~openstack.compute.v2.keypair.Keypair`,
comprised of the properties on the Keypair class.
:returns: The results of keypair creation
:rtype: :class:`~openstack.compute.v2.keypair.Keypair`
"""
return self._create(_keypair.Keypair, **attrs)
def delete_keypair(self, keypair, ignore_missing=True, user_id=None):
"""Delete a keypair
:param keypair: The value can be either the ID of a keypair or a
:class:`~openstack.compute.v2.keypair.Keypair` instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the keypair does not exist. When set to ``True``, no exception
will be set when attempting to delete a nonexistent keypair.
:param str user_id: Optional user_id owning the keypair
:returns: ``None``
"""
# NOTE(gtema): it is necessary to overload normal logic since query
# parameters are not properly respected in typical DELETE case
res = self._get_resource(_keypair.Keypair, keypair)
try:
delete_params = {'user_id': user_id} if user_id else {}
res.delete(self, params=delete_params)
except exceptions.NotFoundException:
if ignore_missing:
return None
raise
def get_keypair(self, keypair, user_id=None):
"""Get a single keypair
:param keypair: The value can be the ID of a keypair or a
:class:`~openstack.compute.v2.keypair.Keypair` instance.
:param str user_id: Optional user_id owning the keypair
:returns: One :class:`~openstack.compute.v2.keypair.Keypair`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
# NOTE(gtema): it is necessary to overload normal logic since query
# parameters are not properly respected in typical fetch case
res = self._get_resource(_keypair.Keypair, keypair)
get_params = {'user_id': user_id} if user_id else {}
return res.fetch(
self, error_message=f"No Keypair found for {keypair}", **get_params
)
def find_keypair(self, name_or_id, ignore_missing=True, *, user_id=None):
"""Find a single keypair
:param name_or_id: The name or ID of a keypair.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the resource does not exist. When set to ``True``, None will be
returned when attempting to find a nonexistent resource.
:param str user_id: Optional user_id owning the keypair
:returns: One :class:`~openstack.compute.v2.keypair.Keypair` or None
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
:raises: :class:`~openstack.exceptions.DuplicateResource` when multiple
resources are found.
"""
attrs = {'user_id': user_id} if user_id else {}
return self._find(
_keypair.Keypair,
name_or_id,
ignore_missing=ignore_missing,
**attrs,
)
def keypairs(self, **query):
"""Return a generator of keypairs
:param kwargs query: Optional query parameters to be sent to limit
the resources being returned.
:returns: A generator of keypair objects
:rtype: :class:`~openstack.compute.v2.keypair.Keypair`
"""
return self._list(_keypair.Keypair, **query)
# ========== Limits ==========
def get_limits(self, **query):
"""Retrieve limits that are applied to the project's account
:returns: A Limits object, including both
:class:`~openstack.compute.v2.limits.AbsoluteLimits` and
:class:`~openstack.compute.v2.limits.RateLimits`
:rtype: :class:`~openstack.compute.v2.limits.Limits`
"""
res = self._get_resource(limits.Limits, None)
return res.fetch(self, **query)
# ========== Servers ==========
def create_server(self, **attrs):
"""Create a new server from attributes
:param dict attrs: Keyword arguments which will be used to create
a :class:`~openstack.compute.v2.server.Server`,
comprised of the properties on the Server class.
:returns: The results of server creation
:rtype: :class:`~openstack.compute.v2.server.Server`
"""
return self._create(_server.Server, **attrs)
def delete_server(self, server, ignore_missing=True, force=False):
"""Delete a server
:param server: The value can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be
raised when the server does not exist.
When set to ``True``, no exception will be set when
attempting to delete a nonexistent server
:param bool force: When set to ``True``, the server deletion will be
forced immediately.
:returns: ``None``
"""
if force:
server = self._get_resource(_server.Server, server)
server.force_delete(self)
else:
self._delete(_server.Server, server, ignore_missing=ignore_missing)
def find_server(
self,
name_or_id,
ignore_missing=True,
*,
details=True,
all_projects=False,
):
"""Find a single server
:param name_or_id: The name or ID of a server.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the resource does not exist. When set to ``True``, None will be
returned when attempting to find a nonexistent resource.
:param bool details: When set to ``False``
instances with only basic data will be returned. The default,
``True``, will cause instances with full data to be returned.
:param bool all_projects: When set to ``True``, search for server
by name across all projects. Note that this will likely result in a
higher chance of duplicates. Admin-only by default.
:returns: One :class:`~openstack.compute.v2.server.Server` or None
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
:raises: :class:`~openstack.exceptions.DuplicateResource` when multiple
resources are found.
"""
query = {}
if all_projects:
query['all_projects'] = True
list_base_path = '/servers/detail' if details else None
return self._find(
_server.Server,
name_or_id,
ignore_missing=ignore_missing,
list_base_path=list_base_path,
**query,
)
def get_server(self, server):
"""Get a single server
:param server: The value can be the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: One :class:`~openstack.compute.v2.server.Server`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
return self._get(_server.Server, server)
def servers(self, details=True, all_projects=False, **query):
"""Retrieve a generator of servers
:param bool details: When set to ``False``
instances with only basic data will be returned. The default,
``True``, will cause instances with full data to be returned.
:param bool all_projects: When set to ``True``, lists servers from all
projects. Admin-only by default.
:param kwargs query: Optional query parameters to be sent to limit
the servers being returned. Available parameters can be seen
under https://docs.openstack.org/api-ref/compute/#list-servers
:returns: A generator of server instances.
"""
if all_projects:
query['all_projects'] = True
base_path = '/servers/detail' if details else None
return self._list(_server.Server, base_path=base_path, **query)
def update_server(self, server, **attrs):
"""Update a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param attrs: The attributes to update on the server represented
by ``server``.
:returns: The updated server
:rtype: :class:`~openstack.compute.v2.server.Server`
"""
return self._update(_server.Server, server, **attrs)
def change_server_password(self, server, new_password):
"""Change the administrator password
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param str new_password: The new password to be set.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.change_password(self, new_password)
def get_server_password(self, server):
"""Get the administrator password
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: encrypted password.
"""
server = self._get_resource(_server.Server, server)
return server.get_password(self)
def clear_server_password(self, server):
"""Clear the administrator password
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.clear_password(self)
def reset_server_state(self, server, state):
"""Reset the state of server
:param server: The server can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server`.
:param state: The state of the server to be set, `active` or
`error` are valid.
:returns: None
"""
res = self._get_base_resource(server, _server.Server)
res.reset_state(self, state)
def reboot_server(self, server, reboot_type):
"""Reboot a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param str reboot_type: The type of reboot to perform.
"HARD" and "SOFT" are the current options.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.reboot(self, reboot_type)
def rebuild_server(self, server, image, **attrs):
"""Rebuild a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param str name: The name of the server
:param str admin_password: The administrator password
:param bool preserve_ephemeral: Indicates whether the server
is rebuilt with the preservation of the ephemeral partition.
*Default: False*
:param str image: The id of an image to rebuild with. *Default: None*
:param str access_ipv4: The IPv4 address to rebuild with.
*Default: None*
:param str access_ipv6: The IPv6 address to rebuild with.
*Default: None*
:param dict metadata: A dictionary of metadata to rebuild with.
*Default: None*
:param personality: A list of dictionaries, each including a
**path** and **contents** key, to be injected
into the rebuilt server at launch.
*Default: None*
:returns: The rebuilt :class:`~openstack.compute.v2.server.Server`
instance.
"""
server = self._get_resource(_server.Server, server)
return server.rebuild(self, image=image, **attrs)
def resize_server(self, server, flavor):
"""Resize a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param flavor: Either the ID of a flavor or a
:class:`~openstack.compute.v2.flavor.Flavor` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
flavor_id = resource.Resource._get_id(flavor)
server.resize(self, flavor_id)
def confirm_server_resize(self, server):
"""Confirm a server resize
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.confirm_resize(self)
def revert_server_resize(self, server):
"""Revert a server resize
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.revert_resize(self)
def create_server_image(
self,
server,
name,
metadata=None,
wait=False,
timeout=120,
):
"""Create an image from a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param str name: The name of the image to be created.
:param dict metadata: A dictionary of metadata to be set on the image.
:returns: :class:`~openstack.image.v2.image.Image` object.
"""
server = self._get_resource(_server.Server, server)
image_id = server.create_image(self, name, metadata)
image = self._connection.get_image(image_id)
if not wait:
return image
return self._connection.wait_for_image(image, timeout=timeout)
def backup_server(self, server, name, backup_type, rotation):
"""Backup a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param name: The name of the backup image.
:param backup_type: The type of the backup, for example, daily.
:param rotation: The rotation of the back up image, the oldest
image will be removed when image count exceed
the rotation count.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.backup(self, name, backup_type, rotation)
def pause_server(self, server):
"""Pauses a server and changes its status to ``PAUSED``.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.pause(self)
def unpause_server(self, server):
"""Unpauses a paused server and changes its status to ``ACTIVE``.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.unpause(self)
def suspend_server(self, server):
"""Suspends a server and changes its status to ``SUSPENDED``.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.suspend(self)
def resume_server(self, server):
"""Resumes a suspended server and changes its status to ``ACTIVE``.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.resume(self)
def lock_server(self, server, locked_reason=None):
"""Locks a server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param locked_reason: The reason behind locking the server. Limited to
255 characters in length.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.lock(self, locked_reason=locked_reason)
def unlock_server(self, server):
"""Unlocks a locked server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.unlock(self)
def rescue_server(self, server, admin_pass=None, image_ref=None):
"""Puts a server in rescue mode and changes it status to ``RESCUE``.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param admin_pass: The password for the rescued server. If you omit
this parameter, the operation generates a new
password.
:param image_ref: The image reference to use to rescue your server.
This can be the image ID or its full URL. If you
omit this parameter, the base image reference will
be used.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.rescue(self, admin_pass=admin_pass, image_ref=image_ref)
def unrescue_server(self, server):
"""Unrescues a server and changes its status to ``ACTIVE``.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.unrescue(self)
def evacuate_server(
self,
server,
host=None,
admin_pass=None,
force=None,
*,
on_shared_storage=None,
):
"""Evacuates a server from a failed host to a new host.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param host: An optional parameter specifying the name or ID of the
host to which the server is evacuated.
:param admin_pass: An optional parameter specifying the administrative
password to access the evacuated or rebuilt server.
:param force: Force an evacuation by not verifying the provided
destination host by the scheduler. (New in API version
2.29).
:param on_shared_storage: Whether the host is using shared storage.
(Optional) (Only supported before API version 2.14)
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.evacuate(
self,
host=host,
admin_pass=admin_pass,
force=force,
on_shared_storage=on_shared_storage,
)
def start_server(self, server):
"""Starts a stopped server and changes its state to ``ACTIVE``.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.start(self)
def stop_server(self, server):
"""Stops a running server and changes its state to ``SHUTOFF``.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.stop(self)
def restore_server(self, server):
"""Restore a soft-deleted server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.restore(self)
def shelve_server(self, server):
"""Shelves a server.
All associated data and resources are kept but anything still in
memory is not retained. Policy defaults enable only users with
administrative role or the owner of the server to perform this
operation. Cloud provides could change this permission though.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.shelve(self)
def shelve_offload_server(self, server):
"""Shelve-offloads, or removes, a server
Data and resource associations are deleted.
Policy defaults enable only users with administrative role or the owner
of the server to perform this operation. Cloud provides could change
this permission though.
Note that in some clouds, shelved servers are automatically offloaded,
sometimes after a certain time period.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.shelve_offload(self)
def unshelve_server(self, server, *, host=None):
"""Unshelves or restores a shelved server.
Policy defaults enable only users with administrative role or the
owner of the server to perform this operation. Cloud provides could
change this permission though.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param host: An optional parameter specifying the name the compute
host to unshelve to. (New in API version 2.91).
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.unshelve(self, host=host)
def trigger_server_crash_dump(self, server):
"""Trigger a crash dump in a server.
When a server starts behaving oddly at a fundamental level, it maybe be
useful to get a kernel level crash dump to debug further. The crash
dump action forces a crash dump followed by a system reboot of the
server. Once the server comes back online, you can find a Kernel Crash
Dump file in a certain location of the filesystem. For example, for
Ubuntu you can find it in the /var/crash directory.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.trigger_crash_dump(self)
def add_tag_to_server(self, server, tag):
"""Add a tag to a server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param tag: The tag to add.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.add_tag(self, tag)
def remove_tag_from_server(self, server, tag):
"""Remove a tag from a server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param tag: The tag to remove.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.remove_tag(self, tag)
def remove_tags_from_server(self, server):
"""Remove all tags from a server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param tag: The tag to remove.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.remove_all_tags(self)
# ========== Server security groups ==========
def fetch_server_security_groups(self, server):
"""Fetch security groups with details for a server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: updated :class:`~openstack.compute.v2.server.Server` instance
"""
server = self._get_resource(_server.Server, server)
return server.fetch_security_groups(self)
def add_security_group_to_server(self, server, security_group):
"""Add a security group to a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param security_group: Either the ID or name of a security group or a
:class:`~openstack.network.v2.security_group.SecurityGroup`
instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
security_group = self._get_resource(_sg.SecurityGroup, security_group)
server.add_security_group(
self,
security_group.name or security_group.id,
)
def remove_security_group_from_server(self, server, security_group):
"""Remove a security group from a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param security_group: Either the ID or name of a security group or a
:class:`~openstack.network.v2.security_group.SecurityGroup`
instance.
:returns: None
"""
server = self._get_resource(_server.Server, server)
security_group = self._get_resource(_sg.SecurityGroup, security_group)
server.remove_security_group(
self,
security_group.name or security_group.id,
)
# ========== Server IPs ==========
def add_fixed_ip_to_server(self, server, network_id):
"""Adds a fixed IP address to a server instance.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param network_id: The ID of the network from which a fixed IP address
is about to be allocated.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.add_fixed_ip(self, network_id)
def remove_fixed_ip_from_server(self, server, address):
"""Removes a fixed IP address from a server instance.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param address: The fixed IP address to be disassociated from the
server.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.remove_fixed_ip(self, address)
def add_floating_ip_to_server(self, server, address, fixed_address=None):
"""Adds a floating IP address to a server instance.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param address: The floating IP address to be added to the server.
:param fixed_address: The fixed IP address to be associated with the
floating IP address. Used when the server is
connected to multiple networks.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.add_floating_ip(self, address, fixed_address=fixed_address)
def remove_floating_ip_from_server(self, server, address):
"""Removes a floating IP address from a server instance.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param address: The floating IP address to be disassociated from the
server.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.remove_floating_ip(self, address)
# ========== Server Interfaces ==========
def create_server_interface(self, server, **attrs):
"""Create a new server interface from attributes
:param server: The server can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance
that the interface belongs to.
:param dict attrs: Keyword arguments which will be used to create
a :class:`~openstack.compute.v2.server_interface.ServerInterface`,
comprised of the properties on the ServerInterface class.
:returns: The results of server interface creation
:rtype: :class:`~openstack.compute.v2.server_interface.ServerInterface`
"""
server_id = resource.Resource._get_id(server)
return self._create(
_server_interface.ServerInterface,
server_id=server_id,
**attrs,
)
def delete_server_interface(
self,
server_interface,
server=None,
ignore_missing=True,
):
"""Delete a server interface
:param server_interface:
The value can be either the ID of a server interface or a
:class:`~openstack.compute.v2.server_interface.ServerInterface`
instance.
:param server: This parameter need to be specified when ServerInterface
ID is given as value. It can be either the ID of a
server or a :class:`~openstack.compute.v2.server.Server`
instance that the interface belongs to.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be
raised when the server interface does not exist.
When set to ``True``, no exception will be set when
attempting to delete a nonexistent server interface.
:returns: ``None``
"""
server_id = self._get_uri_attribute(
server_interface,
server,
"server_id",
)
server_interface = resource.Resource._get_id(server_interface)
self._delete(
_server_interface.ServerInterface,
server_interface,
server_id=server_id,
ignore_missing=ignore_missing,
)
def get_server_interface(self, server_interface, server=None):
"""Get a single server interface
:param server_interface:
The value can be the ID of a server interface or a
:class:`~openstack.compute.v2.server_interface.ServerInterface`
instance.
:param server: This parameter need to be specified when ServerInterface
ID is given as value. It can be either the ID of a
server or a :class:`~openstack.compute.v2.server.Server`
instance that the interface belongs to.
:returns: One
:class:`~openstack.compute.v2.server_interface.ServerInterface`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
server_id = self._get_uri_attribute(
server_interface,
server,
"server_id",
)
server_interface = resource.Resource._get_id(server_interface)
return self._get(
_server_interface.ServerInterface,
server_id=server_id,
port_id=server_interface,
)
def server_interfaces(self, server, **query):
"""Return a generator of server interfaces
:param server: The server can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server`.
:param query: Optional query parameters to be sent to limit the
resources being returned.
:returns: A generator of ServerInterface objects
:rtype: :class:`~openstack.compute.v2.server_interface.ServerInterface`
"""
server_id = resource.Resource._get_id(server)
return self._list(
_server_interface.ServerInterface,
server_id=server_id,
**query,
)
def server_ips(self, server, network_label=None):
"""Return a generator of server IPs
:param server: The server can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server`.
:param network_label: The name of a particular network to list
IP addresses from.
:returns: A generator of ServerIP objects
:rtype: :class:`~openstack.compute.v2.server_ip.ServerIP`
"""
server_id = resource.Resource._get_id(server)
return self._list(
server_ip.ServerIP,
server_id=server_id,
network_label=network_label,
)
def availability_zones(self, details=False):
"""Return a generator of availability zones
:param bool details: Return extra details about the availability
zones. This defaults to `False` as it generally
requires extra permission.
:returns: A generator of availability zone
:rtype:
:class:`~openstack.compute.v2.availability_zone.AvailabilityZone`
"""
base_path = '/os-availability-zone/detail' if details else None
return self._list(
availability_zone.AvailabilityZone,
base_path=base_path,
)
# ========== Server Metadata ==========
def get_server_metadata(self, server):
"""Return a dictionary of metadata for a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` or
:class:`~openstack.compute.v2.server.ServerDetail`
instance.
:returns: A :class:`~openstack.compute.v2.server.Server` with the
server's metadata. All keys and values are Unicode text.
:rtype: :class:`~openstack.compute.v2.server.Server`
"""
res = self._get_base_resource(server, _server.Server)
return res.fetch_metadata(self)
def set_server_metadata(self, server, **metadata):
"""Update metadata for a server
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param kwargs metadata: Key/value pairs to be updated in the server's
metadata. No other metadata is modified
by this call. All keys and values are stored
as Unicode.
:returns: A :class:`~openstack.compute.v2.server.Server` with only the
server's metadata. All keys and values are Unicode text.
:rtype: :class:`~openstack.compute.v2.server.Server`
"""
res = self._get_base_resource(server, _server.Server)
return res.set_metadata(self, metadata=metadata)
def delete_server_metadata(self, server, keys=None):
"""Delete metadata for a server
Note: This method will do a HTTP DELETE request for every key in keys.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param list keys: The keys to delete. If left empty complete
metadata will be removed.
:rtype: ``None``
"""
res = self._get_base_resource(server, _server.Server)
if keys is not None:
# Create a set as a snapshot of keys to avoid "changed during
# iteration"
for key in set(keys):
res.delete_metadata_item(self, key)
else:
res.delete_metadata(self)
# ========== Server Groups ==========
def create_server_group(self, **attrs):
"""Create a new server group from attributes
:param dict attrs: Keyword arguments which will be used to create
a :class:`~openstack.compute.v2.server_group.ServerGroup`,
comprised of the properties on the ServerGroup class.
:returns: The results of server group creation
:rtype: :class:`~openstack.compute.v2.server_group.ServerGroup`
"""
return self._create(_server_group.ServerGroup, **attrs)
def delete_server_group(self, server_group, ignore_missing=True):
"""Delete a server group
:param server_group: The value can be either the ID of a server group
or a :class:`~openstack.compute.v2.server_group.ServerGroup`
instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be
raised when the server group does not exist.
When set to ``True``, no exception will be set when
attempting to delete a nonexistent server group.
:returns: ``None``
"""
self._delete(
_server_group.ServerGroup,
server_group,
ignore_missing=ignore_missing,
)
def find_server_group(
self,
name_or_id,
ignore_missing=True,
*,
all_projects=False,
):
"""Find a single server group
:param name_or_id: The name or ID of a server group.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the resource does not exist. When set to ``True``, None will be
returned when attempting to find a nonexistent resource.
:param bool all_projects: When set to ``True``, search for server
groups by name across all projects. Note that this will likely
result in a higher chance of duplicates. Admin-only by default.
:returns: One :class:`~openstack.compute.v2.server_group.ServerGroup`
or None
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
:raises: :class:`~openstack.exceptions.DuplicateResource` when multiple
resources are found.
"""
query = {}
if all_projects:
query['all_projects'] = True
return self._find(
_server_group.ServerGroup,
name_or_id,
ignore_missing=ignore_missing,
**query,
)
def get_server_group(self, server_group):
"""Get a single server group
:param server_group: The value can be the ID of a server group or a
:class:`~openstack.compute.v2.server_group.ServerGroup`
instance.
:returns:
A :class:`~openstack.compute.v2.server_group.ServerGroup` object.
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
return self._get(_server_group.ServerGroup, server_group)
def server_groups(self, *, all_projects=False, **query):
"""Return a generator of server groups
:param bool all_projects: When set to ``True``, lists servers groups
from all projects. Admin-only by default.
:param kwargs query: Optional query parameters to be sent to limit
the resources being returned.
:returns: A generator of ServerGroup objects
:rtype: :class:`~openstack.compute.v2.server_group.ServerGroup`
"""
if all_projects:
query['all_projects'] = True
return self._list(_server_group.ServerGroup, **query)
# ========== Hypervisors ==========
def hypervisors(self, details=False, **query):
"""Return a generator of hypervisors
:param bool details: When set to the default, ``False``,
:class:`~openstack.compute.v2.hypervisor.Hypervisor`
instances will be returned with only basic information populated.
:param kwargs query: Optional query parameters to be sent to limit
the resources being returned.
:returns: A generator of hypervisor
:rtype: class: `~openstack.compute.v2.hypervisor.Hypervisor`
"""
base_path = '/os-hypervisors/detail' if details else None
if (
'hypervisor_hostname_pattern' in query
and not utils.supports_microversion(self, '2.53')
):
# Until 2.53 we need to use other API
base_path = '/os-hypervisors/{pattern}/search'.format(
pattern=query.pop('hypervisor_hostname_pattern')
)
return self._list(_hypervisor.Hypervisor, base_path=base_path, **query)
def find_hypervisor(
self,
name_or_id,
ignore_missing=True,
*,
details=True,
):
"""Find a single hypervisor
:param name_or_id: The name or ID of a hypervisor
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the resource does not exist. When set to ``True``, None will be
returned when attempting to find a nonexistent resource.
:param bool details: When set to ``False``
instances with only basic data will be returned. The default,
``True``, will cause instances with full data to be returned.
:returns: One: class:`~openstack.compute.v2.hypervisor.Hypervisor`
or None
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
:raises: :class:`~openstack.exceptions.DuplicateResource` when multiple
resources are found.
"""
list_base_path = '/os-hypervisors/detail' if details else None
return self._find(
_hypervisor.Hypervisor,
name_or_id,
list_base_path=list_base_path,
ignore_missing=ignore_missing,
)
def get_hypervisor(self, hypervisor):
"""Get a single hypervisor
:param hypervisor: The value can be the ID of a hypervisor or a
:class:`~openstack.compute.v2.hypervisor.Hypervisor`
instance.
:returns:
A :class:`~openstack.compute.v2.hypervisor.Hypervisor` object.
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
return self._get(_hypervisor.Hypervisor, hypervisor)
def get_hypervisor_uptime(self, hypervisor):
"""Get uptime information for hypervisor
:param hypervisor: The value can be the ID of a hypervisor or a
:class:`~openstack.compute.v2.hypervisor.Hypervisor`
instance.
:returns:
A :class:`~openstack.compute.v2.hypervisor.Hypervisor` object.
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
hypervisor = self._get_resource(_hypervisor.Hypervisor, hypervisor)
return hypervisor.get_uptime(self)
# ========== Services ==========
def update_service_forced_down(
self,
service,
host=None,
binary=None,
forced=True,
):
"""Update service forced_down information
:param service: Either the ID of a service or a
:class:`~openstack.compute.v2.service.Service` instance.
:param str host: The host where service runs.
:param str binary: The name of service.
:param bool forced: Whether or not this service was forced down
manually by an administrator after the service was fenced.
:returns: Updated service instance
:rtype: class: `~openstack.compute.v2.service.Service`
"""
if utils.supports_microversion(self, '2.53'):
return self.update_service(service, forced_down=forced)
service = self._get_resource(_service.Service, service)
if (not host or not binary) and (
not service.host or not service.binary
):
raise ValueError(
'Either service instance should have host and binary '
'or they should be passed'
)
service.set_forced_down(self, host, binary, forced)
force_service_down = update_service_forced_down
def disable_service(
self,
service,
host=None,
binary=None,
disabled_reason=None,
):
"""Disable a service
:param service: Either the ID of a service or a
:class:`~openstack.compute.v2.service.Service` instance.
:param str host: The host where service runs.
:param str binary: The name of service.
:param str disabled_reason: The reason of force down a service.
:returns: Updated service instance
:rtype: class: `~openstack.compute.v2.service.Service`
"""
if utils.supports_microversion(self, '2.53'):
attrs = {'status': 'disabled'}
if disabled_reason:
attrs['disabled_reason'] = disabled_reason
return self.update_service(service, **attrs)
service = self._get_resource(_service.Service, service)
return service.disable(self, host, binary, disabled_reason)
def enable_service(self, service, host=None, binary=None):
"""Enable a service
:param service: Either the ID of a service or a
:class:`~openstack.compute.v2.service.Service` instance.
:param str host: The host where service runs.
:param str binary: The name of service.
:returns: Updated service instance
:rtype: class: `~openstack.compute.v2.service.Service`
"""
if utils.supports_microversion(self, '2.53'):
return self.update_service(service, status='enabled')
service = self._get_resource(_service.Service, service)
return service.enable(self, host, binary)
def services(self, **query):
"""Return a generator of service
:params dict query: Query parameters
:returns: A generator of service
:rtype: class: `~openstack.compute.v2.service.Service`
"""
return self._list(_service.Service, **query)
def find_service(self, name_or_id, ignore_missing=True, **query):
"""Find a service from name or id to get the corresponding info
:param name_or_id: The name or id of a service
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be
raised when the resource does not exist.
When set to ``True``, None will be returned when
attempting to find a nonexistent resource.
:param dict query: Additional attributes like 'host'
:returns: One: class:`~openstack.compute.v2.service.Service` or None
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
:raises: :class:`~openstack.exceptions.DuplicateResource` when multiple
resources are found.
"""
return self._find(
_service.Service,
name_or_id,
ignore_missing=ignore_missing,
**query,
)
def delete_service(self, service, ignore_missing=True):
"""Delete a service
:param service:
The value can be either the ID of a service or a
:class:`~openstack.compute.v2.service.Service` instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the service does not exist. When set to ``True``, no exception
will be set when attempting to delete a nonexistent service.
:returns: ``None``
"""
self._delete(_service.Service, service, ignore_missing=ignore_missing)
def update_service(self, service, **attrs):
"""Update a service
:param service: Either the ID of a service or a
:class:`~openstack.compute.v2.service.Service` instance.
:param attrs: The attributes to update on the service represented
by ``service``.
:returns: The updated service
:rtype: :class:`~openstack.compute.v2.service.Service`
"""
if utils.supports_microversion(self, '2.53'):
return self._update(_service.Service, service, **attrs)
raise exceptions.SDKException(
'Method require at least microversion 2.53'
)
# ========== Volume Attachments ==========
# TODO(stephenfin): Make the volume argument required in 2.0
def create_volume_attachment(self, server, volume=None, **attrs):
"""Create a new volume attachment from attributes
:param server: The value can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance that the
volume is attached to.
:param volume: The value can be either the ID of a volume or a
:class:`~openstack.block_storage.v3.volume.Volume` instance.
:param dict attrs: Keyword arguments which will be used to create a
:class:`~openstack.compute.v2.volume_attachment.VolumeAttachment`,
comprised of the properties on the VolumeAttachment class.
:returns: The results of volume attachment creation
:rtype:
:class:`~openstack.compute.v2.volume_attachment.VolumeAttachment`
"""
# if the user didn't pass the new 'volume' argument, they're probably
# calling things using a legacy parameter
if volume is None:
# there are two ways to pass this legacy parameter: either using
# the openstacksdk alias, 'volume_id', or using the real nova field
# name, 'volumeId'
if 'volume_id' in attrs:
volume_id = attrs.pop('volume_id')
elif 'volumeId' in attrs:
volume_id = attrs.pop('volumeId')
else:
# the user has used neither the new way nor the old way so they
# should start using the new way
# NOTE(stephenfin): we intentionally mimic the behavior of a
# missing positional parameter in stdlib
# https://github.com/python/cpython/blob/v3.10.0/Lib/inspect.py#L1464-L1467
raise TypeError(
'create_volume_attachment() missing 1 required positional '
'argument: volume'
)
# encourage users to the new way so we can eventually remove this
# mess of logic
deprecation_msg = (
'This method was called with a volume_id or volumeId '
'argument. This is legacy behavior that will be removed in '
'a future version. Update callers to use a volume argument.'
)
warnings.warn(
deprecation_msg,
os_warnings.RemovedInSDK50Warning,
)
else:
volume_id = resource.Resource._get_id(volume)
server_id = resource.Resource._get_id(server)
return self._create(
_volume_attachment.VolumeAttachment,
server_id=server_id,
volume_id=volume_id,
**attrs,
)
def update_volume_attachment(
self,
server,
volume,
volume_id=None,
**attrs,
):
"""Update a volume attachment
Note that the underlying API expects a volume ID, not a volume
attachment ID. There is currently no way to update volume attachments
by their own ID.
:param server: The value can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance that the
volume is attached to.
:param volume: The value can be either the ID of a volume or a
:class:`~openstack.block_storage.v3.volume.Volume` instance.
:param volume_id: The ID of a volume to swap to. If this is not
specified, we will default to not swapping the volume.
:param attrs: The attributes to update on the volume attachment
represented by ``volume_attachment``.
:returns: ``None``
"""
new_volume_id = volume_id
server_id = resource.Resource._get_id(server)
volume_id = resource.Resource._get_id(volume)
if new_volume_id is None:
new_volume_id = volume_id
return self._update(
_volume_attachment.VolumeAttachment,
id=volume_id,
server_id=server_id,
volume_id=new_volume_id,
**attrs,
)
# TODO(stephenfin): Remove this hack in openstacksdk 2.0
def _verify_server_volume_args(self, server, volume):
deprecation_msg = (
'The server and volume arguments to this function appear to '
'be backwards and have been reversed. This is a breaking '
'change introduced in openstacksdk 1.0. This shim will be '
'removed in a future version'
)
# if we have even partial type information and things look as they
# should, we can assume the user did the right thing
if isinstance(server, _server.Server) or isinstance(
volume, _volume.Volume
):
return server, volume
# conversely, if there's type info and things appear off, tell the user
if isinstance(server, _volume.Volume) or isinstance(
volume, _server.Server
):
warnings.warn(
deprecation_msg,
os_warnings.RemovedInSDK50Warning,
)
return volume, server
# without type info we have to try a find the server corresponding to
# the provided ID and validate it
if self.find_server(server, ignore_missing=True) is not None:
return server, volume
else:
warnings.warn(
deprecation_msg,
os_warnings.RemovedInSDK50Warning,
)
return volume, server
def delete_volume_attachment(self, server, volume, ignore_missing=True):
"""Delete a volume attachment
Note that the underlying API expects a volume ID, not a volume
attachment ID. There is currently no way to delete volume attachments
by their own ID.
:param server: The value can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance that the
volume is attached to.
:param volume: The value can be the ID of a volume or a
:class:`~openstack.block_storage.v3.volume.Volume` instance.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the volume attachment does not exist. When set to ``True``, no
exception will be set when attempting to delete a nonexistent
volume attachment.
:returns: ``None``
"""
server, volume = self._verify_server_volume_args(server, volume)
server_id = resource.Resource._get_id(server)
volume_id = resource.Resource._get_id(volume)
self._delete(
_volume_attachment.VolumeAttachment,
None,
id=volume_id,
server_id=server_id,
ignore_missing=ignore_missing,
)
def get_volume_attachment(self, server, volume):
"""Get a single volume attachment
Note that the underlying API expects a volume ID, not a volume
attachment ID. There is currently no way to retrieve volume attachments
by their own ID.
:param server: The value can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance that the
volume is attached to.
:param volume: The value can be the ID of a volume or a
:class:`~openstack.block_storage.v3.volume.Volume` instance.
:returns: One
:class:`~openstack.compute.v2.volume_attachment.VolumeAttachment`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
server_id = resource.Resource._get_id(server)
volume_id = resource.Resource._get_id(volume)
return self._get(
_volume_attachment.VolumeAttachment,
id=volume_id,
server_id=server_id,
)
def volume_attachments(self, server, **query):
"""Return a generator of volume attachments
:param server: The server can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server`.
:params dict query: Query parameters
:returns: A generator of VolumeAttachment objects
:rtype:
:class:`~openstack.compute.v2.volume_attachment.VolumeAttachment`
"""
server_id = resource.Resource._get_id(server)
return self._list(
_volume_attachment.VolumeAttachment,
server_id=server_id,
**query,
)
# ========== Server Migrations ==========
def migrate_server(self, server, *, host=None):
"""Migrate a server from one host to another
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param str host: The host to which to migrate the server.
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.migrate(self, host=host)
def live_migrate_server(
self,
server,
host=None,
force=False,
block_migration=None,
disk_over_commit=None,
):
"""Live migrate a server from one host to target host
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param str host: The host to which to migrate the server. If the Nova
service is too old, the host parameter implies force=True which
causes the Nova scheduler to be bypassed. On such clouds, a
``ValueError`` will be thrown if ``host`` is given without
``force``.
:param bool force: Force a live-migration by not verifying the provided
destination host by the scheduler. This is unsafe and not
recommended.
:param block_migration: Perform a block live migration to the
destination host by the scheduler. Can be 'auto', True or False.
Some clouds are too old to support 'auto', in which case a
ValueError will be thrown. If omitted, the value will be 'auto' on
clouds that support it, and False on clouds that do not.
:param disk_over_commit: Whether to allow disk over-commit on the
destination host. (Optional)
:returns: None
"""
server = self._get_resource(_server.Server, server)
server.live_migrate(
self,
host=host,
force=force,
block_migration=block_migration,
disk_over_commit=disk_over_commit,
)
def abort_server_migration(
self,
server_migration,
server,
ignore_missing=True,
):
"""Abort an in-progress server migration
:param server_migration: The value can be either the ID of a server
migration or a
:class:`~openstack.compute.v2.server_migration.ServerMigration`
instance.
:param server: This parameter needs to be specified when
ServerMigration ID is given as value. It can be either the ID of a
server or a :class:`~openstack.compute.v2.server.Server` instance
that the migration belongs to.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the server migration does not exist. When set to ``True``, no
exception will be set when attempting to delete a nonexistent
server migration.
:returns: ``None``
"""
server_id = self._get_uri_attribute(
server_migration,
server,
'server_id',
)
server_migration = resource.Resource._get_id(server_migration)
self._delete(
_server_migration.ServerMigration,
server_migration,
server_id=server_id,
ignore_missing=ignore_missing,
)
def force_complete_server_migration(self, server_migration, server=None):
"""Force complete an in-progress server migration
:param server_migration: The value can be either the ID of a server
migration or a
:class:`~openstack.compute.v2.server_migration.ServerMigration`
instance.
:param server: This parameter needs to be specified when
ServerMigration ID is given as value. It can be either the ID of a
server or a :class:`~openstack.compute.v2.server.Server` instance
that the migration belongs to.
:returns: ``None``
"""
server_id = self._get_uri_attribute(
server_migration,
server,
'server_id',
)
server_migration = self._get_resource(
_server_migration.ServerMigration,
server_migration,
server_id=server_id,
)
server_migration.force_complete(self)
def get_server_migration(
self,
server_migration,
server,
ignore_missing=True,
):
"""Get a single server migration
:param server_migration: The value can be the ID of a server migration
or a
:class:`~openstack.compute.v2.server_migration.ServerMigration`
instance.
:param server: This parameter need to be specified when ServerMigration
ID is given as value. It can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance that the
migration belongs to.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the server migration does not exist. When set to ``True``, no
exception will be set when attempting to delete a nonexistent
server migration.
:returns: One
:class:`~openstack.compute.v2.server_migration.ServerMigration`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
server_id = self._get_uri_attribute(
server_migration,
server,
'server_id',
)
server_migration = resource.Resource._get_id(server_migration)
return self._get(
_server_migration.ServerMigration,
server_migration,
server_id=server_id,
ignore_missing=ignore_missing,
)
def server_migrations(self, server):
"""Return a generator of migrations for a server.
:param server: The server can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server`.
:returns: A generator of ServerMigration objects
:rtype:
:class:`~openstack.compute.v2.server_migration.ServerMigration`
"""
server_id = resource.Resource._get_id(server)
return self._list(
_server_migration.ServerMigration,
server_id=server_id,
)
# ========== Migrations ==========
def migrations(self, **query):
"""Return a generator of migrations for all servers.
:param kwargs query: Optional query parameters to be sent to limit
the migrations being returned.
:returns: A generator of Migration objects
:rtype: :class:`~openstack.compute.v2.migration.Migration`
"""
return self._list(_migration.Migration, **query)
# ========== Server diagnostics ==========
def get_server_diagnostics(self, server):
"""Get a single server diagnostics
:param server: This parameter need to be specified when ServerInterface
ID is given as value. It can be either the ID of a
server or a :class:`~openstack.compute.v2.server.Server`
instance that the interface belongs to.
:returns: One
:class:`~openstack.compute.v2.server_diagnostics.ServerDiagnostics`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
server_id = self._get_resource(_server.Server, server).id
return self._get(
_server_diagnostics.ServerDiagnostics,
server_id=server_id,
requires_id=False,
)
# ========== Project usage ============
def usages(self, start=None, end=None, **query):
"""Get project usages.
:param datetime.datetime start: Usage range start date.
:param datetime.datetime end: Usage range end date.
:param dict query: Additional query parameters to use.
:returns: A list of compute ``Usage`` objects.
"""
if start is not None:
query['start'] = start.isoformat()
if end is not None:
query['end'] = end.isoformat()
return self._list(_usage.Usage, **query)
def get_usage(self, project, start=None, end=None, **query):
"""Get usage for a single project.
:param project: ID or instance of
:class:`~openstack.identity.project.Project` of the project for
which the usage should be retrieved.
:param datetime.datetime start: Usage range start date.
:param datetime.datetime end: Usage range end date.
:param dict query: Additional query parameters to use.
:returns: A compute ``Usage`` object.
"""
project = self._get_resource(_project.Project, project)
if start is not None:
query['start'] = start.isoformat()
if end is not None:
query['end'] = end.isoformat()
res = self._get_resource(_usage.Usage, project.id)
return res.fetch(self, **query)
# ========== Server consoles ==========
def create_server_remote_console(self, server, **attrs):
"""Create a remote console on the server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:returns: One
:class:`~openstack.compute.v2.server_remote_console.ServerRemoteConsole`
"""
server_id = resource.Resource._get_id(server)
return self._create(
_src.ServerRemoteConsole,
server_id=server_id,
**attrs,
)
def get_server_console_url(self, server, console_type):
"""Create a remote console on the server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param console_type: Type of the console connection.
:returns: Dictionary with console type and url
"""
server = self._get_resource(_server.Server, server)
return server.get_console_url(self, console_type)
def get_server_console_output(self, server, length=None):
"""Return the console output for a server.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param length: Optional number of line to fetch from the end of console
log. All lines will be returned if this is not specified.
:returns: The console output as a dict. Control characters will be
escaped to create a valid JSON string.
"""
server = self._get_resource(_server.Server, server)
return server.get_console_output(self, length=length)
def create_console(self, server, console_type, console_protocol=None):
"""Create a remote console on the server.
When microversion supported is higher then 2.6 remote console is
created, otherwise deprecated call to get server console is issued.
:param server: Either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance.
:param console_type: Type of the remote console. Supported values as:
* novnc
* spice-html5
* rdp-html5
* serial
* webmks (supported after 2.8)
:param console_protocol: Optional console protocol (is respected only
after microversion 2.6).
:returns: Dictionary with console type, url and optionally protocol.
"""
server = self._get_resource(_server.Server, server)
# NOTE: novaclient supports undocumented type xcpvnc also supported
# historically by OSC. We support it, but do not document either.
if utils.supports_microversion(self, '2.6'):
console = self._create(
_src.ServerRemoteConsole,
server_id=server.id,
type=console_type,
protocol=console_protocol,
)
return console.to_dict()
else:
return server.get_console_url(self, console_type)
# ========== Quota class sets ==========
def get_quota_class_set(self, quota_class_set='default'):
"""Get a single quota class set
Only one quota class is permitted, ``default``.
:param quota_class_set: The value can be the ID of a quota class set
(only ``default`` is supported) or a
:class:`~openstack.compute.v2.quota_class_set.QuotaClassSet`
instance.
:returns: One
:class:`~openstack.compute.v2.quota_class_set.QuotaClassSet`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
return self._get(_quota_class_set.QuotaClassSet, quota_class_set)
def update_quota_class_set(self, quota_class_set, **attrs):
"""Update a QuotaClassSet.
Only one quota class is permitted, ``default``.
:param quota_class_set: Either the ID of a quota class set (only
``default`` is supported) or a
:class:`~openstack.compute.v2.quota_class_set.QuotaClassSet`
instance.
:param attrs: The attributes to update on the QuotaClassSet represented
by ``quota_class_set``.
:returns: The updated QuotaSet
:rtype: :class:`~openstack.compute.v2.quota_set.QuotaSet`
"""
return self._update(
_quota_class_set.QuotaClassSet, quota_class_set, **attrs
)
# ========== Quota sets ==========
def get_quota_set(self, project, usage=False, **query):
"""Show QuotaSet information for the project.
:param project: ID or instance of
:class:`~openstack.identity.project.Project` of the project for
which the quota should be retrieved
:param bool usage: When set to ``True`` quota usage and reservations
would be filled.
:param dict query: Additional query parameters to use.
:returns: One :class:`~openstack.compute.v2.quota_set.QuotaSet`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
project = self._get_resource(_project.Project, project)
res = self._get_resource(
_quota_set.QuotaSet,
None,
project_id=project.id,
)
base_path = '/os-quota-sets/%(project_id)s/detail' if usage else None
return res.fetch(self, base_path=base_path, **query)
def get_quota_set_defaults(self, project):
"""Show QuotaSet defaults for the project.
:param project: ID or instance of
:class:`~openstack.identity.project.Project` of the project for
which the quota should be retrieved
:returns: One :class:`~openstack.compute.v2.quota_set.QuotaSet`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no resource can be found.
"""
project = self._get_resource(_project.Project, project)
res = self._get_resource(
_quota_set.QuotaSet,
None,
project_id=project.id,
)
return res.fetch(
self, base_path='/os-quota-sets/%(project_id)s/defaults'
)
def revert_quota_set(self, project, **query):
"""Reset Quota for the project/user.
:param project: ID or instance of
:class:`~openstack.identity.project.Project` of the project for
which the quota should be reset.
:param dict query: Additional parameters to be used.
:returns: ``None``
"""
project = self._get_resource(_project.Project, project)
res = self._get_resource(
_quota_set.QuotaSet, None, project_id=project.id
)
if not query:
query = {}
return res.delete(self, **query)
def update_quota_set(self, project, *, user=None, **attrs):
"""Update a QuotaSet.
:param project: ID or instance of
:class:`~openstack.identity.project.Project` of the project for
which the quota should be reset.
:param user_id: Optional ID of the user to set quotas as.
:param attrs: The attributes to update on the QuotaSet represented
by ``quota_set``.
:returns: The updated QuotaSet
:rtype: :class:`~openstack.compute.v2.quota_set.QuotaSet`
"""
if 'project_id' in attrs or isinstance(project, _quota_set.QuotaSet):
warnings.warn(
"The signature of 'update_quota_set' has changed and it "
"now expects a Project as the first argument, in line "
"with the other quota set methods.",
os_warnings.RemovedInSDK50Warning,
)
if user is not None:
raise exceptions.SDKException(
'The user argument can only be provided once the entire '
'call has been updated.'
)
if 'query' in attrs:
warnings.warn(
"The query argument is no longer supported and should "
"be removed.",
os_warnings.RemovedInSDK50Warning,
)
query = attrs.pop('query') or {}
else:
query = {}
res = self._get_resource(_quota_set.QuotaSet, project, **attrs)
return res.commit(self, **query)
else:
project = self._get_resource(_project.Project, project)
attrs['project_id'] = project.id
if user:
user = self._get_resource(_user.User, user)
query = {'user_id': user.id}
else:
query = {}
# we don't use Proxy._update since that doesn't allow passing
# arbitrary query string parameters
quota_set = self._get_resource(_quota_set.QuotaSet, None, **attrs)
return quota_set.commit(self, **query)
# ========== Server actions ==========
def get_server_action(self, server_action, server, ignore_missing=True):
"""Get a single server action
:param server_action: The value can be the ID of a server action or a
:class:`~openstack.compute.v2.server_action.ServerAction` instance.
:param server: This parameter need to be specified when ServerAction ID
is given as value. It can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server` instance that the
action is associated with.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be raised when
the server action does not exist. When set to ``True``, no
exception will be set when attempting to retrieve a non-existent
server action.
:returns: One :class:`~openstack.compute.v2.server_action.ServerAction`
:raises: :class:`~openstack.exceptions.NotFoundException` when no
resource can be found.
"""
server_id = self._get_uri_attribute(server_action, server, 'server_id')
server_action = resource.Resource._get_id(server_action)
return self._get(
_server_action.ServerAction,
server_id=server_id,
request_id=server_action,
ignore_missing=ignore_missing,
)
def server_actions(self, server):
"""Return a generator of server actions
:param server: The server can be either the ID of a server or a
:class:`~openstack.compute.v2.server.Server`.
:returns: A generator of ServerAction objects
:rtype: :class:`~openstack.compute.v2.server_action.ServerAction`
"""
server_id = resource.Resource._get_id(server)
return self._list(_server_action.ServerAction, server_id=server_id)
# ========== Utilities ==========
def wait_for_server(
self,
server: _server.Server,
status: str = 'ACTIVE',
failures: ty.Optional[list[str]] = None,
interval: ty.Union[int, float, None] = 2,
wait: ty.Optional[int] = 120,
callback: ty.Optional[ty.Callable[[int], None]] = None,
) -> _server.Server:
"""Wait for a server to be in a particular status.
:param server: The :class:`~openstack.compute.v2.server.Server` to wait
on to reach the specified status.
:type server: :class:`~openstack.compute.v2.server.Server`:
:param status: Desired status.
:type status: str
:param failures: Statuses that would be interpreted as failures.
:type failures: :py:class:`list`
:param interval: Number of seconds to wait before to consecutive
checks. Default to 2.
:type interval: int
:param wait: Maximum number of seconds to wait before the change.
Default to 120.
:param callback: A callback function. This will be called with a single
value, progress, which is a percentage value from 0-100.
:type callback: callable
:returns: The resource is returned on success.
:raises: :class:`~openstack.exceptions.ResourceTimeout` if transition
to the desired status failed to occur in specified seconds.
:raises: :class:`~openstack.exceptions.ResourceFailure` if the resource
has transited to one of the failure statuses.
:raises: :class:`~AttributeError` if the resource does not have a
``status`` attribute.
"""
failures = ['ERROR'] if failures is None else failures
return resource.wait_for_status(
self,
server,
status,
failures,
interval,
wait,
callback=callback,
)
def wait_for_status(
self,
res: resource.ResourceT,
status: str,
failures: ty.Optional[list[str]] = None,
interval: ty.Union[int, float, None] = 2,
wait: ty.Optional[int] = None,
attribute: str = 'status',
callback: ty.Optional[ty.Callable[[int], None]] = None,
) -> resource.ResourceT:
"""Wait for the resource to be in a particular status.
:param session: The session to use for making this request.
:param resource: The resource to wait on to reach the status. The
resource must have a status attribute specified via ``attribute``.
:param status: Desired status of the resource.
:param failures: Statuses that would indicate the transition
failed such as 'ERROR'. Defaults to ['ERROR'].
:param interval: Number of seconds to wait between checks.
:param wait: Maximum number of seconds to wait for transition.
Set to ``None`` to wait forever.
:param attribute: Name of the resource attribute that contains the
status.
:param callback: A callback function. This will be called with a single
value, progress. This is API specific but is generally a percentage
value from 0-100.
:return: The updated resource.
:raises: :class:`~openstack.exceptions.ResourceTimeout` if the
transition to status failed to occur in ``wait`` seconds.
:raises: :class:`~openstack.exceptions.ResourceFailure` if the resource
transitioned to one of the states in ``failures``.
:raises: :class:`~AttributeError` if the resource does not have a
``status`` attribute
"""
return resource.wait_for_status(
self, res, status, failures, interval, wait, attribute, callback
)
def wait_for_delete(
self,
res: resource.ResourceT,
interval: int = 2,
wait: int = 120,
callback: ty.Optional[ty.Callable[[int], None]] = None,
) -> resource.ResourceT:
"""Wait for a resource to be deleted.
:param res: The resource to wait on to be deleted.
:param interval: Number of seconds to wait before to consecutive
checks.
:param wait: Maximum number of seconds to wait before the change.
:param callback: A callback function. This will be called with a single
value, progress, which is a percentage value from 0-100.
:returns: The resource is returned on success.
:raises: :class:`~openstack.exceptions.ResourceTimeout` if transition
to delete failed to occur in the specified seconds.
"""
return resource.wait_for_delete(self, res, interval, wait, callback)
def _get_cleanup_dependencies(self):
return {
'compute': {
'before': ['block_storage', 'network', 'identity', 'image']
}
}
def _service_cleanup(
self,
dry_run=True,
client_status_queue=None,
identified_resources=None,
filters=None,
resource_evaluation_fn=None,
skip_resources=None,
):
if self.should_skip_resource_cleanup("server", skip_resources):
return
servers = []
for obj in self.servers():
need_delete = self._service_cleanup_del_res(
self.delete_server,
obj,
dry_run=dry_run,
client_status_queue=client_status_queue,
identified_resources=identified_resources,
filters=filters,
resource_evaluation_fn=resource_evaluation_fn,
)
if not dry_run and need_delete:
# In the dry run we identified, that server will go. To propely
# identify consequences we need to tell others, that the port
# will disappear as well
for port in self._connection.network.ports(device_id=obj.id):
identified_resources[port.id] = port
servers.append(obj)
# We actually need to wait for servers to really disappear, since they
# might be still holding ports on the subnet
for server in servers:
self.wait_for_delete(server)
for obj in self.server_groups():
# Do not delete server groups that still have members
if obj.member_ids:
continue
self._service_cleanup_del_res(
self.delete_server_group,
obj,
dry_run=dry_run,
client_status_queue=client_status_queue,
identified_resources=identified_resources,
filters=filters,
resource_evaluation_fn=resource_evaluation_fn,
)
|