|
| 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.") |
0 commit comments