def add_action_group(cmd, client, resource_group_name, activity_log_alert_name, action_group_ids, reset=False, webhook_properties=None, strict=False): from azure.mgmt.monitor.models import ActivityLogAlertActionGroup as ActionGroup settings = _get_alert_settings(client, resource_group_name, activity_log_alert_name) # normalize the action group ids rids = _normalize_names(cmd.cli_ctx, action_group_ids, resource_group_name, 'microsoft.insights', 'actionGroups') if reset: action_groups = [ActionGroup(action_group_id=rid, webhook_properties=webhook_properties) for rid in rids] else: action_groups = dict((each.action_group_id, each) for each in settings.actions.action_groups) for rid in rids: if rid in action_groups and webhook_properties != action_groups[rid].webhook_properties and strict: raise ValueError('Fails to override webhook properties of action group {} in strict mode.'.format(rid)) action_groups[rid] = ActionGroup(action_group_id=rid, webhook_properties=webhook_properties) action_groups = list(action_groups.values()) settings.actions.action_groups = action_groups return client.create_or_update(resource_group_name=resource_group_name, activity_log_alert_name=activity_log_alert_name, activity_log_alert=settings)
def create(cmd, client, resource_group_name, activity_log_alert_name, scopes=None, condition=None, action_groups=frozenset(), tags=None, disable=False, description=None, webhook_properties=None): from msrestazure.tools import resource_id from azure.mgmt.monitor.models import (ActivityLogAlertResource, AlertRuleAllOfCondition, AlertRuleLeafCondition, ActionList) from azure.mgmt.monitor.models import ActionGroup from azure.cli.core.commands.client_factory import get_subscription_id from knack.util import CLIError if not scopes: scopes = [ resource_id(subscription=get_subscription_id(cmd.cli_ctx), resource_group=resource_group_name) ] if _get_alert_settings(client, resource_group_name, activity_log_alert_name, throw_if_missing=False): raise CLIError( 'The activity log alert {} already exists in resource group {}.'. format(activity_log_alert_name, resource_group_name)) # Add alert conditions condition = condition or AlertRuleAllOfCondition(all_of=[ AlertRuleLeafCondition(field='category', equals='ServiceHealth') ]) # Add action groups action_group_rids = _normalize_names(cmd.cli_ctx, action_groups, resource_group_name, 'microsoft.insights', 'actionGroups') action_groups = [ ActionGroup(action_group_id=i, webhook_properties=webhook_properties) for i in action_group_rids ] alert_actions = ActionList(action_groups=action_groups) settings = ActivityLogAlertResource(location='global', scopes=scopes, condition=condition, actions=alert_actions, enabled=not disable, description=description, tags=tags) return client.create_or_update( resource_group_name=resource_group_name, activity_log_alert_name=activity_log_alert_name, activity_log_alert=settings)