From c6244351a746c84cd6931164ddc523b378b3a293 Mon Sep 17 00:00:00 2001 From: Ran Gu Date: Wed, 6 Nov 2019 16:56:06 +0800 Subject: [PATCH] Support first_ip for allocate_block_subnet From NSX 3.0.0, NSXT support to allocate a block subnet with speciified start IP within IP block. Change-Id: I44ef7b4e9b0b5bd423a896381179e499a1de0e51 --- .../tests/unit/v3/policy/test_resources.py | 33 +++++++++++++++++-- vmware_nsxlib/v3/policy/core_defs.py | 14 ++++++++ vmware_nsxlib/v3/policy/core_resources.py | 9 +++-- 3 files changed, 50 insertions(+), 6 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index b30d5905..228b338a 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -3811,14 +3811,41 @@ class TestPolicyIpPool(NsxPolicyLibTestCase): ip_block_id = 'block-id' size = 256 ip_subnet_id = 'subnet-id' + start_ip = '192.168.1.0' - with mock.patch.object(self.policy_api, - "create_or_update") as api_call: + with mock.patch.object( + self.policy_api, "create_or_update") as api_call, \ + mock.patch.object(self.resourceApi, 'version', '3.0.0'): self.resourceApi.allocate_block_subnet( ip_pool_id, ip_block_id, size, ip_subnet_id, - tenant=TEST_TENANT) + tenant=TEST_TENANT, start_ip=start_ip) expected_def = core_defs.IpPoolBlockSubnetDef( + nsx_version='3.0.0', + ip_pool_id=ip_pool_id, + ip_block_id=ip_block_id, + ip_subnet_id=ip_subnet_id, + size=size, + tenant=TEST_TENANT, + start_ip=start_ip) + self.assert_called_with_def(api_call, expected_def) + + def test_allocate_block_subnet_with_unsupported_attribute(self): + ip_pool_id = '111' + ip_block_id = 'block-id' + size = 256 + ip_subnet_id = 'subnet-id' + start_ip = '192.168.1.0' + + with mock.patch.object( + self.policy_api, "create_or_update") as api_call, \ + mock.patch.object(self.resourceApi, 'version', '2.5.0'): + self.resourceApi.allocate_block_subnet( + ip_pool_id, ip_block_id, size, ip_subnet_id, + tenant=TEST_TENANT, start_ip=start_ip) + + expected_def = core_defs.IpPoolBlockSubnetDef( + nsx_version='2.5.0', ip_pool_id=ip_pool_id, ip_block_id=ip_block_id, ip_subnet_id=ip_subnet_id, diff --git a/vmware_nsxlib/v3/policy/core_defs.py b/vmware_nsxlib/v3/policy/core_defs.py index 1846e7ee..4f360592 100644 --- a/vmware_nsxlib/v3/policy/core_defs.py +++ b/vmware_nsxlib/v3/policy/core_defs.py @@ -1186,8 +1186,22 @@ class IpPoolBlockSubnetDef(IpPoolSubnetDef): self._set_attr_if_specified( body, 'ip_block_id', body_attr='ip_block_path', value=ip_block_path) + self._set_attr_if_supported(body, 'start_ip') return body + def _version_dependant_attr_supported(self, attr): + if (version.LooseVersion(self.nsx_version) >= + version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): + if attr == 'start_ip': + return True + + LOG.warning( + "Ignoring %s for %s %s: this feature is not supported." + "Current NSX version: %s. Minimum supported version: %s", + attr, self.resource_type, self.attrs.get('name', ''), + self.nsx_version, nsx_constants.NSX_VERSION_3_0_0) + return False + class IpPoolStaticSubnetDef(IpPoolSubnetDef): '''Infra IpPool static subnet''' diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index b25ddb09..2a32238c 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -2701,7 +2701,8 @@ class NsxPolicyIpPoolApi(NsxPolicyResourceBase): def allocate_block_subnet(self, ip_pool_id, ip_block_id, size, ip_subnet_id=None, auto_assign_gateway=IGNORE, name=IGNORE, description=IGNORE, tags=IGNORE, - tenant=constants.POLICY_INFRA_TENANT): + tenant=constants.POLICY_INFRA_TENANT, + start_ip=IGNORE): ip_subnet_id = self._init_obj_uuid(ip_subnet_id) args = self._get_user_args( ip_pool_id=ip_pool_id, @@ -2712,9 +2713,11 @@ class NsxPolicyIpPoolApi(NsxPolicyResourceBase): name=name, description=description, tags=tags, - tenant=tenant) + tenant=tenant, + start_ip=start_ip) - ip_subnet_def = core_defs.IpPoolBlockSubnetDef(**args) + ip_subnet_def = core_defs.IpPoolBlockSubnetDef( + nsx_version=self.version, **args) self._create_or_store(ip_subnet_def) def release_block_subnet(self, ip_pool_id, ip_subnet_id,