diff --git a/vmware_nsx/common/utils.py b/vmware_nsx/common/utils.py index ff20ebc3f0..2640c8b3bd 100644 --- a/vmware_nsx/common/utils.py +++ b/vmware_nsx/common/utils.py @@ -91,6 +91,15 @@ def is_nsxv_version_6_3(nsx_version): version.LooseVersion('6.3')) +def is_nsxv_dhcp_binding_supported(nsx_version): + return ((version.LooseVersion(nsx_version) >= + version.LooseVersion('6.3.3')) or + (version.LooseVersion(nsx_version) >= + version.LooseVersion('6.2.8') and + version.LooseVersion(nsx_version) < + version.LooseVersion('6.3'))) + + def get_tags(**kwargs): tags = ([dict(tag=value, scope=key) for key, value in six.iteritems(kwargs)]) diff --git a/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py b/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py index 04e123d8ad..821a783304 100644 --- a/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py +++ b/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py @@ -2118,7 +2118,7 @@ def get_dhcp_binding_mappings_for_hostname(nsxv_manager, edge_id): return bindings_get -def get_dhcp_binding_for_binding_id(nsxv_manager, edge_id, binding_id): +def _get_dhcp_binding_for_binding_id(nsxv_manager, edge_id, binding_id): dhcp_config = query_dhcp_service_config(nsxv_manager, edge_id) if dhcp_config: for binding in dhcp_config['staticBindings']['staticBindings']: @@ -2126,6 +2126,25 @@ def get_dhcp_binding_for_binding_id(nsxv_manager, edge_id, binding_id): return binding +def _get_dhcp_binding(nsxv_manager, edge_id, binding_id): + try: + h, dhcp_binding = nsxv_manager.vcns.get_dhcp_binding(edge_id, + binding_id) + return dhcp_binding + except Exception: + return + + +def get_dhcp_binding_for_binding_id(nsxv_manager, edge_id, binding_id): + # API for specific binding is supported in NSX 6.2.8 and 6.3.3 onwards + ver = nsxv_manager.vcns.get_version() + if c_utils.is_nsxv_dhcp_binding_supported(ver): + return _get_dhcp_binding(nsxv_manager, edge_id, binding_id) + else: + return _get_dhcp_binding_for_binding_id(nsxv_manager, edge_id, + binding_id) + + def query_dhcp_service_config(nsxv_manager, edge_id): """Retrieve the current DHCP configuration from the edge.""" _, dhcp_config = nsxv_manager.vcns.query_dhcp_configuration(edge_id) diff --git a/vmware_nsx/plugins/nsx_v/vshield/vcns.py b/vmware_nsx/plugins/nsx_v/vshield/vcns.py index f8ebd5faf3..17e6640267 100644 --- a/vmware_nsx/plugins/nsx_v/vshield/vcns.py +++ b/vmware_nsx/plugins/nsx_v/vshield/vcns.py @@ -561,6 +561,13 @@ class Vcns(object): binding_id) return self.do_request(HTTP_DELETE, uri, decode=False) + def get_dhcp_binding(self, edge_id, binding_id): + """Get a dhcp static binding from the edge.""" + uri = self._build_uri_path(edge_id, + DHCP_SERVICE, DHCP_BINDING_RESOURCE, + binding_id) + return self.do_request(HTTP_GET, uri, decode=False) + def create_security_group(self, request): """Creates a security group container in nsx. diff --git a/vmware_nsx/tests/unit/nsx_v/vshield/fake_vcns.py b/vmware_nsx/tests/unit/nsx_v/vshield/fake_vcns.py index df0e33f2dc..685dbbda36 100644 --- a/vmware_nsx/tests/unit/nsx_v/vshield/fake_vcns.py +++ b/vmware_nsx/tests/unit/nsx_v/vshield/fake_vcns.py @@ -304,6 +304,15 @@ class FakeVcns(object): response = '' return (header, response) + def get_dhcp_binding(self, edge_id, binding_id): + if binding_id not in self._dhcp_bindings[edge_id]: + raise Exception(_("binding %s does not exist") % binding_id) + response = self._dhcp_bindings[edge_id][binding_id] + header = { + 'status': 200 + } + return (header, response) + def create_bridge(self, edge_id, request): if edge_id not in self._edges: raise Exception(_("Edge %s does not exist") % edge_id)