From 63391fb512fe29ebc27399506b0829d0f5ee51e2 Mon Sep 17 00:00:00 2001 From: Ying Xie Date: Tue, 3 Dec 2019 22:46:45 +0000 Subject: [PATCH 1/3] [neighbor advertiser] raise exception when http endpoint return failure Signed-off-by: Ying Xie --- scripts/neighbor_advertiser | 1 + 1 file changed, 1 insertion(+) diff --git a/scripts/neighbor_advertiser b/scripts/neighbor_advertiser index df46ea7916..ba76a66fed 100644 --- a/scripts/neighbor_advertiser +++ b/scripts/neighbor_advertiser @@ -361,6 +361,7 @@ def wrapped_ferret_request(request_slice, https_endpoint, http_endpoint): response = requests.post(http_endpoint, json=request_slice, timeout=DEFAULT_REQUEST_TIMEOUT) + response.raise_for_status() return response From 19cc9d5d44f5ab540f536ccf3b5c72a3b463a99f Mon Sep 17 00:00:00 2001 From: Danny Allen Date: Wed, 4 Dec 2019 15:01:31 -0800 Subject: [PATCH 2/3] Delete deprecated error checking code --- scripts/neighbor_advertiser | 8 -------- 1 file changed, 8 deletions(-) diff --git a/scripts/neighbor_advertiser b/scripts/neighbor_advertiser index ba76a66fed..6506c4df23 100644 --- a/scripts/neighbor_advertiser +++ b/scripts/neighbor_advertiser @@ -380,14 +380,6 @@ def post_neighbor_advertiser_slice(ferret_service_vip): log_error('The request failed, vip: {}, error: {}'.format(ferret_service_vip, e)) return None - # Handle response errors - if not response: - log_error('Failed to set up neighbor advertiser slice, vip: {}, no response obtained'.format(ferret_service_vip)) - return None - if response and not response.ok: - log_error('Failed to set up neighbor advertiser slice, vip: {}, error_code: {}, error_content: {}'.format(ferret_service_vip, response.status_code, response.content)) - return None - neighbor_advertiser_configuration = json.loads(response.content) ferret_server_ipv4_addr = neighbor_advertiser_configuration['ipv4Addr'] From 5014e06ff86232ca155393b9e9fbbe69aa4006fd Mon Sep 17 00:00:00 2001 From: Danny Allen Date: Wed, 4 Dec 2019 15:11:38 -0800 Subject: [PATCH 3/3] Add back check for None --- scripts/neighbor_advertiser | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/scripts/neighbor_advertiser b/scripts/neighbor_advertiser index 6506c4df23..c42fadd464 100644 --- a/scripts/neighbor_advertiser +++ b/scripts/neighbor_advertiser @@ -339,6 +339,7 @@ def construct_neighbor_advertiser_slice(): return slice_obj + def wrapped_ferret_request(request_slice, https_endpoint, http_endpoint): """ Attempts to reach ferret by first trying HTTPS, failing over to HTTP in @@ -355,29 +356,42 @@ def wrapped_ferret_request(request_slice, https_endpoint, http_endpoint): json=request_slice, timeout=DEFAULT_REQUEST_TIMEOUT, verify=False) + + if not response: + raise RuntimeError("No response obtained from HTTPS endpoint") + + # If the request is unsuccessful (e.g. has a non 2xx response code), + # we'll try HTTP response.raise_for_status() except Exception as e: log_info("Connect HTTPS Ferret endpoint failed (error: {}), trying HTTP...".format(e)) response = requests.post(http_endpoint, json=request_slice, timeout=DEFAULT_REQUEST_TIMEOUT) + + if not response: + raise RuntimeError("No response obtained from HTTP endpoint") + + # If the request is unsuccessful (e.g. has a non 2xx response code), + # we'll consider it failed response.raise_for_status() return response + def post_neighbor_advertiser_slice(ferret_service_vip): request_slice = construct_neighbor_advertiser_slice() save_as_json(request_slice, NEIGHBOR_ADVERTISER_REQUEST_SLICE_PATH) - https_endpoint = 'https://{}:448{}{}'.format(ferret_service_vip, FERRET_NEIGHBOR_ADVERTISER_API_PREFIX, get_switch_name()) - http_endpoint = 'http://{}:85{}{}'.format(ferret_service_vip, FERRET_NEIGHBOR_ADVERTISER_API_PREFIX, get_switch_name()) + https_endpoint = "https://{}:448{}{}".format(ferret_service_vip, FERRET_NEIGHBOR_ADVERTISER_API_PREFIX, get_switch_name()) + http_endpoint = "http://{}:85{}{}".format(ferret_service_vip, FERRET_NEIGHBOR_ADVERTISER_API_PREFIX, get_switch_name()) response = None for retry in range(DEFAULT_FERRET_QUERY_RETRIES): try: response = wrapped_ferret_request(request_slice, https_endpoint, http_endpoint) except Exception as e: - log_error('The request failed, vip: {}, error: {}'.format(ferret_service_vip, e)) + log_error("The request failed, vip: {}, error: {}".format(ferret_service_vip, e)) return None neighbor_advertiser_configuration = json.loads(response.content) @@ -385,15 +399,15 @@ def post_neighbor_advertiser_slice(ferret_service_vip): # Retry the request if the provided DIP is in the device VLAN if is_dip_in_device_vlan(ferret_server_ipv4_addr): - log_info('Failed to set up neighbor advertiser slice, vip: {}, dip {} is in device VLAN (attempt {}/{})'.format(ferret_service_vip, ferret_server_ipv4_addr, retry + 1, DEFAULT_FERRET_QUERY_RETRIES)) + log_info("Failed to set up neighbor advertiser slice, vip: {}, dip {} is in device VLAN (attempt {}/{})".format(ferret_service_vip, ferret_server_ipv4_addr, retry + 1, DEFAULT_FERRET_QUERY_RETRIES)) continue # If all the proceeding checks pass, return the provided DIP save_as_json(neighbor_advertiser_configuration, NEIGHBOR_ADVERTISER_RESPONSE_CONFIG_PATH) - log_info('Successfully set up neighbor advertiser slice, vip: {}, dip: {}'.format(ferret_service_vip, ferret_server_ipv4_addr)) + log_info("Successfully set up neighbor advertiser slice, vip: {}, dip: {}".format(ferret_service_vip, ferret_server_ipv4_addr)) return ferret_server_ipv4_addr - log_error('Failed to set up neighbor advertiser slice, vip: {}, returned dips were in device VLAN'.format(ferret_service_vip)) + log_error("Failed to set up neighbor advertiser slice, vip: {}, returned dips were in device VLAN".format(ferret_service_vip)) return None