Skip to content

Commit

Permalink
Merge pull request #1352 from grycap/devel
Browse files Browse the repository at this point in the history
Devel
  • Loading branch information
micafer authored Mar 25, 2022
2 parents 73c8994 + 823ea98 commit 52b7efd
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 6 deletions.
2 changes: 1 addition & 1 deletion IM/VirtualMachine.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ def add_public_net(radl):

return public_net, num_net

def setIps(self, public_ips, private_ips, remove_old=False, ignore_nets=None):
def setIps(self, public_ips, private_ips, remove_old=True, ignore_nets=None):
"""
Set the specified IPs in the VM RADL info
"""
Expand Down
10 changes: 8 additions & 2 deletions IM/connectors/EC2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import time
import requests
from netaddr import IPNetwork, spanning_cidr
from netaddr import IPNetwork, IPAddress, spanning_cidr

try:
import boto.ec2
Expand Down Expand Up @@ -1031,7 +1031,13 @@ def setIPsFromInstance(self, vm, instance):
num_nets += 1
num_pub_nets = 1
if instance.private_ip_address is not None and len(instance.private_ip_address) > 0:
private_ips = [instance.private_ip_address]
is_private = any([IPAddress(instance.private_ip_address) in IPNetwork(mask)
for mask in Config.PRIVATE_NET_MASKS])
if is_private:
private_ips = [instance.private_ip_address]
else:
public_ips = [instance.private_ip_address]
num_pub_nets += 1
num_nets += 1

vm.setIps(public_ips, private_ips)
Expand Down
2 changes: 1 addition & 1 deletion IM/connectors/OCCI.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def setIPs(self, vm, occi_res, auth_data, auth_header):
not public_ips and vm.requested_radl.hasPublicNet(vm.info.systems[0].name)):
self.manage_public_ips(vm, auth_data, auth_header)

vm.setIps(public_ips, private_ips, remove_old=True)
vm.setIps(public_ips, private_ips)

@staticmethod
def get_property_from_category(occi_res, category, prop_name):
Expand Down
4 changes: 2 additions & 2 deletions IM/connectors/OpenStack.py
Original file line number Diff line number Diff line change
Expand Up @@ -730,7 +730,7 @@ def setIPsFromInstance(self, vm, node):
priv_ips.append(ipu)
else:
pub_ips.append(ipu)
vm.setIps(pub_ips, priv_ips)
vm.setIps(pub_ips, priv_ips, False)

else:
# if addresses are not available use the old method
Expand All @@ -741,7 +741,7 @@ def setIPsFromInstance(self, vm, node):
private_ips.append(ip)
else:
public_ips.append(ip)
vm.setIps(public_ips, private_ips, True)
vm.setIps(public_ips, private_ips)

if vm.state == VirtualMachine.RUNNING:
if self.add_public_ip_count < self.MAX_ADD_IP_COUNT:
Expand Down
9 changes: 9 additions & 0 deletions test/unit/connectors/Azure.py
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,15 @@ def test_30_updateVMInfo(self, credentials, dns_client, compute_client, network_
{'arecords': [{'ipv4_address': '13.0.0.1'}], 'ttl': 300})])
self.assertNotIn("ERROR", self.log.getvalue(), msg="ERROR found in log: %s" % self.log.getvalue())

# Test using PRIVATE_NET_MASKS setting 10.0.0.0/8 as public net
old_priv = Config.PRIVATE_NET_MASKS
Config.PRIVATE_NET_MASKS = ["172.16.0.0/12", "192.168.0.0/16"]
ip_conf.public_ip_address = None
success, vm = azure_cloud.updateVMInfo(vm, auth)
Config.PRIVATE_NET_MASKS = old_priv
self.assertEqual(vm.getPublicIP(), "10.0.0.1")
self.assertEqual(vm.getPrivateIP(), None)

@patch('IM.connectors.Azure.ComputeManagementClient')
@patch('IM.connectors.Azure.ClientSecretCredential')
def test_40_stop(self, credentials, compute_client):
Expand Down
12 changes: 12 additions & 0 deletions test/unit/connectors/EC2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
from IM.VirtualMachine import VirtualMachine
from IM.InfrastructureInfo import InfrastructureInfo
from IM.connectors.EC2 import EC2CloudConnector
from IM.config import Config
from mock import patch, MagicMock, call


Expand Down Expand Up @@ -406,6 +407,17 @@ def test_30_updateVMInfo(self, record_sets, connect_to_region, get_connection):
self.assertEqual(change.add_value.call_args_list, [call('158.42.1.1')])
self.assertEqual(conn.create_route.call_args_list, [call('routet-id', '10.0.10.0/24', instance_id='int-id')])

# Test using PRIVATE_NET_MASKS setting 10.0.0.0/8 as public net
old_priv = Config.PRIVATE_NET_MASKS
Config.PRIVATE_NET_MASKS = ["172.16.0.0/12", "192.168.0.0/16"]
instance.ip_address = None
instance.private_ip_address = "10.0.0.1"
conn.get_all_addresses.return_value = []
success, vm = ec2_cloud.updateVMInfo(vm, auth)
Config.PRIVATE_NET_MASKS = old_priv
self.assertEqual(vm.getPublicIP(), "10.0.0.1")
self.assertEqual(vm.getPrivateIP(), None)

@patch('IM.connectors.EC2.EC2CloudConnector.get_connection')
def test_30_updateVMInfo_spot(self, get_connection):
radl_data = """
Expand Down

0 comments on commit 52b7efd

Please sign in to comment.