def create_vm(access_token, subscription_id, resource_group, vm_name, vm_size, publisher, offer,
              sku, version, nic_id, location, storage_type='Standard_LRS', osdisk_name=None,
              username='azure', password=None, public_key=None):
    '''Create a new Azure virtual machine.

    Args:
        access_token (str): A valid Azure authentication token.
        subscription_id (str): Azure subscription id.
        resource_group (str): Azure resource group name.
        vm_name (str): Name of the new virtual machine.
        vm_size (str): Size of virtual machine, e.g. 'Standard_D1_v2'.
        publisher (str): VM image publisher. E.g. 'MicrosoftWindowsServer'.
        offer (str): VM image offer. E.g. 'WindowsServer'.
        sku (str): VM image sku. E.g. '2016-Datacenter'.
        version (str): VM image version. E.g. 'latest'.
        nic_id (str): Resource id of a NIC.
        location (str): Azure data center location. E.g. westus.
        storage_type (str): Optional storage type. Default 'Standard_LRS'.
        osdisk_name (str): Optional OS disk name. Default is None.
        username (str): Optional user name. Default is 'azure'.
        password (str): Optional password. Default is None (not required if using public_key).
        public_key (str): Optional public key. Default is None (not required if using password,
            e.g. on Windows).

    Returns:
        HTTP response. JSON body of the virtual machine properties.
    '''
    endpoint = ''.join([get_rm_endpoint(),
                        '/subscriptions/', subscription_id,
                        '/resourceGroups/', resource_group,
                        '/providers/Microsoft.Compute/virtualMachines/', vm_name,
                        '?api-version=', COMP_API])
    if osdisk_name is None:
        osdisk_name = vm_name + 'osdisk'
    vm_body = {'name': vm_name}
    vm_body['location'] = location
    properties = {'hardwareProfile': {'vmSize': vm_size}}
    image_reference = {'publisher': publisher,
                       'offer': offer, 'sku': sku, 'version': version}
    storage_profile = {'imageReference': image_reference}
    os_disk = {'name': osdisk_name}
    os_disk['managedDisk'] = {'storageAccountType': storage_type}
    os_disk['caching'] = 'ReadWrite'
    os_disk['createOption'] = 'fromImage'
    storage_profile['osDisk'] = os_disk
    properties['storageProfile'] = storage_profile
    os_profile = {'computerName': vm_name}
    os_profile['adminUsername'] = username
    if password is not None:
        os_profile['adminPassword'] = password
    if public_key is not None:
        if password is None:
            disable_pswd = True
        else:
            disable_pswd = False
        linux_config = {'disablePasswordAuthentication': disable_pswd}
        pub_key = {'path': '/home/' + username + '/.ssh/authorized_keys'}
        pub_key['keyData'] = public_key
        linux_config['ssh'] = {'publicKeys': [pub_key]}
        os_profile['linuxConfiguration'] = linux_config
    properties['osProfile'] = os_profile
    network_profile = {'networkInterfaces': [
        {'id': nic_id, 'properties': {'primary': True}}]}
    properties['networkProfile'] = network_profile
    vm_body['properties'] = properties
    body = json.dumps(vm_body)
    return do_put(endpoint, body, access_token)
def create_vm ( access_token , subscription_id , resource_group , vm_name , vm_size , publisher , offer , sku , version , nic_id , location , storage_type = 'Standard_LRS' , osdisk_name = None , username = 'azure' , password = None , public_key = None ) : endpoint = '' . join ( [ get_rm_endpoint ( ) , '/subscriptions/' , subscription_id , '/resourceGroups/' , resource_group , '/providers/Microsoft.Compute/virtualMachines/' , vm_name , '?api-version=' , COMP_API ] ) if osdisk_name is None : osdisk_name = vm_name + 'osdisk' vm_body = { 'name' : vm_name } vm_body [ 'location' ] = location properties = { 'hardwareProfile' : { 'vmSize' : vm_size } } image_reference = { 'publisher' : publisher , 'offer' : offer , 'sku' : sku , 'version' : version } storage_profile = { 'imageReference' : image_reference } os_disk = { 'name' : osdisk_name } os_disk [ 'managedDisk' ] = { 'storageAccountType' : storage_type } os_disk [ 'caching' ] = 'ReadWrite' os_disk [ 'createOption' ] = 'fromImage' storage_profile [ 'osDisk' ] = os_disk properties [ 'storageProfile' ] = storage_profile os_profile = { 'computerName' : vm_name } os_profile [ 'adminUsername' ] = username if password is not None : os_profile [ 'adminPassword' ] = password if public_key is not None : if password is None : disable_pswd = True else : disable_pswd = False linux_config = { 'disablePasswordAuthentication' : disable_pswd } pub_key = { 'path' : '/home/' + username + '/.ssh/authorized_keys' } pub_key [ 'keyData' ] = public_key linux_config [ 'ssh' ] = { 'publicKeys' : [ pub_key ] } os_profile [ 'linuxConfiguration' ] = linux_config properties [ 'osProfile' ] = os_profile network_profile = { 'networkInterfaces' : [ { 'id' : nic_id , 'properties' : { 'primary' : True } } ] } properties [ 'networkProfile' ] = network_profile vm_body [ 'properties' ] = properties body = json . dumps ( vm_body ) return do_put ( endpoint , body , access_token )
Create a new Azure virtual machine.

    Args:
        access_token (str): A valid Azure authentication token.
        subscription_id (str): Azure subscription id.
        resource_group (str): Azure resource group name.
        vm_name (str): Name of the new virtual machine.
        vm_size (str): Size of virtual machine, e.g. 'Standard_D1_v2'.
        publisher (str): VM image publisher. E.g. 'MicrosoftWindowsServer'.
        offer (str): VM image offer. E.g. 'WindowsServer'.
        sku (str): VM image sku. E.g. '2016-Datacenter'.
        version (str): VM image version. E.g. 'latest'.
        nic_id (str): Resource id of a NIC.
        location (str): Azure data center location. E.g. westus.
        storage_type (str): Optional storage type. Default 'Standard_LRS'.
        osdisk_name (str): Optional OS disk name. Default is None.
        username (str): Optional user name. Default is 'azure'.
        password (str): Optional password. Default is None (not required if using public_key).
        public_key (str): Optional public key. Default is None (not required if using password,
            e.g. on Windows).

    Returns:
        HTTP response. JSON body of the virtual machine properties.