From c9c0bf02d92137410d287846c9db83454d9bcaaf Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Sun, 21 May 2017 13:32:55 +0300 Subject: [PATCH] New API to get the firewall section of a router Change-Id: I327f0a0f3e6751297afbca1bbeba86da1e9ec0cf --- vmware_nsxlib/tests/unit/v3/test_constants.py | 8 +++++++- vmware_nsxlib/tests/unit/v3/test_resources.py | 12 +++++++++++- vmware_nsxlib/v3/core_resources.py | 15 +++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/vmware_nsxlib/tests/unit/v3/test_constants.py b/vmware_nsxlib/tests/unit/v3/test_constants.py index b0feaba6..0f2f51fd 100644 --- a/vmware_nsxlib/tests/unit/v3/test_constants.py +++ b/vmware_nsxlib/tests/unit/v3/test_constants.py @@ -106,11 +106,17 @@ FAKE_CONTAINER_PORT = { FAKE_ROUTER_UUID = uuidutils.generate_uuid() +FAKE_ROUTER_FW_SEC_UUID = uuidutils.generate_uuid() FAKE_ROUTER = { "resource_type": "LogicalRouter", "revision": 0, "id": FAKE_ROUTER_UUID, - "display_name": FAKE_NAME + "display_name": FAKE_NAME, + "firewall_sections": [{ + "is_valid": True, + "target_type": "FirewallSection", + "target_id": FAKE_ROUTER_FW_SEC_UUID + }], } FAKE_ROUTER_PORT_UUID = uuidutils.generate_uuid() diff --git a/vmware_nsxlib/tests/unit/v3/test_resources.py b/vmware_nsxlib/tests/unit/v3/test_resources.py index ba2aa2f7..d0cff624 100644 --- a/vmware_nsxlib/tests/unit/v3/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/test_resources.py @@ -23,6 +23,7 @@ from vmware_nsxlib.tests.unit.v3 import mocks from vmware_nsxlib.tests.unit.v3 import nsxlib_testcase from vmware_nsxlib.tests.unit.v3 import test_client from vmware_nsxlib.tests.unit.v3 import test_constants +from vmware_nsxlib.v3 import core_resources from vmware_nsxlib.v3 import exceptions from vmware_nsxlib.v3 import nsx_constants from vmware_nsxlib.v3 import resources @@ -434,7 +435,8 @@ class LogicalRouterTestCase(nsxlib_testcase.NsxClientTestCase): def _mocked_lrouter(self, session_response=None): return self.mocked_resource( - resources.LogicalRouter, session_response=session_response) + core_resources.NsxLibLogicalRouter, + session_response=session_response) def test_create_logical_router(self): """Test creating a router returns the correct response and 201 status. @@ -479,6 +481,14 @@ class LogicalRouterTestCase(nsxlib_testcase.NsxClientTestCase): 'delete', router, 'https://1.2.3.4/api/v1/logical-routers/%s?force=True' % uuid) + def test_get_logical_router_fw_section(self): + fake_router = test_constants.FAKE_ROUTER.copy() + + router = self._mocked_lrouter() + section_id = router.get_firewall_section_id( + test_constants.FAKE_ROUTER_UUID, router_body=fake_router) + self.assertEqual(test_constants.FAKE_ROUTER_FW_SEC_UUID, section_id) + class LogicalRouterPortTestCase(nsxlib_testcase.NsxClientTestCase): diff --git a/vmware_nsxlib/v3/core_resources.py b/vmware_nsxlib/v3/core_resources.py index 1df3628c..0e251a12 100644 --- a/vmware_nsxlib/v3/core_resources.py +++ b/vmware_nsxlib/v3/core_resources.py @@ -515,6 +515,21 @@ class NsxLibLogicalRouter(utils.NsxLibApiBase): return _do_update() + def get_firewall_section_id(self, lrouter_id, router_body=None): + """Return the id of the auto created firewall section of the router + + If the router was already retrieved from the backend it is possible + to give it as an input to avoid another backend call. + """ + if not router_body: + router_body = self.get(lrouter_id) + if 'firewall_sections' in router_body: + firewall_sections = router_body['firewall_sections'] + for sec in firewall_sections: + if (sec.get('is_valid') and + sec.get('target_type') == "FirewallSection"): + return firewall_sections[0].get('target_id') + class NsxLibEdgeCluster(utils.NsxLibApiBase):