text
stringlengths 1
93.6k
|
|---|
GROUP_NAME = 'azure-sample-group-virtual-machines'
|
# Network
|
VNET_NAME = 'azure-sample-vnet'
|
SUBNET_NAME = 'azure-sample-subnet'
|
# VM
|
OS_DISK_NAME = 'azure-sample-osdisk'
|
STORAGE_ACCOUNT_NAME = haikunator.haikunate(delimiter='')
|
IP_CONFIG_NAME = 'azure-sample-ip-config'
|
NIC_NAME = 'azure-sample-nic'
|
USERNAME = 'userlogin'
|
PASSWORD = 'Pa$$w0rd91'
|
VM_NAME = 'VmName'
|
VM_REFERENCE = {
|
'linux': {
|
'publisher': 'Canonical',
|
'offer': 'UbuntuServer',
|
'sku': '16.04.0-LTS',
|
'version': 'latest'
|
},
|
'windows': {
|
'publisher': 'MicrosoftWindowsServer',
|
'offer': 'WindowsServer',
|
'sku': '2016-Datacenter',
|
'version': 'latest'
|
}
|
}
|
def get_credentials():
|
subscription_id = os.environ['AZURE_SUBSCRIPTION_ID']
|
credentials = ServicePrincipalCredentials(
|
client_id=os.environ['AZURE_CLIENT_ID'],
|
secret=os.environ['AZURE_CLIENT_SECRET'],
|
tenant=os.environ['AZURE_TENANT_ID']
|
)
|
return credentials, subscription_id
|
def run_example():
|
"""Virtual Machine management example."""
|
#
|
# Create all clients with an Application (service principal) token provider
|
#
|
credentials, subscription_id = get_credentials()
|
resource_client = ResourceManagementClient(credentials, subscription_id)
|
compute_client = ComputeManagementClient(credentials, subscription_id)
|
network_client = NetworkManagementClient(credentials, subscription_id)
|
###########
|
# Prepare #
|
###########
|
# Create Resource group
|
print('\nCreate Resource Group')
|
resource_client.resource_groups.create_or_update(
|
GROUP_NAME, {'location': LOCATION})
|
try:
|
# Create a NIC
|
nic = create_nic(network_client)
|
#############
|
# VM Sample #
|
#############
|
# Create Linux VM
|
print('\nCreating Linux Virtual Machine')
|
vm_parameters = create_vm_parameters(nic.id, VM_REFERENCE['linux'])
|
async_vm_creation = compute_client.virtual_machines.create_or_update(
|
GROUP_NAME, VM_NAME, vm_parameters)
|
async_vm_creation.wait()
|
# Tag the VM
|
print('\nTag Virtual Machine')
|
async_vm_update = compute_client.virtual_machines.create_or_update(
|
GROUP_NAME,
|
VM_NAME,
|
{
|
'location': LOCATION,
|
'tags': {
|
'who-rocks': 'python',
|
'where': 'on azure'
|
}
|
}
|
)
|
async_vm_update.wait()
|
# Create managed data disk
|
print('\nCreate (empty) managed Data Disk')
|
async_disk_creation = compute_client.disks.create_or_update(
|
GROUP_NAME,
|
'mydatadisk1',
|
{
|
'location': LOCATION,
|
'disk_size_gb': 1,
|
'creation_data': {
|
Subsets and Splits
No community queries yet
The top public SQL queries from the community will appear here once available.