1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174
|
# coding: utf-8
#-------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
#--------------------------------------------------------------------------
# TEST SCENARIO COVERAGE
# ----------------------
# Methods Total : 63
# Methods Covered : 63
# Examples Total : 87
# Examples Tested : 87
# Coverage % : 100
# ----------------------
# covered ops:
# action_groups: 7/7
# activity_log_alerts: 6/6
# activity_logs: 1/1
# autoscale_settings: 6/6
# baselines: 1/1
# alert_rule_incidents: 0/2 # TODO: cannot test it in this sub
# alert_rules: 0/6 # TODO: cannot test it in this sub
# baseline: 0/1 # TODO: need check whether it is outdated
# diagnostic_settings: 4/4
# diagnostic_settings_category: 2/2
# guest_diagnostics_settings: 0/6 TODO: InvalidResourceType, it seems like outdated
# guest_diagnostics_settings_association: 0/6 TODO: InvalidResourceType, it seems like outdated
# event_categories: 1/1
# log_profiles: 5/5
# metric_alerts: 6/6
# metric_alerts_status: 2/2
# metric_baseline: 0/2 TODO: bad request
# metric_definitions: 1/1
# metric_namespaces: 1/1
# metrics: 1/1
# operations: 1/1
# scheduled_query_rules: 6/6
# service_diagnostic_settings: 0/3 TODO: InvalidResourceType, it seems like outdated
# tenant_activity_logs: 1/1
# vm_insights: 1/1
import time
import unittest
import azure.mgmt.monitor
import azure.mgmt.monitor.models
from devtools_testutils import AzureMgmtTestCase, RandomNameResourceGroupPreparer
AZURE_LOCATION = 'eastus'
class MgmtMonitorClientTest(AzureMgmtTestCase):
def setUp(self):
super(MgmtMonitorClientTest, self).setUp()
self.mgmt_client = self.create_mgmt_client(
azure.mgmt.monitor.MonitorClient
)
if self.is_live:
from azure.mgmt.storage import StorageManagementClient
self.storage_client = self.create_mgmt_client(
StorageManagementClient
)
from azure.mgmt.eventhub import EventHubManagementClient
self.eventhub_client = self.create_mgmt_client(
azure.mgmt.eventhub.EventHubManagementClient
)
from azure.mgmt.loganalytics import LogAnalyticsManagementClient
self.loganalytics_client = self.create_mgmt_client(
LogAnalyticsManagementClient
)
from azure.mgmt.web import WebSiteManagementClient
self.web_client = self.create_mgmt_client(
WebSiteManagementClient
)
from azure.mgmt.compute import ComputeManagementClient
self.vm_client = self.create_mgmt_client(
ComputeManagementClient
)
from azure.mgmt.network import NetworkManagementClient
self.network_client = self.create_mgmt_client(
NetworkManagementClient
)
from azure.mgmt.applicationinsights import ApplicationInsightsManagementClient
self.insight_client = self.create_mgmt_client(
ApplicationInsightsManagementClient
)
from azure.mgmt.logic import LogicManagementClient
self.logic_client = self.create_mgmt_client(
LogicManagementClient
)
def create_workflow(self, group_name, location, workflow_name):
workflow = self.logic_client.workflows.create_or_update(
group_name,
workflow_name,
azure.mgmt.logic.models.Workflow(
location=location,
definition={
"$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
"contentVersion": "1.0.0.0",
"parameters": {},
"triggers": {},
"actions": {},
"outputs": {}
}
)
)
return workflow
# use track 1 version
def create_storage_account(self,
group_name,
location,
storage_name
):
from azure.mgmt.storage import models
params_create = models.StorageAccountCreateParameters(
sku=models.Sku(name=models.SkuName.standard_lrs),
kind=models.Kind.storage,
location=location
)
result_create = self.storage_client.storage_accounts.create(
group_name,
storage_name,
params_create,
)
account = result_create.result()
return account.id
# use eventhub track 1 verison
def create_event_hub_authorization_rule(
self,
group_name,
location,
name_space,
eventhub,
authorization_rule,
storage_account_id
):
# NamespaceCreate[put]
BODY = {
"sku": {
"name": "Standard",
"tier": "Standard"
},
"location": location,
"tags": {
"tag1": "value1",
"tag2": "value2"
}
}
result = self.eventhub_client.namespaces.create_or_update(group_name, name_space, BODY)
result.result()
# NameSpaceAuthorizationRuleCreate[put]
BODY = {
"rights": [
"Listen",
"Send",
"Manage"
]
}
result = self.eventhub_client.namespaces.create_or_update_authorization_rule(group_name, name_space, authorization_rule, BODY["rights"])
# EventHubCreate[put]
BODY = {
"message_retention_in_days": "4",
"partition_count": "4",
"status": "Active",
"capture_description": {
"enabled": True,
"encoding": "Avro",
"interval_in_seconds": "120",
"size_limit_in_bytes": "10485763",
"destination": {
"name": "EventHubArchive.AzureBlockBlob",
"storage_account_resource_id": storage_account_id,
"blob_container": "container",
"archive_name_format": "{Namespace}/{EventHub}/{PartitionId}/{Year}/{Month}/{Day}/{Hour}/{Minute}/{Second}"
}
}
}
result = self.eventhub_client.event_hubs.create_or_update(group_name, name_space, eventhub, BODY)
# EventHubAuthorizationRuleCreate[put]
BODY = {
"rights": [
"Listen",
"Send",
"Manage"
]
}
result = self.eventhub_client.event_hubs.create_or_update_authorization_rule(group_name, name_space, eventhub, authorization_rule, BODY["rights"])
# use track 1 version
def create_workspace(
self,
group_name,
location,
workspace_name
):
BODY = {
"sku": {
"name": "PerNode"
},
"retention_in_days": 30,
"location": location,
"tags": {
"tag1": "val1"
}
}
result = self.loganalytics_client.workspaces.create_or_update(
group_name,
workspace_name,
BODY
)
return result.result()
# use track 1 version
def create_site(self, group_name, location, site_name, app_service_plan_name):
# server_farm_async_operation = self.web_client.app_service_plans.create_or_update(
# group_name,
# app_service_plan_name,
# azure.mgmt.web.models.AppServicePlan(
# location=location,
# sku=azure.mgmt.web.models.SkuDescription(
# name='S1',
# capacity=1,
# tier='Standard'
# )
# )
# )
# server_farm = server_farm_async_operation.result()
# Create a Site to be hosted in the Server Farm
site_async_operation = self.web_client.web_apps.create_or_update(
group_name,
site_name,
azure.mgmt.web.models.Site(
location=self.region,
# server_farm_id=server_farm.id
)
)
site = site_async_operation.result()
return site
# use track 1 version
def create_virtual_network(self, group_name, location, network_name, subnet_name):
azure_operation_poller = self.network_client.virtual_networks.create_or_update(
group_name,
network_name,
{
'location': location,
'address_space': {
'address_prefixes': ['10.0.0.0/16']
}
},
)
result_create = azure_operation_poller.result()
async_subnet_creation = self.network_client.subnets.create_or_update(
group_name,
network_name,
subnet_name,
{'address_prefix': '10.0.0.0/24'}
)
subnet_info = async_subnet_creation.result()
return subnet_info
# use track 1 version
def create_network_interface(self, group_name, location, nic_name, subnet):
async_nic_creation = self.network_client.network_interfaces.create_or_update(
group_name,
nic_name,
{
'location': location,
'ip_configurations': [{
'name': 'MyIpConfig',
'subnet': {
'id': subnet.id
}
}]
}
)
nic_info = async_nic_creation.result()
return nic_info.id
# use track 1 version
def create_vm(
self,
group_name,
location,
vm_name,
network_name,
subnet_name,
interface_name
):
subnet = self.create_virtual_network(group_name, location, network_name, subnet_name)
NIC_ID = self.create_network_interface(group_name, location, interface_name, subnet)
# Create a vm with empty data disks.[put]
BODY = {
"location": "eastus",
"hardware_profile": {
"vm_size": "Standard_D2_v2"
},
"storage_profile": {
"image_reference": {
"sku": "2016-Datacenter",
"publisher": "MicrosoftWindowsServer",
"version": "latest",
"offer": "WindowsServer"
},
"os_disk": {
"caching": "ReadWrite",
"managed_disk": {
"storage_account_type": "Standard_LRS"
},
"name": "myVMosdisk",
"create_option": "FromImage"
},
"data_disks": [
{
"disk_size_gb": "1023",
"create_option": "Empty",
"lun": "0"
},
{
"disk_size_gb": "1023",
"create_option": "Empty",
"lun": "1"
}
]
},
"os_profile": {
"admin_username": "testuser",
"computer_name": "myVM",
"admin_password": "Aa1!zyx_",
"windows_configuration": {
"enable_automatic_updates": True # need automatic update for reimage
}
},
"network_profile": {
"network_interfaces": [
{
# "id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Network/networkInterfaces/" + NIC_ID + "",
"id": NIC_ID,
"properties": {
"primary": True
}
}
]
}
}
result = self.vm_client.virtual_machines.create_or_update(group_name, vm_name, BODY)
return result.result()
# use track 1 version
def create_vmss(
self,
group_name,
location,
vmss_name,
network_name,
subnet_name,
interface_name
):
subnet = self.create_virtual_network(group_name, location, network_name, subnet_name)
NIC_ID = self.create_network_interface(group_name, location, interface_name, subnet)
# Create a scale set with empty data disks on each vm.[put]
BODY = {
"sku": {
"tier": "Standard",
"capacity": "2",
"name": "Standard_D1_v2"
},
"location": location,
"overprovision": True,
"virtual_machine_profile": {
"storage_profile": {
"image_reference": {
"offer": "UbuntuServer",
"publisher": "Canonical",
"sku": "18.04-LTS",
"version": "latest"
},
"os_disk": {
"caching": "ReadWrite",
"managed_disk": {
"storage_account_type": "Standard_LRS"
},
"create_option": "FromImage",
"disk_size_gb": "512"
}
},
"os_profile": {
"computer_name_prefix": "testPC",
"admin_username": "testuser",
"admin_password": "Aa!1()-xyz"
},
"network_profile": {
"network_interface_configurations": [
{
"name": "testPC",
"primary": True,
"enable_ipforwarding": True,
"ip_configurations": [
{
"name": "testPC",
"properties": {
"subnet": {
# "id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Network/virtualNetworks/" + NETWORK_NAME + "/subnets/" + SUBNET_NAME + ""
"id": subnet.id
}
}
}
]
}
]
}
},
"upgrade_policy": {
"mode": "Manual"
},
"upgrade_mode": "Manual"
}
result = self.vm_client.virtual_machine_scale_sets.create_or_update(group_name, vmss_name, BODY)
vmss = result.result()
return vmss
@RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
def test_monitor_diagnostic_settings(self, resource_group):
SUBSCRIPTION_ID = self.settings.SUBSCRIPTION_ID
RESOURCE_GROUP = resource_group.name
# RESOURCE_URI = "subscriptions/{}/resourcegroups/{}".format(SUBSCRIPTION_ID, RESOURCE_GROUP)
STORAGE_ACCOUNT_NAME = self.get_resource_name("storageaccountx")
NAMESPACE_NAME = self.get_resource_name("namespacex")
EVENTHUB_NAME = self.get_resource_name("eventhubx")
AUTHORIZATIONRULE_NAME = self.get_resource_name("authorizationrulex")
INSIGHT_NAME = self.get_resource_name("insightx")
WORKSPACE_NAME = self.get_resource_name("workspacex")
WORKFLOW_NAME = self.get_resource_name("workflow")
if self.is_live:
storage_account_id = self.create_storage_account(RESOURCE_GROUP, AZURE_LOCATION, STORAGE_ACCOUNT_NAME)
self.create_event_hub_authorization_rule(RESOURCE_GROUP, AZURE_LOCATION, NAMESPACE_NAME, EVENTHUB_NAME, AUTHORIZATIONRULE_NAME, storage_account_id)
workspace = self.create_workspace(RESOURCE_GROUP, AZURE_LOCATION, WORKSPACE_NAME)
workflow = self.create_workflow(RESOURCE_GROUP, AZURE_LOCATION, WORKFLOW_NAME)
RESOURCE_URI = workflow.id
workspace_id = workspace.id
else:
RESOURCE_URI = "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Logic/workflows/" + WORKFLOW_NAME
workspace_id = "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.OperationalInsights/workspaces/" + WORKSPACE_NAME
# Creates or Updates the diagnostic setting[put]
BODY = {
"storage_account_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Storage/storageAccounts/" + STORAGE_ACCOUNT_NAME + "",
# "workspace_id": "",
"workspace_id": workspace_id,
# "event_hub_authorization_rule_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/microsoft.eventhub/namespaces/" + NAMESPACE_NAME + "/eventhubs/" + EVENTHUB_NAME + "/authorizationrules/" + AUTHORIZATIONRULE_NAME + "",
"event_hub_authorization_rule_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/microsoft.eventhub/namespaces/" + NAMESPACE_NAME + "/authorizationrules/" + AUTHORIZATIONRULE_NAME,
"event_hub_name": EVENTHUB_NAME,
"metrics": [
# {
# "category": "WorkflowMetrics",
# "enabled": True,
# "retention_policy": {
# "enabled": False,
# "days": "0"
# }
# }
],
"logs": [
{
"category": "WorkflowRuntime",
"enabled": True,
"retention_policy": {
"enabled": False,
"days": "0"
}
}
],
# "log_analytics_destination_type": "Dedicated"
}
diagnostic_settings = self.mgmt_client.diagnostic_settings.create_or_update(RESOURCE_URI, INSIGHT_NAME, BODY)
# TODO: resourceGroups has been changed to resourcegroups
RESOURCE_URI = "subscriptions/{sub}/resourcegroups/{group}/providers/microsoft.logic/workflows/{workflow}".format(
sub=SUBSCRIPTION_ID,
group=RESOURCE_GROUP,
workflow=WORKFLOW_NAME
)
# List diagnostic settings categories
categories = self.mgmt_client.diagnostic_settings_category.list(RESOURCE_URI)
# List diagnostic settings[get]
result = self.mgmt_client.diagnostic_settings.list(RESOURCE_URI)
# Gets the diagnostic setting[get]
result = self.mgmt_client.diagnostic_settings.get(RESOURCE_URI, INSIGHT_NAME)
# Get diagnostic settings category
self.mgmt_client.diagnostic_settings_category.get(RESOURCE_URI, categories.value[0].name)
# Deletes the diagnostic setting[delete]
result = self.mgmt_client.diagnostic_settings.delete(RESOURCE_URI, INSIGHT_NAME)
@RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
def test_log_profiles(self, resource_group):
SUBSCRIPTION_ID = self.settings.SUBSCRIPTION_ID
RESOURCE_GROUP = resource_group.name
LOGPROFILE_NAME = self.get_resource_name("logprofilex")
STORAGE_ACCOUNT_NAME = self.get_resource_name("storageaccountx")
if self.is_live:
storage_account_id = self.create_storage_account(RESOURCE_GROUP, AZURE_LOCATION, STORAGE_ACCOUNT_NAME)
else:
storage_account_id = "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Storage/storageAccounts/" + STORAGE_ACCOUNT_NAME
# Create or update a log profile[put]
BODY = {
"location": "",
"locations": [
"global"
],
"categories": [
"Write",
"Delete",
"Action"
],
"retention_policy": {
"enabled": True,
"days": "3"
},
# "storage_account_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Storage/storageAccounts/" + STORAGE_ACCOUNT_NAME + "",
"storage_account_id": storage_account_id,
# "service_bus_rule_id": ""
}
result = self.mgmt_client.log_profiles.create_or_update(LOGPROFILE_NAME, BODY)
if self.is_live:
time.sleep(30)
# Get log profile[get]
result = self.mgmt_client.log_profiles.get(LOGPROFILE_NAME)
# List log profiles[get]
result = self.mgmt_client.log_profiles.list()
# TODO: azure.core.exceptions.HttpResponseError: (Method not allowed) Exception of type 'Microsoft.WindowsAzure.Management.Monitoring.MonitoringServiceException' was thrown.
# Update a log profile[patch]
# BODY = {
# "locations": [
# "global"
# ],
# "categories": [
# "Write",
# "Delete",
# "Action"
# ],
# "retention_policy": {
# "enabled": True,
# "days": "3"
# }
# }
# result = self.mgmt_client.log_profiles.update(LOGPROFILE_NAME, BODY)
# Delete log profile[delete]
result = self.mgmt_client.log_profiles.delete(LOGPROFILE_NAME)
@unittest.skip("cannot create or modify classic metric alerts")
@RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
def test_alert_rule(self, resource_group):
SUBSCRIPTION_ID = self.settings.SUBSCRIPTION_ID
RESOURCE_GROUP = resource_group.name
APP_SERVICE_PLAN_NAME = self.get_resource_name('pyarmappserviceplan')
SITE_NAME = self.get_resource_name('pyarmsite')
ALERTRULE_NAME = rule_name = self.get_resource_name('alertrule')
# site = self.create_site(RESOURCE_GROUP, AZURE_LOCATION, SITE_NAME, APP_SERVICE_PLAN_NAME)
VM_NAME = "vm_name"
NETWORK_NAME = "networkxx"
SUBNET_NAME = "subnetx"
INTERFACE_NAME = "interfacexx"
if self.is_live:
vm = self.create_vm(RESOURCE_GROUP, AZURE_LOCATION, VM_NAME, NETWORK_NAME, SUBNET_NAME, INTERFACE_NAME)
resource_id = vm.id
else:
resource_id = "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Compute/virtualMachines/" + VM_NAME
# I need a subclass of "RuleDataSource"
data_source = azure.mgmt.monitor.models.RuleMetricDataSource(
resource_uri=resource_id,
metric_name='CPU Credits Consumed'
)
# I need a subclasses of "RuleCondition"
rule_condition = azure.mgmt.monitor.models.ThresholdRuleCondition(
data_source=data_source,
operator='GreaterThanOrEqual',
threshold=90,
window_size='PT5M',
time_aggregation='Average'
)
# I need a subclass of "RuleAction"
rule_action = azure.mgmt.monitor.models.RuleEmailAction(
send_to_service_owners=True,
custom_emails=[
'monitoringemail@microsoft.com'
]
)
# TODO: You cannot create or modify classic metric alerts for this subscription as this subscription 92f95d8f-3c67-4124-91c7-8cf07cdbf241 is being migrated or has been migrated to use new metric alerts. Learn more - aka.ms/alertclassicretirement
my_alert = self.mgmt_client.alert_rules.create_or_update(
resource_group.name,
rule_name,
{
'location': AZURE_LOCATION,
'name_properties_name': rule_name,
'description': 'Testing Alert rule creation',
'is_enabled': True,
'condition': rule_condition,
'actions': [
# rule_action
]
}
)
# Get an alert rule[get]
result = self.mgmt_client.alert_rules.get(resource_group.name, ALERTRULE_NAME)
# List alert rules[get]
result = self.mgmt_client.alert_rules.list_by_resource_group(resource_group.name)
# List alert rule incidents
incidents = self.mgmt_client.alert_rule_incidents.list_by_alert_rule(resource_group.name, ALERTRULE_NAME)
# Get alert rule incident
result = self.mgmt_client.alert_rule_incidents.get(resource_group.name, ALERTRULE_NAME, incidents.value[0].name)
# List alert rules[get]
result = self.mgmt_client.alert_rules.list_by_subscription(resource_group.name)
# TODO: fix later
# Patch an alert rule[patch]
# BODY = {
# "name": "chiricutin",
# "description": "Pura Vida",
# "is_enabled": True,
# "condition": {
# "odata.type": "Microsoft.Azure.Management.Insights.Models.ThresholdRuleCondition",
# # "data_source": {
# # "odata.type": "Microsoft.Azure.Management.Insights.Models.RuleMetricDataSource",
# # # "resource_uri": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Web/sites/" + SITE_NAME + "",
# # "resource_uri": site.id,
# # "metric_name": "Requests"
# # },
# "operator": "GreaterThan",
# "threshold": "3",
# "window_size": "PT5M",
# "time_aggregation": "Total"
# },
# # "last_updated_time": "2016-11-23T21:23:52.0221265Z",
# "actions": []
# }
# result = self.mgmt_client.alert_rules.update(resource_group.name, ALERTRULE_NAME, BODY)
# Delete an alert rulte[delete]
result = self.mgmt_client.alert_rules.delete(resource_group.name, ALERTRULE_NAME)
@RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
def test_metric_alerts(self, resource_group):
SUBSCRIPTION_ID = self.settings.SUBSCRIPTION_ID
RESOURCE_GROUP = resource_group.name
METRIC_ALERT_NAME = "metricnamexx"
VM_NAME = "vm_name"
NETWORK_NAME = "networkxx"
SUBNET_NAME = "subnetx"
INTERFACE_NAME = "interfacexx"
INSIGHT_NAME = "PercentageCPU"
if self.is_live:
vm = self.create_vm(RESOURCE_GROUP, AZURE_LOCATION, VM_NAME, NETWORK_NAME, SUBNET_NAME, INTERFACE_NAME)
RESOURCE_URI = vm.id
else:
RESOURCE_URI = "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Compute/virtualMachines/" + VM_NAME
# Get a list of operations for a resource provider[get]
result = self.mgmt_client.operations.list()
# Create or update a metric alert[put]
BODY = {
"location": "global",
"description": "This is the description of the rule1",
"severity": "3",
"enabled": True,
"scopes": [
# "/subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/gigtest/providers/Microsoft.Compute/virtualMachines/gigwadme"
RESOURCE_URI
],
"evaluation_frequency": "PT1M",
"window_size": "PT15M",
"target_resource_type": "Microsoft.Compute/virtualMachines",
"target_resource_region": "southcentralus",
"criteria": {
"odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria",
"all_of": [
{
"criterion_type": "DynamicThresholdCriterion",
"name": "High_CPU_80",
"metric_name": "Percentage CPU",
"metric_namespace": "microsoft.compute/virtualmachines",
"operator": "GreaterOrLessThan",
"time_aggregation": "Average",
"dimensions": [],
"alert_sensitivity": "Medium",
"failing_periods": {
"number_of_evaluation_periods": "4",
"min_failing_periods_to_alert": "4"
},
# "ignore_data_before": "2019-04-04T21:00:00Z"
}
]
},
"auto_mitigate": False,
"actions": [
# {
# "action_group_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/microsoft.insights/notificationgroups/" + NOTIFICATIONGROUP_NAME + "",
# "web_hook_properties": {
# "key11": "value11",
# "key12": "value12"
# }
# }
]
}
result = self.mgmt_client.metric_alerts.create_or_update(resource_group.name, METRIC_ALERT_NAME, BODY)
if self.is_live:
time.sleep(30)
# Get a web test alert rule[get]
result = self.mgmt_client.metric_alerts.get(resource_group.name, METRIC_ALERT_NAME)
# List metric alerts status
alerts_status = self.mgmt_client.metric_alerts_status.list(resource_group.name, METRIC_ALERT_NAME)
# Get an alert rule status[get]
STATUS_NAME = alerts_status.value[0].name
result = self.mgmt_client.metric_alerts_status.list_by_name(resource_group.name, METRIC_ALERT_NAME, STATUS_NAME)
# List metric alert rules[get]
result = self.mgmt_client.metric_alerts.list_by_resource_group(resource_group.name)
# List metric alert rules[get]
result = self.mgmt_client.metric_alerts.list_by_subscription()
# Get Metric Definitions without filter[get]
result = self.mgmt_client.metric_definitions.list(RESOURCE_URI, INSIGHT_NAME)
# Get Metric Namespaces without filter[get]
result = self.mgmt_client.metric_namespaces.list(RESOURCE_URI, INSIGHT_NAME)
# Get onboarding status
result = self.mgmt_client.vm_insights.get_onboarding_status(RESOURCE_URI)
# Get event categories[get]
result = self.mgmt_client.event_categories.list()
# List metrics
result = self.mgmt_client.metrics.list(RESOURCE_URI)
# Get metric baselines[get]
result = self.mgmt_client.baselines.list(RESOURCE_URI, INSIGHT_NAME)
# TODO: outdated
# Get metric baseline[get]
# result = self.mgmt_client.baseline.get(RESOURCE_URI)
# TODO: Operation returned an invalid status 'Bad Request'
# Get Metric for data[get]
# result = self.mgmt_client.metric_baseline.get(RESOURCE_URI, INSIGHT_NAME)
# TODO: outdated
# Calculate baseline[post]
# BODY = {
# "sensitivities": [
# # "Low",
# INSIGHT_NAME
# ],
# "values": [
# "0",
# "62"
# ]
# }
# result = self.mgmt_client.metric_baseline.calculate_baseline(RESOURCE_URI, BODY)
# TODO: fix later
# Update a metric alert[put]
# BODY = {
# "location": "global",
# "description": "This is the description of the rule1",
# "severity": "3",
# "enabled": True,
# "scopes": [
# vm.id
# ],
# "evaluation_frequency": "PT1M",
# "window_size": "PT15M",
# "target_resource_type": "Microsoft.Compute/virtualMachines",
# "target_resource_region": "southcentralus",
# "criteria": {
# "odata.type": "Microsoft.Azure.Monitor.MultipleResourceMultipleMetricCriteria",
# "all_of": [
# {
# "criterion_type": "DynamicThresholdCriterion",
# "name": "High_CPU_80",
# "metric_name": "Percentage CPU",
# "metric_namespace": "microsoft.compute/virtualmachines",
# "operator": "GreaterOrLessThan",
# "time_aggregation": "Average",
# "dimensions": [],
# "alert_sensitivity": "Medium",
# "failing_periods": {
# "number_of_evaluation_periods": "4",
# "min_failing_periods_to_alert": "4"
# }
# }
# ]
# },
# "auto_mitigate": False,
# "actions": []
# }
# result = self.mgmt_client.metric_alerts.update(resource_group.name, METRIC_ALERT_NAME, BODY)
# Delete an alert rule[delete]
result = self.mgmt_client.metric_alerts.delete(resource_group.name, METRIC_ALERT_NAME)
@RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
def test_action_groups(self, resource_group):
ACTION_GROUP_NAME = self.get_resource_name("actiongroup")
# Create or update an action group[put]
BODY = {
"location": "Global",
"group_short_name": "sample",
"enabled": True,
"email_receivers": [
{
"name": "John Doe's email",
"email_address": "johndoe@email.com",
"use_common_alert_schema": False
}
],
"sms_receivers": [
{
"name": "John Doe's mobile",
"country_code": "1",
"phone_number": "1234567890"
}
]
}
result = self.mgmt_client.action_groups.create_or_update(resource_group.name, ACTION_GROUP_NAME, BODY)
# Get an action group[get]
result = self.mgmt_client.action_groups.get(resource_group.name, ACTION_GROUP_NAME)
# List action groups[get]
result = self.mgmt_client.action_groups.list_by_resource_group(resource_group.name)
# List action groups[get]
result = self.mgmt_client.action_groups.list_by_subscription_id()
# Enable the receiver[post]
BODY = {
"receiver_name": "John Doe's mobile"
}
result = self.mgmt_client.action_groups.enable_receiver(resource_group.name, ACTION_GROUP_NAME, BODY)
# Patch an action group[patch]
BODY = {
"tags": {
"key1": "value1",
"key2": "value2"
},
"properties": {
"enabled": False
}
}
result = self.mgmt_client.action_groups.update(resource_group.name, ACTION_GROUP_NAME, BODY)
# Delete an action group[delete]
result = self.mgmt_client.action_groups.delete(resource_group.name, ACTION_GROUP_NAME)
@RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
def test_activity_log_alerts(self, resource_group):
SUBSCRIPTION_ID = self.settings.SUBSCRIPTION_ID
ACTIVITY_LOG_ALERT_NAME = self.get_resource_name("activitylogalertx")
# Create or update an activity log alert[put]
BODY = {
"location": "Global",
"scopes": [
"subscriptions/" + SUBSCRIPTION_ID
],
"enabled": True,
"condition": {
"all_of": [
{
"field": "category",
"equals": "Administrative"
},
{
"field": "level",
"equals": "Error"
}
]
},
"actions": {
"action_groups": [
# {
# "action_group_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/microsoft.insights/actionGroups/" + ACTION_GROUP_NAME + "",
# "webhook_properties": {
# "sample_webhook_property": "samplePropertyValue"
# }
# }
]
},
"description": "Sample activity log alert description"
}
result = self.mgmt_client.activity_log_alerts.create_or_update(resource_group.name, ACTIVITY_LOG_ALERT_NAME, BODY)
# Get an activity log alert[get]
result = self.mgmt_client.activity_log_alerts.get(resource_group.name, ACTIVITY_LOG_ALERT_NAME)
# List activity log alerts[get]
result = self.mgmt_client.activity_log_alerts.list_by_resource_group(resource_group.name)
# List activity log alerts by subscription[get]
result = self.mgmt_client.activity_log_alerts.list_by_subscription_id()
# List activity_logs[get]
FILTER = "resourceGroupName eq '{}'".format(resource_group.name)
result = self.mgmt_client.activity_logs.list(FILTER)
# List tenant activity logs
FILTER = "resourceGroupName eq '{}'".format(resource_group.name)
result = self.mgmt_client.tenant_activity_logs.list(FILTER)
# Patch an activity log alert[patch]
BODY = {
"tags": {
"key1": "value1",
"key2": "value2"
},
"properties": {
"enabled": False
}
}
result = self.mgmt_client.activity_log_alerts.update(resource_group.name, ACTIVITY_LOG_ALERT_NAME, BODY)
# Delete an activity log alert[delete]
result = self.mgmt_client.activity_log_alerts.delete(resource_group.name, ACTIVITY_LOG_ALERT_NAME)
@RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
def test_autoscale_settings(self, resource_group):
SUBSCRIPTION_ID = self.settings.SUBSCRIPTION_ID
RESOURCE_GROUP = resource_group.name
AUTOSCALESETTING_NAME = "autoscalesetting"
VMSS_NAME = "vmss_name"
NETWORK_NAME = "networkxx"
SUBNET_NAME = "subnetx"
INTERFACE_NAME = "interfacexx"
if self.is_live:
vmss = self.create_vmss(RESOURCE_GROUP, AZURE_LOCATION, VMSS_NAME, NETWORK_NAME, SUBNET_NAME, INTERFACE_NAME)
vmss_id = vmss.id
else:
vmss_id = "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.Compute/virtualMachineScaleSets/" + VMSS_NAME
# Create or update an autoscale setting[put]
BODY = {
"location": "West US",
"profiles": [
{
"name": "adios",
"capacity": {
"minimum": "1",
"maximum": "10",
"default": "1"
},
"rules": [
]
}
],
"enabled": True,
"target_resource_uri": vmss_id,
"notifications": [
{
"operation": "Scale",
"email": {
"send_to_subscription_administrator": True,
"send_to_subscription_co_administrators": True,
"custom_emails": [
"gu@ms.com",
"ge@ns.net"
]
},
"webhooks": [
]
}
]
}
result = self.mgmt_client.autoscale_settings.create_or_update(resource_group.name, AUTOSCALESETTING_NAME, BODY)
# Get an autoscale setting[get]
result = self.mgmt_client.autoscale_settings.get(resource_group.name, AUTOSCALESETTING_NAME)
# List autoscale settings[get]
result = self.mgmt_client.autoscale_settings.list_by_resource_group(resource_group.name)
# List autoscale settings[get]
result = self.mgmt_client.autoscale_settings.list_by_subscription()
# Update an autoscale setting[put]
BODY = {
"location": "West US",
"profiles": [
{
"name": "adios",
"capacity": {
"minimum": "1",
"maximum": "10",
"default": "1"
},
"rules": [
]
}
],
"enabled": True,
"target_resource_uri": vmss_id,
"notifications": [
{
"operation": "Scale",
"email": {
"send_to_subscription_administrator": True,
"send_to_subscription_co_administrators": True,
"custom_emails": [
"gu@ms.com",
"ge@ns.net"
]
},
"webhooks": [
]
}
]
}
result = self.mgmt_client.autoscale_settings.update(resource_group.name, AUTOSCALESETTING_NAME, BODY)
# Delete an autoscale setting[delete]
result = self.mgmt_client.autoscale_settings.delete(resource_group.name, AUTOSCALESETTING_NAME)
@RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
def test_scheduled_query_rules(self, resource_group):
RESOURCE_GROUP = resource_group.name
WORKSPACE_NAME = self.get_resource_name("workspacex")
SCHEDULED_QUERY_RULE_NAME = self.get_resource_name("scheduledqueryrule")
SUBSCRIPTION_ID = self.settings.SUBSCRIPTION_ID
if self.is_live:
workspace = self.create_workspace(RESOURCE_GROUP, AZURE_LOCATION, WORKSPACE_NAME)
workspace_id = workspace.id
else:
workspace_id = "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.OperationalInsights/workspaces/" + WORKSPACE_NAME
# Create or Update rule - AlertingAction[put]
BODY = {
"location": "eastus",
"description": "log alert description",
"enabled": "true",
# "last_updated_time": "2017-06-23T21:23:52.0221265Z",
"provisioning_state": "Succeeded",
"source": {
"query": "Heartbeat | summarize AggregatedValue = count() by bin(TimeGenerated, 5m)",
# "data_source_id": "/subscriptions/" + SUBSCRIPTION_ID + "/resourceGroups/" + RESOURCE_GROUP + "/providers/Microsoft.OperationalInsights/workspaces/" + WORKSPACE_NAME + "",
"data_source_id": workspace_id,
"query_type": "ResultCount"
},
"schedule": {
"frequency_in_minutes": "15",
"time_window_in_minutes": "15"
},
"action": {
"odata.type": "Microsoft.WindowsAzure.Management.Monitoring.Alerts.Models.Microsoft.AppInsights.Nexus.DataContracts.Resources.ScheduledQueryRules.AlertingAction",
"severity": "1",
"azns_action": {
"action_group": [],
"email_subject": "Email Header",
"custom_webhook_payload": "{}"
},
"trigger": {
"threshold_operator": "GreaterThan",
"threshold": "3",
"metric_trigger": {
"threshold_operator": "GreaterThan",
"threshold": "5",
"metric_trigger_type": "Consecutive",
"metric_column": "Computer"
}
}
}
}
result = self.mgmt_client.scheduled_query_rules.create_or_update(resource_group.name, SCHEDULED_QUERY_RULE_NAME, BODY)
# Get rule[get]
result = self.mgmt_client.scheduled_query_rules.get(resource_group.name, SCHEDULED_QUERY_RULE_NAME)
# List rules[get]
result = self.mgmt_client.scheduled_query_rules.list_by_resource_group(resource_group.name)
# List rules[get]
result = self.mgmt_client.scheduled_query_rules.list_by_subscription()
# Patch Log Search Rule[patch]
BODY = {
"enabled": "true"
}
result = self.mgmt_client.scheduled_query_rules.update(resource_group.name, SCHEDULED_QUERY_RULE_NAME, BODY)
# Delete rule[delete]
result = self.mgmt_client.scheduled_query_rules.delete(resource_group.name, SCHEDULED_QUERY_RULE_NAME)
@unittest.skip("(InvalidResourceType) The resource type could not be found in the namespace 'microsoft.insights' for api version '2018-06-01-preview'.")
@RandomNameResourceGroupPreparer(location=AZURE_LOCATION)
def test_guest_diagnostics_settings(self, resource_group):
DIAGNOSTIC_SETTINGS_NAME = self.get_resource_name("diagnosticsettings")
BODY = {
"location": AZURE_LOCATION
}
self.mgmt_client.guest_diagnostics_settings.create_or_update(resource_group.name, DIAGNOSTIC_SETTINGS_NAME, BODY)
self.mgmt_client.guest_diagnostics_settings.list_by_resource_group(resource_group.name)
self.mgmt_client.guest_diagnostics_settings.list()
self.mgmt_client.guest_diagnostics_settings.get(resource_group.name, DIAGNOSTIC_SETTINGS_NAME)
BODY = {
"location": AZURE_LOCATION
}
self.mgmt_client.guest_diagnostics_settings.update(resource_group.name, DIAGNOSTIC_SETTINGS_NAME, BODY)
self.mgmt_client.guest_diagnostics_settings.delete(resource_group.name, DIAGNOSTIC_SETTINGS_NAME)
#------------------------------------------------------------------------------
if __name__ == '__main__':
unittest.main()
|