tenant_id = 'your-tenant-id' application_id = 'your-application-id' application_secret = 'your-application-secret' rgname = 'your resource group' name = 'your vm name' # authenticate access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) # initialize haikunator h = Haikunator() # create NSG nsg_name = name + 'nsg' print('Creating NSG: ' + nsg_name) rmreturn = azurerm.create_nsg(access_token, subscription_id, rgname, nsg_name, location) nsg_id = rmreturn.json()['id'] print('nsg_id = ' + nsg_id) # create NSG rule nsg_rule = 'ssh' print('Creating NSG rule: ' + nsg_rule) rmreturn = azurerm.create_nsg_rule(access_token, subscription_id, rgname, nsg_name, nsg_rule, description='ssh rule', destination_range='22') print(rmreturn)
app_id = configData['appId'] app_secret = configData['appSecret'] subscription_id = configData['subscriptionId'] # authenticate access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) # create resource group print('Creating resource group: ' + name) rmreturn = azurerm.create_resource_group(access_token, subscription_id, name, location) print(rmreturn) # create NSG nsg_name = name + 'nsg' print('Creating NSG: ' + nsg_name) rmreturn = azurerm.create_nsg(access_token, subscription_id, name, nsg_name, location) nsg_id = rmreturn.json()['id'] print('nsg_id = ' + nsg_id) # create NSG rule nsg_rule = 'ssh' print('Creating NSG rule: ' + nsg_rule) rmreturn = azurerm.create_nsg_rule(access_token, subscription_id, name, nsg_name, nsg_rule, description='ssh rule', destination_range='22') print(rmreturn) print(json.dumps(rmreturn.json(), sort_keys=False, indent=2, separators=(',', ': '))) # create storage account print('Creating storage account: ' + name) rmreturn = azurerm.create_storage_account(access_token, subscription_id, name, name, location, storage_type='Premium_LRS') print(rmreturn)
def setUp(self): # Load Azure app defaults try: with open('azurermconfig.json') as config_file: config_data = json.load(config_file) except FileNotFoundError: sys.exit("Error: Expecting azurermconfig.json in current folder") tenant_id = config_data['tenantId'] app_id = config_data['appId'] app_secret = config_data['appSecret'] self.subscription_id = config_data['subscriptionId'] self.access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) self.location = config_data['location'] # generate names used in tests self.h = Haikunator() self.rgname = self.h.haikunate() self.vnet = self.h.haikunate() # self.saname = self.h.haikunate(delimiter='') self.vmname = self.h.haikunate(delimiter='') self.vmssname = self.h.haikunate(delimiter='') self.asname = self.h.haikunate() # generate RSA Key for compute resources key = rsa.generate_private_key(backend=default_backend(), public_exponent=65537, \ key_size=2048) self.public_key = key.public_key().public_bytes(serialization.Encoding.OpenSSH, \ serialization.PublicFormat.OpenSSH).decode('utf-8') # create resource group print('Creating resource group: ' + self.rgname) response = azurerm.create_resource_group(self.access_token, self.subscription_id, \ self.rgname, self.location) self.assertEqual(response.status_code, 201) # create vnet print('Creating vnet: ' + self.vnet) response = azurerm.create_vnet(self.access_token, self.subscription_id, self.rgname, \ self.vnet, self.location, address_prefix='10.0.0.0/16', nsg_id=None) self.assertEqual(response.status_code, 201) self.subnet_id = response.json()['properties']['subnets'][0]['id'] # create public ip address for VM NIC self.ipname = self.vnet + 'ip' print('Creating VM NIC public ip address: ' + self.ipname) dns_label = self.vnet response = azurerm.create_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname, dns_label, self.location) self.assertEqual(response.status_code, 201) self.ip_id = response.json()['id'] # create public ip address for VMSS LB self.ipname2 = self.vnet + 'ip2' print('Creating VMSS LB public ip address: ' + self.ipname2) dns_label2 = self.vnet + '2' response = azurerm.create_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname2, dns_label2, self.location) self.assertEqual(response.status_code, 201) self.ip2_id = response.json()['id'] # create NSG nsg_name = self.vnet + 'nsg' print('Creating NSG: ' + nsg_name) response = azurerm.create_nsg(self.access_token, self.subscription_id, self.rgname, \ nsg_name, self.location) self.assertEqual(response.status_code, 201) # print(json.dumps(response.json())) self.nsg_id = response.json()['id'] # create NSG rule time.sleep(5) nsg_rule = 'ssh' print('Creating NSG rule: ' + nsg_rule) response = azurerm.create_nsg_rule(self.access_token, self.subscription_id, self.rgname, \ nsg_name, nsg_rule, description='ssh rule', destination_range='22') # print(json.dumps(response.json())) self.assertEqual(response.status_code, 201) # create nic for VM create # sleep long enough for subnet to finish creating time.sleep(10) nic_name = self.vnet + 'nic' print('Creating nic: ' + nic_name) response = azurerm.create_nic(self.access_token, self.subscription_id, self.rgname, \ nic_name, self.ip_id, self.subnet_id, self.location) self.assertEqual(response.status_code, 201) self.nic_id = response.json()['id'] # create load balancer with nat pool for VMSS create lb_name = self.vnet + 'lb' print('Creating load balancer with nat pool: ' + lb_name) response = azurerm.create_lb_with_nat_pool(self.access_token, self.subscription_id, \ self.rgname, lb_name, self.ip2_id, '50000', '50100', '22', self.location) self.be_pool_id = response.json( )['properties']['backendAddressPools'][0]['id'] self.lb_pool_id = response.json( )['properties']['inboundNatPools'][0]['id']
def main(): '''Main routine.''' # validate command line arguments arg_parser = argparse.ArgumentParser() arg_parser.add_argument('--vmname', '-n', required=True, action='store', help='Name') arg_parser.add_argument('--rgname', '-g', required=True, action='store', help='Resource Group Name') arg_parser.add_argument('--user', '-u', required=False, action='store', default='azure', help='Optional username') arg_parser.add_argument('--password', '-p', required=False, action='store', help='Optional password') arg_parser.add_argument('--sshkey', '-k', required=False, action='store', help='SSH public key') arg_parser.add_argument('--sshpath', '-s', required=False, action='store', help='SSH public key file path') arg_parser.add_argument('--location', '-l', required=False, action='store', help='Location, e.g. eastus') arg_parser.add_argument('--vmsize', required=False, action='store', default='Standard_D1_V2', help='VM size, defaults to Standard_D1_V2') arg_parser.add_argument('--dns', '-d', required=False, action='store', help='DNS, e.g. myuniquename') arg_parser.add_argument( '--vnet', required=False, action='store', help='Optional VNET Name (else first VNET in resource group used)') arg_parser.add_argument('--nowait', action='store_true', default=False, help='Do not wait for VM to finish provisioning') arg_parser.add_argument( '--nonsg', action='store_true', default=False, help='Do not create a network security group on the NIC') arg_parser.add_argument('--verbose', '-v', action='store_true', default=False, help='Print operational details') args = arg_parser.parse_args() name = args.vmname rgname = args.rgname vnet = args.vnet location = args.location username = args.user password = args.password sshkey = args.sshkey sshpath = args.sshpath verbose = args.verbose dns_label = args.dns no_wait = args.nowait no_nsg = args.nonsg vmsize = args.vmsize # make sure all authentication scenarios are handled if sshkey is not None and sshpath is not None: sys.exit( 'Error: You can provide an SSH public key, or a public key file path, not both.' ) if password is not None and (sshkey is not None or sshpath is not None): sys.exit('Error: provide a password or SSH key (or nothing), not both') use_password = False if password is not None: use_password = True else: if sshkey is None and sshpath is None: # no auth parameters were provided # look for ~/id_rsa.pub home = os.path.expanduser('~') sshpath = home + os.sep + '.ssh' + os.sep + 'id_rsa.pub' if os.path.isfile(sshpath) is False: print('Default public key file not found.') use_password = True password = Haikunator().haikunate( delimiter=',') # creates random password print('Created new password = '******'Default public key file found') if use_password is False: print('Reading public key..') if sshkey is None: # at this point sshpath should have a valid Value with open(sshpath, 'r') as pub_ssh_file_fd: sshkey = pub_ssh_file_fd.read() # Load Azure app defaults try: with open('azurermconfig.json') as config_file: config_data = json.load(config_file) except FileNotFoundError: sys.exit("Error: Expecting azurermconfig.json in current folder") tenant_id = config_data['tenantId'] app_id = config_data['appId'] app_secret = config_data['appSecret'] subscription_id = config_data['subscriptionId'] # authenticate access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) # if no location parameter was specified now would be a good time to figure out the location if location is None: try: rgroup = azurerm.get_resource_group(access_token, subscription_id, rgname) location = rgroup['location'] except KeyError: print('Cannot find resource group ' + rgname + '. Check connection/authorization.') print( json.dumps(rgroup, sort_keys=False, indent=2, separators=(',', ': '))) sys.exit() print('location = ' + location) # get VNET print('Getting VNet') vnet_not_found = False if vnet is None: print('VNet not set, checking resource group') # get first VNET in resource group try: vnets = azurerm.list_vnets_rg(access_token, subscription_id, rgname) # print(json.dumps(vnets, sort_keys=False, indent=2, separators=(',', ': '))) vnetresource = vnets['value'][0] except IndexError: print('No VNET found in resource group.') vnet_not_found = True vnet = name + 'vnet' else: print('Getting VNet: ' + vnet) vnetresource = azurerm.get_vnet(access_token, subscription_id, rgname, vnet) if 'properties' not in vnetresource: print('VNet ' + vnet + ' not found in resource group ' + rgname) vnet_not_found = True if vnet_not_found is True: # create a vnet print('Creating vnet: ' + vnet) rmresource = azurerm.create_vnet(access_token, subscription_id, rgname, vnet, location, \ address_prefix='10.0.0.0/16', nsg_id=None) if rmresource.status_code != 201: print('Error ' + str(vnetresource.status_code) + ' creating VNET. ' + vnetresource.text) sys.exit() vnetresource = azurerm.get_vnet(access_token, subscription_id, rgname, vnet) try: subnet_id = vnetresource['properties']['subnets'][0]['id'] except KeyError: print('Subnet not found for VNet ' + vnet) sys.exit() if verbose is True: print('subnet_id = ' + subnet_id) public_ip_name = name + 'ip' if dns_label is None: dns_label = name + 'dns' print('Creating public ipaddr') rmreturn = azurerm.create_public_ip(access_token, subscription_id, rgname, public_ip_name, dns_label, location) if rmreturn.status_code not in [200, 201]: print(rmreturn.text) sys.exit('Error: ' + str(rmreturn.status_code) + ' from azurerm.create_public_ip()') ip_id = rmreturn.json()['id'] if verbose is True: print('ip_id = ' + ip_id) print('Waiting for IP provisioning..') waiting = True while waiting: pip = azurerm.get_public_ip(access_token, subscription_id, rgname, public_ip_name) if pip['properties']['provisioningState'] == 'Succeeded': waiting = False time.sleep(1) if no_nsg is True: nsg_id = None else: # create NSG nsg_name = name + 'nsg' print('Creating NSG: ' + nsg_name) rmreturn = azurerm.create_nsg(access_token, subscription_id, rgname, nsg_name, location) if rmreturn.status_code not in [200, 201]: print('Error ' + str(rmreturn.status_code) + ' creating NSG. ' + rmreturn.text) sys.exit() nsg_id = rmreturn.json()['id'] # create NSG rule for ssh, scp nsg_rule = 'ssh' print('Creating NSG rule: ' + nsg_rule) rmreturn = azurerm.create_nsg_rule(access_token, subscription_id, rgname, nsg_name, nsg_rule, description='ssh rule', destination_range='22') if rmreturn.status_code not in [200, 201]: print('Error ' + str(rmreturn.status_code) + ' creating NSG rule. ' + rmreturn.text) sys.exit() # create NIC nic_name = name + 'nic' print('Creating NIC: ' + nic_name) rmreturn = azurerm.create_nic(access_token, subscription_id, rgname, nic_name, ip_id, subnet_id, location, nsg_id=nsg_id) if rmreturn.status_code not in [200, 201]: print('Error ' + rmreturn.status_code + ' creating NSG rule. ' + rmreturn.text) sys.exit() nic_id = rmreturn.json()['id'] print('Waiting for NIC provisioning..') waiting = True while waiting: nic = azurerm.get_nic(access_token, subscription_id, rgname, nic_name) if nic['properties']['provisioningState'] == 'Succeeded': waiting = False time.sleep(1) # create VM vm_name = name #publisher = 'CoreOS' #offer = 'CoreOS' #sku = 'Stable' publisher = 'Canonical' offer = 'UbuntuServer' sku = '16.04-LTS' version = 'latest' print('Creating VM: ' + vm_name) if use_password is True: rmreturn = azurerm.create_vm(access_token, subscription_id, rgname, vm_name, vmsize, publisher, offer, sku, version, nic_id, location, username=username, password=password) else: rmreturn = azurerm.create_vm(access_token, subscription_id, rgname, vm_name, vmsize, publisher, offer, sku, version, nic_id, location, username=username, public_key=sshkey) if rmreturn.status_code != 201: sys.exit('Error ' + rmreturn.status_code + ' creating VM. ' + rmreturn.text) if no_wait is False: print('Waiting for VM provisioning..') waiting = True while waiting: vm_model = azurerm.get_vm(access_token, subscription_id, rgname, vm_name) if vm_model['properties']['provisioningState'] == 'Succeeded': waiting = False time.sleep(5) print('VM provisioning complete.') print('Connect with:') print('ssh ' + dns_label + '.' + location + '.cloudapp.azure.com -l ' + username)
def setUp(self): # Load Azure app defaults try: with open('azurermconfig.json') as configFile: configData = json.load(configFile) except FileNotFoundError: print("Error: Expecting vmssConfig.json in current folder") sys.exit() tenant_id = configData['tenantId'] app_id = configData['appId'] app_secret = configData['appSecret'] self.subscription_id = configData['subscriptionId'] self.access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) self.location = configData['location'] # generate names used in tests self.h = Haikunator() self.rgname = self.h.haikunate() self.vnet = self.h.haikunate() # self.saname = self.h.haikunate(delimiter='') self.vmname = self.h.haikunate(delimiter='') self.vmssname = self.h.haikunate(delimiter='') self.asname = self.h.haikunate() # generate RSA Key for compute resources key = rsa.generate_private_key(backend=default_backend(), public_exponent=65537, \ key_size=2048) self.public_key = key.public_key().public_bytes(serialization.Encoding.OpenSSH, \ serialization.PublicFormat.OpenSSH).decode('utf-8') # create resource group print('Creating resource group: ' + self.rgname) response = azurerm.create_resource_group(self.access_token, self.subscription_id, \ self.rgname, self.location) self.assertEqual(response.status_code, 201) # create vnet print('Creating vnet: ' + self.vnet) response = azurerm.create_vnet(self.access_token, self.subscription_id, self.rgname, \ self.vnet, self.location, address_prefix='10.0.0.0/16', nsg_id=None) self.assertEqual(response.status_code, 201) self.subnet_id = response.json()['properties']['subnets'][0]['id'] # create public ip address for VM NIC self.ipname = self.vnet + 'ip' print('Creating VM NIC public ip address: ' + self.ipname) dns_label = self.vnet response = azurerm.create_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname, dns_label, self.location) self.assertEqual(response.status_code, 201) self.ip_id = response.json()['id'] # create public ip address for VMSS LB self.ipname2 = self.vnet + 'ip2' print('Creating VMSS LB public ip address: ' + self.ipname2) dns_label2 = self.vnet + '2' response = azurerm.create_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname2, dns_label2, self.location) self.assertEqual(response.status_code, 201) self.ip2_id = response.json()['id'] # create NSG nsg_name = self.vnet + 'nsg' print('Creating NSG: ' + nsg_name) response = azurerm.create_nsg(self.access_token, self.subscription_id, self.rgname, \ nsg_name, self.location) self.assertEqual(response.status_code, 201) # print(json.dumps(response.json())) self.nsg_id = response.json()['id'] # create NSG rule nsg_rule = 'ssh' print('Creating NSG rule: ' + nsg_rule) response = azurerm.create_nsg_rule(self.access_token, self.subscription_id, self.rgname, \ nsg_name, nsg_rule, description='ssh rule', destination_range='22') self.assertEqual(response.status_code, 201) # create nic for VM create nic_name = self.vnet + 'nic' print('Creating nic: ' + nic_name) response = azurerm.create_nic(self.access_token, self.subscription_id, self.rgname, \ nic_name, self.ip_id, self.subnet_id, self.location) self.assertEqual(response.status_code, 201) self.nic_id = response.json()['id'] # create load balancer with nat pool for VMSS create lb_name = self.vnet + 'lb' print('Creating load balancer with nat pool: ' + lb_name) response = azurerm.create_lb_with_nat_pool(self.access_token, self.subscription_id, \ self.rgname, lb_name, self.ip2_id, '50000', '50100', '22', self.location) self.be_pool_id = response.json()['properties']['backendAddressPools'][0]['id'] self.lb_pool_id = response.json()['properties']['inboundNatPools'][0]['id']
def main(): '''Main routine.''' # validate command line arguments arg_parser = argparse.ArgumentParser() arg_parser.add_argument('--name', '-n', required=True, action='store', help='Name') arg_parser.add_argument('--rgname', '-g', required=True, action='store', help='Resource Group Name') arg_parser.add_argument('--location', '-l', required=True, action='store', help='Location, e.g. eastus') arg_parser.add_argument('--verbose', '-v', action='store_true', default=False, help='Print operational details') args = arg_parser.parse_args() name = args.name rgname = args.rgname location = args.location # Load Azure app defaults try: with open('azurermconfig.json') as config_file: config_data = json.load(config_file) except FileNotFoundError: sys.exit('Error: Expecting azurermconfig.json in current folder') tenant_id = config_data['tenantId'] app_id = config_data['appId'] app_secret = config_data['appSecret'] subscription_id = config_data['subscriptionId'] # authenticate access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) # initialize haikunator hkn = Haikunator() # create NSG nsg_name = name + 'nsg' print('Creating NSG: ' + nsg_name) rmreturn = azurerm.create_nsg(access_token, subscription_id, rgname, nsg_name, location) nsg_id = rmreturn.json()['id'] print('nsg_id = ' + nsg_id) # create NSG rule nsg_rule = 'ssh' print('Creating NSG rule: ' + nsg_rule) rmreturn = azurerm.create_nsg_rule(access_token, subscription_id, rgname, nsg_name, nsg_rule, description='ssh rule', destination_range='22') print(rmreturn) print(json.dumps(rmreturn.json(), sort_keys=False, indent=2, separators=(',', ': '))) # create VNET vnetname = name + 'vnet' print('Creating VNet: ' + vnetname) rmreturn = azurerm.create_vnet(access_token, subscription_id, rgname, vnetname, location, nsg_id=nsg_id) print(rmreturn) # print(json.dumps(rmreturn.json(), sort_keys=False, indent=2, separators=(',', ': '))) subnet_id = rmreturn.json()['properties']['subnets'][0]['id'] print('subnet_id = ' + subnet_id) # create public IP address public_ip_name = name + 'ip' dns_label = name + 'ip' print('Creating public IP address: ' + public_ip_name) rmreturn = azurerm.create_public_ip(access_token, subscription_id, rgname, public_ip_name, dns_label, location) print(rmreturn) ip_id = rmreturn.json()['id'] print('ip_id = ' + ip_id) print('Waiting for IP provisioning..') waiting = True while waiting: ipa = azurerm.get_public_ip(access_token, subscription_id, rgname, public_ip_name) if ipa['properties']['provisioningState'] == 'Succeeded': waiting = False time.sleep(1) # create NIC nic_name = name + 'nic' print('Creating NIC: ' + nic_name) rmreturn = azurerm.create_nic(access_token, subscription_id, rgname, nic_name, ip_id, subnet_id, location) #print(json.dumps(rmreturn.json(), sort_keys=False, indent=2, separators=(',', ': '))) nic_id = rmreturn.json()['id'] print('Waiting for NIC provisioning..') waiting = True while waiting: nic = azurerm.get_nic(access_token, subscription_id, rgname, nic_name) if nic['properties']['provisioningState'] == 'Succeeded': waiting = False time.sleep(1) # create VM vm_name = name vm_size = 'Standard_D1' publisher = 'CoreOS' offer = 'CoreOS' sku = 'Stable' version = 'latest' username = '******' password = hkn.haikunate(delimiter=',') # creates random password print('password = '******'Creating VM: ' + vm_name) rmreturn = azurerm.create_vm(access_token, subscription_id, rgname, vm_name, vm_size, publisher, offer, sku, version, nic_id, location, username=username, password=password) print(rmreturn) print(json.dumps(rmreturn.json(), sort_keys=False, indent=2, separators=(',', ': ')))
def test_network(self): # create public ip print('Creating public ip address: ' + self.ipname) dns_label = self.vnet response = azurerm.create_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname, dns_label, self.location) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], self.ipname) # print(json.dumps(response.json())) ip_id = response.json()['id'] # create public ip for load balancer print('Creating public ip address for load balancer: ' + self.lbipname) dns_label = self.vnet + 'lb' response = azurerm.create_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.lbipname, dns_label, self.location) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], self.lbipname) # print(json.dumps(response.json())) lbip_id = response.json()['id'] # create vnet print('Creating vnet: ' + self.vnet) response = azurerm.create_vnet(self.access_token, self.subscription_id, self.rgname, \ self.vnet, self.location, address_prefix='10.0.0.0/16', nsg_id=None) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], self.vnet) subnet_id = response.json()['properties']['subnets'][0]['id'] # create NSG nsg_name = self.vnet + 'nsg' print('Creating NSG: ' + nsg_name) response = azurerm.create_nsg(self.access_token, self.subscription_id, self.rgname, \ nsg_name, self.location) self.assertEqual(response.status_code, 201) # print(json.dumps(response.json())) self.assertEqual(response.json()['name'], nsg_name) nsg_id = response.json()['id'] # create NSG rule nsg_rule = 'ssh' print('Creating NSG rule: ' + nsg_rule) response = azurerm.create_nsg_rule(self.access_token, self.subscription_id, self.rgname, \ nsg_name, nsg_rule, description='ssh rule', destination_range='22') self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], nsg_rule) # create nic nic_name = self.vnet + 'nic' # sleep long enough for subnet to finish creating time.sleep(10) print('Creating nic: ' + nic_name) response = azurerm.create_nic(self.access_token, self.subscription_id, self.rgname, \ nic_name, ip_id, subnet_id, self.location, nsg_id=nsg_id) # print(response.text) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], nic_name) nic_id = response.json()['id'] # create load balancer with nat pool lb_name = self.vnet + 'lb' print('Creating load balancer with nat pool: ' + lb_name) response = azurerm.create_lb_with_nat_pool(self.access_token, self.subscription_id, \ self.rgname, lb_name, lbip_id, '50000', '50100', '22', self.location) # print(json.dumps(response.json())) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], lb_name) # get public ip print('Getting public ip ' + self.ipname) response = azurerm.get_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname) self.assertEqual(response['name'], self.ipname) # get vnet print('Getting vnet: ' + self.vnet) response = azurerm.get_vnet(self.access_token, self.subscription_id, self.rgname, \ self.vnet) self.assertEqual(response['name'], self.vnet) # list vnets print('Listing vnets in sub') response = azurerm.list_vnets(self.access_token, self.subscription_id) # print(json.dumps(response, sort_keys=False, indent=2, separators=(',', ': '))) self.assertTrue(len(response['value']) > 0) # get nic print('Getting nic: ' + nic_name) response = azurerm.get_nic(self.access_token, self.subscription_id, self.rgname, \ nic_name) self.assertEqual(response['name'], nic_name) # list nics in resource group print('Listing nics in resource group: ' + self.rgname) response = azurerm.list_nics_rg(self.access_token, self.subscription_id, self.rgname) self.assertEqual(response['value'][0]['name'], nic_name) # list nics in subscription print('Listing nics in subscription.') response = azurerm.list_nics(self.access_token, self.subscription_id) self.assertTrue(len(response['value']) > 0) # delete nsg rule print('Deleting nsg rule: ' + nsg_rule) response = azurerm.delete_nsg_rule(self.access_token, self.subscription_id, self.rgname, \ nsg_name, nsg_rule) self.assertEqual(response.status_code, 202) # delete nic print('Deleting nic: ' + nic_name) response = azurerm.delete_nic(self.access_token, self.subscription_id, self.rgname, \ nic_name) self.assertEqual(response.status_code, 202) # delete nsg print('Deleting nsg: ' + nsg_name) response = azurerm.delete_nsg(self.access_token, self.subscription_id, self.rgname, \ nsg_name) self.assertEqual(response.status_code, 202) # delete public ip print('Deleting public ip ' + self.ipname) response = azurerm.delete_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname) self.assertEqual(response.status_code, 202) # delete load balancer print('Deleting load balancer ' + lb_name) response = azurerm.delete_load_balancer(self.access_token, self.subscription_id, self.rgname, \ lb_name) self.assertEqual(response.status_code, 202) # delete vnet print('Deleting vnet: ' + self.vnet) response = azurerm.delete_vnet(self.access_token, self.subscription_id, self.rgname, \ self.vnet) self.assertEqual(response.status_code, 202) # get network usage print('Getting network usage') response = azurerm.get_network_usage(self.access_token, self.subscription_id, self.location) self.assertTrue(len(response['value']) > 0)
def main(): '''Main routine.''' # validate command line arguments arg_parser = argparse.ArgumentParser() arg_parser.add_argument('--name', '-n', required=True, action='store', help='Name of vmss') arg_parser.add_argument('--capacity', '-c', required=True, action='store', help='Number of VMs') arg_parser.add_argument('--location', '-l', action='store', help='Location, e.g. eastus') arg_parser.add_argument('--verbose', '-v', action='store_true', default=False, help='Print operational details') args = arg_parser.parse_args() name = args.name location = args.location capacity = args.capacity # Load Azure app defaults try: with open('azurermconfig.json') as config_file: config_data = json.load(config_file) except FileNotFoundError: print("Error: Expecting azurermconfig.json in current folder") sys.exit() tenant_id = config_data['tenantId'] app_id = config_data['appId'] app_secret = config_data['appSecret'] subscription_id = config_data['subscriptionId'] # authenticate access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) # create resource group print('Creating resource group: ' + name) rmreturn = azurerm.create_resource_group(access_token, subscription_id, name, location) print(rmreturn) # create NSG nsg_name = name + 'nsg' print('Creating NSG: ' + nsg_name) rmreturn = azurerm.create_nsg(access_token, subscription_id, name, nsg_name, location) nsg_id = rmreturn.json()['id'] print('nsg_id = ' + nsg_id) # create NSG rule nsg_rule = 'ssh' print('Creating NSG rule: ' + nsg_rule) rmreturn = azurerm.create_nsg_rule(access_token, subscription_id, name, nsg_name, nsg_rule, description='ssh rule', destination_range='22') #print(json.dumps(rmreturn.json(), sort_keys=False, indent=2, separators=(',', ': '))) # create VNET vnetname = name + 'vnet' print('Creating VNet: ' + vnetname) rmreturn = azurerm.create_vnet(access_token, subscription_id, name, vnetname, location, nsg_id=nsg_id) print(rmreturn) # print(json.dumps(rmreturn.json(), sort_keys=False, indent=2, separators=(',', ': '))) subnet_id = rmreturn.json()['properties']['subnets'][0]['id'] print('subnet_id = ' + subnet_id) # create public IP address public_ip_name = name + 'ip' dns_label = name + 'ip' print('Creating public IP address: ' + public_ip_name) rmreturn = azurerm.create_public_ip(access_token, subscription_id, name, public_ip_name, dns_label, location) print(rmreturn) ip_id = rmreturn.json()['id'] print('ip_id = ' + ip_id) # create load balancer with nat pool lb_name = vnetname + 'lb' print('Creating load balancer with nat pool: ' + lb_name) rmreturn = azurerm.create_lb_with_nat_pool(access_token, subscription_id, name, lb_name, ip_id, '50000', '50100', '22', location) be_pool_id = rmreturn.json()['properties']['backendAddressPools'][0]['id'] lb_pool_id = rmreturn.json()['properties']['inboundNatPools'][0]['id'] # create VMSS vmss_name = name vm_size = 'Standard_D1_v2' publisher = 'Canonical' offer = 'UbuntuServer' sku = '16.04-LTS' version = 'latest' username = '******' password = Haikunator().haikunate(delimiter=',') # creates random password print('Password = '******'Creating VMSS: ' + vmss_name) rmreturn = azurerm.create_vmss(access_token, subscription_id, name, vmss_name, vm_size, capacity, publisher, offer, sku, version, subnet_id, be_pool_id, lb_pool_id, location, username=username, password=password) print(rmreturn) print( json.dumps(rmreturn.json(), sort_keys=False, indent=2, separators=(',', ': ')))
def test_network(self): # create public ip print('Creating public ip address: ' + self.ipname) dns_label = self.vnet response = azurerm.create_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname, dns_label, self.location) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], self.ipname) # print(json.dumps(response.json())) ip_id = response.json()['id'] # create public ip for load balancer print('Creating public ip address for load balancer: ' + self.lbipname) dns_label = self.vnet + 'lb' response = azurerm.create_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.lbipname, dns_label, self.location) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], self.lbipname) # print(json.dumps(response.json())) lbip_id = response.json()['id'] # create vnet print('Creating vnet: ' + self.vnet) response = azurerm.create_vnet(self.access_token, self.subscription_id, self.rgname, \ self.vnet, self.location, address_prefix='10.0.0.0/16', nsg_id=None) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], self.vnet) subnet_id = response.json()['properties']['subnets'][0]['id'] # create NSG nsg_name = self.vnet + 'nsg' print('Creating NSG: ' + nsg_name) response = azurerm.create_nsg(self.access_token, self.subscription_id, self.rgname, \ nsg_name, self.location) self.assertEqual(response.status_code, 201) # print(json.dumps(response.json())) self.assertEqual(response.json()['name'], nsg_name) nsg_id = response.json()['id'] # create NSG rule nsg_rule = 'ssh' print('Creating NSG rule: ' + nsg_rule) response = azurerm.create_nsg_rule(self.access_token, self.subscription_id, self.rgname, \ nsg_name, nsg_rule, description='ssh rule', destination_range='22') self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], nsg_rule) # create nic nic_name = self.vnet + 'nic' print('Creating nic: ' + nic_name) response = azurerm.create_nic(self.access_token, self.subscription_id, self.rgname, \ nic_name, ip_id, subnet_id, self.location) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], nic_name) nic_id = response.json()['id'] # create load balancer with nat pool lb_name = self.vnet + 'lb' print('Creating load balancer with nat pool: ' + lb_name) response = azurerm.create_lb_with_nat_pool(self.access_token, self.subscription_id, \ self.rgname, lb_name, lbip_id, '50000', '50100', '22', self.location) # print(json.dumps(response.json())) self.assertEqual(response.status_code, 201) self.assertEqual(response.json()['name'], lb_name) # get public ip print('Getting public ip ' + self.ipname) response = azurerm.get_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname) self.assertEqual(response['name'], self.ipname) # get vnet print('Getting vnet: ' + self.vnet) response = azurerm.get_vnet(self.access_token, self.subscription_id, self.rgname, \ self.vnet) self.assertEqual(response['name'], self.vnet) # list vnets print('Listing vnets in sub') response = azurerm.list_vnets(self.access_token, self.subscription_id) # print(json.dumps(response, sort_keys=False, indent=2, separators=(',', ': '))) self.assertTrue(len(response['value']) > 0) # get nic print('Getting nic: ' + nic_name) response = azurerm.get_nic(self.access_token, self.subscription_id, self.rgname, \ nic_name) self.assertEqual(response['name'], nic_name) # list nics in resource group print('Listing nics in resource group: ' + self.rgname) response = azurerm.list_nics_rg(self.access_token, self.subscription_id, self.rgname) self.assertEqual(response['value'][0]['name'], nic_name) # list nics in subscription print('Listing nics in subscription.') response = azurerm.list_nics(self.access_token, self.subscription_id) self.assertTrue(len(response['value']) > 0) # delete nsg rule print('Deleting nsg rule: ' + nsg_rule) response = azurerm.delete_nsg_rule(self.access_token, self.subscription_id, self.rgname, \ nsg_name, nsg_rule) self.assertEqual(response.status_code, 202) # delete nsg print('Deleting nsg: ' + nsg_name) response = azurerm.delete_nsg(self.access_token, self.subscription_id, self.rgname, \ nsg_name) self.assertEqual(response.status_code, 202) # delete nic print('Deleting nic: ' + nic_name) response = azurerm.delete_nic(self.access_token, self.subscription_id, self.rgname, \ nic_name) self.assertEqual(response.status_code, 202) # delete public ip print('Deleting public ip ' + self.ipname) response = azurerm.delete_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname) self.assertEqual(response.status_code, 202) # delete load balancer print('Deleting load balancer ' + lb_name) response = azurerm.delete_load_balancer(self.access_token, self.subscription_id, self.rgname, \ lb_name) self.assertEqual(response.status_code, 202) # delete vnet print('Deleting vnet: ' + self.vnet) response = azurerm.delete_vnet(self.access_token, self.subscription_id, self.rgname, \ self.vnet) self.assertEqual(response.status_code, 202) # get network usage print('Getting network usage') response = azurerm.get_network_usage(self.access_token, self.subscription_id, self.location) self.assertTrue(len(response['value']) > 0)
def setUp(self): # Load Azure app defaults try: with open('azurermconfig.json') as configFile: configData = json.load(configFile) except FileNotFoundError: print("Error: Expecting vmssConfig.json in current folder") sys.exit() tenant_id = configData['tenantId'] app_id = configData['appId'] app_secret = configData['appSecret'] self.subscription_id = configData['subscriptionId'] self.access_token = azurerm.get_access_token(tenant_id, app_id, app_secret) self.location = configData['location'] # generate names used in tests self.rgname = Haikunator.haikunate() self.vnet = Haikunator.haikunate(delimiter='') self.saname = Haikunator.haikunate(delimiter='') self.vmname = Haikunator.haikunate(delimiter='') self.vmssname = Haikunator.haikunate(delimiter='') # create resource group print('Creating resource group: ' + self.rgname) response = azurerm.create_resource_group(self.access_token, self.subscription_id, \ self.rgname, self.location) self.assertEqual(response.status_code, 201) # create vnet print('Creating vnet: ' + self.vnet) response = azurerm.create_vnet(self.access_token, self.subscription_id, self.rgname, \ self.vnet, self.location, address_prefix='10.0.0.0/16', nsg_id=None) self.assertEqual(response.status_code, 201) self.subnet_id = response.json()['properties']['subnets'][0]['id'] # create public ip address for VM NIC self.ipname = self.vnet + 'ip' print('Creating VM NIC public ip address: ' + self.ipname) dns_label = self.vnet response = azurerm.create_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname, dns_label, self.location) self.assertEqual(response.status_code, 201) self.ip_id = response.json()['id'] # create public ip address for VMSS LB self.ipname2 = self.vnet + 'ip2' print('Creating VMSS LB public ip address: ' + self.ipname2) dns_label2 = self.vnet + '2' response = azurerm.create_public_ip(self.access_token, self.subscription_id, self.rgname, \ self.ipname2, dns_label2, self.location) self.assertEqual(response.status_code, 201) self.ip2_id = response.json()['id'] # create storage account for VM print('Creating storage account: ' + self.saname) response = azurerm.create_storage_account(self.access_token, self.subscription_id, self.rgname, \ self.saname, self.location, storage_type='Standard_LRS') self.assertEqual(response.status_code, 202) # create 5 storage accounts for vmssname print('Creating storage accounts for scale set') self.container_list = [] for count in range(5): sa_name = ''.join(choice(ascii_lowercase) for i in range(10)) print(sa_name) response = azurerm.create_storage_account(self.access_token, self.subscription_id, \ self.rgname, sa_name, self.location, storage_type='Standard_LRS') self.assertEqual(response.status_code, 202) container = 'https://' + sa_name + '.blob.core.windows.net/' + self.vmssname + 'vhd' self.container_list.append(container) # create NSG nsg_name = self.vnet + 'nsg' print('Creating NSG: ' + nsg_name) response = azurerm.create_nsg(self.access_token, self.subscription_id, self.rgname, \ nsg_name, self.location) self.assertEqual(response.status_code, 201) # print(json.dumps(response.json())) self.nsg_id = response.json()['id'] # create NSG rule nsg_rule = 'ssh' print('Creating NSG rule: ' + nsg_rule) response = azurerm.create_nsg_rule(self.access_token, self.subscription_id, self.rgname, \ nsg_name, nsg_rule, description='ssh rule', destination_range='22') self.assertEqual(response.status_code, 201) # create nic for VM create nic_name = self.vnet + 'nic' print('Creating nic: ' + nic_name) response = azurerm.create_nic(self.access_token, self.subscription_id, self.rgname, \ nic_name, self.ip_id, self.subnet_id, self.location) self.assertEqual(response.status_code, 201) self.nic_id = response.json()['id'] # create load balancer with nat pool for VMSS create lb_name = self.vnet + 'lb' print('Creating load balancer with nat pool: ' + lb_name) response = azurerm.create_lb_with_nat_pool(self.access_token, self.subscription_id, \ self.rgname, lb_name, self.ip2_id, '50000', '50100', '22', self.location) self.be_pool_id = response.json()['properties']['backendAddressPools'][0]['id'] self.lb_pool_id = response.json()['properties']['inboundNatPools'][0]['id']