diff --git a/IM/connectors/Azure.py b/IM/connectors/Azure.py index 0babbd9a6..3d4307a9a 100644 --- a/IM/connectors/Azure.py +++ b/IM/connectors/Azure.py @@ -422,10 +422,9 @@ def create_nics(self, radl, credentials, subscription_id, group_name, subnets, n hasPublicIP = False hasPrivateIP = False pub_network_name = None + publicAdded = False while system.getValue("net_interface." + str(i) + ".connection"): network_name = system.getValue("net_interface." + str(i) + ".connection") - # TODO: check how to do that - # fixed_ip = system.getValue("net_interface." + str(i) + ".ip") network = radl.get_network_by_id(network_name) if network.isPublic(): @@ -434,11 +433,17 @@ def create_nics(self, radl, credentials, subscription_id, group_name, subnets, n else: hasPrivateIP = True + if not publicAdded and network_name in subnets: + subnet_network_mask = IPNetwork(subnets[network_name].address_prefix) + is_private = any([IPAddress(subnet_network_mask.ip) in IPNetwork(mask) + for mask in Config.PRIVATE_NET_MASKS]) + if not is_private: + publicAdded = True + i += 1 i = 0 res = [] - publicAdded = False while system.getValue("net_interface." + str(i) + ".connection"): network_name = system.getValue("net_interface." + str(i) + ".connection") fixed_ip = system.getValue("net_interface." + str(i) + ".ip") diff --git a/test/unit/connectors/Azure.py b/test/unit/connectors/Azure.py index 98a82c619..f88b27681 100755 --- a/test/unit/connectors/Azure.py +++ b/test/unit/connectors/Azure.py @@ -16,6 +16,7 @@ # You should have received a copy of the GNU General Public License # along with this program. If not, see . +from distutils.command.config import config import sys import unittest @@ -30,6 +31,7 @@ from IM.connectors.Azure import AzureCloudConnector from azure.core.exceptions import ResourceNotFoundError from mock import patch, MagicMock, call +from IM.config import Config class TestAzureConnector(TestCloudConnectorBase): @@ -165,6 +167,7 @@ def test_20_launch(self, save_data, credentials, network_client, compute_client, subnet_create = MagicMock() subnet_create_res = MagicMock() subnet_create_res.id = "subnet-0" + subnet_create_res.address_prefix = "10.0.1.0/24" subnet_create.result.return_value = subnet_create_res nclient.subnets.begin_create_or_update.return_value = subnet_create nclient.subnets.get.side_effect = ResourceNotFoundError() @@ -308,6 +311,14 @@ def test_20_launch(self, save_data, credentials, network_client, compute_client, self.assertEqual(nclient.subnets.get.call_args_list[3][0][2], 'subnet1') self.assertEqual(nclient.subnets.begin_create_or_update.call_count, 3) self.assertEqual(nclient.virtual_networks.begin_create_or_update.call_count, 3) + self.assertEqual(nclient.public_ip_addresses.begin_create_or_update.call_count, 7) + + old_priv = Config.PRIVATE_NET_MASKS + Config.PRIVATE_NET_MASKS = ["172.16.0.0/12", "192.168.0.0/16"] + res = azure_cloud.launch(inf, radl, radl, 1, auth) + Config.PRIVATE_NET_MASKS = old_priv + # Check that public ip is not created + self.assertEqual(nclient.public_ip_addresses.begin_create_or_update.call_count, 7) @patch('IM.connectors.Azure.NetworkManagementClient') @patch('IM.connectors.Azure.ComputeManagementClient')