From a0de2475770bdfda9c2c639a02d5838e634beaa6 Mon Sep 17 00:00:00 2001 From: asarfaty Date: Tue, 14 Jan 2020 11:14:04 +0200 Subject: [PATCH] Support policy segment & port admin state Change-Id: I1254d4bccec44c1b7fa65c106a1261e00c57f2f2 --- .../tests/unit/v3/policy/test_resources.py | 22 ++++++++++-- vmware_nsxlib/v3/nsx_constants.py | 1 + vmware_nsxlib/v3/policy/__init__.py | 3 +- vmware_nsxlib/v3/policy/core_defs.py | 35 +++++++++++++++++-- vmware_nsxlib/v3/policy/core_resources.py | 28 ++++++++++++--- 5 files changed, 77 insertions(+), 12 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index 72f3f29b..446dcaf7 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -3736,7 +3736,7 @@ class TestPolicySegment(NsxPolicyLibTestCase): self.resourceApi = self.policy_lib.segment def _test_create(self, tier1_id=None, tier0_id=None, mdproxy=None, - dhcp_server=None): + dhcp_server=None, admin_state=None): name = 'test' description = 'desc' subnets = [core_defs.Subnet(gateway_address="2.2.2.0/24")] @@ -3755,11 +3755,15 @@ class TestPolicySegment(NsxPolicyLibTestCase): if dhcp_server: kwargs['dhcp_server_config_id'] = dhcp_server + if admin_state: + kwargs['admin_state'] = admin_state with mock.patch.object(self.policy_api, "create_or_update") as api_call: result = self.resourceApi.create_or_overwrite(name, **kwargs) + if admin_state: + kwargs['admin_state'] = admin_state if 'UP' else 'DOWN' expected_def = core_defs.SegmentDef( nsx_version='3.0.0', segment_id=mock.ANY, @@ -3786,6 +3790,12 @@ class TestPolicySegment(NsxPolicyLibTestCase): def test_create_with_dhcp_server_config(self): self._test_create(dhcp_server='dhcp1') + def test_create_with_admin_state_up(self): + self._test_create(admin_state=True) + + def test_create_with_admin_state_down(self): + self._test_create(admin_state=False) + def test_delete(self): segment_id = '111' with mock.patch.object(self.policy_api, "delete") as api_call: @@ -3815,14 +3825,18 @@ class TestPolicySegment(NsxPolicyLibTestCase): def test_update(self): segment_id = '111' name = 'new name' + admin_state = False with self.mock_get(segment_id, name), \ self.mock_create_update() as update_call: self.resourceApi.update(segment_id, name=name, + admin_state=admin_state, tenant=TEST_TENANT) - expected_def = core_defs.SegmentDef(segment_id=segment_id, + expected_def = core_defs.SegmentDef(nsx_version='3.0.0', + segment_id=segment_id, name=name, + admin_state=admin_state, tenant=TEST_TENANT) self.assert_called_with_def(update_call, expected_def) @@ -4235,6 +4249,7 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): allocate_addresses = "BOTH" tags = [{'scope': 'a', 'tag': 'b'}] hyperbus_mode = 'DISABLE' + admin_state = True with mock.patch.object( self.policy_api, "create_or_update") as api_call, \ @@ -4245,7 +4260,7 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): attachment_type=attachment_type, vif_id=vif_id, app_id=app_id, context_id=context_id, traffic_tag=traffic_tag, allocate_addresses=allocate_addresses, - hyperbus_mode=hyperbus_mode, + hyperbus_mode=hyperbus_mode, admin_state=admin_state, tags=tags, tenant=TEST_TENANT) @@ -4262,6 +4277,7 @@ class TestPolicySegmentPort(NsxPolicyLibTestCase): context_id=context_id, traffic_tag=traffic_tag, allocate_addresses=allocate_addresses, + admin_state=admin_state, tags=tags, tenant=TEST_TENANT, hyperbus_mode=hyperbus_mode) diff --git a/vmware_nsxlib/v3/nsx_constants.py b/vmware_nsxlib/v3/nsx_constants.py index 94403ba6..43911d1e 100644 --- a/vmware_nsxlib/v3/nsx_constants.py +++ b/vmware_nsxlib/v3/nsx_constants.py @@ -180,6 +180,7 @@ FEATURE_NSX_POLICY_NETWORKING = 'NSX Policy Networking' FEATURE_NSX_POLICY_MDPROXY = 'NSX Policy Metadata Proxy' FEATURE_NSX_POLICY_DHCP = 'NSX Policy DHCP' FEATURE_NSX_POLICY_GLOBAL_CONFIG = 'NSX Policy Global Config' +FEATURE_NSX_POLICY_ADMIN_STATE = 'NSX Policy Segment admin state' # FEATURE available depending on Inventory service backend version FEATURE_CONTAINER_CLUSTER_INVENTORY = 'Container Cluster Inventory' diff --git a/vmware_nsxlib/v3/policy/__init__.py b/vmware_nsxlib/v3/policy/__init__.py index 53e2a9b1..a24f684a 100644 --- a/vmware_nsxlib/v3/policy/__init__.py +++ b/vmware_nsxlib/v3/policy/__init__.py @@ -37,7 +37,6 @@ class NsxPolicyLib(lib.NsxLibBase): def init_api(self): # Initialize the policy client - # TODO(annak): move the API class to separate file self.policy_api = core_defs.NsxPolicyApi(self.client) # NSX manager api will be used as a pass-through for apis which are @@ -187,6 +186,8 @@ class NsxPolicyLib(lib.NsxLibBase): return True if (feature == nsx_constants.FEATURE_NSX_POLICY_GLOBAL_CONFIG): return True + if feature == nsx_constants.FEATURE_NSX_POLICY_ADMIN_STATE: + return True return (feature == nsx_constants.FEATURE_NSX_POLICY) diff --git a/vmware_nsxlib/v3/policy/core_defs.py b/vmware_nsxlib/v3/policy/core_defs.py index b65d07bc..6a434757 100644 --- a/vmware_nsxlib/v3/policy/core_defs.py +++ b/vmware_nsxlib/v3/policy/core_defs.py @@ -864,9 +864,9 @@ class SegmentDef(BaseSegmentDef): def _version_dependant_attr_supported(self, attr): if (version.LooseVersion(self.nsx_version) >= version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): - if attr == 'metadata_proxy_id': - return True - if attr == 'dhcp_server_config_id': + if attr in ('metadata_proxy_id', + 'dhcp_server_config_id', + 'admin_state'): return True else: LOG.warning( @@ -937,6 +937,15 @@ class SegmentDef(BaseSegmentDef): body_attr='dhcp_config_path', value=path) + if (self.has_attr('admin_state') and + self._version_dependant_attr_supported('admin_state')): + if self.get_attr('admin_state'): + admin_state = nsx_constants.ADMIN_STATE_UP + else: + admin_state = nsx_constants.ADMIN_STATE_DOWN + self._set_attr_if_specified(body, 'admin_state', + value=admin_state) + return body @@ -987,6 +996,15 @@ class DhcpV6StaticBindingConfig(DhcpV4StaticBindingConfig): 'ip_addresses', 'sntp_servers', 'preferred_time']) + if (self.has_attr('admin_state') and + self._version_dependant_attr_supported('admin_state')): + if self.get_attr('admin_state'): + admin_state = nsx_constants.ADMIN_STATE_UP + else: + admin_state = nsx_constants.ADMIN_STATE_DOWN + self._set_attr_if_specified(body, 'admin_state', + value=admin_state) + return body @@ -1048,6 +1066,15 @@ class SegmentPortDef(ResourceDef): self._set_attr_if_supported(body, 'hyperbus_mode') body['attachment'] = attachment + if (self.has_attr('admin_state') and + self._version_dependant_attr_supported('admin_state')): + if self.get_attr('admin_state'): + admin_state = nsx_constants.ADMIN_STATE_UP + else: + admin_state = nsx_constants.ADMIN_STATE_DOWN + self._set_attr_if_specified(body, 'admin_state', + value=admin_state) + return body def _version_dependant_attr_supported(self, attr): @@ -1055,6 +1082,8 @@ class SegmentPortDef(ResourceDef): version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): if attr == 'hyperbus_mode': return True + if attr == 'admin_state': + return True LOG.warning( "Ignoring %s for %s %s: this feature is not supported." diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 2cecea30..2c1efc19 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -15,6 +15,7 @@ # import abc +from distutils import version import sys import decorator @@ -1869,6 +1870,7 @@ class NsxPolicySegmentApi(NsxPolicyResourceBase): ip_pool_id=IGNORE, metadata_proxy_id=IGNORE, dhcp_server_config_id=IGNORE, + admin_state=IGNORE, tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): @@ -1891,6 +1893,7 @@ class NsxPolicySegmentApi(NsxPolicyResourceBase): ip_pool_id=ip_pool_id, metadata_proxy_id=metadata_proxy_id, dhcp_server_config_id=dhcp_server_config_id, + admin_state=admin_state, tags=tags, tenant=tenant) self._create_or_store(segment_def) @@ -1921,9 +1924,9 @@ class NsxPolicySegmentApi(NsxPolicyResourceBase): def update(self, segment_id, name=IGNORE, description=IGNORE, tier1_id=IGNORE, tier0_id=IGNORE, subnets=IGNORE, dns_domain_name=IGNORE, - vlan_ids=IGNORE, tags=IGNORE, metadata_proxy_id=IGNORE, - dhcp_server_config_id=IGNORE, - tenant=constants.POLICY_INFRA_TENANT): + vlan_ids=IGNORE, metadata_proxy_id=IGNORE, + dhcp_server_config_id=IGNORE, admin_state=IGNORE, + tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): self._update(segment_id=segment_id, name=name, @@ -1935,6 +1938,7 @@ class NsxPolicySegmentApi(NsxPolicyResourceBase): vlan_ids=vlan_ids, metadata_proxy_id=metadata_proxy_id, dhcp_server_config_id=dhcp_server_config_id, + admin_state=admin_state, tags=tags, tenant=tenant) @@ -2007,7 +2011,12 @@ class NsxPolicySegmentApi(NsxPolicyResourceBase): @check_allowed_passthrough def set_admin_state(self, segment_id, admin_state, tenant=constants.POLICY_INFRA_TENANT): - """Set the segment admin state using the passthrough api""" + """Set the segment admin state using the passthrough/policy api""" + if (version.LooseVersion(self.version) >= + version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): + return self.update(segment_id, admin_state=admin_state, + tenant=tenant) + realization_info = self.wait_until_realized( segment_id, entity_type='RealizedLogicalSwitch', tenant=tenant) @@ -2049,6 +2058,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): traffic_tag=IGNORE, allocate_addresses=IGNORE, hyperbus_mode=IGNORE, + admin_state=IGNORE, tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): @@ -2065,6 +2075,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): traffic_tag=traffic_tag, allocate_addresses=allocate_addresses, hyperbus_mode=hyperbus_mode, + admin_state=admin_state, tags=tags, tenant=tenant) self._create_or_store(port_def) @@ -2094,6 +2105,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): description=IGNORE, address_bindings=IGNORE, hyperbus_mode=IGNORE, + admin_state=IGNORE, tags=IGNORE, tenant=constants.POLICY_INFRA_TENANT): @@ -2103,6 +2115,7 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): description=description, address_bindings=address_bindings, hyperbus_mode=hyperbus_mode, + admin_state=admin_state, tags=tags, tenant=tenant) @@ -2181,7 +2194,12 @@ class NsxPolicySegmentPortApi(NsxPolicyResourceBase): @check_allowed_passthrough def set_admin_state(self, segment_id, port_id, admin_state, tenant=constants.POLICY_INFRA_TENANT): - """Set the segment port admin state using the passthrough api""" + """Set the segment port admin state using the passthrough/policy api""" + if (version.LooseVersion(self.version) >= + version.LooseVersion(nsx_constants.NSX_VERSION_3_0_0)): + return self.update(segment_id, port_id, admin_state=admin_state, + tenant=tenant) + realization_info = self.wait_until_realized( segment_id, port_id, entity_type='RealizedLogicalPort', tenant=tenant)