From b442394c98379ed4cbd9b07b153bd3181c968828 Mon Sep 17 00:00:00 2001 From: Rongrong_Miao Date: Wed, 21 Apr 2021 15:23:30 -0700 Subject: [PATCH] [T0API] Added SCOPE parameter in static route In setting T0 static route, a scope parameter is needed. This patch fixes the problem with previous implementation by adding the scope field in static route definition Issue: # Jira: # Signed-off-by: Rongrong_Miao Change-Id: I9b6e579e8e57e13cb1ba9e797c7348e23e3aaa8f (cherry picked from commit d8596e784e6e6f641c6d195de8cbd7f5af0408fc) --- .../tests/unit/v3/policy/test_resources.py | 105 +++++++++--------- vmware_nsxlib/v3/policy/core_defs.py | 11 +- vmware_nsxlib/v3/policy/core_resources.py | 6 +- 3 files changed, 69 insertions(+), 53 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py index cf0a2fcb..fd82d24d 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -6931,6 +6931,61 @@ class TestPolicyGlobalConfig(NsxPolicyLibTestCase): api_put.assert_not_called() +class TestPolicyTier0StaticRoute(NsxPolicyLibTestCase): + + def setUp(self, *args, **kwargs): + super(TestPolicyTier0StaticRoute, self).setUp() + self.resourceApi = self.policy_lib.tier0_static_route + + def test_create(self): + name = 'test' + description = 'desc' + tier0_id = '111' + static_route_id = '222' + network = '1.1.1.1/24' + nexthop = '2.2.2.2' + scope = 'ContainerT0' + + with mock.patch.object(self.policy_api, + "create_or_update") as api_call: + result = self.resourceApi.create_or_overwrite( + name, tier0_id, + static_route_id=static_route_id, + description=description, + network=network, + next_hop=nexthop, + tenant=TEST_TENANT, + scope=scope + ) + + expected_def = core_defs.Tier0StaticRoute( + tier0_id=tier0_id, + static_route_id=static_route_id, + name=name, + description=description, + network=network, + next_hop=nexthop, + tenant=TEST_TENANT, + scope=scope + ) + self.assert_called_with_def(api_call, expected_def) + self.assertIsNotNone(result) + + def test_delete(self): + tier0_id = '111' + static_route_id = '222' + with mock.patch.object(self.policy_api, "delete") as api_call: + self.resourceApi.delete( + tier0_id, + static_route_id, + tenant=TEST_TENANT) + expected_def = core_defs.Tier0StaticRoute( + tier0_id=tier0_id, + static_route_id=static_route_id, + tenant=TEST_TENANT) + self.assert_called_with_def(api_call, expected_def) + + class TestNsxPolicyObjectRolePermissionGroup(NsxPolicyLibTestCase): def setUp(self, *args, **kwargs): @@ -7014,53 +7069,3 @@ class TestNsxPolicyObjectRolePermissionGroup(NsxPolicyLibTestCase): role_name=role_name, tenant=TEST_TENANT) self.assert_called_with_def(api_call, expected_def) - - -class TestPolicyTier0StaticRoute(NsxPolicyLibTestCase): - - def setUp(self, *args, **kwargs): - super(TestPolicyTier0StaticRoute, self).setUp() - self.resourceApi = self.policy_lib.tier0_static_route - - def test_create(self): - name = 'test' - description = 'desc' - tier0_id = '111' - static_route_id = '222' - network = '1.1.1.1/24' - nexthop = '2.2.2.2' - - with mock.patch.object(self.policy_api, - "create_or_update") as api_call: - result = self.resourceApi.create_or_overwrite( - name, tier0_id, - static_route_id=static_route_id, - description=description, - network=network, - next_hop=nexthop, - tenant=TEST_TENANT) - - expected_def = core_defs.Tier0StaticRoute( - tier0_id=tier0_id, - static_route_id=static_route_id, - name=name, - description=description, - network=network, - next_hop=nexthop, - tenant=TEST_TENANT) - self.assert_called_with_def(api_call, expected_def) - self.assertIsNotNone(result) - - def test_delete(self): - tier0_id = '111' - static_route_id = '222' - with mock.patch.object(self.policy_api, "delete") as api_call: - self.resourceApi.delete( - tier0_id, - static_route_id, - tenant=TEST_TENANT) - expected_def = core_defs.Tier0StaticRoute( - tier0_id=tier0_id, - static_route_id=static_route_id, - tenant=TEST_TENANT) - self.assert_called_with_def(api_call, expected_def) diff --git a/vmware_nsxlib/v3/policy/core_defs.py b/vmware_nsxlib/v3/policy/core_defs.py index 4b5c1403..141384ee 100644 --- a/vmware_nsxlib/v3/policy/core_defs.py +++ b/vmware_nsxlib/v3/policy/core_defs.py @@ -740,8 +740,17 @@ class RouterStaticRoute(ResourceDef): # next hops if self.has_attr('next_hop'): + next_hops = [] + param_dict = dict() next_hop = self.get_attr('next_hop') - next_hops = [{'ip_address': next_hop}] + param_dict['ip_address'] = next_hop + + # scope parameter + if self.has_attr('scope'): + scope_path = self.get_attr('scope') + param_dict['scope'] = [scope_path] + + next_hops.append(param_dict) self._set_attr_if_specified(body, 'next_hop', body_attr='next_hops', value=next_hops) diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 65f2bcb7..29815516 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -2012,7 +2012,8 @@ class NSXPolicyTier0StaticRouteApi(NsxPolicyResourceBase): network=IGNORE, next_hop=IGNORE, tags=IGNORE, - tenant=constants.POLICY_INFRA_TENANT): + tenant=constants.POLICY_INFRA_TENANT, + scope=IGNORE): static_route_id = self._init_obj_uuid(static_route_id) static_route_def = self._init_def(tier0_id=tier0_id, static_route_id=static_route_id, @@ -2021,7 +2022,8 @@ class NSXPolicyTier0StaticRouteApi(NsxPolicyResourceBase): network=network, next_hop=next_hop, tags=tags, - tenant=tenant) + tenant=tenant, + scope=scope) self._create_or_store(static_route_def) return static_route_id