From 902e400e8f0cb45b23931062e9fa7bf507de8d24 Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Wed, 29 May 2024 16:33:19 +0200 Subject: [PATCH 1/2] Remove private nets in case of no floating pools --- IM/connectors/OpenStack.py | 21 ++++++++++++++++++ requirements-tests.txt | 2 +- setup.py | 2 +- test/unit/connectors/OpenStack.py | 37 +++++++++++++++++++++++++++++++ 4 files changed, 60 insertions(+), 2 deletions(-) diff --git a/IM/connectors/OpenStack.py b/IM/connectors/OpenStack.py index b5f0c7eb..fce63032 100644 --- a/IM/connectors/OpenStack.py +++ b/IM/connectors/OpenStack.py @@ -1305,6 +1305,12 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data): self.log_info("No private networks found. Force the creation.") self.set_nets_to_create(radl) + # TODO: + if not driver.ex_list_floating_ip_pools(): + self.log_info("No floating IP pools found. Avoid the public VMs" + " to be attached to a private one.") + self.remove_private_nets(radl) + with inf._lock: self.create_networks(driver, radl, inf) @@ -1412,6 +1418,21 @@ def get_private_nets(self, driver): res.append(net) return res + @staticmethod + def remove_private_nets(radl): + """ + Remove the private networks + """ + if radl.hasPublicNet(radl.systems[0].name): + i = 0 + while radl.systems[0].getValue("net_interface." + str(i) + ".connection"): + net_name = radl.systems[0].getValue("net_interface." + str(i) + ".connection") + network = radl.get_network_by_id(net_name) + if not network.isPublic(): + radl.systems[0].remove_net_interface(i) + else: + i += 1 + @staticmethod def set_nets_to_create(radl): """ diff --git a/requirements-tests.txt b/requirements-tests.txt index 19525766..e08ffc8c 100644 --- a/requirements-tests.txt +++ b/requirements-tests.txt @@ -5,7 +5,7 @@ PyYAML cheroot boto >= 2.29 apache-libcloud >= 3.3.1 -RADL >= 1.1.6 +RADL >= 1.3.3 bottle netaddr requests >= 2.19 diff --git a/setup.py b/setup.py index 9727677c..dfb350ae 100644 --- a/setup.py +++ b/setup.py @@ -68,7 +68,7 @@ description="IM is a tool to manage virtual infrastructures on Cloud deployments", platforms=["any"], install_requires=["ansible >=2.4", "paramiko >= 1.14", "PyYAML", suds_pkg, sqlite_pkg, "cheroot", - "boto >= 2.29", "apache-libcloud >= 3.2.0", "RADL >= 1.1.5", "bottle", "netaddr", + "boto >= 2.29", "apache-libcloud >= 3.2.0", "RADL >= 1.3.3", "bottle", "netaddr", "requests >= 2.19", "scp", "tosca-parser", 'defusedxml', 'urllib3>=1.23', 'hvac', 'psutil', 'scar', 'requests-cache >= 1.0.0', 'packaging'] ) diff --git a/test/unit/connectors/OpenStack.py b/test/unit/connectors/OpenStack.py index 70c693ff..dadc9db2 100644 --- a/test/unit/connectors/OpenStack.py +++ b/test/unit/connectors/OpenStack.py @@ -1055,6 +1055,43 @@ def test_get_driver(self, get_driver): ost_cloud.get_driver(auth) self.assertEqual(get_driver.call_count, 2) + def test_remove_private_nets(self): + radl_data = """ + network net2 (outbound = 'yes') + network net1 () + system test ( + net_interface.0.connection = 'net1' and + net_interface.1.connection = 'net2' and + net_interface.1.dns_name = 'test' and + disk.0.os.name = 'linux' + )""" + radl = radl_parse.parse_radl(radl_data) + OpenStackCloudConnector.remove_private_nets(radl) + + self.assertEqual(radl.systems[0].getValue('net_interface.0.connection'), 'net2') + self.assertEqual(radl.systems[0].getValue('net_interface.0.dns_name'), 'test') + self.assertIsNone(radl.systems[0].getValue('net_interface.1.connection')) + self.assertIsNone(radl.systems[0].getValue('net_interface.1.dns_name')) + + radl_data = """ + network net1 (outbound = 'yes') + network net2 () + network net3 () + network net4 (outbound = 'yes') + system test ( + net_interface.0.connection = 'net1' and + net_interface.1.connection = 'net2' and + net_interface.2.connection = 'net3' and + net_interface.3.connection = 'net4' and + disk.0.os.name = 'linux' + )""" + radl = radl_parse.parse_radl(radl_data) + OpenStackCloudConnector.remove_private_nets(radl) + + self.assertEqual(radl.systems[0].getValue('net_interface.1.connection'), 'net4') + self.assertIsNone(radl.systems[0].getValue('net_interface.2.connection')) + self.assertIsNone(radl.systems[0].getValue('net_interface.3.connection')) + if __name__ == '__main__': unittest.main() From 0caa0464cda0473ffb5891a0906fae7168adb45f Mon Sep 17 00:00:00 2001 From: Miguel Caballer Date: Thu, 30 May 2024 08:22:53 +0200 Subject: [PATCH 2/2] remove TODO --- IM/connectors/OpenStack.py | 1 - 1 file changed, 1 deletion(-) diff --git a/IM/connectors/OpenStack.py b/IM/connectors/OpenStack.py index fce63032..8c8ea47b 100644 --- a/IM/connectors/OpenStack.py +++ b/IM/connectors/OpenStack.py @@ -1305,7 +1305,6 @@ def launch(self, inf, radl, requested_radl, num_vm, auth_data): self.log_info("No private networks found. Force the creation.") self.set_nets_to_create(radl) - # TODO: if not driver.ex_list_floating_ip_pools(): self.log_info("No floating IP pools found. Avoid the public VMs" " to be attached to a private one.")