-
Notifications
You must be signed in to change notification settings - Fork 65
/
Copy path851557_Pavan.py
147 lines (112 loc) · 4.31 KB
/
851557_Pavan.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
from azure.common.credentials import ServicePrincipalCredentials
from azure.mgmt.resource import ResourceManagementClient
from azure.mgmt.compute import ComputeManagementClient
from azure.mgmt.network import NetworkManagementClient
SUBSCRIPTION_ID = 'subscription-id'
GROUP_NAME = 'myResourceGroup'
LOCATION = 'westus'
VM_NAME = 'myVM'
#to get Active directory credentials
def get_credentials():
credentials = ServicePrincipalCredentials(
client_id = 'application-id',
secret = 'authentication-key',
tenant = 'tenant-id'
)
return credentials
#To create a Resource group
def create_resource_group(rg_client):
rg_params = { 'location':LOCATION }
rg_result = rg_client.resource_groups.create_or_update(GROUP_NAME, rg_params)
#To create a public ip address
def create_public_ip_address(network_client):
pip_params = {
'location': LOCATION,
'public_ip_allocation_method': 'Dynamic'
}
pip_result = network_client.public_ip_addresses.create_or_update(GROUP_NAME,'myIPAddress',p_params)
return pip_result.result()
#To create Virtual Network
def create_vnet(network_client):
vnet_params = {
'location': LOCATION,
'address_space': {
'address_prefixes': ['10.0.0.0/16']
}
}
vnet_result = network_client.virtual_networks.create_or_update(GROUP_NAME,'myVNet',vnet_params)
return vnet_result.result()
#To create subnets in the vnet
def create_subnet(network_client):
subnet_params = {
'address_prefix': '10.0.0.0/24'
}
subnet_result = network_client.subnets.create_or_update(GROUP_NAME,'myVNet','mySubnet',subnet_params)
return subnet_result.result()
#To create network interface
def create_nic(network_client):
subnet_info = network_client.subnets.get(GROUP_NAME, 'myVNet', 'mySubnet')
publicIPAddress = network_client.public_ip_addresses.get(GROUP_NAME,'myIPAddress')
nic_params = {
'location': LOCATION,
'ip_configurations': [{
'name': 'myIPConfig',
'public_ip_address': publicIPAddress,
'subnet': {
'id': subnet_info.id
}
}]
}
nic_result = network_client.network_interfaces.create_or_update(GROUP_NAME, 'myNic', nic_params)
return nic_result.result()
# To create the virtual machine
def create_vm(network_client, compute_client):
nic = network_client.network_interfaces.get(GROUP_NAME, 'myNic')
vm_params = {
'location': LOCATION,
'os_profile': {
'computer_name': VM_NAME,
'admin_username': 'azureuser',
'admin_password': 'Azure12345678'
},
'hardware_profile': {
'vm_size': 'Standard_DS1'
},
'storage_profile': {
'image_reference': {
'publisher': 'MicrosoftWindowsServer',
'offer': 'WindowsServer',
'sku': '2012-R2-Datacenter',
'version': 'latest'
}
},
'network_profile': {
'network_interfaces': [{
'id': nic.id
}]
}
}
vm_result = compute_client.virtual_machines.create_or_update(GROUP_NAME, VM_NAME, vm_params)
return vm_result.result()
#To stop the virtual machine
def stop_vm(compute_client):
compute_client.virtual_machines.power_off(GROUP_NAME, VM_NAME)
#Main function
if __name__ == "__main__":
credentials = get_credentials()
rg_client = ResourceManagementClient(credentials,SUBSCRIPTION_ID)
network_client = NetworkManagementClient(credentials,SUBSCRIPTION_ID)
compute_client = ComputeManagementClient(credentials,SUBSCRIPTION_ID)
create_resource_group(resource_group_client)
pip_result = create_public_ip_address(network_client)
print(pip_result)
vnet_result = create_vnet(network_client)
print(vnet_result)
subn_result = create_subnet(network_client)
print(subn_result)
nic_result = create_nic(network_client)
print(nic_result)
vm_result = create_vm(network_client, compute_client)
print(vm_result)
# To stop the virtual machine
#stop_vm(compute_client)