aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVenkata Harshavardhan Reddy Allu <venkataharshavardhan_ven@srmuniv.edu.in>2019-03-21 20:56:23 +0530
committerManuel Buil <mbuil@suse.com>2019-03-22 14:12:02 +0000
commit312bba313681f408461832a8e4fe43b4de56ede0 (patch)
treefc80ccd7d7adede99769de4f795e3a7502f2f5a2
parent06ed156d9a9eb6cf8f61e7a7b3d9d35f30e6d755 (diff)
Add new methods to openstack_utils
When we use OSM as MANO_COMPONENT, we don't have methods to retrieve the object instances of ports and VMs. So, we need these methods to gather details of a : - VM using the 'id' get_instance(id) - port using their 'ip_address' get_port_by_ip(ip_address) Change-Id: Ica45bf49dddb38a3811b049d67c363ac606fde7a Signed-off-by: Venkata Harshavardhan Reddy Allu <venkataharshavardhan_ven@srmuniv.edu.in>
-rw-r--r--sfc/lib/openstack_utils.py17
-rw-r--r--sfc/unit_tests/unit/lib/test_openstack_utils.py30
2 files changed, 47 insertions, 0 deletions
diff --git a/sfc/lib/openstack_utils.py b/sfc/lib/openstack_utils.py
index 59ccdac..c46ff12 100644
--- a/sfc/lib/openstack_utils.py
+++ b/sfc/lib/openstack_utils.py
@@ -188,6 +188,12 @@ class OpenStackSFC:
return instance, port_list
+ def get_instance(self, instance_id):
+ """
+ Return a dictionary of metadata for a server instance
+ """
+ return self.conn.compute.get_server_metadata(instance_id)
+
def get_av_zones(self):
'''
Return the availability zone each host belongs to
@@ -237,6 +243,17 @@ class OpenStackSFC:
raise Exception("There is no VM with name '{}'!!".format(vm_name))
+ def get_port_by_ip(self, ip_address):
+ """
+ Return a dictionary of metadata for a port instance
+ by its ip_address
+ """
+
+ ports = self.conn.network.ports()
+ for port in ports:
+ if port.fixed_ips[0]['ip_address'] == ip_address:
+ return self.conn.network.get_port(port.id)
+
def assign_floating_ip(self, vm, vm_port):
'''
Assign floating ips to all the VMs
diff --git a/sfc/unit_tests/unit/lib/test_openstack_utils.py b/sfc/unit_tests/unit/lib/test_openstack_utils.py
index aa7ef12..bdd53d3 100644
--- a/sfc/unit_tests/unit/lib/test_openstack_utils.py
+++ b/sfc/unit_tests/unit/lib/test_openstack_utils.py
@@ -531,6 +531,20 @@ class SfcOpenStackUtilsTesting(unittest.TestCase):
self.os_sfc.creators)
mock_log.info.assert_has_calls(log_calls)
+ def test_get_instance(self):
+ """
+ Checks the proper functionality of get_instance function
+ """
+
+ mock_instance_id = 'instance-abyz'
+ mock_instance = Mock()
+ mock_instance.id = mock_instance_id
+ mock_instance.name = 'test-instance'
+ mock_instance.hypervisor_hostname = 'nova-abyz'
+ self.conn.compute.get_server_metadata.return_value = mock_instance
+ result = self.os_sfc.get_instance(mock_instance_id)
+ self.assertEqual(result, mock_instance)
+
@patch.object(os_sfc_utils.OpenStackSFC, 'get_hypervisor_hosts')
def test_get_av_zones(self, mock_hosts):
"""
@@ -630,6 +644,22 @@ class SfcOpenStackUtilsTesting(unittest.TestCase):
result = self.os_sfc.get_vm_compute('dev_vm')
self.assertEqual('mock_host', result)
+ def test_get_port_by_ip(self):
+ """
+ Checks the proper functonality of get_port_by_ip function
+ """
+
+ mock_port_ip_address = 'e.f.g.h'
+ mock_port_one, mock_port_two = Mock(), Mock()
+ mock_port_one.id = 'port-abcd'
+ mock_port_two.id = 'port-efgz'
+ mock_port_one.fixed_ips = [{'ip_address': 'a.b.c.d'}]
+ mock_port_two.fixed_ips = [{'ip_address': 'e.f.g.h'}]
+ self.conn.network.ports.return_value = [mock_port_one, mock_port_two]
+ self.conn.network.get_port.return_value = mock_port_two
+ result = self.os_sfc.get_port_by_ip(mock_port_ip_address)
+ self.assertEqual(result, mock_port_two)
+
@patch('sfc.lib.openstack_utils.logger', autospec=True)
@patch('sfc.lib.openstack_utils.cr_inst.OpenStackVmInstance',
autospec=True)