-
Notifications
You must be signed in to change notification settings - Fork 8
/
Program.cs
219 lines (177 loc) · 8.99 KB
/
Program.cs
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Microsoft.Azure.Management.Compute.Fluent;
using Microsoft.Azure.Management.Compute.Fluent.Models;
using Microsoft.Azure.Management.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
using System;
namespace ManageVirtualMachine
{
public class Program
{
/**
* Azure Compute sample for managing virtual machines -
* - Create a virtual machine with managed OS Disk
* - Start a virtual machine
* - Stop a virtual machine
* - Restart a virtual machine
* - Update a virtual machine
* - Tag a virtual machine (there are many possible variations here)
* - Attach data disks
* - Detach data disks
* - List virtual machines
* - Delete a virtual machine.
*/
public static void RunSample(IAzure azure)
{
var region = Region.USWestCentral;
var windowsVmName = Utilities.CreateRandomName("wVM");
var linuxVmName = Utilities.CreateRandomName("lVM");
var rgName = Utilities.CreateRandomName("rgCOMV");
var userName = Utilities.CreateUsername();
var password = Utilities.CreatePassword();
try
{
//=============================================================
// Create a Windows virtual machine
// Prepare a creatable data disk for VM
//
var dataDiskCreatable = azure.Disks.Define(Utilities.CreateRandomName("dsk-"))
.WithRegion(region)
.WithExistingResourceGroup(rgName)
.WithData()
.WithSizeInGB(100);
// Create a data disk to attach to VM
//
var dataDisk = azure.Disks.Define(Utilities.CreateRandomName("dsk-"))
.WithRegion(region)
.WithNewResourceGroup(rgName)
.WithData()
.WithSizeInGB(50)
.Create();
Utilities.Log("Creating a Windows VM");
var t1 = new DateTime();
var windowsVM = azure.VirtualMachines.Define(windowsVmName)
.WithRegion(region)
.WithNewResourceGroup(rgName)
.WithNewPrimaryNetwork("10.0.0.0/28")
.WithPrimaryPrivateIPAddressDynamic()
.WithoutPrimaryPublicIPAddress()
.WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012R2Datacenter)
.WithAdminUsername(userName)
.WithAdminPassword(password)
.WithNewDataDisk(10)
.WithNewDataDisk(dataDiskCreatable)
.WithExistingDataDisk(dataDisk)
.WithSize(VirtualMachineSizeTypes.Parse("Standard_D2a_v4"))
.Create();
var t2 = new DateTime();
Utilities.Log($"Created VM: (took {(t2 - t1).TotalSeconds} seconds) " + windowsVM.Id);
// Print virtual machine details
Utilities.PrintVirtualMachine(windowsVM);
//=============================================================
// Update - Tag the virtual machine
windowsVM.Update()
.WithTag("who-rocks", "java")
.WithTag("where", "on azure")
.Apply();
Utilities.Log("Tagged VM: " + windowsVM.Id);
//=============================================================
// Update - Add data disk
windowsVM.Update()
.WithNewDataDisk(10)
.Apply();
Utilities.Log("Added a data disk to VM" + windowsVM.Id);
Utilities.PrintVirtualMachine(windowsVM);
//=============================================================
// Update - detach data disk
windowsVM.Update()
.WithoutDataDisk(0)
.Apply();
Utilities.Log("Detached data disk at lun 0 from VM " + windowsVM.Id);
//=============================================================
// Restart the virtual machine
Utilities.Log("Restarting VM: " + windowsVM.Id);
windowsVM.Restart();
Utilities.Log("Restarted VM: " + windowsVM.Id + "; state = " + windowsVM.PowerState);
//=============================================================
// Stop (powerOff) the virtual machine
Utilities.Log("Powering OFF VM: " + windowsVM.Id);
windowsVM.PowerOff();
Utilities.Log("Powered OFF VM: " + windowsVM.Id + "; state = " + windowsVM.PowerState);
// Get the network where Windows VM is hosted
var network = windowsVM.GetPrimaryNetworkInterface().PrimaryIPConfiguration.GetNetwork();
//=============================================================
// Create a Linux VM in the same virtual network
Utilities.Log("Creating a Linux VM in the network");
var linuxVM = azure.VirtualMachines.Define(linuxVmName)
.WithRegion(region)
.WithExistingResourceGroup(rgName)
.WithExistingPrimaryNetwork(network)
.WithSubnet("subnet1") // Referencing the default subnet name when no name specified at creation
.WithPrimaryPrivateIPAddressDynamic()
.WithoutPrimaryPublicIPAddress()
.WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
.WithRootUsername(userName)
.WithRootPassword(password)
.WithSize(VirtualMachineSizeTypes.Parse("Standard_D2a_v4"))
.Create();
Utilities.Log("Created a Linux VM (in the same virtual network): " + linuxVM.Id);
Utilities.PrintVirtualMachine(linuxVM);
//=============================================================
// List virtual machines in the resource group
var resourceGroupName = windowsVM.ResourceGroupName;
Utilities.Log("Printing list of VMs =======");
foreach (var virtualMachine in azure.VirtualMachines.ListByResourceGroup(resourceGroupName))
{
Utilities.PrintVirtualMachine(virtualMachine);
}
//=============================================================
// Delete the virtual machine
Utilities.Log("Deleting VM: " + windowsVM.Id);
azure.VirtualMachines.DeleteById(windowsVM.Id);
Utilities.Log("Deleted VM: " + windowsVM.Id);
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + rgName);
azure.ResourceGroups.DeleteByName(rgName);
Utilities.Log("Deleted Resource Group: " + rgName);
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception g)
{
Utilities.Log(g);
}
}
}
public static void Main(string[] args)
{
try
{
//=============================================================
// Authenticate
var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION"));
var azure = Azure
.Configure()
.WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic)
.Authenticate(credentials)
.WithDefaultSubscription();
// Print selected subscription
Utilities.Log("Selected subscription: " + azure.SubscriptionId);
RunSample(azure);
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
}