-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmanaged-disks-python.py
219 lines (196 loc) · 6.84 KB
/
managed-disks-python.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
# Install Azure Python SDK: https://azure-sdk-for-python.readthedocs.io/en/latest/
# pip install azure-mgmt-compute
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.network import NetworkManagementClient
from azure.mgmt.compute.models import *
from azure.mgmt.network.models import *
credentials = ServicePrincipalCredentials(
client_id = "",
secret = "",
tenant = ""
)
subscription_id = ""
rg_name = "avtestpy2"
location = "eastus"
resource = ResourceManagementClient(credentials, subscription_id)
compute = ComputeManagementClient(credentials, subscription_id)
network = NetworkManagementClient(credentials, subscription_id)
# Create resource group
print('\nCreate Resource Group')
resource.resource_groups.create_or_update(rg_name, {'location':location})
# Create virtual network
async_op = network.virtual_networks.create_or_update(rg_name, "avtest-vnet",
{
'location':location,
'address_space':{
'address_prefixes':['10.0.0.0/16']
},
'subnets': [
{
'name': 'default',
'address_prefix': '10.0.0.0/24'
}
]
}
)
async_op.wait()
# Create public IP address
print('\nCreate public IP address')
async_op = network.public_ip_addresses.create_or_update(rg_name, "avtest-publicip",
{
'location':location
})
async_op.wait()
# Create network interface
print('\nCreate network interface')
subnet = network.subnets.get(rg_name, "avtest-vnet", "default")
public_ip = network.public_ip_addresses.get(rg_name, "avtest-publicip")
async_op = network.network_interfaces.create_or_update(rg_name, "avtest-nic",
{
'location':location,
'ip_configurations':[{
'name':'ipConf1',
'subnet':{
'id':subnet.id
},
'public_ip_address':{
'id':public_ip.id
}
}]
})
nic = async_op.result()
# Create virtual machine with managed OS Disk
print('\nCreate Linux VM with managed OS disk')
nic = network.network_interfaces.get(rg_name, "avtest-nic")
async_op = compute.virtual_machines.create_or_update(rg_name, "avtest-vm",
{
'location':location,
'os_profile':{
'computer_name':'avtest-vm',
'admin_username':'azureuser',
'admin_password':'P@ssw0rd$123'
},
'hardware_profile':{
'vm_size':'Standard_DS2_v2'
},
'storage_profile':{
'image_reference':{
'publisher':'OpenLogic',
'offer':'CentOS',
'sku':'7.3',
'version':'latest'
},
'os_disk': {
'name':'avtest-vm-osdisk',
'caching':'ReadWrite',
'create_option':'FromImage',
'managed_disk':{
'storage_account_type':'Premium_LRS'
}
}
},
'network_profile':{
'network_interfaces':[{
'id':nic.id
}]
}
})
vm = async_op.result()
# Create empty data disks
print('\nCreate three empty managed data disks')
async_op1 = compute.disks.create_or_update(rg_name, "avtest-datadisk1", Disk(location, CreationData(create_option = "Empty"), account_type = StorageAccountTypes.standard_lrs, disk_size_gb = 128))
async_op2 = compute.disks.create_or_update(rg_name, "avtest-datadisk2", Disk(location, CreationData(create_option = "Empty"), account_type = StorageAccountTypes.standard_lrs, disk_size_gb = 128))
async_op3 = compute.disks.create_or_update(rg_name, "avtest-datadisk3", Disk(location, CreationData(create_option = "Empty"), account_type = StorageAccountTypes.standard_lrs, disk_size_gb = 128))
async_op1.wait()
async_op2.wait()
async_op3.wait()
# Attach managed data disk to VM
print('\nVM storage profile of the OS disk:')
vm = compute.virtual_machines.get(rg_name, "avtest-vm")
print(vm.storage_profile.os_disk.managed_disk)
os_disk = compute.disks.get(rg_name, "avtest-vm-osdisk")
data_disk_1 = compute.disks.get(rg_name, "avtest-datadisk1")
data_disk_2 = compute.disks.get(rg_name, "avtest-datadisk2")
data_disk_3 = compute.disks.get(rg_name, "avtest-datadisk3")
print('\nManaged data disk 1:')
print(data_disk_1)
print('\nAttach three managed data disks to VM')
async_op = compute.virtual_machines.create_or_update(rg_name, "avtest-vm",
{
'location':location,
'storage_profile':{
'data_disks':[
{
'lun':0,
'create_option':'Attach',
'caching':'ReadOnly',
'managed_disk':{
'id':data_disk_1.id,
'storage_account_type':'Premium_LRS'
}
},
{
'lun':1,
'create_option':'Attach',
'caching':'ReadOnly',
'managed_disk':{
'id':data_disk_2.id,
'storage_account_type':'Premium_LRS'
}
}
,
{
'lun':2,
'create_option':'Attach',
'caching':'ReadOnly',
'managed_disk':{
'id':data_disk_3.id,
'storage_account_type':'Standard_LRS'
}
}
]
}
})
async_op.wait()
# Detach managed data disk from VM
vm = compute.virtual_machines.get(rg_name, "avtest-vm")
data_disks = vm.storage_profile.data_disks
print('\nCurrent data disks attached to the VM:')
for disk in data_disks:
print(disk.managed_disk.id)
print('\nDetach one of the three data disks by name from VM')
data_disks[:] = [disk for disk in data_disks if disk.name != "avtest-datadisk2"]
async_op = compute.virtual_machines.create_or_update(rg_name, "avtest-vm", vm)
async_op.wait()
# # List managed disks in resource group
print('\nList managed disks in resource group')
for disk in compute.disks.list_by_resource_group(rg_name):
print(disk)
# Create snapshot from disk
print('\nCreate snapshot from managed OS disk')
async_op = compute.snapshots.create_or_update(rg_name, "avtest-vm-osdisk-snapshot",
{
'location':location,
'creation_data':{
'create_option':'Copy',
'source_resource_id':os_disk.id
}
})
snapshot = async_op.result()
print(snapshot)
# Create managed disks from Snapshot
print('\nCreate managed disk from snapshot')
snapshot = compute.snapshots.get(rg_name, "avtest-vm-osdisk-snapshot")
async_op = compute.disks.create_or_update(rg_name, "avtest-vm-osdisk-copy",
{
'location':location,
'creation_data':{
'create_option':'Copy',
'source_resource_id':snapshot.id
}
})
disk = async_op.result()
print(disk)
print("\nDone")