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
This commit is contained in:
Abhishek Raut 2019-01-22 19:21:04 -08:00
parent ec40ec29c7
commit a0731e2d69
2 changed files with 171 additions and 1 deletions

View File

@ -2361,6 +2361,150 @@ class TestPolicySegment(NsxPolicyLibTestCase):
{'id': segment_id, 'connectivity_path': None, 'subnets': None}) {'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): class TestPolicySegmentProfileBase(NsxPolicyLibTestCase):
def setUp(self, resource_api_name='segment_security_profile', def setUp(self, resource_api_name='segment_security_profile',

View File

@ -2073,9 +2073,11 @@ class NsxPolicyIpPoolApi(NsxPolicyResourceBase):
tags=tags, tags=tags,
tenant=tenant) 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, name=IGNORE, description=IGNORE, tags=IGNORE,
tenant=constants.POLICY_INFRA_TENANT): 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) ip_allocation_id = self._init_obj_uuid(ip_allocation_id)
args = self._get_user_args( args = self._get_user_args(
@ -2155,6 +2157,30 @@ class NsxPolicyIpPoolApi(NsxPolicyResourceBase):
tenant=tenant) tenant=tenant)
return self.policy_api.get(ip_subnet_def) 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): class NsxPolicyCommunicationMapApi(NsxPolicyResourceBase):
"""NSX Policy CommunicationMap (Under a Domain).""" """NSX Policy CommunicationMap (Under a Domain)."""