-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy path850929-betcy.py
108 lines (94 loc) · 3.35 KB
/
850929-betcy.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
import os
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.storage import StorageManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute import ComputeManagementClient
from haikunator import Haikunator
haikunator = Haikunator()
# Azure Datacenter
LOCATION = 'westus'
# Resource Group
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': 'MicrosoftWindowsServerEssentials',
'offer': 'WindowsServerEssentials',
'sku': 'WindowsServerEssentials',
'version': 'latest'
}
}
def run_example():
"""Resource Group management example."""
subscription_id = os.environ.get(
'AZURE_SUBSCRIPTION_ID',
'11111111-1111-1111-1111-111111111111') # your Azure Subscription Id
credentials = ServicePrincipalCredentials(
client_id=os.environ['AZURE_CLIENT_ID'],
secret=os.environ['AZURE_CLIENT_SECRET'],
tenant=os.environ['AZURE_TENANT_ID']
)
resource_client = ResourceManagementClient(credentials, subscription_id)
compute_client = ComputeManagementClient(credentials, subscription_id)
storage_client = StorageManagementClient(credentials, subscription_id)
network_client = NetworkManagementClient(credentials, subscription_id)
# for resource group
resource_client.resource_groups.create_or_update(
GROUP_NAME, {'location': LOCATION})
# for storage account
storage_async_operation = storage_client.storage_accounts.create(
GROUP_NAME,
STORAGE_ACCOUNT_NAME,
{
'sku': {'name': 'standard_lrs'},
'kind': 'storage',
'location': LOCATION
}
)
storage_async_operation.wait()
nic = create_nic(network_client)
# 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()
# to attach a data disk
async_vm_update = compute_client.virtual_machines.create_or_update(
GROUP_NAME,
VM_NAME,
{
'location': LOCATION,
'storage_profile': {
'data_disks': [{
'name': 'mydatadisk1',
'disk_size_gb': 1,
'lun': 0,
'vhd': {
'uri': "http://{}.blob.core.windows.net/vhds/mydatadisk1.vhd".format(
STORAGE_ACCOUNT_NAME)
},
'create_option': 'Empty'
}]
}
}
)
async_vm_update.wait()
if __name__ == "__main__":
run_example()