From 75237720f698c37a7689a20f4f9cb83f9ffc7b2d Mon Sep 17 00:00:00 2001 From: Dmitry Tantsur Date: Thu, 8 Sep 2022 18:30:35 +0200 Subject: [PATCH] Update register_machine to use the Ironic format for ports It's confusing to use two different formats across the SDK. Also allow a simple list of MAC addresses, since that's what most people presumably want. Change-Id: I47448850225f04f24fb0dfc6a1700b5e2c26a450 --- openstack/cloud/_baremetal.py | 35 ++++++++++++++----- .../tests/unit/cloud/test_baremetal_node.py | 20 ++++++----- 2 files changed, 38 insertions(+), 17 deletions(-) diff --git a/openstack/cloud/_baremetal.py b/openstack/cloud/_baremetal.py index f20e6d6bf..0041e0990 100644 --- a/openstack/cloud/_baremetal.py +++ b/openstack/cloud/_baremetal.py @@ -24,6 +24,25 @@ import jsonpatch from openstack.cloud import exc +def _normalize_port_list(nics): + ports = [] + for row in nics: + if isinstance(row, str): + address = row + row = {} + elif 'mac' in row: + address = row.pop('mac') + else: + try: + address = row.pop('address') + except KeyError: + raise TypeError( + "Either 'address' or 'mac' must be provided " + "for port %s" % row) + ports.append(dict(row, address=address)) + return ports + + class BaremetalCloudMixin: def list_nics(self): @@ -182,10 +201,12 @@ class BaremetalCloudMixin: Example:: [ - {'mac': 'aa:bb:cc:dd:ee:01'}, - {'mac': 'aa:bb:cc:dd:ee:02'} + {'address': 'aa:bb:cc:dd:ee:01'}, + {'address': 'aa:bb:cc:dd:ee:02'} ] + Alternatively, you can provide an array of MAC addresses. + :param wait: Boolean value, defaulting to false, to wait for the node to reach the available state where the node can be provisioned. It must be noted, when set to false, the method will still wait for @@ -233,11 +254,9 @@ class BaremetalCloudMixin: # Create NICs before trying to run cleaning created_nics = [] try: - for row in nics: - address = row.pop('mac') + for port in _normalize_port_list(nics): nic = self.baremetal.create_port(node_id=machine.id, - address=address, - **row) + **port) created_nics.append(nic.id) except Exception: @@ -295,9 +314,9 @@ class BaremetalCloudMixin: "Error unregistering node '%s': Exception occured while" " waiting to be able to proceed: %s" % (machine['uuid'], e)) - for nic in nics: + for nic in _normalize_port_list(nics): try: - port = next(self.baremetal.ports(address=nic['mac'])) + port = next(self.baremetal.ports(address=nic['address'])) except StopIteration: continue self.baremetal.delete_port(port.id) diff --git a/openstack/tests/unit/cloud/test_baremetal_node.py b/openstack/tests/unit/cloud/test_baremetal_node.py index c9c2e09e1..888258bef 100644 --- a/openstack/tests/unit/cloud/test_baremetal_node.py +++ b/openstack/tests/unit/cloud/test_baremetal_node.py @@ -1063,7 +1063,7 @@ class TestBaremetalNode(base.IronicTestCase): def test_register_machine(self): mac_address = '00:01:02:03:04:05' - nics = [{'mac': mac_address}] + nics = [{'address': mac_address}] node_uuid = self.fake_baremetal_node['uuid'] # TODO(TheJulia): There is a lot of duplication # in testing creation. Surely this hsould be a helper @@ -1104,7 +1104,7 @@ class TestBaremetalNode(base.IronicTestCase): # accounted for newer microversions. def test_register_machine_enroll(self): mac_address = '00:01:02:03:04:05' - nics = [{'mac': mac_address}] + nics = [{'address': mac_address, 'pxe_enabled': False}] node_uuid = self.fake_baremetal_node['uuid'] node_to_post = { 'chassis_uuid': None, @@ -1143,7 +1143,8 @@ class TestBaremetalNode(base.IronicTestCase): uri=self.get_mock_url( resource='ports'), validate=dict(json={'address': mac_address, - 'node_uuid': node_uuid}), + 'node_uuid': node_uuid, + 'pxe_enabled': False}), json=self.fake_baremetal_port), dict( method='PUT', @@ -1166,7 +1167,7 @@ class TestBaremetalNode(base.IronicTestCase): def test_register_machine_enroll_wait(self): mac_address = self.fake_baremetal_port - nics = [{'mac': mac_address}] + nics = [{'address': mac_address}] node_uuid = self.fake_baremetal_node['uuid'] node_to_post = { 'chassis_uuid': None, @@ -1241,7 +1242,7 @@ class TestBaremetalNode(base.IronicTestCase): def test_register_machine_enroll_failure(self): mac_address = '00:01:02:03:04:05' - nics = [{'mac': mac_address}] + nics = [{'address': mac_address}] node_uuid = self.fake_baremetal_node['uuid'] node_to_post = { 'chassis_uuid': None, @@ -1291,7 +1292,7 @@ class TestBaremetalNode(base.IronicTestCase): def test_register_machine_enroll_timeout(self): mac_address = '00:01:02:03:04:05' - nics = [{'mac': mac_address}] + nics = [{'address': mac_address}] node_uuid = self.fake_baremetal_node['uuid'] node_to_post = { 'chassis_uuid': None, @@ -1345,7 +1346,7 @@ class TestBaremetalNode(base.IronicTestCase): def test_register_machine_enroll_timeout_wait(self): mac_address = '00:01:02:03:04:05' - nics = [{'mac': mac_address}] + nics = [{'address': mac_address}] node_uuid = self.fake_baremetal_node['uuid'] node_to_post = { 'chassis_uuid': None, @@ -1414,7 +1415,7 @@ class TestBaremetalNode(base.IronicTestCase): def test_register_machine_port_create_failed(self): mac_address = '00:01:02:03:04:05' - nics = [{'mac': mac_address}] + nics = [{'address': mac_address}] node_uuid = self.fake_baremetal_node['uuid'] node_to_post = { 'chassis_uuid': None, @@ -1455,7 +1456,8 @@ class TestBaremetalNode(base.IronicTestCase): def test_register_machine_several_ports_create_failed(self): mac_address = '00:01:02:03:04:05' mac_address2 = mac_address[::-1] - nics = [{'mac': mac_address}, {'mac': mac_address2}] + # Verify a couple of ways to provide MACs + nics = [mac_address, {'mac': mac_address2}] node_uuid = self.fake_baremetal_node['uuid'] node_to_post = { 'chassis_uuid': None,