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
|
# 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 requests
from openstack.baremetal.v1 import _common
from openstack.baremetal.v1 import allocation as _allocation
from openstack.baremetal.v1 import chassis as _chassis
from openstack.baremetal.v1 import conductor as _conductor
from openstack.baremetal.v1 import deploy_templates as _deploytemplates
from openstack.baremetal.v1 import driver as _driver
from openstack.baremetal.v1 import node as _node
from openstack.baremetal.v1 import port as _port
from openstack.baremetal.v1 import port_group as _portgroup
from openstack.baremetal.v1 import volume_connector as _volumeconnector
from openstack.baremetal.v1 import volume_target as _volumetarget
from openstack import exceptions
from openstack import proxy
from openstack import resource
from openstack import utils
class Proxy(proxy.Proxy):
retriable_status_codes = _common.RETRIABLE_STATUS_CODES
_resource_registry = {
"allocation": _allocation.Allocation,
"chassis": _chassis.Chassis,
"conductor": _conductor.Conductor,
"deploy_template": _deploytemplates.DeployTemplate,
"driver": _driver.Driver,
"node": _node.Node,
"port": _port.Port,
"port_group": _portgroup.PortGroup,
"volume_connector": _volumeconnector.VolumeConnector,
"volume_target": _volumetarget.VolumeTarget,
}
def _get_with_fields(self, resource_type, value, fields=None):
"""Fetch a bare metal resource.
:param resource_type: The type of resource to get.
:type resource_type: :class:`~openstack.resource.Resource`
:param value: The value to get. Can be either the ID of a
resource or a :class:`~openstack.resource.Resource`
subclass.
:param fields: Limit the resource fields to fetch.
:returns: The result of the ``fetch``
:rtype: :class:`~openstack.resource.Resource`
"""
res = self._get_resource(resource_type, value)
kwargs = {}
if fields:
kwargs['fields'] = _common.fields_type(fields, resource_type)
return res.fetch(
self,
error_message=f"No {resource_type.__name__} found for {value}",
**kwargs,
)
# ========== Chassis ==========
def chassis(self, details=False, **query):
"""Retrieve a generator of chassis.
:param details: A boolean indicating whether the detailed information
for every chassis should be returned.
:param dict query: Optional query parameters to be sent to
restrict the chassis to be returned. Available parameters include:
* ``fields``: A list containing one or more fields to be returned
in the response. This may lead to some performance gain
because other fields of the resource are not refreshed.
* ``limit``: Requests at most the specified number of items be
returned from the query.
* ``marker``: Specifies the ID of the last-seen chassis. Use the
``limit`` parameter to make an initial limited request and
use the ID of the last-seen chassis from the response as
the ``marker`` value in a subsequent limited request.
* ``sort_dir``: Sorts the response by the requested sort direction.
A valid value is ``asc`` (ascending) or ``desc``
(descending). Default is ``asc``. You can specify multiple
pairs of sort key and sort direction query parameters. If
you omit the sort direction in a pair, the API uses the
natural sorting direction of the server attribute that is
provided as the ``sort_key``.
* ``sort_key``: Sorts the response by the this attribute value.
Default is ``id``. You can specify multiple pairs of sort
key and sort direction query parameters. If you omit the
sort direction in a pair, the API uses the natural sorting
direction of the server attribute that is provided as the
``sort_key``.
:returns: A generator of chassis instances.
"""
return _chassis.Chassis.list(self, details=details, **query)
def create_chassis(self, **attrs):
"""Create a new chassis from attributes.
:param dict attrs: Keyword arguments that will be used to create a
:class:`~openstack.baremetal.v1.chassis.Chassis`.
:returns: The results of chassis creation.
:rtype: :class:`~openstack.baremetal.v1.chassis.Chassis`.
"""
return self._create(_chassis.Chassis, **attrs)
def find_chassis(self, name_or_id, ignore_missing=True):
"""Find a single chassis.
:param str name_or_id: The ID of a chassis.
:param bool ignore_missing: When set to ``False``, an exception of
:class:`~openstack.exceptions.NotFoundException` will be raised
when the chassis does not exist. When set to `True``, None will
be returned when attempting to find a nonexistent chassis.
:returns: One :class:`~openstack.baremetal.v1.chassis.Chassis` object
or None.
"""
return self._find(
_chassis.Chassis, name_or_id, ignore_missing=ignore_missing
)
def get_chassis(self, chassis, fields=None):
"""Get a specific chassis.
:param chassis: The value can be the ID of a chassis or a
:class:`~openstack.baremetal.v1.chassis.Chassis` instance.
:param fields: Limit the resource fields to fetch.
:returns: One :class:`~openstack.baremetal.v1.chassis.Chassis`
:raises: :class:`~openstack.exceptions.NotFoundException` when no
chassis matching the name or ID could be found.
"""
return self._get_with_fields(_chassis.Chassis, chassis, fields=fields)
def update_chassis(self, chassis, **attrs):
"""Update a chassis.
:param chassis: Either the ID of a chassis, or an instance
of :class:`~openstack.baremetal.v1.chassis.Chassis`.
:param dict attrs: The attributes to update on the chassis represented
by the ``chassis`` parameter.
:returns: The updated chassis.
:rtype: :class:`~openstack.baremetal.v1.chassis.Chassis`
"""
return self._update(_chassis.Chassis, chassis, **attrs)
def patch_chassis(self, chassis, patch):
"""Apply a JSON patch to the chassis.
:param chassis: The value can be the ID of a chassis or a
:class:`~openstack.baremetal.v1.chassis.Chassis` instance.
:param patch: JSON patch to apply.
:returns: The updated chassis.
:rtype: :class:`~openstack.baremetal.v1.chassis.Chassis`
"""
return self._get_resource(_chassis.Chassis, chassis).patch(self, patch)
def delete_chassis(self, chassis, ignore_missing=True):
"""Delete a chassis.
:param chassis: The value can be either the ID of a chassis or
a :class:`~openstack.baremetal.v1.chassis.Chassis` instance.
:param bool ignore_missing: When set to ``False``, an exception
:class:`~openstack.exceptions.NotFoundException` will be raised
when the chassis could not be found. When set to ``True``, no
exception will be raised when attempting to delete a non-existent
chassis.
:returns: The instance of the chassis which was deleted.
:rtype: :class:`~openstack.baremetal.v1.chassis.Chassis`.
"""
return self._delete(
_chassis.Chassis, chassis, ignore_missing=ignore_missing
)
# ========== Drivers ==========
def drivers(self, details=False, **query):
"""Retrieve a generator of drivers.
:param bool details: A boolean indicating whether the detailed
information for every driver should be returned.
:param kwargs query: Optional query parameters to be sent to limit
the resources being returned.
:returns: A generator of driver instances.
"""
# NOTE(dtantsur): details are available starting with API microversion
# 1.30. Thus we do not send any value if not needed.
if details:
query['details'] = True
return self._list(_driver.Driver, **query)
def get_driver(self, driver):
"""Get a specific driver.
:param driver: The value can be the name of a driver or a
:class:`~openstack.baremetal.v1.driver.Driver` instance.
:returns: One :class:`~openstack.baremetal.v1.driver.Driver`
:raises: :class:`~openstack.exceptions.NotFoundException` when no
driver matching the name could be found.
"""
return self._get(_driver.Driver, driver)
def list_driver_vendor_passthru(self, driver):
"""Get driver's vendor_passthru methods.
:param driver: The value can be the name of a driver or a
:class:`~openstack.baremetal.v1.driver.Driver` instance.
:returns: One :dict: of vendor methods with corresponding usages
:raises: :class:`~openstack.exceptions.NotFoundException` when no
driver matching the name could be found.
"""
driver = self.get_driver(driver)
return driver.list_vendor_passthru(self)
def call_driver_vendor_passthru(
self,
driver: ty.Union[str, _driver.Driver],
verb: str,
method: str,
body: object = None,
) -> requests.Response:
"""Call driver's vendor_passthru method.
:param driver: The value can be the name of a driver or a
:class:`~openstack.baremetal.v1.driver.Driver` instance.
:param verb: One of GET, POST, PUT, DELETE,
depending on the driver and method.
:param method: Name of vendor method.
:param body: passed to the vendor function as json body.
:returns: Server response
"""
return self.get_driver(driver).call_vendor_passthru(
self, verb, method, body
)
# ========== Nodes ==========
def nodes(self, details=False, **query):
"""Retrieve a generator of nodes.
:param details: A boolean indicating whether the detailed information
for every node should be returned.
:param dict query: Optional query parameters to be sent to restrict
the nodes returned. Available parameters include:
* ``associated``: Only return those which are, or are not,
associated with an ``instance_id``.
* ``conductor_group``: Only return those in the specified
``conductor_group``.
* ``driver``: Only return those with the specified ``driver``.
* ``fault``: Only return those with the specified fault type.
* ``fields``: A list containing one or more fields to be returned
in the response. This may lead to some performance gain
because other fields of the resource are not refreshed.
* ``instance_id``: Only return the node with this specific instance
UUID or an empty set if not found.
* ``is_maintenance``: Only return those with ``maintenance`` set to
``True`` or ``False``.
* ``limit``: Requests at most the specified number of nodes be
returned from the query.
* ``marker``: Specifies the ID of the last-seen node. Use the
``limit`` parameter to make an initial limited request and
use the ID of the last-seen node from the response as
the ``marker`` value in a subsequent limited request.
* ``provision_state``: Only return those nodes with the specified
``provision_state``.
* ``resource_class``: Only return those with the specified
``resource_class``.
* ``shard``: Only return nodes matching the supplied shard key.
* ``sort_dir``: Sorts the response by the requested sort direction.
A valid value is ``asc`` (ascending) or ``desc``
(descending). Default is ``asc``. You can specify multiple
pairs of sort key and sort direction query parameters. If
you omit the sort direction in a pair, the API uses the
natural sorting direction of the server attribute that is
provided as the ``sort_key``.
* ``sort_key``: Sorts the response by the this attribute value.
Default is ``id``. You can specify multiple pairs of sort
key and sort direction query pa rameters. If you omit the
sort direction in a pair, the API uses the natural sorting
direction of the server attribute that is provided as the
``sort_key``.
:returns: A generator of :class:`~openstack.baremetal.v1.node.Node`
"""
return _node.Node.list(self, details=details, **query)
def create_node(self, **attrs):
"""Create a new node from attributes.
See :meth:`~openstack.baremetal.v1.node.Node.create` for an explanation
of the initial provision state.
:param dict attrs: Keyword arguments that will be used to create a
:class:`~openstack.baremetal.v1.node.Node`.
:returns: The results of node creation.
:rtype: :class:`~openstack.baremetal.v1.node.Node`.
"""
return self._create(_node.Node, **attrs)
def find_node(self, name_or_id, ignore_missing=True):
"""Find a single node.
:param str name_or_id: The name or ID of a node.
:param bool ignore_missing: When set to ``False``, an exception of
:class:`~openstack.exceptions.NotFoundException` will be raised
when the node does not exist. When set to `True``, None will
be returned when attempting to find a nonexistent node.
:returns: One :class:`~openstack.baremetal.v1.node.Node` object
or None.
"""
return self._find(
_node.Node, name_or_id, ignore_missing=ignore_missing
)
def get_node(self, node, fields=None):
"""Get a specific node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param fields: Limit the resource fields to fetch.
:returns: One :class:`~openstack.baremetal.v1.node.Node`
:raises: :class:`~openstack.exceptions.NotFoundException` when no
node matching the name or ID could be found.
"""
return self._get_with_fields(_node.Node, node, fields=fields)
def get_node_inventory(self, node):
"""Get a specific node's hardware inventory.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:returns: The node inventory
:raises: :class:`~openstack.exceptions.NotFoundException` when no
inventory could be found.
"""
res = self._get_resource(_node.Node, node)
return res.get_node_inventory(self, node)
def update_node(self, node, retry_on_conflict=True, **attrs):
"""Update a node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param bool retry_on_conflict: Whether to retry HTTP CONFLICT error.
Most of the time it can be retried, since it is caused by the node
being locked. However, when setting ``instance_id``, this is
a normal code and should not be retried.
:param dict attrs: The attributes to update on the node represented
by the ``node`` parameter.
:returns: The updated node.
:rtype: :class:`~openstack.baremetal.v1.node.Node`
"""
res = self._get_resource(_node.Node, node, **attrs)
return res.commit(self, retry_on_conflict=retry_on_conflict)
def patch_node(
self, node, patch, reset_interfaces=None, retry_on_conflict=True
):
"""Apply a JSON patch to the node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param patch: JSON patch to apply.
:param bool reset_interfaces: whether to reset the node hardware
interfaces to their defaults. This works only when changing
drivers. Added in API microversion 1.45.
:param bool retry_on_conflict: Whether to retry HTTP CONFLICT error.
Most of the time it can be retried, since it is caused by the node
being locked. However, when setting ``instance_id``, this is
a normal code and should not be retried.
See `Update Node
<https://docs.openstack.org/api-ref/baremetal/?expanded=update-node-detail#update-node>`_
for details.
:returns: The updated node.
:rtype: :class:`~openstack.baremetal.v1.node.Node`
"""
res = self._get_resource(_node.Node, node)
return res.patch(
self,
patch,
retry_on_conflict=retry_on_conflict,
reset_interfaces=reset_interfaces,
)
def set_node_provision_state(
self,
node,
target,
config_drive=None,
clean_steps=None,
rescue_password=None,
wait=False,
timeout=None,
deploy_steps=None,
):
"""Run an action modifying node's provision state.
This call is asynchronous, it will return success as soon as the Bare
Metal service acknowledges the request.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param target: Provisioning action, e.g. ``active``, ``provide``.
See the Bare Metal service documentation for available actions.
:param config_drive: Config drive to pass to the node, only valid
for ``active` and ``rebuild`` targets. You can use functions from
:mod:`openstack.baremetal.configdrive` to build it.
:param clean_steps: Clean steps to execute, only valid for ``clean``
target.
:param rescue_password: Password for the rescue operation, only valid
for ``rescue`` target.
:param wait: Whether to wait for the node to get into the expected
state. The expected state is determined from a combination of
the current provision state and ``target``.
:param timeout: If ``wait`` is set to ``True``, specifies how much (in
seconds) to wait for the expected state to be reached. The value of
``None`` (the default) means no client-side timeout.
:param deploy_steps: Deploy steps to execute, only valid for ``active``
and ``rebuild`` target.
:returns: The updated :class:`~openstack.baremetal.v1.node.Node`
:raises: ValueError if ``config_drive``, ``clean_steps``,
``deploy_steps`` or ``rescue_password`` are provided with an
invalid ``target``.
"""
res = self._get_resource(_node.Node, node)
return res.set_provision_state(
self,
target,
config_drive=config_drive,
clean_steps=clean_steps,
rescue_password=rescue_password,
wait=wait,
timeout=timeout,
deploy_steps=deploy_steps,
)
def get_node_boot_device(self, node):
"""Get node boot device
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:return: The node boot device
"""
res = self._get_resource(_node.Node, node)
return res.get_boot_device(self)
def set_node_boot_device(self, node, boot_device, persistent=False):
"""Set node boot device
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param boot_device: Boot device to assign to the node.
:param persistent: If the boot device change is maintained after node
reboot
:return: The updated :class:`~openstack.baremetal.v1.node.Node`
"""
res = self._get_resource(_node.Node, node)
return res.set_boot_device(self, boot_device, persistent=persistent)
def get_node_supported_boot_devices(self, node):
"""Get supported boot devices for node
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:return: The node boot device
"""
res = self._get_resource(_node.Node, node)
return res.get_supported_boot_devices(self)
def set_node_boot_mode(self, node, target):
"""Make a request to change node's boot mode
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param target: Boot mode to set for node, one of either 'uefi'/'bios'.
"""
res = self._get_resource(_node.Node, node)
return res.set_boot_mode(self, target)
def set_node_secure_boot(self, node, target):
"""Make a request to change node's secure boot state
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param target: Boolean indicating secure boot state to set.
True/False corresponding to 'on'/'off' respectively.
"""
res = self._get_resource(_node.Node, node)
return res.set_secure_boot(self, target)
def inject_nmi_to_node(self, node):
"""Inject NMI to node.
Injects a non-maskable interrupt (NMI) message to the node. This is
used when response time is critical, such as during non-recoverable
hardware errors. In addition, virsh inject-nmi is useful for triggering
a crashdump in Windows guests.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:return: None
"""
res = self._get_resource(_node.Node, node)
res.inject_nmi(self)
def wait_for_nodes_provision_state(
self,
nodes,
expected_state,
timeout=None,
abort_on_failed_state=True,
fail=True,
):
"""Wait for the nodes to reach the expected state.
:param nodes: List of nodes - name, ID or
:class:`~openstack.baremetal.v1.node.Node` instance.
:param expected_state: The expected provisioning state to reach.
:param timeout: If ``wait`` is set to ``True``, specifies how much (in
seconds) to wait for the expected state to be reached. The value of
``None`` (the default) means no client-side timeout.
:param abort_on_failed_state: If ``True`` (the default), abort waiting
if any node reaches a failure state which does not match the
expected one. Note that the failure state for ``enroll`` ->
``manageable`` transition is ``enroll`` again.
:param fail: If set to ``False`` this call will not raise on timeouts
and provisioning failures.
:return: If `fail` is ``True`` (the default), the list of
:class:`~openstack.baremetal.v1.node.Node` instances that reached
the requested state. If `fail` is ``False``, a
:class:`~openstack.baremetal.v1.node.WaitResult` named tuple.
:raises: :class:`~openstack.exceptions.ResourceFailure` if a node
reaches an error state and ``abort_on_failed_state`` is ``True``.
:raises: :class:`~openstack.exceptions.ResourceTimeout` on timeout.
"""
log_nodes = ', '.join(
n.id if isinstance(n, _node.Node) else n for n in nodes
)
finished = []
failed = []
remaining = nodes
try:
for count in utils.iterate_timeout(
timeout,
f"Timeout waiting for nodes {log_nodes} to reach "
f"target state '{expected_state}'",
):
nodes = [self.get_node(n) for n in remaining]
remaining = []
for n in nodes:
try:
if n._check_state_reached(
self, expected_state, abort_on_failed_state
):
finished.append(n)
else:
remaining.append(n)
except exceptions.ResourceFailure:
if fail:
raise
else:
failed.append(n)
if not remaining:
if fail:
return finished
else:
return _node.WaitResult(finished, failed, [])
self.log.debug(
'Still waiting for nodes %(nodes)s to reach state '
'"%(target)s"',
{
'nodes': ', '.join(n.id for n in remaining),
'target': expected_state,
},
)
except exceptions.ResourceTimeout:
if fail:
raise
else:
return _node.WaitResult(finished, failed, remaining)
def set_node_power_state(self, node, target, wait=False, timeout=None):
"""Run an action modifying node's power state.
This call is asynchronous, it will return success as soon as the Bare
Metal service acknowledges the request.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param target: Target power state, one of
:class:`~openstack.baremetal.v1.node.PowerAction` or a string.
:param wait: Whether to wait for the node to get into the expected
state.
:param timeout: If ``wait`` is set to ``True``, specifies how much (in
seconds) to wait for the expected state to be reached. The value of
``None`` (the default) means no client-side timeout.
"""
self._get_resource(_node.Node, node).set_power_state(
self, target, wait=wait, timeout=timeout
)
def wait_for_node_power_state(self, node, expected_state, timeout=None):
"""Wait for the node to reach the power state.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param timeout: How much (in seconds) to wait for the target state
to be reached. The value of ``None`` (the default) means
no timeout.
:returns: The updated :class:`~openstack.baremetal.v1.node.Node`
"""
res = self._get_resource(_node.Node, node)
return res.wait_for_power_state(self, expected_state, timeout=timeout)
def wait_for_node_reservation(self, node, timeout=None):
"""Wait for a lock on the node to be released.
Bare metal nodes in ironic have a reservation lock that
is used to represent that a conductor has locked the node
while performing some sort of action, such as changing
configuration as a result of a machine state change.
This lock can occur during power syncronization, and prevents
updates to objects attached to the node, such as ports.
Note that nothing prevents a conductor from acquiring the lock again
after this call returns, so it should be treated as best effort.
Returns immediately if there is no reservation on the node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param timeout: How much (in seconds) to wait for the lock to be
released. The value of ``None`` (the default) means no timeout.
:returns: The updated :class:`~openstack.baremetal.v1.node.Node`
"""
res = self._get_resource(_node.Node, node)
return res.wait_for_reservation(self, timeout=timeout)
def validate_node(self, node, required=('boot', 'deploy', 'power')):
"""Validate required information on a node.
:param node: The value can be either the name or ID of a node or
a :class:`~openstack.baremetal.v1.node.Node` instance.
:param required: List of interfaces that are required to pass
validation. The default value is the list of minimum required
interfaces for provisioning.
:return: dict mapping interface names to
:class:`~openstack.baremetal.v1.node.ValidationResult` objects.
:raises: :exc:`~openstack.exceptions.ValidationException` if validation
fails for a required interface.
"""
res = self._get_resource(_node.Node, node)
return res.validate(self, required=required)
def set_node_maintenance(self, node, reason=None):
"""Enable maintenance mode on the node.
:param node: The value can be either the name or ID of a node or
a :class:`~openstack.baremetal.v1.node.Node` instance.
:param reason: Optional reason for maintenance.
:return: This :class:`Node` instance.
"""
res = self._get_resource(_node.Node, node)
return res.set_maintenance(self, reason)
def unset_node_maintenance(self, node):
"""Disable maintenance mode on the node.
:param node: The value can be either the name or ID of a node or
a :class:`~openstack.baremetal.v1.node.Node` instance.
:return: This :class:`Node` instance.
"""
res = self._get_resource(_node.Node, node)
return res.unset_maintenance(self)
def delete_node(self, node, ignore_missing=True):
"""Delete a node.
:param node: The value can be either the name or ID of a node or
a :class:`~openstack.baremetal.v1.node.Node` instance.
:param bool ignore_missing: When set to ``False``, an exception
:class:`~openstack.exceptions.NotFoundException` will be raised
when the node could not be found. When set to ``True``, no
exception will be raised when attempting to delete a non-existent
node.
:returns: The instance of the node which was deleted.
:rtype: :class:`~openstack.baremetal.v1.node.Node`.
"""
return self._delete(_node.Node, node, ignore_missing=ignore_missing)
# ========== Node actions ==========
def add_node_trait(self, node, trait):
"""Add a trait to a node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param trait: trait to remove from the node.
:returns: The updated node
"""
res = self._get_resource(_node.Node, node)
return res.add_trait(self, trait)
def remove_node_trait(self, node, trait, ignore_missing=True):
"""Remove a trait from a node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param trait: trait to remove from the node.
:param bool ignore_missing: When set to ``False``, an exception
:class:`~openstack.exceptions.NotFoundException` will be raised
when the trait could not be found. When set to ``True``, no
exception will be raised when attempting to delete a non-existent
trait.
:returns: The updated :class:`~openstack.baremetal.v1.node.Node`
"""
res = self._get_resource(_node.Node, node)
return res.remove_trait(self, trait, ignore_missing=ignore_missing)
def call_node_vendor_passthru(self, node, verb, method, body=None):
"""Calls vendor_passthru for a node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param verb: The HTTP verb, one of GET, SET, POST, DELETE.
:param method: The method to call using vendor_passthru.
:param body: The JSON body in the HTTP call.
:returns: The raw response from the method.
"""
res = self._get_resource(_node.Node, node)
return res.call_vendor_passthru(self, verb, method, body)
def list_node_vendor_passthru(self, node):
"""Lists vendor_passthru for a node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:returns: A list of vendor_passthru methods for the node.
"""
res = self._get_resource(_node.Node, node)
return res.list_vendor_passthru(self)
def get_node_console(self, node):
"""Get the console for a node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:returns: Connection information for the console.
"""
res = self._get_resource(_node.Node, node)
return res.get_console(self)
def enable_node_console(self, node):
"""Enable the console for a node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:returns: None
"""
res = self._get_resource(_node.Node, node)
return res.set_console_mode(self, True)
def disable_node_console(self, node):
"""Disable the console for a node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:returns: None
"""
res = self._get_resource(_node.Node, node)
return res.set_console_mode(self, False)
def set_node_traits(self, node, traits):
"""Set traits for a node.
Removes any existing traits and adds the traits passed in to this
method.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:param traits: list of traits to add to the node.
:returns: The updated :class:`~openstack.baremetal.v1.node.Node`
"""
res = self._get_resource(_node.Node, node)
return res.set_traits(self, traits)
def list_node_firmware(self, node):
"""Lists firmware components for a node.
:param node: The value can be the name or ID of a node or a
:class:`~openstack.baremetal.v1.node.Node` instance.
:returns: A list of the node's firmware components.
"""
res = self._get_resource(_node.Node, node)
return res.list_firmware(self)
# ========== Ports ==========
def ports(self, details=False, **query):
"""Retrieve a generator of ports.
:param details: A boolean indicating whether the detailed information
for every port should be returned.
:param dict query: Optional query parameters to be sent to restrict
the ports returned. Available parameters include:
* ``address``: Only return ports with the specified physical
hardware address, typically a MAC address.
* ``driver``: Only return those with the specified ``driver``.
* ``fields``: A list containing one or more fields to be returned
in the response. This may lead to some performance gain
because other fields of the resource are not refreshed.
* ``limit``: Requests at most the specified number of ports be
returned from the query.
* ``marker``: Specifies the ID of the last-seen port. Use the
``limit`` parameter to make an initial limited request and
use the ID of the last-seen port from the response as
the ``marker`` value in a subsequent limited request.
* ``node``:only return the ones associated with this specific node
(name or UUID), or an empty set if not found.
* ``node_id``:only return the ones associated with this specific
node UUID, or an empty set if not found.
* ``portgroup``: only return the ports associated with this
specific Portgroup (name or UUID), or an empty set if not
found. Added in API microversion 1.24.
* ``sort_dir``: Sorts the response by the requested sort direction.
A valid value is ``asc`` (ascending) or ``desc``
(descending). Default is ``asc``. You can specify multiple
pairs of sort key and sort direction query parameters. If
you omit the sort direction in a pair, the API uses the
natural sorting direction of the server attribute that is
provided as the ``sort_key``.
* ``sort_key``: Sorts the response by the this attribute value.
Default is ``id``. You can specify multiple pairs of sort
key and sort direction query parameters. If you omit the
sort direction in a pair, the API uses the natural sorting
direction of the server attribute that is provided as the
``sort_key``.
:returns: A generator of port instances.
"""
return _port.Port.list(self, details=details, **query)
def create_port(self, **attrs):
"""Create a new port from attributes.
:param dict attrs: Keyword arguments that will be used to create a
:class:`~openstack.baremetal.v1.port.Port`.
:returns: The results of port creation.
:rtype: :class:`~openstack.baremetal.v1.port.Port`.
"""
return self._create(_port.Port, **attrs)
def find_port(self, name_or_id, ignore_missing=True):
"""Find a single port.
:param str name_or_id: The ID of a port.
:param bool ignore_missing: When set to ``False``, an exception of
:class:`~openstack.exceptions.NotFoundException` will be raised
when the port does not exist. When set to `True``, None will
be returned when attempting to find a nonexistent port.
:returns: One :class:`~openstack.baremetal.v1.port.Port` object
or None.
"""
return self._find(
_port.Port, name_or_id, ignore_missing=ignore_missing
)
def get_port(self, port, fields=None):
"""Get a specific port.
:param port: The value can be the ID of a port or a
:class:`~openstack.baremetal.v1.port.Port` instance.
:param fields: Limit the resource fields to fetch.
:returns: One :class:`~openstack.baremetal.v1.port.Port`
:raises: :class:`~openstack.exceptions.NotFoundException` when no
port matching the name or ID could be found.
"""
return self._get_with_fields(_port.Port, port, fields=fields)
def update_port(self, port, **attrs):
"""Update a port.
:param port: Either the ID of a port or an instance
of :class:`~openstack.baremetal.v1.port.Port`.
:param dict attrs: The attributes to update on the port represented
by the ``port`` parameter.
:returns: The updated port.
:rtype: :class:`~openstack.baremetal.v1.port.Port`
"""
return self._update(_port.Port, port, **attrs)
def patch_port(self, port, patch):
"""Apply a JSON patch to the port.
:param port: The value can be the ID of a port or a
:class:`~openstack.baremetal.v1.port.Port` instance.
:param patch: JSON patch to apply.
:returns: The updated port.
:rtype: :class:`~openstack.baremetal.v1.port.Port`
"""
return self._get_resource(_port.Port, port).patch(self, patch)
def delete_port(self, port, ignore_missing=True):
"""Delete a port.
:param port: The value can be either the ID of a port or
a :class:`~openstack.baremetal.v1.port.Port` instance.
:param bool ignore_missing: When set to ``False``, an exception
:class:`~openstack.exceptions.NotFoundException` will be raised
when the port could not be found. When set to ``True``, no
exception will be raised when attempting to delete a non-existent
port.
:returns: The instance of the port which was deleted.
:rtype: :class:`~openstack.baremetal.v1.port.Port`.
"""
return self._delete(_port.Port, port, ignore_missing=ignore_missing)
# ========== Port groups ==========
def port_groups(self, details=False, **query):
"""Retrieve a generator of port groups.
:param details: A boolean indicating whether the detailed information
for every port group should be returned.
:param dict query: Optional query parameters to be sent to restrict
the port groups returned. Available parameters include:
* ``address``: Only return portgroups with the specified physical
hardware address, typically a MAC address.
* ``fields``: A list containing one or more fields to be returned
in the response. This may lead to some performance gain
because other fields of the resource are not refreshed.
* ``limit``: Requests at most the specified number of portgroups
returned from the query.
* ``marker``: Specifies the ID of the last-seen portgroup. Use the
``limit`` parameter to make an initial limited request and
use the ID of the last-seen portgroup from the response as
the ``marker`` value in a subsequent limited request.
* ``node``:only return the ones associated with this specific node
(name or UUID), or an empty set if not found.
* ``sort_dir``: Sorts the response by the requested sort direction.
A valid value is ``asc`` (ascending) or ``desc``
(descending). Default is ``asc``. You can specify multiple
pairs of sort key and sort direction query parameters. If
you omit the sort direction in a pair, the API uses the
natural sorting direction of the server attribute that is
provided as the ``sort_key``.
* ``sort_key``: Sorts the response by the this attribute value.
Default is ``id``. You can specify multiple pairs of sort
key and sort direction query parameters. If you omit the
sort direction in a pair, the API uses the natural sorting
direction of the server attribute that is provided as the
``sort_key``.
:returns: A generator of port group instances.
"""
return _portgroup.PortGroup.list(self, details=details, **query)
def create_port_group(self, **attrs):
"""Create a new portgroup from attributes.
:param dict attrs: Keyword arguments that will be used to create a
:class:`~openstack.baremetal.v1.port_group.PortGroup`.
:returns: The results of portgroup creation.
:rtype: :class:`~openstack.baremetal.v1.port_group.PortGroup`.
"""
return self._create(_portgroup.PortGroup, **attrs)
def find_port_group(self, name_or_id, ignore_missing=True):
"""Find a single port group.
:param str name_or_id: The name or ID of a portgroup.
:param bool ignore_missing: When set to ``False``, an exception of
:class:`~openstack.exceptions.NotFoundException` will be raised
when the port group does not exist. When set to `True``, None will
be returned when attempting to find a nonexistent port group.
:returns: One :class:`~openstack.baremetal.v1.port_group.PortGroup`
object or None.
"""
return self._find(
_portgroup.PortGroup, name_or_id, ignore_missing=ignore_missing
)
def get_port_group(self, port_group, fields=None):
"""Get a specific port group.
:param port_group: The value can be the name or ID of a chassis or a
:class:`~openstack.baremetal.v1.port_group.PortGroup` instance.
:param fields: Limit the resource fields to fetch.
:returns: One :class:`~openstack.baremetal.v1.port_group.PortGroup`
:raises: :class:`~openstack.exceptions.NotFoundException` when no
port group matching the name or ID could be found.
"""
return self._get_with_fields(
_portgroup.PortGroup, port_group, fields=fields
)
def update_port_group(self, port_group, **attrs):
"""Update a port group.
:param port_group: Either the name or the ID of a port group or
an instance of
:class:`~openstack.baremetal.v1.port_group.PortGroup`.
:param dict attrs: The attributes to update on the port group
represented by the ``port_group`` parameter.
:returns: The updated port group.
:rtype: :class:`~openstack.baremetal.v1.port_group.PortGroup`
"""
return self._update(_portgroup.PortGroup, port_group, **attrs)
def patch_port_group(self, port_group, patch):
"""Apply a JSON patch to the port_group.
:param port_group: The value can be the ID of a port group or a
:class:`~openstack.baremetal.v1.port_group.PortGroup` instance.
:param patch: JSON patch to apply.
:returns: The updated port group.
:rtype: :class:`~openstack.baremetal.v1.port_group.PortGroup`
"""
res = self._get_resource(_portgroup.PortGroup, port_group)
return res.patch(self, patch)
def delete_port_group(self, port_group, ignore_missing=True):
"""Delete a port group.
:param port_group: The value can be either the name or ID of
a port group or a
:class:`~openstack.baremetal.v1.port_group.PortGroup`
instance.
:param bool ignore_missing: When set to ``False``, an exception
:class:`~openstack.exceptions.NotFoundException` will be raised
when the port group could not be found. When set to ``True``, no
exception will be raised when attempting to delete a non-existent
port group.
:returns: The instance of the port group which was deleted.
:rtype: :class:`~openstack.baremetal.v1.port_group.PortGroup`.
"""
return self._delete(
_portgroup.PortGroup, port_group, ignore_missing=ignore_missing
)
# ========== VIFs ==========
def attach_vif_to_node(
self,
node: ty.Union[_node.Node, str],
vif_id: str,
retry_on_conflict: bool = True,
*,
port_id: ty.Optional[str] = None,
port_group_id: ty.Optional[str] = None,
) -> None:
"""Attach a VIF to the node.
The exact form of the VIF ID depends on the network interface used by
the node. In the most common case it is a Network service port
(NOT a Bare Metal port) ID. A VIF can only be attached to one node
at a time.
:param node: The value can be either the name or ID of a node or
a :class:`~openstack.baremetal.v1.node.Node` instance.
:param vif_id: Backend-specific VIF ID.
:param retry_on_conflict: Whether to retry HTTP CONFLICT errors.
This can happen when either the VIF is already used on a node or
the node is locked. Since the latter happens more often, the
default value is True.
:param port_id: The UUID of the port to attach the VIF to. Only one of
port_id or port_group_id can be provided.
:param port_group_id: The UUID of the portgroup to attach to. Only one
of port_group_id or port_id can be provided.
:return: None
:raises: :exc:`~openstack.exceptions.NotSupported` if the server
does not support the VIF API.
:raises: :exc:`~openstack.exceptions.InvalidRequest` if both port_id
and port_group_id are provided.
"""
res = self._get_resource(_node.Node, node)
res.attach_vif(
self,
vif_id=vif_id,
retry_on_conflict=retry_on_conflict,
port_id=port_id,
port_group_id=port_group_id,
)
def detach_vif_from_node(self, node, vif_id, ignore_missing=True):
"""Detach a VIF from the node.
The exact form of the VIF ID depends on the network interface used by
the node. In the most common case it is a Network service port
(NOT a Bare Metal port) ID.
:param node: The value can be either the name or ID of a node or
a :class:`~openstack.baremetal.v1.node.Node` instance.
:param string vif_id: Backend-specific VIF ID.
:param bool ignore_missing: When set to ``False``
:class:`~openstack.exceptions.NotFoundException` will be
raised when the VIF does not exist. Otherwise, ``False``
is returned.
:return: ``True`` if the VIF was detached, otherwise ``False``.
:raises: :exc:`~openstack.exceptions.NotSupported` if the server
does not support the VIF API.
"""
res = self._get_resource(_node.Node, node)
return res.detach_vif(self, vif_id, ignore_missing=ignore_missing)
def list_node_vifs(self, node):
"""List IDs of VIFs attached to the node.
The exact form of the VIF ID depends on the network interface used by
the node. In the most common case it is a Network service port
(NOT a Bare Metal port) ID.
:param node: The value can be either the name or ID of a node or
a :class:`~openstack.baremetal.v1.node.Node` instance.
:return: List of VIF IDs as strings.
:raises: :exc:`~openstack.exceptions.NotSupported` if the server
does not support the VIF API.
"""
res = self._get_resource(_node.Node, node)
return res.list_vifs(self)
# ========== Allocations ==========
def allocations(self, **query):
"""Retrieve a generator of allocations.
:param dict query: Optional query parameters to be sent to restrict
the allocation to be returned. Available parameters include:
* ``fields``: A list containing one or more fields to be returned
in the response. This may lead to some performance gain
because other fields of the resource are not refreshed.
* ``limit``: Requests at most the specified number of items be
returned from the query.
* ``marker``: Specifies the ID of the last-seen allocation. Use the
``limit`` parameter to make an initial limited request and
use the ID of the last-seen allocation from the response as
the ``marker`` value in a subsequent limited request.
* ``sort_dir``: Sorts the response by the requested sort direction.
A valid value is ``asc`` (ascending) or ``desc``
(descending). Default is ``asc``. You can specify multiple
pairs of sort key and sort direction query parameters. If
you omit the sort direction in a pair, the API uses the
natural sorting direction of the server attribute that is
provided as the ``sort_key``.
* ``sort_key``: Sorts the response by the this attribute value.
Default is ``id``. You can specify multiple pairs of sort
key and sort direction query parameters. If you omit the
sort direction in a pair, the API uses the natural sorting
direction of the server attribute that is provided as the
``sort_key``.
:returns: A generator of allocation instances.
"""
return _allocation.Allocation.list(self, **query)
def create_allocation(self, **attrs):
"""Create a new allocation from attributes.
:param dict attrs: Keyword arguments that will be used to create a
:class:`~openstack.baremetal.v1.allocation.Allocation`.
:returns: The results of allocation creation.
:rtype: :class:`~openstack.baremetal.v1.allocation.Allocation`.
"""
return self._create(_allocation.Allocation, **attrs)
def get_allocation(self, allocation, fields=None):
"""Get a specific allocation.
:param allocation: The value can be the name or ID of an allocation or
a :class:`~openstack.baremetal.v1.allocation.Allocation` instance.
:param fields: Limit the resource fields to fetch.
:returns: One :class:`~openstack.baremetal.v1.allocation.Allocation`
:raises: :class:`~openstack.exceptions.NotFoundException` when no
allocation matching the name or ID could be found.
"""
return self._get_with_fields(
_allocation.Allocation, allocation, fields=fields
)
def update_allocation(self, allocation, **attrs):
"""Update an allocation.
:param allocation: The value can be the name or ID of an allocation or
a :class:`~openstack.baremetal.v1.allocation.Allocation` instance.
:param dict attrs: The attributes to update on the allocation
represented by the ``allocation`` parameter.
:returns: The updated allocation.
:rtype: :class:`~openstack.baremetal.v1.allocation.Allocation`
"""
return self._update(_allocation.Allocation, allocation, **attrs)
def patch_allocation(self, allocation, patch):
"""Apply a JSON patch to the allocation.
:param allocation: The value can be the name or ID of an allocation or
a :class:`~openstack.baremetal.v1.allocation.Allocation` instance.
:param patch: JSON patch to apply.
:returns: The updated allocation.
:rtype: :class:`~openstack.baremetal.v1.allocation.Allocation`
"""
return self._get_resource(_allocation.Allocation, allocation).patch(
self, patch
)
def delete_allocation(self, allocation, ignore_missing=True):
"""Delete an allocation.
:param allocation: The value can be the name or ID of an allocation or
a :class:`~openstack.baremetal.v1.allocation.Allocation` instance.
:param bool ignore_missing: When set to ``False``, an exception
:class:`~openstack.exceptions.NotFoundException` will be raised
when the allocation could not be found. When set to ``True``, no
exception will be raised when attempting to delete a non-existent
allocation.
:returns: The instance of the allocation which was deleted.
:rtype: :class:`~openstack.baremetal.v1.allocation.Allocation`.
"""
return self._delete(
_allocation.Allocation, allocation, ignore_missing=ignore_missing
)
def wait_for_allocation(
self, allocation, timeout=None, ignore_error=False
):
"""Wait for the allocation to become active.
:param allocation: The value can be the name or ID of an allocation or
a :class:`~openstack.baremetal.v1.allocation.Allocation` instance.
:param timeout: How much (in seconds) to wait for the allocation.
The value of ``None`` (the default) means no client-side timeout.
:param ignore_error: If ``True``, this call will raise an exception
if the allocation reaches the ``error`` state. Otherwise the error
state is considered successful and the call returns.
:returns: The instance of the allocation.
:rtype: :class:`~openstack.baremetal.v1.allocation.Allocation`.
:raises: :class:`~openstack.exceptions.ResourceFailure` if allocation
fails and ``ignore_error`` is ``False``.
:raises: :class:`~openstack.exceptions.ResourceTimeout` on timeout.
"""
res = self._get_resource(_allocation.Allocation, allocation)
return res.wait(self, timeout=timeout, ignore_error=ignore_error)
# ========== Volume connectors ==========
def volume_connectors(self, details=False, **query):
"""Retrieve a generator of volume_connector.
:param details: A boolean indicating whether the detailed information
for every volume_connector should be returned.
:param dict query: Optional query parameters to be sent to restrict
the volume_connectors returned. Available parameters include:
* ``fields``: A list containing one or more fields to be returned
in the response. This may lead to some performance gain
because other fields of the resource are not refreshed.
* ``limit``: Requests at most the specified number of
volume_connector be returned from the query.
* ``marker``: Specifies the ID of the last-seen volume_connector.
Use the ``limit`` parameter to make an initial limited request
and use the ID of the last-seen volume_connector from the
response as the ``marker`` value in subsequent limited request.
* ``node``:only return the ones associated with this specific node
(name or UUID), or an empty set if not found.
* ``sort_dir``:Sorts the response by the requested sort direction.
A valid value is ``asc`` (ascending) or ``desc``
(descending). Default is ``asc``. You can specify multiple
pairs of sort key and sort direction query parameters. If
you omit the sort direction in a pair, the API uses the
natural sorting direction of the server attribute that is
provided as the ``sort_key``.
* ``sort_key``: Sorts the response by the this attribute value.
Default is ``id``. You can specify multiple pairs of sort
key and sort direction query parameters. If you omit the
sort direction in a pair, the API uses the natural sorting
direction of the server attribute that is provided as the
``sort_key``.
:returns: A generator of volume_connector instances.
"""
if details:
query['detail'] = True
return _volumeconnector.VolumeConnector.list(self, **query)
def create_volume_connector(self, **attrs):
"""Create a new volume_connector from attributes.
:param dict attrs: Keyword arguments that will be used to create a
:class:`~openstack.baremetal.v1.volume_connector.VolumeConnector`.
:returns: The results of volume_connector creation.
:rtype:
:class:`~openstack.baremetal.v1.volume_connector.VolumeConnector`.
"""
return self._create(_volumeconnector.VolumeConnector, **attrs)
def find_volume_connector(self, vc_id, ignore_missing=True):
"""Find a single volume connector.
:param str vc_id: The ID of a volume connector.
:param bool ignore_missing: When set to ``False``, an exception of
:class:`~openstack.exceptions.NotFoundException` will be raised
when the volume connector does not exist. When set to `True``,
None will be returned when attempting to find a nonexistent
volume connector.
:returns: One
:class:`~openstack.baremetal.v1.volumeconnector.VolumeConnector`
object or None.
"""
return self._find(
_volumeconnector.VolumeConnector,
vc_id,
ignore_missing=ignore_missing,
)
def get_volume_connector(self, volume_connector, fields=None):
"""Get a specific volume_connector.
:param volume_connector: The value can be the ID of a
volume_connector or a
:class:`~openstack.baremetal.v1.volume_connector.VolumeConnector`
instance.
:param fields: Limit the resource fields to fetch.`
:returns: One
:class: `~openstack.baremetal.v1.volume_connector.VolumeConnector`
:raises: :class:`~openstack.exceptions.NotFoundException` when no
volume_connector matching the name or ID could be found.`
"""
return self._get_with_fields(
_volumeconnector.VolumeConnector, volume_connector, fields=fields
)
def update_volume_connector(self, volume_connector, **attrs):
"""Update a volume_connector.
:param volume_connector: Either the ID of a volume_connector
or an instance of
:class:`~openstack.baremetal.v1.volume_connector.VolumeConnector`.
:param dict attrs: The attributes to update on the
volume_connector represented by the ``volume_connector``
parameter.
:returns: The updated volume_connector.
:rtype:
:class:`~openstack.baremetal.v1.volume_connector.VolumeConnector`
"""
return self._update(
_volumeconnector.VolumeConnector, volume_connector, **attrs
)
def patch_volume_connector(self, volume_connector, patch):
"""Apply a JSON patch to the volume_connector.
:param volume_connector: The value can be the ID of a
volume_connector or a
:class:`~openstack.baremetal.v1.volume_connector.VolumeConnector`
instance.
:param patch: JSON patch to apply.
:returns: The updated volume_connector.
:rtype:
:class:`~openstack.baremetal.v1.volume_connector.VolumeConnector.`
"""
return self._get_resource(
_volumeconnector.VolumeConnector, volume_connector
).patch(self, patch)
def delete_volume_connector(self, volume_connector, ignore_missing=True):
"""Delete an volume_connector.
:param volume_connector: The value can be either the ID of a
volume_connector.VolumeConnector or a
:class:`~openstack.baremetal.v1.volume_connector.VolumeConnector`
instance.
:param bool ignore_missing: When set to ``False``, an exception
:class:`~openstack.exceptions.NotFoundException` will be raised
when the volume_connector could not be found.
When set to ``True``, no exception will be raised when
attempting to delete a non-existent volume_connector.
:returns: The instance of the volume_connector which was deleted.
:rtype:
:class:`~openstack.baremetal.v1.volume_connector.VolumeConnector`.
"""
return self._delete(
_volumeconnector.VolumeConnector,
volume_connector,
ignore_missing=ignore_missing,
)
# ========== Volume targets ==========
def volume_targets(self, details=False, **query):
"""Retrieve a generator of volume_target.
:param details: A boolean indicating whether the detailed information
for every volume_target should be returned.
:param dict query: Optional query parameters to be sent to restrict
the volume_targets returned. Available parameters include:
* ``fields``: A list containing one or more fields to be returned
in the response. This may lead to some performance gain
because other fields of the resource are not refreshed.
* ``limit``: Requests at most the specified number of
volume_connector be returned from the query.
* ``marker``: Specifies the ID of the last-seen volume_target.
Use the ``limit`` parameter to make an initial limited request
and use the ID of the last-seen volume_target from the
response as the ``marker`` value in subsequent limited request.
* ``node``:only return the ones associated with this specific node
(name or UUID), or an empty set if not found.
* ``sort_dir``:Sorts the response by the requested sort direction.
A valid value is ``asc`` (ascending) or ``desc``
(descending). Default is ``asc``. You can specify multiple
pairs of sort key and sort direction query parameters. If
you omit the sort direction in a pair, the API uses the
natural sorting direction of the server attribute that is
provided as the ``sort_key``.
* ``sort_key``: Sorts the response by the this attribute value.
Default is ``id``. You can specify multiple pairs of sort
key and sort direction query parameters. If you omit the
sort direction in a pair, the API uses the natural sorting
direction of the server attribute that is provided as the
``sort_key``.
:returns: A generator of volume_target instances.
"""
if details:
query['detail'] = True
return _volumetarget.VolumeTarget.list(self, **query)
def create_volume_target(self, **attrs):
"""Create a new volume_target from attributes.
:param dict attrs: Keyword arguments that will be used to create a
:class:`~openstack.baremetal.v1.volume_target.VolumeTarget`.
:returns: The results of volume_target creation.
:rtype:
:class:`~openstack.baremetal.v1.volume_target.VolumeTarget`.
"""
return self._create(_volumetarget.VolumeTarget, **attrs)
def find_volume_target(self, vt_id, ignore_missing=True):
"""Find a single volume target.
:param str vt_id: The ID of a volume target.
:param bool ignore_missing: When set to ``False``, an exception of
:class:`~openstack.exceptions.NotFoundException` will be raised
when the volume connector does not exist. When set to `True``,
None will be returned when attempting to find a nonexistent
volume target.
:returns: One
:class:`~openstack.baremetal.v1.volumetarget.VolumeTarget`
object or None.
"""
return self._find(
_volumetarget.VolumeTarget, vt_id, ignore_missing=ignore_missing
)
def get_volume_target(self, volume_target, fields=None):
"""Get a specific volume_target.
:param volume_target: The value can be the ID of a
volume_target or a
:class:`~openstack.baremetal.v1.volume_target.VolumeTarget`
instance.
:param fields: Limit the resource fields to fetch.`
:returns: One
:class:`~openstack.baremetal.v1.volume_target.VolumeTarget`
:raises: :class:`~openstack.exceptions.NotFoundException` when no
volume_target matching the name or ID could be found.`
"""
return self._get_with_fields(
_volumetarget.VolumeTarget, volume_target, fields=fields
)
def update_volume_target(self, volume_target, **attrs):
"""Update a volume_target.
:param volume_target: Either the ID of a volume_target
or an instance of
:class:`~openstack.baremetal.v1.volume_target.VolumeTarget`.
:param dict attrs: The attributes to update on the
volume_target represented by the ``volume_target`` parameter.
:returns: The updated volume_target.
:rtype:
:class:`~openstack.baremetal.v1.volume_target.VolumeTarget`
"""
return self._update(_volumetarget.VolumeTarget, volume_target, **attrs)
def patch_volume_target(self, volume_target, patch):
"""Apply a JSON patch to the volume_target.
:param volume_target: The value can be the ID of a
volume_target or a
:class:`~openstack.baremetal.v1.volume_target.VolumeTarget`
instance.
:param patch: JSON patch to apply.
:returns: The updated volume_target.
:rtype:
:class:`~openstack.baremetal.v1.volume_target.VolumeTarget.`
"""
return self._get_resource(
_volumetarget.VolumeTarget, volume_target
).patch(self, patch)
def delete_volume_target(self, volume_target, ignore_missing=True):
"""Delete an volume_target.
:param volume_target: The value can be either the ID of a
volume_target.VolumeTarget or a
:class:`~openstack.baremetal.v1.volume_target.VolumeTarget`
instance.
:param bool ignore_missing: When set to ``False``, an exception
:class:`~openstack.exceptions.NotFoundException` will be raised
when the volume_target could not be found.
When set to ``True``, no exception will be raised when
attempting to delete a non-existent volume_target.
:returns: The instance of the volume_target which was deleted.
:rtype:
:class:`~openstack.baremetal.v1.volume_target.VolumeTarget`.
"""
return self._delete(
_volumetarget.VolumeTarget,
volume_target,
ignore_missing=ignore_missing,
)
# ========== Deploy templates ==========
def deploy_templates(self, details=False, **query):
"""Retrieve a generator of deploy_templates.
:param details: A boolean indicating whether the detailed information
for every deploy_templates should be returned.
:param dict query: Optional query parameters to be sent to
restrict the deploy_templates to be returned.
:returns: A generator of Deploy templates instances.
"""
if details:
query['detail'] = True
return _deploytemplates.DeployTemplate.list(self, **query)
def create_deploy_template(self, **attrs):
"""Create a new deploy_template from attributes.
:param dict attrs: Keyword arguments that will be used to create a
:class:`~openstack.baremetal.v1.deploy_templates.DeployTemplate`.
:returns: The results of deploy_template creation.
:rtype:
:class:`~openstack.baremetal.v1.deploy_templates.DeployTemplate`.
"""
return self._create(_deploytemplates.DeployTemplate, **attrs)
def update_deploy_template(self, deploy_template, **attrs):
"""Update a deploy_template.
:param deploy_template: Either the ID of a deploy_template,
or an instance of
:class:`~openstack.baremetal.v1.deploy_templates.DeployTemplate`.
:param dict attrs: The attributes to update on
the deploy_template represented
by the ``deploy_template`` parameter.
:returns: The updated deploy_template.
:rtype:
:class:`~openstack.baremetal.v1.deploy_templates.DeployTemplate`
"""
return self._update(
_deploytemplates.DeployTemplate, deploy_template, **attrs
)
def delete_deploy_template(self, deploy_template, ignore_missing=True):
"""Delete a deploy_template.
:param deploy_template:The value can be
either the ID of a deploy_template or a
:class:`~openstack.baremetal.v1.deploy_templates.DeployTemplate`
instance.
:param bool ignore_missing: When set to ``False``,
an exception:class:`~openstack.exceptions.NotFoundException`
will be raised when the deploy_template
could not be found.
When set to ``True``, no
exception will be raised when attempting
to delete a non-existent
deploy_template.
:returns: The instance of the deploy_template which was deleted.
:rtype:
:class:`~openstack.baremetal.v1.deploy_templates.DeployTemplate`.
"""
return self._delete(
_deploytemplates.DeployTemplate,
deploy_template,
ignore_missing=ignore_missing,
)
def get_deploy_template(self, deploy_template, fields=None):
"""Get a specific deployment template.
:param deploy_template: The value can be the name or ID
of a deployment template
:class:`~openstack.baremetal.v1.deploy_templates.DeployTemplate`
instance.
:param fields: Limit the resource fields to fetch.
:returns: One
:class:`~openstack.baremetal.v1.deploy_templates.DeployTemplate`
:raises: :class:`~openstack.exceptions.NotFoundException`
when no deployment template matching the name or
ID could be found.
"""
return self._get_with_fields(
_deploytemplates.DeployTemplate, deploy_template, fields=fields
)
def patch_deploy_template(self, deploy_template, patch):
"""Apply a JSON patch to the deploy_templates.
:param deploy_templates: The value can be the ID of a
deploy_template or a
:class:`~openstack.baremetal.v1.deploy_templates.DeployTemplate`
instance.
:param patch: JSON patch to apply.
:returns: The updated deploy_template.
:rtype:
:class:`~openstack.baremetal.v1.deploy_templates.DeployTemplate`
"""
return self._get_resource(
_deploytemplates.DeployTemplate, deploy_template
).patch(self, patch)
# ========== Conductors ==========
def conductors(self, details=False, **query):
"""Retrieve a generator of conductors.
:param bool details: A boolean indicating whether the detailed
information for every conductor should be returned.
:returns: A generator of conductor instances.
"""
if details:
query['details'] = True
return _conductor.Conductor.list(self, **query)
def get_conductor(self, conductor, fields=None):
"""Get a specific conductor.
:param conductor: The value can be the name of a conductor or a
:class:`~openstack.baremetal.v1.conductor.Conductor` instance.
:returns: One :class:`~openstack.baremetal.v1.conductor.Conductor`
:raises: :class:`~openstack.exceptions.NotFoundException` when no
conductor matching the name could be found.
"""
return self._get_with_fields(
_conductor.Conductor, conductor, fields=fields
)
# ========== Utilities ==========
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)
|