From 312bba313681f408461832a8e4fe43b4de56ede0 Mon Sep 17 00:00:00 2001 From: Venkata Harshavardhan Reddy Allu Date: Thu, 21 Mar 2019 20:56:23 +0530 Subject: 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 --- sfc/lib/openstack_utils.py | 17 ++++++++++++++ sfc/unit_tests/unit/lib/test_openstack_utils.py | 30 +++++++++++++++++++++++++ 2 files changed, 47 insertions(+) diff --git a/sfc/lib/openstack_utils.py b/sfc/lib/openstack_utils.py index 59ccdaca..c46ff123 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 aa7ef123..bdd53d36 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) -- cgit 1.2.3-korg