diff --git a/ansible/collections/ansible_collections/nhsd/apigee/plugins/action/deploy_spec.py b/ansible/collections/ansible_collections/nhsd/apigee/plugins/action/deploy_spec.py index 5ad2394e..ea2ae7fc 100644 --- a/ansible/collections/ansible_collections/nhsd/apigee/plugins/action/deploy_spec.py +++ b/ansible/collections/ansible_collections/nhsd/apigee/plugins/action/deploy_spec.py @@ -1,4 +1,5 @@ import json +import time from ansible_collections.nhsd.apigee.plugins.module_utils.models.ansible.deploy_spec import ( DeploySpec, ) @@ -50,11 +51,21 @@ def run(self, tmp=None, task_vars=None): } if not check_mode: - response_dict = utils.post( - constants.APIGEE_DAPI_URL + f"organizations/{args.organization}/specs/doc", - args.access_token, - json=spec_resource, - ) + max_attempts = 3 + for attempt in range(max_attempts): + response_dict = utils.post( + constants.APIGEE_DAPI_URL + f"organizations/{args.organization}/specs/doc", + args.access_token, + json=spec_resource, + status_code=[200, 502], + ) + if response_dict["status_code"] != 502: + break + # Yet another partially broken API... + # Let's honour apigee's request to wait 30s before retry. + sleep_time = 30 + attempt * 10 + print(f"Attempt {attempt+1}/{max_attempts}... received 502 from Apigee. Waiting {sleep_time}s to retry...") + time.sleep(sleep_time) if response_dict.get("failed"): return response_dict spec_resource = response_dict["response"]["body"] diff --git a/ansible/collections/ansible_collections/nhsd/apigee/plugins/module_utils/utils.py b/ansible/collections/ansible_collections/nhsd/apigee/plugins/module_utils/utils.py index 47258265..1fbc013b 100644 --- a/ansible/collections/ansible_collections/nhsd/apigee/plugins/module_utils/utils.py +++ b/ansible/collections/ansible_collections/nhsd/apigee/plugins/module_utils/utils.py @@ -79,7 +79,7 @@ def select_unique( key: str, value: str, valid_lengths: typing.Optional[typing.List[int]] = None, -) -> typing.Optional[typing.Dict[str, typing.Any]]: +) -> typing.List[typing.Dict[str, typing.Any]]: selected = [item for item in items if item.get(key) == value] if not valid_lengths: