From a0731e2d69e5adb36973f07cfd65638c5d7ab8d7 Mon Sep 17 00:00:00 2001 From: Abhishek Raut Date: Tue, 22 Jan 2019 19:21:04 -0800 Subject: [PATCH] Add API to retrieve realized info for IpAllocations Add API to retrieve realized info for IpAllocations ability to retrieve the allocated IpAddress from IpPool from the realized information. In addition to the new APIs, make IpAddress as an optional param while allocating IP from IpPool. Change-Id: If12f292a6af60cd0e6abd8441e18f5e2b9efccb9 --- .../tests/unit/v3/policy/test_resources.py | 144 ++++++++++++++++++ vmware_nsxlib/v3/policy/core_resources.py | 28 +++- 2 files changed, 171 insertions(+), 1 deletion(-) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index 8243fd60..e57d442e 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -2361,6 +2361,150 @@ class TestPolicySegment(NsxPolicyLibTestCase): {'id': segment_id, 'connectivity_path': None, 'subnets': None}) +class TestPolicyIpPool(NsxPolicyLibTestCase): + + def setUp(self, *args, **kwargs): + super(TestPolicyIpPool, self).setUp() + self.resourceApi = self.policy_lib.ip_pool + + def test_create(self): + name = 'test' + description = 'desc' + ip_pool_id = '111' + + with mock.patch.object(self.policy_api, + "create_or_update") as api_call: + self.resourceApi.create_or_overwrite( + name, ip_pool_id, description=description, + tenant=TEST_TENANT) + + expected_def = core_defs.IpPoolDef( + ip_pool_id=ip_pool_id, + name=name, + description=description, + tenant=TEST_TENANT) + + self.assert_called_with_def(api_call, expected_def) + + def test_delete(self): + ip_pool_id = '111' + with mock.patch.object(self.policy_api, "delete") as api_call: + self.resourceApi.delete(ip_pool_id, tenant=TEST_TENANT) + expected_def = core_defs.IpPoolDef(ip_pool_id=ip_pool_id, + tenant=TEST_TENANT) + self.assert_called_with_def(api_call, expected_def) + + def test_get(self): + ip_pool_id = '111' + with mock.patch.object(self.policy_api, "get") as api_call: + self.resourceApi.get(ip_pool_id, tenant=TEST_TENANT) + expected_def = core_defs.IpPoolDef(ip_pool_id=ip_pool_id, + tenant=TEST_TENANT) + self.assert_called_with_def(api_call, expected_def) + + def test_list(self): + with mock.patch.object(self.policy_api, "list") as api_call: + self.resourceApi.list(tenant=TEST_TENANT) + expected_def = core_defs.IpPoolDef(tenant=TEST_TENANT) + self.assert_called_with_def(api_call, expected_def) + + def test_update(self): + ip_pool_id = '111' + name = 'new name' + with mock.patch.object(self.policy_api, + "create_or_update") as update_call: + self.resourceApi.update(ip_pool_id, + name=name, + tenant=TEST_TENANT) + expected_def = core_defs.IpPoolDef(ip_pool_id=ip_pool_id, + name=name, + tenant=TEST_TENANT) + self.assert_called_with_def(update_call, expected_def) + + def test_allocate_ip(self): + ip_pool_id = '111' + ip_allocation_id = 'alloc-id' + with mock.patch.object(self.policy_api, + "create_or_update") as update_call: + self.resourceApi.allocate_ip(ip_pool_id, + ip_allocation_id, + tenant=TEST_TENANT) + expected_def = core_defs.IpPoolAllocationDef( + ip_pool_id=ip_pool_id, + ip_allocation_id=ip_allocation_id, + tenant=TEST_TENANT) + self.assert_called_with_def(update_call, expected_def) + + def test_release_ip(self): + ip_pool_id = '111' + ip_allocation_id = 'alloc-id' + with mock.patch.object(self.policy_api, "delete") as delete_call: + self.resourceApi.release_ip(ip_pool_id, + ip_allocation_id, + tenant=TEST_TENANT) + expected_def = core_defs.IpPoolAllocationDef( + ip_pool_id=ip_pool_id, + ip_allocation_id=ip_allocation_id, + tenant=TEST_TENANT) + self.assert_called_with_def(delete_call, expected_def) + + def test_allocate_block_subnet(self): + ip_pool_id = '111' + ip_block_id = 'block-id' + size = 256 + ip_subnet_id = 'subnet-id' + + with mock.patch.object(self.policy_api, + "create_or_update") as api_call: + self.resourceApi.allocate_block_subnet( + ip_pool_id, ip_block_id, size, ip_subnet_id, + tenant=TEST_TENANT) + + expected_def = core_defs.IpPoolBlockSubnetDef( + ip_pool_id=ip_pool_id, + ip_block_id=ip_block_id, + ip_subnet_id=ip_subnet_id, + size=size, + tenant=TEST_TENANT) + self.assert_called_with_def(api_call, expected_def) + + def test_release_block_subnet(self): + ip_pool_id = '111' + ip_subnet_id = 'subnet-id' + with mock.patch.object(self.policy_api, "delete") as delete_call: + self.resourceApi.release_block_subnet(ip_pool_id, + ip_subnet_id, + tenant=TEST_TENANT) + expected_def = core_defs.IpPoolBlockSubnetDef( + ip_pool_id=ip_pool_id, + ip_subnet_id=ip_subnet_id, + tenant=TEST_TENANT) + self.assert_called_with_def(delete_call, expected_def) + + def test_get_ip_alloc_realization_info(self): + ip_pool_id = '111' + ip_allocation_id = 'alloc-id' + result = {'extended_attributes': [{'values': ['5.5.0.8']}]} + with mock.patch.object( + self.resourceApi, "_get_realization_info", + return_value=result) as api_get: + self.resourceApi.get_ip_alloc_realization_info( + ip_pool_id, ip_allocation_id, tenant=TEST_TENANT) + api_get.assert_called_once() + + def test_get_realized_allocated_ip(self): + ip_pool_id = '111' + ip_allocation_id = 'alloc-id' + result = {'extended_attributes': [{'values': ['5.5.0.8']}]} + with mock.patch.object( + self.resourceApi, "_get_realization_info", + return_value=result) as api_get: + ip = self.resourceApi.get_realized_allocated_ip( + ip_pool_id, ip_allocation_id, tenant=TEST_TENANT) + self.assertEqual('5.5.0.8', ip) + api_get.assert_called_once() + + class TestPolicySegmentProfileBase(NsxPolicyLibTestCase): def setUp(self, resource_api_name='segment_security_profile', diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 976a607c..592039b7 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -2073,9 +2073,11 @@ class NsxPolicyIpPoolApi(NsxPolicyResourceBase): tags=tags, tenant=tenant) - def allocate_ip(self, ip_pool_id, ip_address, ip_allocation_id=None, + def allocate_ip(self, ip_pool_id, ip_allocation_id=None, ip_address=IGNORE, name=IGNORE, description=IGNORE, tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): + # If ip_address is not set, a random IP will be allocated + # from the pool. ip_allocation_id = self._init_obj_uuid(ip_allocation_id) args = self._get_user_args( @@ -2155,6 +2157,30 @@ class NsxPolicyIpPoolApi(NsxPolicyResourceBase): tenant=tenant) return self.policy_api.get(ip_subnet_def) + def get_ip_alloc_realization_info(self, ip_pool_id, ip_allocation_id, + entity_type=None, + tenant=constants.POLICY_INFRA_TENANT): + ip_allocation_def = core_defs.IpPoolAllocationDef( + ip_pool_id=ip_pool_id, + ip_allocation_id=ip_allocation_id, + tenant=tenant) + return self._get_realization_info(ip_allocation_def, + entity_type=entity_type) + + def get_realized_allocated_ip(self, ip_pool_id, ip_allocation_id, + entity_type=None, + tenant=constants.POLICY_INFRA_TENANT): + # Retrieve the allocated IpAddress for allocation ID + # Return None in case the IP is not yet allocated + realized_info = self.get_ip_alloc_realization_info( + ip_pool_id, ip_allocation_id, entity_type, tenant) + if realized_info: + try: + return realized_info['extended_attributes'][0].get( + 'values')[0] + except IndexError: + return + class NsxPolicyCommunicationMapApi(NsxPolicyResourceBase): """NSX Policy CommunicationMap (Under a Domain)."""