From e7fe099f25800f20a3e4cd9448d4609a7d16cd79 Mon Sep 17 00:00:00 2001 From: bmc-msft <41130664+bmc-msft@users.noreply.github.com> Date: Mon, 22 Feb 2021 19:40:07 -0500 Subject: [PATCH] handle delayed AAD resources in deployments (#585) --- src/deployment/deploy.py | 44 ++++++++++++++++++++++++++-------- src/deployment/registration.py | 10 +++++++- 2 files changed, 43 insertions(+), 11 deletions(-) diff --git a/src/deployment/deploy.py b/src/deployment/deploy.py index 8c36bf8cda..ee9a88e467 100644 --- a/src/deployment/deploy.py +++ b/src/deployment/deploy.py @@ -384,16 +384,40 @@ def deploy_template(self) -> None: mode=DeploymentMode.incremental, template=template, parameters=params ) ) - result = client.deployments.create_or_update( - self.resource_group, gen_guid(), deployment - ).result() - if result.properties.provisioning_state != "Succeeded": - logger.error( - "error deploying: %s", - json.dumps(result.as_dict(), indent=4, sort_keys=True), - ) - sys.exit(1) - self.results["deploy"] = result.properties.outputs + count = 0 + tries = 10 + error: Optional[Exception] = None + while count < tries: + count += 1 + + try: + result = client.deployments.create_or_update( + self.resource_group, gen_guid(), deployment + ).result() + if result.properties.provisioning_state != "Succeeded": + logger.error( + "error deploying: %s", + json.dumps(result.as_dict(), indent=4, sort_keys=True), + ) + sys.exit(1) + self.results["deploy"] = result.properties.outputs + return + except Exception as err: + error = err + as_repr = repr(err) + # Modeled after Azure-CLI. See: + # https://github.com/Azure/azure-cli/blob/ + # 3a2f6009cff788fde3b0170823c9129f187b2812/src/azure-cli-core/ + # azure/cli/core/commands/arm.py#L1086 + if ( + "PrincipalNotFound" in as_repr + and "does not exist in the directory" in as_repr + ): + logging.info("application principal not available in AAD yet") + if error: + raise error + else: + raise Exception("unknown error deploying") def assign_scaleset_identity_role(self) -> None: if self.upgrade: diff --git a/src/deployment/registration.py b/src/deployment/registration.py index 6f828b772d..9cc34ba05a 100644 --- a/src/deployment/registration.py +++ b/src/deployment/registration.py @@ -235,7 +235,15 @@ def add_application_password(app_object_id: UUID) -> Tuple[str, str]: return add_application_password_impl(app_object_id) except GraphQueryError as err: error = err - logging.warning("unable to create app password: %s", err.message) + # modeled after AZ-CLI's handling of missing application + # See: https://github.com/Azure/azure-cli/blob/ + # e015d5bcba0c2d21dc42189daa43dc1eb82d2485/src/azure-cli/ + # azure/cli/command_modules/util/tests/ + # latest/test_rest.py#L191-L192 + if "Request_ResourceNotFound" in repr(err): + logging.info("app unavailable in AAD, unable to create password yet") + else: + logging.warning("unable to create app password: %s", err.message) time.sleep(wait_duration) if error: raise error