From d17f3aad1ba79041c7cf8c2607bc90687cae8171 Mon Sep 17 00:00:00 2001 From: Adit Sarfaty Date: Mon, 23 Apr 2018 14:22:56 +0300 Subject: [PATCH] NSX|v: Fix create_dhcp_binding error handling create_dhcp_binding may fail with timeout or other types of exceptions which do not have a response field. This patch checks its existince before trying to decode it. Change-Id: Ie62d3965a10bf07795c1bd56b7c6191eb5c73251 --- .../plugins/nsx_v/vshield/edge_utils.py | 46 ++++++++++--------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py b/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py index fe79be3c4d..7d597f59b9 100644 --- a/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py +++ b/vmware_nsx/plugins/nsx_v/vshield/edge_utils.py @@ -1879,28 +1879,30 @@ class EdgeManager(object): except nsxapi_exc.VcnsApiException as e: with excutils.save_and_reraise_exception(): binding_id = None - desc = jsonutils.loads(e.response) - if desc.get('errorCode') == ( - vcns_const.NSX_ERROR_DHCP_DUPLICATE_MAC): - bindings = get_dhcp_binding_mappings(self.nsxv_manager, - edge_id) - binding_id = bindings.get(binding['macAddress'].lower()) - LOG.debug("Duplicate MAC for %s with binding %s", - binding['macAddress'], binding_id) - elif desc.get('errorCode') == ( - vcns_const.NSX_ERROR_DHCP_OVERLAPPING_IP): - bindings = get_dhcp_binding_mappings_for_ips( - self.nsxv_manager, edge_id) - binding_id = bindings.get(binding['ipAddress']) - LOG.debug("Overlapping IP %s with binding %s", - binding['ipAddress'], binding_id) - elif desc.get('errorCode') == ( - vcns_const.NSX_ERROR_DHCP_DUPLICATE_HOSTNAME): - bindings = get_dhcp_binding_mappings_for_hostname( - self.nsxv_manager, edge_id) - binding_id = bindings.get(binding['hostname']) - LOG.debug("Overlapping hostname %s with binding %s", - binding['hostname'], binding_id) + if e.response: + desc = jsonutils.loads(e.response) + if desc.get('errorCode') == ( + vcns_const.NSX_ERROR_DHCP_DUPLICATE_MAC): + bindings = get_dhcp_binding_mappings(self.nsxv_manager, + edge_id) + binding_id = bindings.get( + binding['macAddress'].lower()) + LOG.debug("Duplicate MAC for %s with binding %s", + binding['macAddress'], binding_id) + elif desc.get('errorCode') == ( + vcns_const.NSX_ERROR_DHCP_OVERLAPPING_IP): + bindings = get_dhcp_binding_mappings_for_ips( + self.nsxv_manager, edge_id) + binding_id = bindings.get(binding['ipAddress']) + LOG.debug("Overlapping IP %s with binding %s", + binding['ipAddress'], binding_id) + elif desc.get('errorCode') == ( + vcns_const.NSX_ERROR_DHCP_DUPLICATE_HOSTNAME): + bindings = get_dhcp_binding_mappings_for_hostname( + self.nsxv_manager, edge_id) + binding_id = bindings.get(binding['hostname']) + LOG.debug("Overlapping hostname %s with binding %s", + binding['hostname'], binding_id) if binding_id: self.nsxv_manager.vcns.delete_dhcp_binding( edge_id, binding_id)