Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[neighbor advertiser] raise exception when http endpoint return failure #758

Merged
merged 3 commits into from
Dec 5, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 21 additions & 14 deletions scripts/neighbor_advertiser
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -355,52 +356,58 @@ 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()
daall marked this conversation as resolved.
Show resolved Hide resolved

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))
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))
log_error("The request failed, vip: {}, error: {}".format(ferret_service_vip, e))
return None

neighbor_advertiser_configuration = json.loads(response.content)
ferret_server_ipv4_addr = neighbor_advertiser_configuration['ipv4Addr']

# 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


Expand Down