From 3d5cbe2f3a69cebe7473d889e14775ab7ce198dd Mon Sep 17 00:00:00 2001 From: Rongrong_Miao Date: Mon, 5 Apr 2021 09:23:00 -0700 Subject: [PATCH] Added Tier 0 static routes Adds Tier 0 static routes API to support dev on NCP side on multi VRF and multi T0 topology Issue: # Jira: # Signed-off-by: Rongrong_Miao Change-Id: I73756350b23dbd8f23c8e22ad84abe93b49831a4 (cherry picked from commit 4dcc68b807a95a8a41afdcf783fe9adf2fccbd24) --- .../tests/unit/v3/policy/test_resources.py | 51 ++++++++++++++- vmware_nsxlib/v3/policy/__init__.py | 2 + vmware_nsxlib/v3/policy/core_resources.py | 62 +++++++++++++++++++ 3 files changed, 114 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 2c52c422..cf0a2fcb 100644 --- a/vmware_nsxlib/tests/unit/v3/policy/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/policy/test_resources.py @@ -7013,5 +7013,54 @@ class TestNsxPolicyObjectRolePermissionGroup(NsxPolicyLibTestCase): path_prefix=path_prefix, 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/__init__.py b/vmware_nsxlib/v3/policy/__init__.py index 805a103e..f519f115 100644 --- a/vmware_nsxlib/v3/policy/__init__.py +++ b/vmware_nsxlib/v3/policy/__init__.py @@ -80,6 +80,8 @@ class NsxPolicyLib(lib.NsxLibBase): self.tier0_prefix_list = core_resources.NsxPolicyTier0PrefixListApi( *args) self.tier0_bgp = core_resources.NsxPolicyTier0BgpApi(*args) + self.tier0_static_route = ( + core_resources.NSXPolicyTier0StaticRouteApi(*args)) self.tier1 = core_resources.NsxPolicyTier1Api(*args) self.tier1_segment = core_resources.NsxPolicyTier1SegmentApi(*args) self.tier1_nat_rule = core_resources.NsxPolicyTier1NatRuleApi( diff --git a/vmware_nsxlib/v3/policy/core_resources.py b/vmware_nsxlib/v3/policy/core_resources.py index 36adf9b0..65f2bcb7 100644 --- a/vmware_nsxlib/v3/policy/core_resources.py +++ b/vmware_nsxlib/v3/policy/core_resources.py @@ -2000,6 +2000,68 @@ class NsxPolicyTier1NatRuleApi(NsxPolicyResourceBase): enabled=enabled) +class NSXPolicyTier0StaticRouteApi(NsxPolicyResourceBase): + + @property + def entry_def(self): + return core_defs.Tier0StaticRoute + + def create_or_overwrite(self, name, tier0_id, + static_route_id=None, + description=IGNORE, + network=IGNORE, + next_hop=IGNORE, + tags=IGNORE, + tenant=constants.POLICY_INFRA_TENANT): + 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, + name=name, + description=description, + network=network, + next_hop=next_hop, + tags=tags, + tenant=tenant) + self._create_or_store(static_route_def) + return static_route_id + + def delete(self, tier0_id, static_route_id, + tenant=constants.POLICY_INFRA_TENANT): + static_route_def = self.entry_def(tier0_id=tier0_id, + static_route_id=static_route_id, + tenant=tenant) + self._delete_with_retry(static_route_def) + + def get(self, tier0_id, static_route_id, + tenant=constants.POLICY_INFRA_TENANT, silent=False): + static_route_def = self.entry_def(tier0_id=tier0_id, + static_route_id=static_route_id, + tenant=tenant) + return self.policy_api.get(static_route_def, silent=silent) + + def list(self, tier0_id, + tenant=constants.POLICY_INFRA_TENANT): + static_route_def = self.entry_def(tier0_id=tier0_id, + tenant=tenant) + return self._list(static_route_def) + + def update(self, tier0_id, static_route_id, + name=IGNORE, + description=IGNORE, + network=IGNORE, + next_hop=IGNORE, + tags=IGNORE, + tenant=constants.POLICY_INFRA_TENANT): + self._update(tier0_id=tier0_id, + static_route_id=static_route_id, + name=name, + description=description, + network=network, + next_hop=next_hop, + tags=tags, + tenant=tenant) + + class NsxPolicyTier1StaticRouteApi(NsxPolicyResourceBase): @property