From 360624fcdf9ec36a14b07f3039e39ad4eb1d368d Mon Sep 17 00:00:00 2001 From: BigCat20196 <1095260342@qq.com> Date: Fri, 23 Jul 2021 10:46:24 +0800 Subject: [PATCH 1/5] create samples --- example_internal_load_balancer.py | 605 ++++++++++++++++++++++++++++++ example_public_load_balancer.py | 520 +++++++++++++++++++++++++ requirements.txt | 9 +- 3 files changed, 1129 insertions(+), 5 deletions(-) create mode 100644 example_internal_load_balancer.py create mode 100644 example_public_load_balancer.py diff --git a/example_internal_load_balancer.py b/example_internal_load_balancer.py new file mode 100644 index 0000000..8304498 --- /dev/null +++ b/example_internal_load_balancer.py @@ -0,0 +1,605 @@ +import os + +from azure.identity import DefaultAzureCredential +from azure.mgmt.resource import ResourceManagementClient +from azure.mgmt.network import NetworkManagementClient +from azure.mgmt.network import models as network_models +from azure.mgmt.compute import ComputeManagementClient +from azure.mgmt.compute import models as compute_models + +# Azure Datacenter +LOCATION = 'eastus' +SKU = 'Standard' +# Resource Group +GROUP_NAME = 'myInternalLoadBalancer1' + +# Network +VNET_NAME = 'myVNet' +ADDRESS_PREFIXES = '10.1.0.0/16' +SUBNET_NAME = 'myBackendSubnet' +SUBNET_PREFIXES = '10.1.0.0/24' + +# Nic ip-config +NIC_IP_CONFIG_NAME = 'ipconfig1' + +# Public IP address for the bastion host +BASTION_PUBLIC_IP_NAME = 'myBastionIP' + +# Bastion subnet +IP_CONFIG_NAME = 'myBastionIPConfig' +BASTION_SUBNET_NAME = 'AzureBastionSubnet' +BASTION_SUBNET_PREFIXES = '10.1.1.0/24' + +# Bastion host +BASTION_NAME = 'myBastionHost' + +# Network security group +NSG_NAME = 'myNSG' + +# Network Security Group Rule +NSG_RULE_NAME = 'myNSGRuleHTTP' + +# Network interfaces for the virtual machines +NIC_NAMES = ['myNicVM1', 'myNicVM2', 'myNicVM3'] + +# Virtual machine +VM_NAMES = ['myVM1', 'myVM2', 'myVM3'] +# VN_IMAGE = 'win2019datacenter' +VM_ADMIN_USERNAME = 'azureuser' +VM_ADMIN_PASSEORD = 'P@ssw0rd123' + +# Public IP address - Standard +PUBLIC_IP_NAME = 'myPublicIP' + +# Public Load balancer +LB_NAME = 'myLoadBalancer' +FIP_NAME = 'myFrontEnd' +BP_NAME = 'myBackEndPool' + +# Health probe +LB_PROBE_NAME = 'myHealthProbe' +LB_PROTOCOL = 'tcp' +LB_PORT = 80 + +# Load balancer rule +LB_RELE_NAME = 'myHTTPRule' + +# NIC Test +NIC_TEST_NAME = 'myNicTestVM' + +# VM Test +VM_TEST_NAME = 'myTestVM' +################################# + +# Single IP for the outbound connectivity +PUBLIC_IP_OB_NAME = 'myPublicIPOutbound' + +# Public IP prefix for the outbound connectivity +PUBLIC_IP_PREDIX_OB_NAME = 'myPublicIPPrefixOutbound' +PUBLIC_IP_PREDIX_OB_LENGTH = 28 + +# Outbound frontend IP configuration +FIP_OB_NAME = 'myFrontEndOutbound' + +# Outbound pool +OB_POOL_NAME = 'myBackendPoolOutbound' + +# Oubtbound rule +OB_RULE = 'myOutboundRule' + + +# Manage resources and resource groups - create, update and delete a resource group, +# deploy a solution into a resource group, export an ARM template. Create, read, update +# and delete a resource +# +# This script expects that the following environment vars are set: +# +# AZURE_TENANT_ID: with your Azure Active Directory tenant id or domain +# AZURE_CLIENT_ID: with your Azure Active Directory Application Client ID +# AZURE_CLIENT_SECRET: with your Azure Active Directory Application Secret +# AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id +# + + +def run_example(): + import time + start_time = time.time() + """Resource Group management example.""" + # + # Create all clients with an Application (service principal) token provider + # + subscription_id = os.environ.get( + 'AZURE_SUBSCRIPTION_ID', + '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id + + resource_client = ResourceManagementClient( + credential=DefaultAzureCredential(), + subscription_id=subscription_id + ) + network_client = NetworkManagementClient( + credential=DefaultAzureCredential(), + subscription_id=subscription_id + ) + compute_client = ComputeManagementClient( + credential=DefaultAzureCredential(), + subscription_id=subscription_id + ) + + # Create Resource group + rg = resource_client.resource_groups.create_or_update( + GROUP_NAME, {'location': LOCATION}) + print('Create Resource Group:\n{}'.format(rg)) + + # Create VNet + network = network_client.virtual_networks.begin_create_or_update( + GROUP_NAME, + VNET_NAME, + network_models.VirtualNetwork( + location=LOCATION, + address_space=network_models.AddressSpace( + address_prefixes=[ADDRESS_PREFIXES] + ), + subnets=[network_models.Subnet( + name=SUBNET_NAME, + address_prefix=SUBNET_PREFIXES + ), ] + ) + ).result() + print("Create Vnet:\n{}".format(network)) + + # Create PublicIP for the Bastion Host + bastion_public_ip = network_client.public_ip_addresses.begin_create_or_update( + GROUP_NAME, + BASTION_PUBLIC_IP_NAME, + network_models.PublicIPAddress( + location=LOCATION, + public_ip_allocation_method="Static", + sku=network_models.PublicIPPrefixSku(name="Standard") + ) + ).result() + print("Create Public IP for the bastion host:\n{}".format(bastion_public_ip)) + + # Create Bastion Subnet + bastion_subnet = network_client.subnets.begin_create_or_update( + GROUP_NAME, + VNET_NAME, + BASTION_SUBNET_NAME, + {'address_prefix': BASTION_SUBNET_PREFIXES} + ).result() + print("Create a bastion subnet:\n{}".format(bastion_subnet)) + + # Create Bastion Host + ip_configuration = network_models.BastionHostIPConfiguration( + name=NIC_IP_CONFIG_NAME, + subnet=network_models.SubResource(id=bastion_subnet.id), + public_ip_address=network_models.SubResource(id=bastion_public_ip.id) + ) + bastion_parameters = network_models.BastionHost( + location=LOCATION, + ip_configurations=[ip_configuration] + ) + + bastion_host = network_client.bastion_hosts.begin_create_or_update( + GROUP_NAME, + BASTION_NAME, + parameters=bastion_parameters + ).result() + print("Create a bastion host:\n{}".format(bastion_host)) + + # Create Network Security Group + nsg_parameters = network_models.NetworkSecurityGroup(location=LOCATION) + network_security_group = network_client.network_security_groups.begin_create_or_update( + GROUP_NAME, + NSG_NAME, + parameters=nsg_parameters + ).result() + print("Create Network Security Group:\n{}".format(network_security_group)) + + # Create Network Security Group Rule + nsg_rule_parameters = network_models.SecurityRule( + protocol='*', + direction='inbound', + source_address_prefix='*', + source_port_range='*', + destination_address_prefix='*', + destination_port_range=80, + access='allow', + priority=200 + ) + nsg_rule = network_client.security_rules.begin_create_or_update( + GROUP_NAME, + NSG_NAME, + NSG_RULE_NAME, + nsg_rule_parameters + ).result() + print("Create Network Security Group Rule:\n{}".format(nsg_rule)) + + # Create Backend Servers - Standard + # Create Network Interfaces for the VMs + network_interface_parameters = network_models.NetworkInterface( + location=LOCATION, + enable_accelerated_networking=True, + ip_configurations=[network_models.NetworkInterfaceIPConfiguration( + name=IP_CONFIG_NAME, + subnet=network_models.Subnet(id=network.subnets[0].id) + ), + ], + network_security_group=network_models.NetworkSecurityGroup(id=network_security_group.id) + ) + network_interfaces = [] + for nic_name in NIC_NAMES: + network_interface = network_client.network_interfaces.begin_create_or_update( + GROUP_NAME, + nic_name, + network_interface_parameters + ).result() + network_interfaces.append(network_interface) + print("Create Network Interface named {}:\n{}".format(nic_name, network_interface)) + + ############################################################## + # From here, we create the VM and link the LB inside the NIC # + ############################################################## + + # Create VMs + vms = [] + for i in range(3): + vm = compute_client.virtual_machines.begin_create_or_update( + GROUP_NAME, + VM_NAMES[i], + { + "location": LOCATION, + "hardware_profile": { + "vm_size": "Standard_D2_v2" + }, + "storage_profile": { + "image_reference": { + "sku": "2019-Datacenter", + "publisher": "MicrosoftWindowsServer", + "version": "latest", + "offer": "WindowsServer" + }, + "os_disk": { + "caching": "ReadWrite", + "managed_disk": { + "storage_account_type": "Standard_LRS" + }, + "name": "myVMosdisk"+str(i+1), + "create_option": "FromImage" + }, + "data_disks": [ + { + "disk_size_gb": "1023", + "create_option": "Empty", + "lun": "0" + } + ] + }, + "os_profile": { + "admin_username": VM_ADMIN_USERNAME, + "computer_name": "myVM", + "admin_password": VM_ADMIN_PASSEORD, + "windows_configuration": { + "enable_automatic_updates": True # need automatic update for reimage + } + }, + "network_profile": { + "network_interfaces": [ + { + "id": network_interfaces[i].id, + "properties": { + "primary": True + } + } + ] + } + } + ).result() + vms.append(vm) + print('Create VM{} :\n{}'.format(str(i+1), vm)) + + # # Create PublicIP for the Load Balancer + # public_ip = network_client.public_ip_addresses.begin_create_or_update( + # GROUP_NAME, + # PUBLIC_IP_NAME, + # network_models.PublicIPAddress( + # location=LOCATION, + # public_ip_allocation_method="Static", + # sku=network_models.PublicIPPrefixSku(name="Standard") + # ) + # ).result() + # print("Create PublicIP for the Load Balancer:\n{}".format(public_ip)) + + # Create Load Balancer + lb_fip_subnets = [network_subnet for network_subnet in network.subnets if network_subnet.name==SUBNET_NAME] + load_balancer_parameters = network_models.LoadBalancer( + location=LOCATION, + sku=network_models.Sku(name=SKU), + frontend_ip_configurations=[ + network_models.FrontendIPConfiguration( + name=FIP_NAME, + subnet=lb_fip_subnets[0] + ) + ], + backend_address_pools=[ + network_models.BackendAddressPool( + name=BP_NAME + ) + ] + ) + + load_balancer = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result() + print("Create Load Balancer:\n{}".format(load_balancer)) + + # Create Health Probe + load_balancer_parameters.probes = [ + network_models.Probe( + protocol=LB_PROTOCOL, + port=LB_PORT, + name=LB_PROBE_NAME + ) + ] + lb_probe = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result().probes + print("Create Health Probe: \n{}".format(eval(str(lb_probe[0])))) + + # Create Load Balancer Rule + load_balancer_parameters.load_balancing_rules = [ + network_models.LoadBalancingRule( + name=LB_RELE_NAME, + protocol='tcp', + frontend_port=80, + backend_port=80, + frontend_ip_configuration=network_models.SubResource(id=load_balancer.frontend_ip_configurations[0].id), + backend_address_pool=network_models.SubResource(id=load_balancer.backend_address_pools[0].id), + probe=network_models.SubResource(id=lb_probe[0].id), + idle_timeout_in_minutes=15, + enable_floating_ip=True + ) + ] + lb_rule = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result().load_balancing_rules + print("Create Load Balancer Rule: \n{}".format(lb_rule[0])) + + # Add VMs to Load Balancer Backend Pool + for i in range(3): + for nic_ipconf in network_interfaces[i].ip_configurations: + if nic_ipconf.name == NIC_IP_CONFIG_NAME: + nic_ipconf.load_balancer_backend_address_pools = [load_balancer.backend_address_pools[0], + ] + add_vm_to_lb_bp = network_client.network_interfaces.begin_create_or_update( + GROUP_NAME, + NIC_NAMES[i], + network_interfaces[i] + ).result().ip_configurations + print("Add {} to Load Balancer Backend Pool: \n{}".format(NIC_NAMES[i], add_vm_to_lb_bp)) + + # Test Load Balancer + # Create NIC for VM to test + nic_test_parameters = network_models.NetworkInterface( + location=LOCATION, + # enable_accelerated_networking=True, + ip_configurations=[network_models.NetworkInterfaceIPConfiguration( + name=NIC_IP_CONFIG_NAME, + subnet=network_models.Subnet(id=network.subnets[0].id) + ), + ], + network_security_group=network_models.NetworkSecurityGroup(id=network_security_group.id) + ) + nic_test = network_client.network_interfaces.begin_create_or_update( + GROUP_NAME, + NIC_TEST_NAME, + nic_test_parameters + ).result() + print("Create Network Interface:\n{}".format(nic_test)) + + # Create VM for test + vm_test_parameters = { + "location": LOCATION, + "hardware_profile": { + "vm_size": "Standard_D2_v2" + }, + "storage_profile": { + "image_reference": { + "sku": "2019-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" + } + ] + }, + "os_profile": { + "admin_username": VM_ADMIN_USERNAME, + "computer_name": "myVM", + "admin_password": VM_ADMIN_PASSEORD, + "windows_configuration": { + "enable_automatic_updates": True # need automatic update for reimage + } + }, + "network_profile": { + "network_interfaces": [ + { + "id": nic_test.id, + "properties": { + "primary": True + } + } + ] + } + } + vm_test = compute_client.virtual_machines.begin_create_or_update( + GROUP_NAME, + VM_TEST_NAME, + vm_test_parameters + ).result() + print('Create VM for test:\n{}'.format(vm_test)) + + print("Running time: {}".format(time.time()-start_time)) + # + # Delete Resource group and everything in it + input("Press enter to delete this Resource Group.") + print('Delete Resource Group') + delete_async_operation = resource_client.resource_groups.delete(GROUP_NAME) + delete_async_operation.wait() + print("\nDeleted: {}".format(GROUP_NAME)) + + + + return + + # Create Public IP for the Outbound Connectivity + outbound_rule_ip = network_client.public_ip_addresses.begin_create_or_update( + GROUP_NAME, + PUBLIC_IP_OB_NAME, + network_models.PublicIPAddress( + location=LOCATION, + public_ip_allocation_method="Static", + sku=network_models.PublicIPPrefixSku(name="Standard") + ) + ).result() + print("Create Public IP for the Outbound Connectivity:\n{}".format(outbound_rule_ip)) + + # Create Frontend IP Configuration + load_balancer_parameters.frontend_ip_configurations.append( + network_models.FrontendIPConfiguration( + name=FIP_OB_NAME, + public_ip_address=outbound_rule_ip + ) + ) + lb_fip_config = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result().frontend_ip_configurations + print("Create Frontend IP Configuration:\n{}".format(lb_fip_config[-1])) + + # Create Outbound Pool + load_balancer_parameters.backend_address_pools.append( + network_models.BackendAddressPool( + name=OB_POOL_NAME, + ) + ) + lb_ob_pool = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result().backend_address_pools + print("Create Outbound Pool:\n{}".format(lb_ob_pool[-1])) + + # Create Outbound Rule for the Outbound Backend Pool + load_balancer_parameters.outbound_rules = [ + network_models.OutboundRule( + name=OB_RULE, + frontend_ip_configurations=[network_models.SubResource(id=lb_fip_config[-1].id)], + protocol='All', + idle_timeout_in_minutes=15, + allocated_outbound_ports=10000, + backend_address_pool=network_models.SubResource(id=lb_ob_pool[-1].id) + ) + ] + lb_ob_rule = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result().outbound_rules + print("Create Outbound Rule for the Outbound Backend Pool:\n{}".format(lb_ob_rule[-1])) + + # Add VMs to the Outbound Pool + ob_pool_info = network_client.load_balancer_backend_address_pools.get( + GROUP_NAME, + LB_NAME, + OB_POOL_NAME + ) + for i in range(3): + vmnic_info = network_client.network_interfaces.get( + GROUP_NAME, NIC_NAMES[i] + ) + for ip_conf in vmnic_info.ip_configurations: + if ip_conf.name == NIC_IP_CONFIG_NAME: + ip_conf.load_balancer_backend_address_pools.append(ob_pool_info) + + add_vm_to_lb_op = network_client.network_interfaces.begin_create_or_update( + GROUP_NAME, + NIC_NAMES[i], + vmnic_info + ).result().ip_configurations + print("Add {} to Load Balancer Outbound Pool: \n{}".format(NIC_NAMES[i], add_vm_to_lb_op)) + + # Get the Public IP Address of the Load Balancer + network_public_ip_info = network_client.public_ip_addresses.get( + GROUP_NAME, PUBLIC_IP_NAME + ) + print("Public IP Address:\n{}".format(network_public_ip_info.ip_address)) + + + + +def delete_resource_group(): + subscription_id = os.environ.get( + 'AZURE_SUBSCRIPTION_ID', + '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id + + resource_client = ResourceManagementClient( + credential=DefaultAzureCredential(), + subscription_id=subscription_id + ) + print('Delete Resource Group') + resource_client.resource_groups.begin_delete( + GROUP_NAME) + + print('Success') + + + +def run_install_iis(): + subscription_id = os.environ.get( + 'AZURE_SUBSCRIPTION_ID', + '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id + + compute_client = ComputeManagementClient( + credential=DefaultAzureCredential(), + subscription_id=subscription_id + ) + RG_NAME = 'CreatePubLBQS-rg' + VM2_NAME = 'myVM2' + VM_EXT_NAME = 'CustomScriptExtension' + SETTINGS = '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $("Hello World from "+$env:computername)"}' + vm_ext_parameters = compute_models.VirtualMachineExtension( + location='eastus', + publisher='Microsoft.Compute', + type_handler_version='1.8', + settings=SETTINGS, + ) + vm_ext = compute_client.virtual_machine_extensions.begin_create_or_update( + RG_NAME, VM2_NAME, VM_EXT_NAME, vm_ext_parameters).result() + + print("Add vm_ext:\n{}".format(vm_ext)) + + +if __name__ == "__main__": + run_example() + # run_install_iis() + # delete_resource_group() \ No newline at end of file diff --git a/example_public_load_balancer.py b/example_public_load_balancer.py new file mode 100644 index 0000000..1759c58 --- /dev/null +++ b/example_public_load_balancer.py @@ -0,0 +1,520 @@ +import os + +from azure.identity import DefaultAzureCredential +from azure.mgmt.resource import ResourceManagementClient +from azure.mgmt.network import NetworkManagementClient +from azure.mgmt.network import models as network_models +from azure.mgmt.compute import ComputeManagementClient +from azure.mgmt.compute import models as compute_models + +# Azure Datacenter +LOCATION = 'eastus' +SKU = 'Standard' +# Resource Group +GROUP_NAME = 'myPublicLoadBalancer1' + +# Network +VNET_NAME = 'myVNet' +ADDRESS_PREFIXES = '10.1.0.0/16' +SUBNET_NAME = 'myBackendSubnet' +SUBNET_PREFIXES = '10.1.0.0/24' + +# Nic ip-config +NIC_IP_CONFIG_NAME = 'ipconfig1' + +# Public IP address for the bastion host +BASTION_PUBLIC_IP_NAME = 'myBastionIP' + +# Bastion subnet +IP_CONFIG_NAME = 'myBastionIPConfig' +BASTION_SUBNET_NAME = 'AzureBastionSubnet' +BASTION_SUBNET_PREFIXES = '10.1.1.0/24' + +# Bastion host +BASTION_NAME = 'myBastionHost' + +# Network security group +NSG_NAME = 'myNSG' + +# Network Security Group Rule +NSG_RULE_NAME = 'myNSGRuleHTTP' + +# Network interfaces for the virtual machines +NIC_NAMES = ['myNicVM1', 'myNicVM2', 'myNicVM3'] + +# Virtual machine +VM_NAMES = ['myVM1', 'myVM2', 'myVM3'] +# VN_IMAGE = 'win2019datacenter' +VM_ADMIN_USERNAME = 'azureuser' +VM_ADMIN_PASSEORD = 'P@ssw0rd123' + +# Public IP address - Standard +PUBLIC_IP_NAME = 'myPublicIP' + +# Public Load balancer +LB_NAME = 'myLoadBalancer' +FIP_NAME = 'myFrontEnd' +BP_NAME = 'myBackEndPool' + +# Health probe +LB_PROBE_NAME = 'myHealthProbe' +LB_PROTOCOL = 'tcp' +LB_PORT = 80 + +# Load balancer rule +LB_RELE_NAME = 'myHTTPRule' + +# Single IP for the outbound connectivity +PUBLIC_IP_OB_NAME = 'myPublicIPOutbound' + +# Public IP prefix for the outbound connectivity +PUBLIC_IP_PREDIX_OB_NAME = 'myPublicIPPrefixOutbound' +PUBLIC_IP_PREDIX_OB_LENGTH = 28 + +# Outbound frontend IP configuration +FIP_OB_NAME = 'myFrontEndOutbound' + +# Outbound pool +OB_POOL_NAME = 'myBackendPoolOutbound' + +# Oubtbound rule +OB_RULE = 'myOutboundRule' + + +# Manage resources and resource groups - create, update and delete a resource group, +# deploy a solution into a resource group, export an ARM template. Create, read, update +# and delete a resource +# +# This script expects that the following environment vars are set: +# +# AZURE_TENANT_ID: with your Azure Active Directory tenant id or domain +# AZURE_CLIENT_ID: with your Azure Active Directory Application Client ID +# AZURE_CLIENT_SECRET: with your Azure Active Directory Application Secret +# AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id +# + + +def run_example(): + import time + start_time = time.time() + """Resource Group management example.""" + # + # Create all clients with an Application (service principal) token provider + # + subscription_id = os.environ.get( + 'AZURE_SUBSCRIPTION_ID', + '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id + + resource_client = ResourceManagementClient( + credential=DefaultAzureCredential(), + subscription_id=subscription_id + ) + network_client = NetworkManagementClient( + credential=DefaultAzureCredential(), + subscription_id=subscription_id + ) + compute_client = ComputeManagementClient( + credential=DefaultAzureCredential(), + subscription_id=subscription_id + ) + + # Create Resource group + rg = resource_client.resource_groups.create_or_update( + GROUP_NAME, {'location': LOCATION}) + print('Create Resource Group:\n{}'.format(rg)) + + # Create VNet + network = network_client.virtual_networks.begin_create_or_update( + GROUP_NAME, + VNET_NAME, + network_models.VirtualNetwork( + location=LOCATION, + address_space=network_models.AddressSpace( + address_prefixes=[ADDRESS_PREFIXES] + ), + subnets=[network_models.Subnet( + name=SUBNET_NAME, + address_prefix=SUBNET_PREFIXES + ), ] + ) + ).result() + print("Create Vnet:\n{}".format(network)) + + # Create PublicIP for the Bastion Host + bastion_public_ip = network_client.public_ip_addresses.begin_create_or_update( + GROUP_NAME, + BASTION_PUBLIC_IP_NAME, + network_models.PublicIPAddress( + location=LOCATION, + public_ip_allocation_method="Static", + sku=network_models.PublicIPPrefixSku(name="Standard") + ) + ).result() + print("Create Public IP for the bastion host:\n{}".format(bastion_public_ip)) + + # Create Bastion Subnet + bastion_subnet = network_client.subnets.begin_create_or_update( + GROUP_NAME, + VNET_NAME, + BASTION_SUBNET_NAME, + {'address_prefix': BASTION_SUBNET_PREFIXES} + ).result() + print("Create a bastion subnet:\n{}".format(bastion_subnet)) + + # Create Bastion Host + ip_configuration = network_models.BastionHostIPConfiguration( + name=NIC_IP_CONFIG_NAME, + subnet=network_models.SubResource(id=bastion_subnet.id), + public_ip_address=network_models.SubResource(id=bastion_public_ip.id) + ) + bastion_parameters = network_models.BastionHost( + location=LOCATION, + ip_configurations=[ip_configuration] + ) + + bastion_host = network_client.bastion_hosts.begin_create_or_update( + GROUP_NAME, + BASTION_NAME, + parameters=bastion_parameters + ).result() + print("Create a bastion host:\n{}".format(bastion_host)) + + # Create Network Security Group + nsg_parameters = network_models.NetworkSecurityGroup(location=LOCATION) + network_security_group = network_client.network_security_groups.begin_create_or_update( + GROUP_NAME, + NSG_NAME, + parameters=nsg_parameters + ).result() + print("Create Network Security Group:\n{}".format(network_security_group)) + + # Create Network Security Group Rule + nsg_rule_parameters = network_models.SecurityRule( + protocol='*', + direction='inbound', + source_address_prefix='*', + source_port_range='*', + destination_address_prefix='*', + destination_port_range=80, + access='allow', + priority=200 + ) + nsg_rule = network_client.security_rules.begin_create_or_update( + GROUP_NAME, + NSG_NAME, + NSG_RULE_NAME, + nsg_rule_parameters + ).result() + print("Create Network Security Group Rule:\n{}".format(nsg_rule)) + + # Create Backend Servers - Standard + # Create Network Interfaces for the VMs + network_interface_parameters = network_models.NetworkInterface( + location=LOCATION, + enable_accelerated_networking=True, + ip_configurations=[network_models.NetworkInterfaceIPConfiguration( + name=NIC_IP_CONFIG_NAME, + subnet=network_models.Subnet(id=network.subnets[0].id) + ), + ], + network_security_group=network_models.NetworkSecurityGroup(id=network_security_group.id) + ) + network_interfaces = [] + for nic_name in NIC_NAMES: + network_interface = network_client.network_interfaces.begin_create_or_update( + GROUP_NAME, + nic_name, + network_interface_parameters + ).result() + network_interfaces.append(network_interface) + print("Create Network Interface named {}:\n{}".format(nic_name, network_interface)) + + ############################################################## + # From here, we create the VM and link the LB inside the NIC # + ############################################################## + + # Create VMs + vms = [] + for i in range(3): + vm = compute_client.virtual_machines.begin_create_or_update( + GROUP_NAME, + VM_NAMES[i], + { + "location": LOCATION, + "hardware_profile": { + "vm_size": "Standard_D2_v2" + }, + "storage_profile": { + "image_reference": { + "sku": "2019-Datacenter", + "publisher": "MicrosoftWindowsServer", + "version": "latest", + "offer": "WindowsServer" + }, + "os_disk": { + "caching": "ReadWrite", + "managed_disk": { + "storage_account_type": "Standard_LRS" + }, + "name": "myVMosdisk"+str(i+1), + "create_option": "FromImage" + }, + "data_disks": [ + { + "disk_size_gb": "1023", + "create_option": "Empty", + "lun": "0" + } + ] + }, + "os_profile": { + "admin_username": VM_ADMIN_USERNAME, + "computer_name": "myVM", + "admin_password": VM_ADMIN_PASSEORD, + "windows_configuration": { + "enable_automatic_updates": True # need automatic update for reimage + } + }, + "network_profile": { + "network_interfaces": [ + { + "id": network_interfaces[i].id, + "properties": { + "primary": True + } + } + ] + } + } + ).result() + vms.append(vm) + print('Create VM{} :\n{}'.format(str(i+1), vm)) + + # Create PublicIP for the Load Balancer + public_ip = network_client.public_ip_addresses.begin_create_or_update( + GROUP_NAME, + PUBLIC_IP_NAME, + network_models.PublicIPAddress( + location=LOCATION, + public_ip_allocation_method="Static", + sku=network_models.PublicIPPrefixSku(name="Standard") + ) + ).result() + print("Create PublicIP for the Load Balancer:\n{}".format(public_ip)) + + # Create Load Balancer + load_balancer_parameters = network_models.LoadBalancer( + location=LOCATION, + sku=network_models.Sku(name=SKU), + frontend_ip_configurations=[ + network_models.FrontendIPConfiguration( + name=FIP_NAME, + public_ip_address=network_models.PublicIPAddress( + id=public_ip.id + ) + ) + ], + backend_address_pools=[ + network_models.BackendAddressPool( + name=BP_NAME + ) + ] + ) + + load_balancer = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result() + print("Create Load Balancer:\n{}".format(load_balancer)) + + # Create Health Probe + load_balancer_parameters.probes = [ + network_models.Probe( + protocol=LB_PROTOCOL, + port=LB_PORT, + name=LB_PROBE_NAME + ) + ] + lb_probe = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result().probes + print("Create Health Probe: \n{}".format(eval(str(lb_probe[0])))) + + # Create Load Balancer Rule + load_balancer_parameters.load_balancing_rules = [ + network_models.LoadBalancingRule( + name=LB_RELE_NAME, + protocol='tcp', + frontend_port=80, + backend_port=80, + frontend_ip_configuration=network_models.SubResource(id=load_balancer.frontend_ip_configurations[0].id), + backend_address_pool=network_models.SubResource(id=load_balancer.backend_address_pools[0].id), + probe=network_models.SubResource(id=lb_probe[0].id), + disable_outbound_snat=True, + idle_timeout_in_minutes=15, + enable_floating_ip=True + ) + ] + lb_rule = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result().load_balancing_rules + print("Create Load Balancer Rule: \n{}".format(lb_rule[0])) + + # Add VMs to Load Balancer Backend Pool + for i in range(3): + for nic_ipconf in network_interfaces[i].ip_configurations: + if nic_ipconf.name == NIC_IP_CONFIG_NAME: + nic_ipconf.load_balancer_backend_address_pools = [load_balancer.backend_address_pools[0], + ] + add_vm_to_lb_bp = network_client.network_interfaces.begin_create_or_update( + GROUP_NAME, + NIC_NAMES[i], + network_interfaces[i] + ).result().ip_configurations + print("Add {} to Load Balancer Backend Pool: \n{}".format(NIC_NAMES[i], add_vm_to_lb_bp)) + + # Create Public IP for the Outbound Connectivity + outbound_rule_ip = network_client.public_ip_addresses.begin_create_or_update( + GROUP_NAME, + PUBLIC_IP_OB_NAME, + network_models.PublicIPAddress( + location=LOCATION, + public_ip_allocation_method="Static", + sku=network_models.PublicIPPrefixSku(name="Standard") + ) + ).result() + print("Create Public IP for the Outbound Connectivity:\n{}".format(outbound_rule_ip)) + + # Create Frontend IP Configuration + load_balancer_parameters.frontend_ip_configurations.append( + network_models.FrontendIPConfiguration( + name=FIP_OB_NAME, + public_ip_address=outbound_rule_ip + ) + ) + lb_fip_config = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result().frontend_ip_configurations + print("Create Frontend IP Configuration:\n{}".format(lb_fip_config[-1])) + + # Create Outbound Pool + load_balancer_parameters.backend_address_pools.append( + network_models.BackendAddressPool( + name=OB_POOL_NAME, + ) + ) + lb_ob_pool = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result().backend_address_pools + print("Create Outbound Pool:\n{}".format(lb_ob_pool[-1])) + + # Create Outbound Rule for the Outbound Backend Pool + load_balancer_parameters.outbound_rules = [ + network_models.OutboundRule( + name=OB_RULE, + frontend_ip_configurations=[network_models.SubResource(id=lb_fip_config[-1].id)], + protocol='All', + idle_timeout_in_minutes=15, + allocated_outbound_ports=10000, + backend_address_pool=network_models.SubResource(id=lb_ob_pool[-1].id) + ) + ] + lb_ob_rule = network_client.load_balancers.begin_create_or_update( + GROUP_NAME, + LB_NAME, + load_balancer_parameters + ).result().outbound_rules + print("Create Outbound Rule for the Outbound Backend Pool:\n{}".format(lb_ob_rule[-1])) + + # Add VMs to the Outbound Pool + ob_pool_info = network_client.load_balancer_backend_address_pools.get( + GROUP_NAME, + LB_NAME, + OB_POOL_NAME + ) + for i in range(3): + vmnic_info = network_client.network_interfaces.get( + GROUP_NAME, NIC_NAMES[i] + ) + for ip_conf in vmnic_info.ip_configurations: + if ip_conf.name == NIC_IP_CONFIG_NAME: + ip_conf.load_balancer_backend_address_pools.append(ob_pool_info) + + add_vm_to_lb_op = network_client.network_interfaces.begin_create_or_update( + GROUP_NAME, + NIC_NAMES[i], + vmnic_info + ).result().ip_configurations + print("Add {} to Load Balancer Outbound Pool: \n{}".format(NIC_NAMES[i], add_vm_to_lb_op)) + + # Get the Public IP Address of the Load Balancer + network_public_ip_info = network_client.public_ip_addresses.get( + GROUP_NAME, PUBLIC_IP_NAME + ) + print("Public IP Address:\n{}".format(network_public_ip_info.ip_address)) + + print("Running time: {}".format(time.time()-start_time)) + # + # Delete Resource group and everything in it + input("Press enter to delete this Resource Group.") + print('Delete Resource Group') + delete_async_operation = resource_client.resource_groups.delete(GROUP_NAME) + delete_async_operation.wait() + print("\nDeleted: {}".format(GROUP_NAME)) + + +def delete_resource_group(): + subscription_id = os.environ.get( + 'AZURE_SUBSCRIPTION_ID', + '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id + + resource_client = ResourceManagementClient( + credential=DefaultAzureCredential(), + subscription_id=subscription_id + ) + print('Delete Resource Group') + resource_client.resource_groups.begin_delete( + GROUP_NAME) + + print('Success') + + + +def run_install_iis(): + subscription_id = os.environ.get( + 'AZURE_SUBSCRIPTION_ID', + '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id + + compute_client = ComputeManagementClient( + credential=DefaultAzureCredential(), + subscription_id=subscription_id + ) + RG_NAME = 'CreatePubLBQS-rg' + VM2_NAME = 'myVM2' + VM_EXT_NAME = 'CustomScriptExtension' + SETTINGS = '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $("Hello World from "+$env:computername)"}' + vm_ext_parameters = compute_models.VirtualMachineExtension( + location='eastus', + publisher='Microsoft.Compute', + type_handler_version='1.8', + settings=SETTINGS, + ) + vm_ext = compute_client.virtual_machine_extensions.begin_create_or_update( + RG_NAME, VM2_NAME, VM_EXT_NAME, vm_ext_parameters).result() + + print("Add vm_ext:\n{}".format(vm_ext)) + + +if __name__ == "__main__": + run_example() + # run_install_iis() + # delete_resource_group() \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 62d8733..c501f74 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1,5 +1,4 @@ -azure-mgmt-resource==0.30.0rc5 -azure-mgmt-storage==0.30.0rc5 -azure-mgmt-network==0.30.0rc5 -azure-mgmt-compute==0.30.0rc5 -haikunator \ No newline at end of file +azure-identity +azure-mgmt-resource==18.0.0 +azure-mgmt-network==19.0.0 +azure-mgmt-compute==22.0.0 \ No newline at end of file From 4857f804a37655c955fbdaf5f297680be0671fc2 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Mon, 26 Jul 2021 11:12:34 +0800 Subject: [PATCH 2/5] update readme.md --- README.md | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 58d9ea4..63bddb6 100644 --- a/README.md +++ b/README.md @@ -88,10 +88,14 @@ or [Azure Portal](http://azure.microsoft.com/documentation/articles/resource-gro export AZURE_CLIENT_SECRET={your client secret} export AZURE_SUBSCRIPTION_ID={your subscription ID} -7. Run the sample. - - python example.py +7. Run the sample for public load balancer. + python example_public_load_balancer.py + Or + Run the sample for internal load balancer. + + python example_internal_load_balancer.py + ## More information - [Azure SDK for Python](http://github.com/Azure/azure-sdk-for-python) From c8d40987e9539e74c050cd9169c4d4a5d93df103 Mon Sep 17 00:00:00 2001 From: msyyc <70930885+msyyc@users.noreply.github.com> Date: Mon, 26 Jul 2021 11:21:18 +0800 Subject: [PATCH 3/5] Update README.md --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 63bddb6..b481c01 100644 --- a/README.md +++ b/README.md @@ -92,6 +92,7 @@ or [Azure Portal](http://azure.microsoft.com/documentation/articles/resource-gro python example_public_load_balancer.py Or + Run the sample for internal load balancer. python example_internal_load_balancer.py From b0ea83785bcd718328ec400bb9076a334cd0d578 Mon Sep 17 00:00:00 2001 From: "CJF\\WIN10" Date: Mon, 26 Jul 2021 11:27:31 +0800 Subject: [PATCH 4/5] Update examples --- example_internal_load_balancer.py | 251 ++++++------------------------ example_public_load_balancer.py | 53 +------ 2 files changed, 52 insertions(+), 252 deletions(-) diff --git a/example_internal_load_balancer.py b/example_internal_load_balancer.py index 8304498..8443cbd 100644 --- a/example_internal_load_balancer.py +++ b/example_internal_load_balancer.py @@ -87,7 +87,6 @@ # Oubtbound rule OB_RULE = 'myOutboundRule' - # Manage resources and resource groups - create, update and delete a resource group, # deploy a solution into a resource group, export an ARM template. Create, read, update # and delete a resource @@ -98,8 +97,6 @@ # AZURE_CLIENT_ID: with your Azure Active Directory Application Client ID # AZURE_CLIENT_SECRET: with your Azure Active Directory Application Secret # AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id -# - def run_example(): import time @@ -173,11 +170,11 @@ def run_example(): name=NIC_IP_CONFIG_NAME, subnet=network_models.SubResource(id=bastion_subnet.id), public_ip_address=network_models.SubResource(id=bastion_public_ip.id) - ) + ) bastion_parameters = network_models.BastionHost( location=LOCATION, ip_configurations=[ip_configuration] - ) + ) bastion_host = network_client.bastion_hosts.begin_create_or_update( GROUP_NAME, @@ -263,7 +260,7 @@ def run_example(): "managed_disk": { "storage_account_type": "Standard_LRS" }, - "name": "myVMosdisk"+str(i+1), + "name": "myVMosdisk" + str(i + 1), "create_option": "FromImage" }, "data_disks": [ @@ -295,22 +292,10 @@ def run_example(): } ).result() vms.append(vm) - print('Create VM{} :\n{}'.format(str(i+1), vm)) - - # # Create PublicIP for the Load Balancer - # public_ip = network_client.public_ip_addresses.begin_create_or_update( - # GROUP_NAME, - # PUBLIC_IP_NAME, - # network_models.PublicIPAddress( - # location=LOCATION, - # public_ip_allocation_method="Static", - # sku=network_models.PublicIPPrefixSku(name="Standard") - # ) - # ).result() - # print("Create PublicIP for the Load Balancer:\n{}".format(public_ip)) + print('Create VM{} :\n{}'.format(str(i + 1), vm)) # Create Load Balancer - lb_fip_subnets = [network_subnet for network_subnet in network.subnets if network_subnet.name==SUBNET_NAME] + lb_fip_subnets = [network_subnet for network_subnet in network.subnets if network_subnet.name == SUBNET_NAME] load_balancer_parameters = network_models.LoadBalancer( location=LOCATION, sku=network_models.Sku(name=SKU), @@ -404,61 +389,60 @@ def run_example(): # Create VM for test vm_test_parameters = { - "location": LOCATION, - "hardware_profile": { - "vm_size": "Standard_D2_v2" - }, - "storage_profile": { - "image_reference": { - "sku": "2019-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" - } - ] + "location": LOCATION, + "hardware_profile": { + "vm_size": "Standard_D2_v2" + }, + "storage_profile": { + "image_reference": { + "sku": "2019-Datacenter", + "publisher": "MicrosoftWindowsServer", + "version": "latest", + "offer": "WindowsServer" + }, + "os_disk": { + "caching": "ReadWrite", + "managed_disk": { + "storage_account_type": "Standard_LRS" }, - "os_profile": { - "admin_username": VM_ADMIN_USERNAME, - "computer_name": "myVM", - "admin_password": VM_ADMIN_PASSEORD, - "windows_configuration": { - "enable_automatic_updates": True # need automatic update for reimage - } - }, - "network_profile": { - "network_interfaces": [ - { - "id": nic_test.id, - "properties": { - "primary": True - } - } - ] + "name": "myVMosdisk", + "create_option": "FromImage" + }, + "data_disks": [ + { + "disk_size_gb": "1023", + "create_option": "Empty", + "lun": "0" } + ] + }, + "os_profile": { + "admin_username": VM_ADMIN_USERNAME, + "computer_name": "myVM", + "admin_password": VM_ADMIN_PASSEORD, + "windows_configuration": { + "enable_automatic_updates": True # need automatic update for reimage } + }, + "network_profile": { + "network_interfaces": [ + { + "id": nic_test.id, + "properties": { + "primary": True + } + } + ] + } + } vm_test = compute_client.virtual_machines.begin_create_or_update( GROUP_NAME, VM_TEST_NAME, vm_test_parameters ).result() print('Create VM for test:\n{}'.format(vm_test)) + print("Running time: {}".format(time.time() - start_time)) - print("Running time: {}".format(time.time()-start_time)) - # # Delete Resource group and everything in it input("Press enter to delete this Resource Group.") print('Delete Resource Group') @@ -466,140 +450,5 @@ def run_example(): delete_async_operation.wait() print("\nDeleted: {}".format(GROUP_NAME)) - - - return - - # Create Public IP for the Outbound Connectivity - outbound_rule_ip = network_client.public_ip_addresses.begin_create_or_update( - GROUP_NAME, - PUBLIC_IP_OB_NAME, - network_models.PublicIPAddress( - location=LOCATION, - public_ip_allocation_method="Static", - sku=network_models.PublicIPPrefixSku(name="Standard") - ) - ).result() - print("Create Public IP for the Outbound Connectivity:\n{}".format(outbound_rule_ip)) - - # Create Frontend IP Configuration - load_balancer_parameters.frontend_ip_configurations.append( - network_models.FrontendIPConfiguration( - name=FIP_OB_NAME, - public_ip_address=outbound_rule_ip - ) - ) - lb_fip_config = network_client.load_balancers.begin_create_or_update( - GROUP_NAME, - LB_NAME, - load_balancer_parameters - ).result().frontend_ip_configurations - print("Create Frontend IP Configuration:\n{}".format(lb_fip_config[-1])) - - # Create Outbound Pool - load_balancer_parameters.backend_address_pools.append( - network_models.BackendAddressPool( - name=OB_POOL_NAME, - ) - ) - lb_ob_pool = network_client.load_balancers.begin_create_or_update( - GROUP_NAME, - LB_NAME, - load_balancer_parameters - ).result().backend_address_pools - print("Create Outbound Pool:\n{}".format(lb_ob_pool[-1])) - - # Create Outbound Rule for the Outbound Backend Pool - load_balancer_parameters.outbound_rules = [ - network_models.OutboundRule( - name=OB_RULE, - frontend_ip_configurations=[network_models.SubResource(id=lb_fip_config[-1].id)], - protocol='All', - idle_timeout_in_minutes=15, - allocated_outbound_ports=10000, - backend_address_pool=network_models.SubResource(id=lb_ob_pool[-1].id) - ) - ] - lb_ob_rule = network_client.load_balancers.begin_create_or_update( - GROUP_NAME, - LB_NAME, - load_balancer_parameters - ).result().outbound_rules - print("Create Outbound Rule for the Outbound Backend Pool:\n{}".format(lb_ob_rule[-1])) - - # Add VMs to the Outbound Pool - ob_pool_info = network_client.load_balancer_backend_address_pools.get( - GROUP_NAME, - LB_NAME, - OB_POOL_NAME - ) - for i in range(3): - vmnic_info = network_client.network_interfaces.get( - GROUP_NAME, NIC_NAMES[i] - ) - for ip_conf in vmnic_info.ip_configurations: - if ip_conf.name == NIC_IP_CONFIG_NAME: - ip_conf.load_balancer_backend_address_pools.append(ob_pool_info) - - add_vm_to_lb_op = network_client.network_interfaces.begin_create_or_update( - GROUP_NAME, - NIC_NAMES[i], - vmnic_info - ).result().ip_configurations - print("Add {} to Load Balancer Outbound Pool: \n{}".format(NIC_NAMES[i], add_vm_to_lb_op)) - - # Get the Public IP Address of the Load Balancer - network_public_ip_info = network_client.public_ip_addresses.get( - GROUP_NAME, PUBLIC_IP_NAME - ) - print("Public IP Address:\n{}".format(network_public_ip_info.ip_address)) - - - - -def delete_resource_group(): - subscription_id = os.environ.get( - 'AZURE_SUBSCRIPTION_ID', - '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id - - resource_client = ResourceManagementClient( - credential=DefaultAzureCredential(), - subscription_id=subscription_id - ) - print('Delete Resource Group') - resource_client.resource_groups.begin_delete( - GROUP_NAME) - - print('Success') - - - -def run_install_iis(): - subscription_id = os.environ.get( - 'AZURE_SUBSCRIPTION_ID', - '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id - - compute_client = ComputeManagementClient( - credential=DefaultAzureCredential(), - subscription_id=subscription_id - ) - RG_NAME = 'CreatePubLBQS-rg' - VM2_NAME = 'myVM2' - VM_EXT_NAME = 'CustomScriptExtension' - SETTINGS = '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $("Hello World from "+$env:computername)"}' - vm_ext_parameters = compute_models.VirtualMachineExtension( - location='eastus', - publisher='Microsoft.Compute', - type_handler_version='1.8', - settings=SETTINGS, - ) - vm_ext = compute_client.virtual_machine_extensions.begin_create_or_update( - RG_NAME, VM2_NAME, VM_EXT_NAME, vm_ext_parameters).result() - - print("Add vm_ext:\n{}".format(vm_ext)) - - if __name__ == "__main__": - run_example() - # run_install_iis() - # delete_resource_group() \ No newline at end of file + run_example() \ No newline at end of file diff --git a/example_public_load_balancer.py b/example_public_load_balancer.py index 1759c58..3740a7c 100644 --- a/example_public_load_balancer.py +++ b/example_public_load_balancer.py @@ -80,7 +80,6 @@ # Oubtbound rule OB_RULE = 'myOutboundRule' - # Manage resources and resource groups - create, update and delete a resource group, # deploy a solution into a resource group, export an ARM template. Create, read, update # and delete a resource @@ -91,8 +90,6 @@ # AZURE_CLIENT_ID: with your Azure Active Directory Application Client ID # AZURE_CLIENT_SECRET: with your Azure Active Directory Application Secret # AZURE_SUBSCRIPTION_ID: with your Azure Subscription Id -# - def run_example(): import time @@ -461,9 +458,8 @@ def run_example(): GROUP_NAME, PUBLIC_IP_NAME ) print("Public IP Address:\n{}".format(network_public_ip_info.ip_address)) - print("Running time: {}".format(time.time()-start_time)) - # + # Delete Resource group and everything in it input("Press enter to delete this Resource Group.") print('Delete Resource Group') @@ -471,50 +467,5 @@ def run_example(): delete_async_operation.wait() print("\nDeleted: {}".format(GROUP_NAME)) - -def delete_resource_group(): - subscription_id = os.environ.get( - 'AZURE_SUBSCRIPTION_ID', - '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id - - resource_client = ResourceManagementClient( - credential=DefaultAzureCredential(), - subscription_id=subscription_id - ) - print('Delete Resource Group') - resource_client.resource_groups.begin_delete( - GROUP_NAME) - - print('Success') - - - -def run_install_iis(): - subscription_id = os.environ.get( - 'AZURE_SUBSCRIPTION_ID', - '11111111-1111-1111-1111-111111111111') # your Azure Subscription Id - - compute_client = ComputeManagementClient( - credential=DefaultAzureCredential(), - subscription_id=subscription_id - ) - RG_NAME = 'CreatePubLBQS-rg' - VM2_NAME = 'myVM2' - VM_EXT_NAME = 'CustomScriptExtension' - SETTINGS = '{"commandToExecute":"powershell Add-WindowsFeature Web-Server; powershell Add-Content -Path \"C:\\inetpub\\wwwroot\\Default.htm\" -Value $("Hello World from "+$env:computername)"}' - vm_ext_parameters = compute_models.VirtualMachineExtension( - location='eastus', - publisher='Microsoft.Compute', - type_handler_version='1.8', - settings=SETTINGS, - ) - vm_ext = compute_client.virtual_machine_extensions.begin_create_or_update( - RG_NAME, VM2_NAME, VM_EXT_NAME, vm_ext_parameters).result() - - print("Add vm_ext:\n{}".format(vm_ext)) - - if __name__ == "__main__": - run_example() - # run_install_iis() - # delete_resource_group() \ No newline at end of file + run_example() \ No newline at end of file From 309499001c48de54e4f45727b45b6bc1271600d9 Mon Sep 17 00:00:00 2001 From: BigCat20196 <1095260342@qq.com> Date: Tue, 27 Jul 2021 10:26:06 +0800 Subject: [PATCH 5/5] Update samples --- example_internal_load_balancer.py | 7 +++---- example_public_load_balancer.py | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/example_internal_load_balancer.py b/example_internal_load_balancer.py index 8443cbd..064174a 100644 --- a/example_internal_load_balancer.py +++ b/example_internal_load_balancer.py @@ -5,7 +5,6 @@ from azure.mgmt.network import NetworkManagementClient from azure.mgmt.network import models as network_models from azure.mgmt.compute import ComputeManagementClient -from azure.mgmt.compute import models as compute_models # Azure Datacenter LOCATION = 'eastus' @@ -446,9 +445,9 @@ def run_example(): # Delete Resource group and everything in it input("Press enter to delete this Resource Group.") print('Delete Resource Group') - delete_async_operation = resource_client.resource_groups.delete(GROUP_NAME) - delete_async_operation.wait() + delete_group = resource_client.resource_groups.begin_delete(GROUP_NAME) print("\nDeleted: {}".format(GROUP_NAME)) + if __name__ == "__main__": - run_example() \ No newline at end of file + run_example() diff --git a/example_public_load_balancer.py b/example_public_load_balancer.py index 3740a7c..7472717 100644 --- a/example_public_load_balancer.py +++ b/example_public_load_balancer.py @@ -5,7 +5,6 @@ from azure.mgmt.network import NetworkManagementClient from azure.mgmt.network import models as network_models from azure.mgmt.compute import ComputeManagementClient -from azure.mgmt.compute import models as compute_models # Azure Datacenter LOCATION = 'eastus' @@ -463,9 +462,9 @@ def run_example(): # Delete Resource group and everything in it input("Press enter to delete this Resource Group.") print('Delete Resource Group') - delete_async_operation = resource_client.resource_groups.delete(GROUP_NAME) - delete_async_operation.wait() + delete_group = resource_client.resource_groups.begin_delete(GROUP_NAME) print("\nDeleted: {}".format(GROUP_NAME)) + if __name__ == "__main__": - run_example() \ No newline at end of file + run_example()