This repository has been archived by the owner on Dec 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathProgram.cs
236 lines (185 loc) · 9.85 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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// 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.Authentication;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
using System;
namespace ManageVirtualMachineWithUnmanagedDisks
{
public class Program
{
private static readonly string UserName = Utilities.CreateUsername();
private static readonly string Password = Utilities.CreatePassword();
private const string DataDiskName = "disk2";
/**
* Azure Compute sample for managing virtual machines -
* - Create a virtual machine
* - Start a virtual machine
* - Stop a virtual machine
* - Restart a virtual machine
* - Update a virtual machine
* - Expand the OS drive
* - 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)
{
string rgName = SdkContext.RandomResourceName("rgCOMV", 24);
string windowsVMName = SdkContext.RandomResourceName("wVM", 24);
string linuxVMName = SdkContext.RandomResourceName("lVM", 24);
try
{
var startTime = DateTimeOffset.Now.UtcDateTime;
var windowsVM = azure.VirtualMachines.Define(windowsVMName)
.WithRegion(Region.USEast)
.WithNewResourceGroup(rgName)
.WithNewPrimaryNetwork("10.0.0.0/28")
.WithPrimaryPrivateIPAddressDynamic()
.WithoutPrimaryPublicIPAddress()
.WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012R2Datacenter)
.WithAdminUsername(UserName)
.WithAdminPassword(Password)
.WithUnmanagedDisks()
.WithSize(VirtualMachineSizeTypes.Parse("Standard_D2a_v4"))
.Create();
var endTime = DateTimeOffset.Now.UtcDateTime;
Utilities.Log($"Created VM: took {(endTime - startTime).TotalSeconds} seconds");
Utilities.PrintVirtualMachine(windowsVM);
windowsVM.Update()
.WithTag("who-rocks", "open source")
.WithTag("where", "on azure")
.Apply();
Utilities.Log("Tagged VM: " + windowsVM.Id);
//=============================================================
// Update - Attach data disks
windowsVM.Update()
.WithNewUnmanagedDataDisk(10)
.DefineUnmanagedDataDisk(DataDiskName)
.WithNewVhd(20)
.WithCaching(CachingTypes.ReadWrite)
.Attach()
.Apply();
Utilities.Log("Attached a new data disk" + DataDiskName + " to VM" + windowsVM.Id);
Utilities.PrintVirtualMachine(windowsVM);
windowsVM.Update()
.WithoutUnmanagedDataDisk(DataDiskName)
.Apply();
Utilities.Log("Detached data disk " + DataDiskName + " from VM " + windowsVM.Id);
//=============================================================
// Update - Resize (expand) the data disk
// First, deallocate the virtual machine and then proceed with resize
Utilities.Log("De-allocating VM: " + windowsVM.Id);
windowsVM.Deallocate();
Utilities.Log("De-allocated VM: " + windowsVM.Id);
var dataDisk = windowsVM.UnmanagedDataDisks[0];
windowsVM.Update()
.UpdateUnmanagedDataDisk(dataDisk.Name)
.WithSizeInGB(30)
.Parent()
.Apply();
//=============================================================
// Update - Expand the OS drive size by 10 GB
int osDiskSizeInGb = windowsVM.OSDiskSize;
if (osDiskSizeInGb == 0)
{
// Server is not returning the OS Disk size, possible bug in server
Utilities.Log("Server is not returning the OS disk size, possible bug in the server?");
Utilities.Log("Assuming that the OS disk size is 256 GB");
osDiskSizeInGb = 256;
}
windowsVM.Update()
.WithOSDiskSizeInGB(osDiskSizeInGb + 10)
.Apply();
Utilities.Log("Expanded VM " + windowsVM.Id + "'s OS disk to " + (osDiskSizeInGb + 10));
//=============================================================
// Start the virtual machine
Utilities.Log("Starting VM " + windowsVM.Id);
windowsVM.Start();
Utilities.Log("Started VM: " + windowsVM.Id + "; state = " + windowsVM.PowerState);
//=============================================================
// 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.USEast)
.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 (Exception ex)
{
Utilities.Log(ex);
}
}
}
public static void Main(string[] args)
{
try
{
//=============================================================
// Authenticate
AzureCredentials 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);
}
}
}
}