Skip to content

Commit

Permalink
Merge pull request #1350 from grycap/devel
Browse files Browse the repository at this point in the history
Use PRIVATE_NET_MASKS in Azure conn
  • Loading branch information
micafer authored Mar 24, 2022
2 parents 736af22 + 6249c81 commit 1e49dce
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
11 changes: 8 additions & 3 deletions IM/connectors/Azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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")
Expand Down
11 changes: 11 additions & 0 deletions test/unit/connectors/Azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

from distutils.command.config import config
import sys
import unittest

Expand All @@ -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):
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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')
Expand Down

0 comments on commit 1e49dce

Please sign in to comment.