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

Better error handling for Log Analytics Workspace generation. #120

Merged
merged 5 commits into from
Jun 3, 2022
Merged
Changes from 2 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
33 changes: 20 additions & 13 deletions src/containerapp/azext_containerapp/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -531,33 +531,40 @@ 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)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know this isn't too related to your change, but we should definitely only be excepting ValidationError here since that's what _ensure_location_allowed raises

We should also revisit _ensure_location_allowed. I took a look at the implementation and the whole function basically is under a try/except and it ignores all exceptions besides ValidationError -- not sure why that is. Seems like we shouldn't be burying errors like that. You can just put a TODO comment on it for now if you don't have time to fix it and retest (and it's sort of out of the scope of this change)


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_customer_id = log_analytics_workspace.customer_id
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:
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
handle_raw_exception(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
Expand Down