Skip to content

Commit d643a08

Browse files
authored
[load] Adding commands to manage notification-rule in azure load testing using CLI (#8622)
* Adding commands for scheduling * added list schedule command * initial tests for scheduling * Updated tests * Enhancing test cases for schedule * Optimizing utils and addressing PR comments * Adding invalid test cases * Addressing PR comments * linting and styling * test cases and recordings updated * nit * Updating version * nit * Addressing PR comments and issues after bug bash * Fixes after running githooks * removed datetime.UTC as not supported in python 3.9 * Working code for notification command * nit * Changes along with test cases for notification rule * Updating version to 2.0.0 * Changes after bug bash * linter fix
1 parent 3ab770e commit d643a08

22 files changed

+7015
-4
lines changed

src/load/HISTORY.rst

+7
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,17 @@
22
33
Release History
44
===============
5+
6+
2.0.0
7+
++++++
8+
* Add commands for creating and managing notification rules using CLI.
9+
10+
511
1.8.0
612
++++++
713
* Add commands for creating and managing schedule triggers using CLI.
814

15+
916
1.7.0
1017
++++++
1118
* Add support for metrics reference identity. Metrics reference identity can be set using `--metrics-reference-identity` argument in 'az load test create' and 'az load test update' commands. Metrics reference identity set in YAML config file under key `referenceIdentities` with `kind` as `Metrics` will also be honoured.

src/load/azext_load/__init__.py

+4
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def load_command_table(self, args):
1919
from azext_load.data_plane.load_test.commands import load_test_commands
2020
from azext_load.data_plane.load_test_run.commands import load_test_run_commands
2121
from azext_load.data_plane.load_trigger.commands import load_trigger_schedule_commands
22+
from azext_load.data_plane.load_notifications.commands import load_notification_commands
2223
from azure.cli.core.aaz import load_aaz_command_table
2324
try:
2425
from . import aaz
@@ -34,6 +35,7 @@ def load_command_table(self, args):
3435
load_test_commands(self, args)
3536
load_test_run_commands(self, args)
3637
load_trigger_schedule_commands(self, args)
38+
load_notification_commands(self, args)
3739
return self.command_table
3840

3941
def load_arguments(self, command):
@@ -42,12 +44,14 @@ def load_arguments(self, command):
4244
from azext_load.data_plane.load_test.params import load_arguments as load_test_arguments
4345
from azext_load.data_plane.load_test_run.params import load_arguments as load_test_run_arguments
4446
from azext_load.data_plane.load_trigger.params import load_arguments as load_trigger_schedule_arguments
47+
from azext_load.data_plane.load_notifications.params import load_arguments as load_notification_arguments
4548

4649
load_arguments(self, command)
4750
load_common_arguments(self, command)
4851
load_test_arguments(self, command)
4952
load_test_run_arguments(self, command)
5053
load_trigger_schedule_arguments(self, command)
54+
load_notification_arguments(self, command)
5155

5256

5357
COMMAND_LOADER_CLS = LoadCommandsLoader

src/load/azext_load/_help.py

+2
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,10 @@
1313
from azext_load.data_plane.load_test.help import helps as data_load_test_helps
1414
from azext_load.data_plane.load_test_run.help import helps as data_load_test_run_helps
1515
from azext_load.data_plane.load_trigger.help import helps as data_load_trigger_helps
16+
from azext_load.data_plane.load_notifications.help import helps as data_load_notifications_helps
1617

1718
helps.update(data_common_helps)
1819
helps.update(data_load_test_helps)
1920
helps.update(data_load_test_run_helps)
2021
helps.update(data_load_trigger_helps)
22+
helps.update(data_load_notifications_helps)

src/load/azext_load/data_plane/help.py

+8
Original file line numberDiff line numberDiff line change
@@ -100,3 +100,11 @@
100100
short-summary: Command group to manage schedule triggers.
101101
long-summary: Command group to manage schedule triggers.
102102
""" + _common_params
103+
104+
helps[
105+
"load notification-rule"
106+
] = """
107+
type: group
108+
short-summary: Command group to manage notification rules.
109+
long-summary: Command group to manage notification rules.
110+
""" + _common_params
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
6+
__path__ = __import__("pkgutil").extend_path(__path__, __name__)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
6+
from azure.cli.core.commands import CliCommandType
7+
8+
admin_custom_sdk = CliCommandType(
9+
operations_tmpl="azext_load.data_plane.load_notifications.custom#{}"
10+
)
11+
12+
13+
def load_notification_commands(self, _):
14+
15+
with self.command_group(
16+
"load notification-rule", custom_command_type=admin_custom_sdk, is_preview=True
17+
) as g:
18+
g.custom_command("create", "create_notification_rule")
19+
g.custom_command("update", "update_notification_rule")
20+
g.custom_show_command("show", "show_notification_rule")
21+
g.custom_command("list", "list_notification_rules")
22+
g.custom_command("delete", "delete_notification_rule", confirmation=True)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
6+
from knack.log import get_logger
7+
from azext_load.data_plane.utils.utils import (
8+
get_admin_data_plane_client)
9+
from azext_load.data_plane.load_notifications import utils
10+
from azure.cli.core.azclierror import InvalidArgumentValueError
11+
from azure.core.exceptions import ResourceNotFoundError
12+
13+
logger = get_logger(__name__)
14+
15+
16+
def create_notification_rule(
17+
cmd,
18+
load_test_resource,
19+
action_groups,
20+
notification_rule_id,
21+
resource_group_name=None,
22+
event=None,
23+
display_name=None,
24+
test_ids=None,
25+
all_tests=False,
26+
all_events=False,
27+
):
28+
client = get_admin_data_plane_client(cmd, load_test_resource, resource_group_name)
29+
body = None
30+
try:
31+
body = client.get_notification_rule(notification_rule_id)
32+
except ResourceNotFoundError:
33+
pass
34+
35+
if body is not None:
36+
msg = "Notification rule with given ID: %s already exists." % notification_rule_id
37+
logger.debug(msg)
38+
raise InvalidArgumentValueError(msg)
39+
40+
logger.info("Creating notification rule.")
41+
notification_rule = utils.get_notification_rule_create_body(
42+
action_groups, event, display_name, test_ids, all_tests, all_events
43+
)
44+
logger.info("Notification rule object to be sent for create: %s", notification_rule)
45+
response = client.create_or_update_notification_rule(notification_rule_id, notification_rule)
46+
logger.info("Notification rule created successfully.")
47+
return response.as_dict()
48+
49+
50+
def update_notification_rule(
51+
cmd,
52+
load_test_resource,
53+
notification_rule_id,
54+
resource_group_name=None,
55+
action_groups=None,
56+
add_event=None,
57+
remove_event=None,
58+
display_name=None,
59+
test_ids=None,
60+
all_tests=False,
61+
):
62+
client = get_admin_data_plane_client(cmd, load_test_resource, resource_group_name)
63+
logger.info("Updating notification rule.")
64+
try:
65+
existing_notification_rule = client.get_notification_rule(notification_rule_id)
66+
except ResourceNotFoundError:
67+
msg = "Notification rule with given ID: %s does not exist." % notification_rule_id
68+
logger.debug(msg)
69+
raise InvalidArgumentValueError(msg)
70+
logger.info("Existing notification rule: %s", existing_notification_rule)
71+
new_notification_rule = utils.get_notification_rule_update_body(
72+
existing_notification_rule,
73+
action_groups,
74+
add_event,
75+
remove_event,
76+
display_name,
77+
test_ids,
78+
all_tests,
79+
)
80+
logger.info("Incoming changes in notification rule: %s", new_notification_rule)
81+
response = client.create_or_update_notification_rule(notification_rule_id, new_notification_rule)
82+
logger.info("Notification rule updated successfully.")
83+
return response.as_dict()
84+
85+
86+
def show_notification_rule(
87+
cmd,
88+
load_test_resource,
89+
notification_rule_id,
90+
resource_group_name=None,
91+
):
92+
client = get_admin_data_plane_client(cmd, load_test_resource, resource_group_name)
93+
logger.info(
94+
"Getting notification rule with id: %s", notification_rule_id
95+
)
96+
response = client.get_notification_rule(notification_rule_id)
97+
logger.debug("Fetched notification rule: %s", response)
98+
return response.as_dict()
99+
100+
101+
def list_notification_rules(
102+
cmd,
103+
load_test_resource,
104+
resource_group_name=None,
105+
test_ids=None,
106+
):
107+
logger.info("Listing notification rules.")
108+
client = get_admin_data_plane_client(cmd, load_test_resource, resource_group_name)
109+
if test_ids:
110+
test_ids = ",".join(test_ids)
111+
logger.info("Filtering notification rules by test ids: %s", test_ids)
112+
responses = client.list_notification_rule(test_ids=test_ids)
113+
logger.info("Retrieved notification rules: %s", responses)
114+
return [response.as_dict() for response in responses]
115+
116+
117+
def delete_notification_rule(
118+
cmd,
119+
load_test_resource,
120+
notification_rule_id,
121+
resource_group_name=None,
122+
):
123+
logger.info(
124+
"Deleting notification rule with id: %s", notification_rule_id
125+
)
126+
client = get_admin_data_plane_client(cmd, load_test_resource, resource_group_name)
127+
client.delete_notification_rule(notification_rule_id)
128+
logger.info("Deleted notification rule.")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
6+
# pylint: disable=line-too-long
7+
# pylint: disable=too-many-lines
8+
9+
helps = {}
10+
11+
helps[
12+
"load notification-rule create"
13+
] = """
14+
type: command
15+
short-summary: Create a new notification rule for load test resource.
16+
examples:
17+
- name: Create a notification rule for all events.
18+
text: |
19+
az load notification-rule create --load-test-resource sample-alt-resource --resource-group sample-rg --action-groups /subscriptions/000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.insights/actiongroups/sample-ag --notification-rule-id notification-sample-id --all-events
20+
- name: Create a notification rule for all tests and TestRunEnded event.
21+
text: |
22+
az load notification-rule create --load-test-resource sample-alt-resource --resource-group sample-rg --action-groups /subscriptions/000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.insights/actiongroups/sample-ag --notification-rule-id notification-sample-id --all-tests --event event-id=event1 type=TestRunEnded status=DONE,FAILED result=PASSED
23+
"""
24+
25+
helps[
26+
"load notification-rule update"
27+
] = """
28+
type: command
29+
short-summary: Update an existing notification rule for load test resource.
30+
examples:
31+
- name: Update a notification rule enabled for all tests.
32+
text: |
33+
az load notification-rule update --load-test-resource sample-alt-resource --resource-group sample-rg --notification-rule-id notification-sample-id --all-tests
34+
- name: Add a TestRunStarted event to an existing notification rule.
35+
text: |
36+
az load notification-rule update --load-test-resource sample-alt-resource --resource-group sample-rg --notification-rule-id notification-sample-id --add-event event-id=event1 type=TestRunStarted
37+
- name: Remove an event from an existing notification rule and update the action group list.
38+
text: |
39+
az load notification-rule update --load-test-resource sample-alt-resource --resource-group sample-rg --notification-rule-id notification-sample-id --remove-event event-id=event1 --action-groups /subscriptions/000000-0000-0000-0000-000000000000/resourcegroups/sample-rg/providers/microsoft.insights/actiongroups/sample-ag
40+
"""
41+
42+
helps[
43+
"load notification-rule show"
44+
] = """
45+
type: command
46+
short-summary: Get the specified notification rule for load test resource.
47+
examples:
48+
- name: Get a notification rule.
49+
text: |
50+
az load notification-rule show --load-test-resource sample-alt-resource --resource-group sample-rg --notification-rule-id notification-sample-id
51+
"""
52+
53+
helps[
54+
"load notification-rule delete"
55+
] = """
56+
type: command
57+
short-summary: Delete the specified notification rule for load test resource.
58+
examples:
59+
- name: Delete a notification rule.
60+
text: |
61+
az load notification-rule delete --load-test-resource sample-alt-resource --resource-group sample-rg --notification-rule-id notification-sample-id --yes
62+
"""
63+
64+
helps[
65+
"load notification-rule list"
66+
] = """
67+
type: command
68+
short-summary: List all the notification rules for load test resource.
69+
examples:
70+
- name: List all notification rules.
71+
text: |
72+
az load notification-rule list --load-test-resource sample-alt-resource --resource-group sample-rg
73+
- name: List all notification rules for the specified test IDs.
74+
text: |
75+
az load notification-rule list --load-test-resource sample-alt-resource --resource-group sample-rg --test-ids sample-test-id
76+
"""
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
# --------------------------------------------------------------------------------------------
2+
# Copyright (c) Microsoft Corporation. All rights reserved.
3+
# Licensed under the MIT License. See License.txt in the project root for license information.
4+
# --------------------------------------------------------------------------------------------
5+
6+
# pylint: disable=too-many-lines
7+
# pylint: disable=too-many-statements
8+
# pylint: disable=line-too-long
9+
10+
from azext_load.data_plane.utils import argtypes
11+
12+
13+
def load_arguments(self, _):
14+
# Load Trigger Schedule
15+
with self.argument_context("load notification-rule") as c:
16+
c.argument("load_test_resource", argtypes.load_test_resource)
17+
c.argument("resource_group_name", argtypes.resource_group)
18+
19+
# Load notification-rule create
20+
with self.argument_context("load notification-rule create") as c:
21+
c.argument("display_name", argtypes.notification_display_name)
22+
c.argument("notification_rule_id", argtypes.notification_rule_id)
23+
c.argument("test_ids", argtypes.notification_rule_test_ids)
24+
c.argument("event", argtypes.notification_rule_event)
25+
c.argument("action_groups", argtypes.action_groups)
26+
c.argument("all_tests", argtypes.notification_all_tests)
27+
c.argument("all_events", argtypes.notification_all_events)
28+
29+
# Load notification-rule update
30+
with self.argument_context("load notification-rule update") as c:
31+
c.argument("display_name", argtypes.notification_display_name)
32+
c.argument("notification_rule_id", argtypes.notification_rule_id)
33+
c.argument("test_ids", argtypes.notification_rule_test_ids)
34+
c.argument("add_event", argtypes.notification_rule_add_event)
35+
c.argument("remove_event", argtypes.notification_rule_remove_event)
36+
c.argument("action_groups", argtypes.action_groups)
37+
c.argument("all_tests", argtypes.notification_all_tests)
38+
39+
# Load notification-rule list
40+
with self.argument_context("load notification-rule list") as c:
41+
c.argument("test_ids", argtypes.notification_rule_test_ids)
42+
43+
# Load notification-rule show
44+
with self.argument_context("load notification-rule show") as c:
45+
c.argument("notification_rule_id", argtypes.notification_rule_id)
46+
47+
# Load notification-rule delete
48+
with self.argument_context("load notification-rule delete") as c:
49+
c.argument("notification_rule_id", argtypes.notification_rule_id)

0 commit comments

Comments
 (0)