From bd4a3694433b7036d9952e161358c51e1d5378d3 Mon Sep 17 00:00:00 2001 From: Haroon Feisal Date: Thu, 2 Jun 2022 13:47:38 -0400 Subject: [PATCH 1/5] Updated error handling. --- src/containerapp/azext_containerapp/_utils.py | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/src/containerapp/azext_containerapp/_utils.py b/src/containerapp/azext_containerapp/_utils.py index ededfff9b06..e1e89835dd0 100644 --- a/src/containerapp/azext_containerapp/_utils.py +++ b/src/containerapp/azext_containerapp/_utils.py @@ -531,33 +531,37 @@ def _generate_log_analytics_if_not_provided(cmd, logs_customer_id, logs_key, loc if logs_customer_id is None and logs_key is None: logger.warning("No Log Analytics workspace provided.") _validate_subscription_registered(cmd, LOG_ANALYTICS_RP) + try: log_analytics_client = log_analytics_client_factory(cmd.cli_ctx) log_analytics_shared_key_client = log_analytics_shared_key_client_factory(cmd.cli_ctx) + except Exception as ex: + handle_raw_exception(ex) - log_analytics_location = location - try: - _ensure_location_allowed(cmd, log_analytics_location, LOG_ANALYTICS_RP, "workspaces") - except Exception: # pylint: disable=broad-except - log_analytics_location = _get_default_log_analytics_location(cmd) + log_analytics_location = location + try: + _ensure_location_allowed(cmd, log_analytics_location, LOG_ANALYTICS_RP, "workspaces") + except Exception: # pylint: disable=broad-except + log_analytics_location = _get_default_log_analytics_location(cmd) - from azure.cli.core.commands import LongRunningOperation - from azure.mgmt.loganalytics.models import Workspace + from azure.cli.core.commands import LongRunningOperation + from azure.mgmt.loganalytics.models import Workspace - workspace_name = _generate_log_analytics_workspace_name(resource_group_name) - workspace_instance = Workspace(location=log_analytics_location) - logger.warning("Generating a Log Analytics workspace with name \"{}\"".format(workspace_name)) # pylint: disable=logging-format-interpolation + workspace_name = _generate_log_analytics_workspace_name(resource_group_name) + workspace_instance = Workspace(location=log_analytics_location) + logger.warning("Generating a Log Analytics workspace with name \"{}\"".format(workspace_name)) # pylint: disable=logging-format-interpolation + try: poller = log_analytics_client.begin_create_or_update(resource_group_name, workspace_name, workspace_instance) log_analytics_workspace = LongRunningOperation(cmd.cli_ctx)(poller) + except Exception as ex: + handle_raw_exception(ex) - logs_customer_id = log_analytics_workspace.customer_id - logs_key = log_analytics_shared_key_client.get_shared_keys( - workspace_name=workspace_name, - resource_group_name=resource_group_name).primary_shared_key + logs_customer_id = log_analytics_workspace.customer_id + logs_key = log_analytics_shared_key_client.get_shared_keys( + workspace_name=workspace_name, + resource_group_name=resource_group_name).primary_shared_key - except Exception as ex: - raise ValidationError("Unable to generate a Log Analytics workspace. You can use \"az monitor log-analytics workspace create\" to create one and supply --logs-customer-id and --logs-key") from ex elif logs_customer_id is None: raise ValidationError("Usage error: Supply the --logs-customer-id associated with the --logs-key") elif logs_key is None: # Try finding the logs-key From d14b49715b670dc0b40cc99850dcca27bd068776 Mon Sep 17 00:00:00 2001 From: Haroon Feisal Date: Thu, 2 Jun 2022 14:47:40 -0400 Subject: [PATCH 2/5] Made sure key retrieval had a try catch. --- src/containerapp/azext_containerapp/_utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/containerapp/azext_containerapp/_utils.py b/src/containerapp/azext_containerapp/_utils.py index e1e89835dd0..93d581aebe3 100644 --- a/src/containerapp/azext_containerapp/_utils.py +++ b/src/containerapp/azext_containerapp/_utils.py @@ -558,9 +558,12 @@ def _generate_log_analytics_if_not_provided(cmd, logs_customer_id, logs_key, loc handle_raw_exception(ex) logs_customer_id = log_analytics_workspace.customer_id - logs_key = log_analytics_shared_key_client.get_shared_keys( - workspace_name=workspace_name, - resource_group_name=resource_group_name).primary_shared_key + try: + logs_key = log_analytics_shared_key_client.get_shared_keys( + workspace_name=workspace_name, + resource_group_name=resource_group_name).primary_shared_key + except Exception as ex: + handle_raw_exception(ex) elif logs_customer_id is None: raise ValidationError("Usage error: Supply the --logs-customer-id associated with the --logs-key") From 4ab4f0be96bd3569a2f34b10fdc1776685053800 Mon Sep 17 00:00:00 2001 From: Haroon Feisal Date: Fri, 3 Jun 2022 14:19:13 -0400 Subject: [PATCH 3/5] Added error handling for --logs-customer-id only case. --- src/containerapp/azext_containerapp/_utils.py | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/src/containerapp/azext_containerapp/_utils.py b/src/containerapp/azext_containerapp/_utils.py index 93d581aebe3..232b0cc1dbd 100644 --- a/src/containerapp/azext_containerapp/_utils.py +++ b/src/containerapp/azext_containerapp/_utils.py @@ -573,7 +573,11 @@ def _generate_log_analytics_if_not_provided(cmd, logs_customer_id, logs_key, loc log_analytics_name = None log_analytics_rg = None - log_analytics = log_analytics_client.list() + + try: + log_analytics = log_analytics_client.list() + except Exception as ex: + handle_raw_exception(ex) for la in log_analytics: if la.customer_id and la.customer_id.lower() == logs_customer_id.lower(): @@ -584,7 +588,10 @@ def _generate_log_analytics_if_not_provided(cmd, logs_customer_id, logs_key, loc if log_analytics_name is None: raise ValidationError('Usage error: Supply the --logs-key associated with the --logs-customer-id') - shared_keys = log_analytics_shared_key_client.get_shared_keys(workspace_name=log_analytics_name, resource_group_name=log_analytics_rg) + try: + shared_keys = log_analytics_shared_key_client.get_shared_keys(workspace_name=log_analytics_name, resource_group_name=log_analytics_rg) + except Exception as ex: + handle_raw_exception(ex) if not shared_keys or not shared_keys.primary_shared_key: raise ValidationError('Usage error: Supply the --logs-key associated with the --logs-customer-id') From dfb0cb5125384811e6a7d17d08b504ad0b6d7634 Mon Sep 17 00:00:00 2001 From: Haroon Feisal Date: Fri, 3 Jun 2022 14:45:43 -0400 Subject: [PATCH 4/5] Updated error handling of _ensure_location_allowed. --- src/containerapp/azext_containerapp/_utils.py | 37 ++++++++++--------- 1 file changed, 19 insertions(+), 18 deletions(-) diff --git a/src/containerapp/azext_containerapp/_utils.py b/src/containerapp/azext_containerapp/_utils.py index 232b0cc1dbd..833410f204f 100644 --- a/src/containerapp/azext_containerapp/_utils.py +++ b/src/containerapp/azext_containerapp/_utils.py @@ -289,27 +289,28 @@ def _validate_subscription_registered(cmd, resource_provider, subscription_id=No def _ensure_location_allowed(cmd, location, resource_provider, resource_type): - providers_client = None try: providers_client = providers_client_factory(cmd.cli_ctx, get_subscription_id(cmd.cli_ctx)) + except Exception as ex: + handle_raw_exception(ex) - if providers_client is not None: + if providers_client is not None: + try: resource_types = getattr(providers_client.get(resource_provider), 'resource_types', []) - res_locations = [] - for res in resource_types: - if res and getattr(res, 'resource_type', "") == resource_type: - res_locations = getattr(res, 'locations', []) - - res_locations = [res_loc.lower().replace(" ", "").replace("(", "").replace(")", "") for res_loc in res_locations if res_loc.strip()] - - location_formatted = location.lower().replace(" ", "") - if location_formatted not in res_locations: - raise ValidationError("Location '{}' is not currently supported. To get list of supported locations, run `az provider show -n {} --query \"resourceTypes[?resourceType=='{}'].locations\"`".format( - location, resource_provider, resource_type)) - except ValidationError as ex: - raise ex - except Exception: # pylint: disable=broad-except - pass + except Exception as ex: + handle_raw_exception(ex) + + res_locations = [] + for res in resource_types: + if res and getattr(res, 'resource_type', "") == resource_type: + res_locations = getattr(res, 'locations', []) + + res_locations = [res_loc.lower().replace(" ", "").replace("(", "").replace(")", "") for res_loc in res_locations if res_loc.strip()] + + location_formatted = location.lower().replace(" ", "") + if location_formatted not in res_locations: + raise ValidationError("Location '{}' is not currently supported. To get list of supported locations, run `az provider show -n {} --query \"resourceTypes[?resourceType=='{}'].locations\"`" + .format(location, resource_provider, resource_type)) def parse_env_var_flags(env_list, is_update_containerapp=False): @@ -541,7 +542,7 @@ def _generate_log_analytics_if_not_provided(cmd, logs_customer_id, logs_key, loc log_analytics_location = location try: _ensure_location_allowed(cmd, log_analytics_location, LOG_ANALYTICS_RP, "workspaces") - except Exception: # pylint: disable=broad-except + except ValidationError: # pylint: disable=broad-except log_analytics_location = _get_default_log_analytics_location(cmd) from azure.cli.core.commands import LongRunningOperation From 820afdd16ab014fdd1070755677a792f129bab64 Mon Sep 17 00:00:00 2001 From: Haroon Feisal Date: Fri, 3 Jun 2022 14:52:48 -0400 Subject: [PATCH 5/5] Default providers_client to None. --- src/containerapp/azext_containerapp/_utils.py | 1 + 1 file changed, 1 insertion(+) diff --git a/src/containerapp/azext_containerapp/_utils.py b/src/containerapp/azext_containerapp/_utils.py index 833410f204f..829cde920f7 100644 --- a/src/containerapp/azext_containerapp/_utils.py +++ b/src/containerapp/azext_containerapp/_utils.py @@ -289,6 +289,7 @@ def _validate_subscription_registered(cmd, resource_provider, subscription_id=No def _ensure_location_allowed(cmd, location, resource_provider, resource_type): + providers_client = None try: providers_client = providers_client_factory(cmd.cli_ctx, get_subscription_id(cmd.cli_ctx)) except Exception as ex: