From 63dbcea970a26a6f6d627de3dc8098a6b3c85880 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Sun, 17 Sep 2017 10:07:17 +0300 Subject: [PATCH] Update update_advertisement depending on NSX version Using the load balancing related args for the router update_advertisement should be done only if the NSX version supports the load balancing feature. Change-Id: I1a94ee58e6b8908e6122a69f6515dfdf2d2c1b28 --- vmware_nsxlib/tests/unit/v3/test_resources.py | 46 +++++++++++++++++++ vmware_nsxlib/v3/core_resources.py | 12 +++++ 2 files changed, 58 insertions(+) diff --git a/vmware_nsxlib/tests/unit/v3/test_resources.py b/vmware_nsxlib/tests/unit/v3/test_resources.py index b3772428..473574b4 100644 --- a/vmware_nsxlib/tests/unit/v3/test_resources.py +++ b/vmware_nsxlib/tests/unit/v3/test_resources.py @@ -646,6 +646,52 @@ class LogicalRouterTestCase(BaseTestResource): (router_id, rule_id)), headers=self.default_headers()) + def test_update_advertisement(self): + router = self.get_mocked_resource() + router_id = test_constants.FAKE_ROUTER_UUID + data = {'advertise_nat_routes': 'a', + 'advertise_nsx_connected_routes': 'b', + 'advertise_static_routes': False, + 'enabled': True, + 'advertise_lb_vip': False, + 'advertise_lb_snat_ip': False} + with mock.patch("vmware_nsxlib.v3.NsxLib.get_version", + return_value='2.1.0'), \ + mock.patch.object(router.client, 'get', + return_value={}): + router.update_advertisement( + router_id, **data) + test_client.assert_json_call( + 'put', router, + ('https://1.2.3.4/api/v1/logical-routers/%s/routing/' + 'advertisement' % router_id), + data=jsonutils.dumps(data, sort_keys=True), + headers=self.default_headers()) + + def test_update_advertisement_no_lb(self): + router = self.get_mocked_resource() + router_id = test_constants.FAKE_ROUTER_UUID + data = {'advertise_nat_routes': 'a', + 'advertise_nsx_connected_routes': 'b', + 'advertise_static_routes': False, + 'enabled': True} + with mock.patch("vmware_nsxlib.v3.NsxLib.get_version", + return_value='1.1.0'), \ + mock.patch.object(router.client, 'get', + return_value={}): + # lb args will be ignored on this nsx version + router.update_advertisement( + router_id, + advertise_lb_vip=False, + advertise_lb_snat_ip=False, + **data) + test_client.assert_json_call( + 'put', router, + ('https://1.2.3.4/api/v1/logical-routers/%s/routing/' + 'advertisement' % router_id), + data=jsonutils.dumps(data, sort_keys=True), + headers=self.default_headers()) + class LogicalRouterPortTestCase(BaseTestResource): diff --git a/vmware_nsxlib/v3/core_resources.py b/vmware_nsxlib/v3/core_resources.py index 57e13f4d..de43bd1b 100644 --- a/vmware_nsxlib/v3/core_resources.py +++ b/vmware_nsxlib/v3/core_resources.py @@ -587,6 +587,18 @@ class NsxLibLogicalRouter(utils.NsxLibApiBase): def update_advertisement(self, logical_router_id, **kwargs): resource = ('logical-routers/%s/routing/advertisement' % logical_router_id) + # ignore load balancing flags if lb is the not supported + if (self.nsxlib and + not self.nsxlib.feature_supported( + nsx_constants.FEATURE_LOAD_BALANCER)): + for arg in ('advertise_lb_vip', 'advertise_lb_snat_ip'): + if kwargs[arg]: + LOG.error("Ignoring %(arg)s for router %(rtr)s " + "update_advertisement: This feature is not " + "supported.", + {'arg': arg, 'rtr': logical_router_id}) + del kwargs[arg] + return self._update_resource_with_retry(resource, kwargs) def create(self, display_name, tags, edge_cluster_uuid=None, tier_0=False,