Skip to content

Commit

Permalink
[Monitor] Add new command: `az monitor action-group test-notification…
Browse files Browse the repository at this point in the history
…s create` (#23411)

* [Monitor]

* update

* Update action_groups.py

* update

* Update action_groups.py

* update

* update

* update

* update

* update

* update

* Update action_groups.py
  • Loading branch information
ZengTaoxu committed Aug 8, 2022
1 parent cd07d57 commit 76c2241
Show file tree
Hide file tree
Showing 32 changed files with 15,010 additions and 4,959 deletions.
2 changes: 1 addition & 1 deletion src/azure-cli-core/azure/cli/core/profiles/_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ def default_api_version(self):
ResourceType.MGMT_SERVICEBUS: '2021-06-01-preview',
ResourceType.MGMT_EVENTHUB: '2022-01-01-preview',
ResourceType.MGMT_MONITOR: SDKProfile('2019-06-01', {
'action_groups': '2021-09-01',
'action_groups': '2022-06-01',
'activity_log_alerts': '2017-04-01',
'activity_logs': '2015-04-01',
'alert_rule_incidents': '2016-03-01',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
|Scenario|Result|ErrorMessage|ErrorStack|ErrorNormalized|StartDt|EndDt|
|step_accept|successed||||2022-06-21 01:57:25.495476|2022-06-21 01:57:25.636503|
|step_show|successed||||2022-06-21 01:57:25.636503|2022-06-21 01:57:25.668147|
|step_accept|successed||||2022-08-03 10:49:41.600694|2022-08-03 10:49:41.890448|
|step_show|successed||||2022-08-03 10:49:41.891446|2022-08-03 10:49:42.114033|
Coverage: 2/2
69 changes: 69 additions & 0 deletions src/azure-cli/azure/cli/command_modules/monitor/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,75 @@
long-summary: This changes the receiver's status from Disabled to Enabled. This operation is only supported for Email or SMS receivers.
"""

helps['monitor action-group wait'] = """
type: command
short-summary: Place the CLI in a waiting state.
"""

helps['monitor action-group test-notifications'] = """
type: group
short-summary: Manage action groups test-notifications
"""

helps['monitor action-group test-notifications create'] = """
type: command
short-summary: Create an action group test-notifications
parameters:
- name: --add-action -a
short-summary: Add receivers to the action group test-notifications
long-summary: |
Usage: --add-action TYPE NAME [ARG ...]
Email:
Format: --add-action email NAME EMAIL_ADDRESS [usecommonalertschema]
Example: --add-action email bob bob@contoso.com
SMS:
Format: --add-action sms NAME COUNTRY_CODE PHONE_NUMBER
Example: --add-action sms charli 1 5551234567
Webhook:
Format: --add-action webhook NAME URI [useaadauth OBJECT_ID IDENTIFIER URI] [usecommonalertschema]
Example: --add-action https://www.contoso.com/alert useaadauth testobj http://identifier usecommonalertschema
Arm Role:
Format: --add-action armrole NAME ROLE_ID [usecommonalertschema]
Example: --add-action armole owner_role 8e3af657-a8ff-443c-a75c-2fe8c4bcb635
Azure App Push:
Format: --add-action azureapppush NAME EMAIL_ADDRESS
Example: --add-action azureapppush test_apppush bob@contoso.com
ITSM:
Format: --add-action itsm NAME WORKSPACE_ID CONNECTION_ID TICKET_CONFIGURATION REGION
Example: --add-action itsm test_itsm test_workspace test_conn ticket_blob useast
Automation runbook:
Format: --add-action automationrunbook NAME AUTOMATION_ACCOUNT_ID RUNBOOK_NAME WEBHOOK_RESOURCE_ID SERVICE_URI [isglobalrunbook] [usecommonalertschema]
Example: --add-action automationrunbook test_runbook test_acc test_book test_webhook test_rsrc http://example.com isglobalrunbook usecommonalertschema
Voice:
Format: --add-action voice NAME COUNTRY_CODE PHONE_NUMBER
Example: --add-action voice charli 1 4441234567
Logic App:
Format: --add-action logicapp NAME RESOURCE_ID CALLBACK_URL [usecommonalertschema]
Example: --add-action logicapp test_logicapp test_rsrc http://callback
Azure Function:
Format: --add-action azurefunction NAME FUNCTION_APP_RESOURCE_ID FUNCTION_NAME HTTP_TRIGGER_URL [usecommonalertschema]
Example: --add-action azurefunction test_function test_rsrc test_func http://trigger usecommonalertschema
Event Hub:
Format: --action eventhub NAME SUBSCRIPTION_ID EVENT_HUB_NAME_SPACE EVENT_HUB_NAME [usecommonalertschema]
Example: --action eventhub test_eventhub 5def922a-3ed4-49c1-b9fd-05ec533819a3 eventhubNameSpace testEventHubName usecommonalertschema
Multiple actions can be specified by using more than one `--add-action` argument.
'useaadauth', 'isglobalrunbook' and 'usecommonalertschema' are optional arguements that only need to be passed to set the respective parameter to True.
If the 'useaadauth' argument is passed, then the OBJECT_ID and IDENTIFIER_URI values are required as well.
examples:
- name: Create an action group test-notifications with action group
text: |
az monitor action-group test-notifications create --action-group MyActionGroup \\
--resource-group MyResourceGroup -a email alice alice@example.com usecommonalertsChema --alert-type budget
- name: Create an action group test-notifications with resource-group
text: |
az monitor action-group test-notifications create --resource-group MyResourceGroup \\
-a email alice alice@example.com usecommonalertsChema --alert-type budget
- name: Create an action group test-notifications
text: |
az monitor action-group test-notifications create -a email alice alice@example.com usecommonalertsChema \\
--alert-type budget
"""

helps['monitor activity-log'] = """
type: group
short-summary: Manage activity logs.
Expand Down
5 changes: 5 additions & 0 deletions src/azure-cli/azure/cli/command_modules/monitor/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -362,6 +362,11 @@ def load_arguments(self, _):
with self.argument_context('monitor action-group enable-receiver') as c:
c.argument('receiver_name', options_list=['--name', '-n'], help='The name of the receiver to resubscribe.')
c.argument('action_group_name', options_list=['--action-group'], help='The name of the action group.')

with self.argument_context('monitor action-group test-notifications create') as c:
c.argument('add_receivers', options_list=['--add-action', '-a'], nargs='+', action=ActionGroupReceiverParameterAction)
c.argument('alert_type', type=str, help='The name of the supported alert type.')
c.argument('action_group_name', options_list=['--action-group'], help='The name of the action group.')
# endregion

# region ActivityLog Alerts
Expand Down
2 changes: 2 additions & 0 deletions src/azure-cli/azure/cli/command_modules/monitor/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,8 @@ def load_command_table(self, _):
g.custom_command('list', 'list_action_groups', table_transformer=action_group_list_table)
g.generic_update_command('update', custom_func_name='update_action_groups', setter_arg_name='action_group',
table_transformer=action_group_list_table)
g.custom_command('test-notifications create', 'post_notifications', table_transformer=action_group_list_table, supports_no_wait=True)
g.wait_command('wait')

with self.command_group('monitor activity-log', activity_log_sdk) as g:
g.custom_command('list', 'list_activity_log', client_factory=cf_activity_log)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,88 @@ def enable_receiver(client, resource_group_name, action_group_name, receiver_nam
return client.enable_receiver(resource_group_name=resource_group_name,
action_group_name=action_group_name,
enable_request=enable_request)


# pylint: disable=too-many-locals
# pylint: disable=no-else-return
def post_notifications(client, alert_type, resource_group_name=None, action_group_name=None, add_receivers=None,
no_wait=False):
from azure.mgmt.monitor.models import NotificationRequestBody
EmailReceivers = []
SmsReceivers = []
WebhookReceivers = []
ItsmReceivers = []
AzureAppPushReceivers = []
AutomationRunbookReceivers = []
VoiceReceivers = []
LogicAppReceivers = []
AzureFunctionReceivers = []
ArmRoleReceivers = []
EventHubReceivers = []
for r in add_receivers:
from azure.mgmt.monitor.models import EmailReceiver, SmsReceiver, WebhookReceiver, ItsmReceiver, \
AzureAppPushReceiver, AutomationRunbookReceiver, VoiceReceiver, LogicAppReceiver, AzureFunctionReceiver, \
ArmRoleReceiver, EventHubReceiver
if isinstance(r, EmailReceiver):
EmailReceivers.append(EmailReceiver(name=r.name, email_address=r.email_address,
use_common_alert_schema=r.use_common_alert_schema))
elif isinstance(r, SmsReceiver):
SmsReceivers.append(SmsReceiver(name=r.name, country_code=r.country_code, phone_number=r.phone_number))
elif isinstance(r, WebhookReceiver):
WebhookReceivers.append(WebhookReceiver(name=r.name, service_uri=r.service_uri,
use_common_alert_schema=r.use_common_alert_schema,
use_aad_auth=r.use_aad_auth, object_id=r.object_id,
identifier_uri=r.identifier_uri, tenant_id=r.tenant_id))
elif isinstance(r, ItsmReceiver):
ItsmReceivers.append(ItsmReceiver(name=r.name, workspace_id=r.workspace_id, connection_id=r.connection_id,
ticket_configuration=r.ticket_configuration, region=r.region))
elif isinstance(r, AzureAppPushReceiver):
AzureAppPushReceivers.append(AzureAppPushReceiver(name=r.name, email_address=r.email_address))
elif isinstance(r, AutomationRunbookReceiver):
AutomationRunbookReceivers\
.append(AutomationRunbookReceiver(automation_account_id=r.automation_account_id,
runbook_name=r.runbook_name,
webhook_resource_id=r.webhook_resource_id,
is_global_runbook=r.is_global_runbook,
name=r.name,
service_uri=r.service_uri,
use_common_alert_schema=r.use_common_alert_schema))
elif isinstance(r, VoiceReceiver):
VoiceReceivers.append(VoiceReceiver(name=r.name, country_code=r.country_code, phone_number=r.phone_number))
elif isinstance(r, LogicAppReceiver):
LogicAppReceivers.append(LogicAppReceiver(name=r.name, resource_id=r.resource_id,
callback_url=r.callback_url))
elif isinstance(r, AzureFunctionReceiver):
AzureFunctionReceivers.append(AzureFunctionReceiver(name=r.name,
function_app_resource_id=r.function_app_resource_id,
function_name=r.function_name,
http_trigger_url=r.http_trigger_url,
use_common_alert_schema=r.use_common_alert_schema))
elif isinstance(r, ArmRoleReceiver):
ArmRoleReceivers.append(ArmRoleReceiver(name=r.name, role_id=r.role_id,
use_common_alert_schema=r.use_common_alert_schema))
elif isinstance(r, EventHubReceiver):
EventHubReceivers.append(EventHubReceiver(name=r.name,
event_hub_name_space=r.event_hub_name_space,
event_hub_name=r.event_hub_name,
subscription_id=r.subscription_id))

notification_request = NotificationRequestBody(alert_type=alert_type, email_receivers=EmailReceivers,
sms_receivers=SmsReceivers, webhook_receivers=WebhookReceivers,
itsm_receivers=ItsmReceivers,
azure_app_push_receivers=AzureAppPushReceivers,
automation_runbook_receivers=AutomationRunbookReceivers,
voice_receivers=VoiceReceivers,
logic_app_receivers=LogicAppReceivers,
azure_function_receivers=AzureFunctionReceivers,
arm_role_receivers=ArmRoleReceivers,
event_hub_receivers=EventHubReceivers)
from azure.cli.core.util import sdk_no_wait
if action_group_name:
return sdk_no_wait(no_wait, client.begin_create_notifications_at_action_group_resource_level,
resource_group_name, action_group_name, notification_request)
elif resource_group_name:
return sdk_no_wait(no_wait, client.begin_create_notifications_at_resource_group_level,
resource_group_name, notification_request)
else:
return sdk_no_wait(no_wait, client.begin_post_test_notifications, notification_request)
Loading

0 comments on commit 76c2241

Please sign in to comment.