-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathProgram.cs
More file actions
213 lines (191 loc) · 9.35 KB
/
Program.cs
File metadata and controls
213 lines (191 loc) · 9.35 KB
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
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Azure;
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Compute;
using Azure.ResourceManager.Compute.Models;
using Azure.ResourceManager.Network;
using Azure.ResourceManager.Network.Models;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Samples.Common;
using Azure.ResourceManager.Storage;
using Azure.ResourceManager.Storage.Models;
namespace ConvertVirtualMachineToManagedDisks
{
public class Program
{
private static ResourceIdentifier? _resourceGroupId = null;
private static string userName = Utilities.CreateUsername();
private static string password = Utilities.CreatePassword();
private static AzureLocation region = AzureLocation.EastUS;
/**
* Azure Compute sample for managing virtual machines -
* - Create a virtual machine with un-managed OS and data disks
* - Deallocate the virtual machine
* - Migrate the virtual machine to use managed disk.
*/
public static async Task RunSample(ArmClient client)
{
var rgName = Utilities.CreateRandomName("rgCOMV");
var storageName = Utilities.CreateRandomName("storage");
var subnetName = Utilities.CreateRandomName("sub");
var vnetName = Utilities.CreateRandomName("vnet");
var nicName = Utilities.CreateRandomName("nic");
var ipConfigName = Utilities.CreateRandomName("config");
var linuxVmName = Utilities.CreateRandomName("VM1");
try
{
//============================================================
// Create resource group
//
var subscription = await client.GetDefaultSubscriptionAsync();
var resourceGroupData = new ResourceGroupData(AzureLocation.SouthCentralUS);
var resourceGroup = (await subscription.GetResourceGroups()
.CreateOrUpdateAsync(WaitUntil.Completed, rgName, resourceGroupData)).Value;
_resourceGroupId = resourceGroup.Id;
var storageData = new StorageAccountCreateOrUpdateContent(
new StorageSku(StorageSkuName.StandardLrs), StorageKind.StorageV2, region);
var storage = (await resourceGroup.GetStorageAccounts()
.CreateOrUpdateAsync(WaitUntil.Completed, storageName, storageData)).Value;
var vnetData = new VirtualNetworkData()
{
Location = region,
AddressPrefixes = { "10.0.0.0/16" },
Subnets = { new SubnetData() { Name = subnetName, AddressPrefix = "10.0.0.0/28" } }
};
var vnet = (await resourceGroup.GetVirtualNetworks()
.CreateOrUpdateAsync(WaitUntil.Completed, vnetName, vnetData)).Value;
var subnet = (await vnet.GetSubnets().GetAsync(subnetName)).Value;
var nicData = new NetworkInterfaceData()
{
Location = region,
IPConfigurations = {
new NetworkInterfaceIPConfigurationData()
{
Name = ipConfigName,
PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic,
Primary = true,
Subnet = new SubnetData()
{
Id = subnet.Id
}
}
}
};
var nic = (await resourceGroup.GetNetworkInterfaces()
.CreateOrUpdateAsync(WaitUntil.Completed, nicName, nicData)).Value;
//=============================================================
// Create a Linux VM using a PIR image with un-managed OS and data disks
Utilities.Log("Creating an un-managed Linux VM");
var vmData = new VirtualMachineData(region)
{
HardwareProfile = new VirtualMachineHardwareProfile()
{
VmSize = VirtualMachineSizeType.StandardDS1V2
},
OSProfile = new VirtualMachineOSProfile()
{
ComputerName = linuxVmName,
AdminUsername = userName,
AdminPassword = password
},
NetworkProfile = new VirtualMachineNetworkProfile()
{
NetworkInterfaces =
{
new VirtualMachineNetworkInterfaceReference()
{
Id = nic.Id,
Primary = true,
}
}
},
StorageProfile = new VirtualMachineStorageProfile()
{
OSDisk = new VirtualMachineOSDisk(DiskCreateOptionType.FromImage)
{
Name = linuxVmName,
OSType = SupportedOperatingSystemType.Linux,
Caching = CachingType.None,
VhdUri = new Uri($"https://{storageName}.blob.core.windows.net/vhds/{linuxVmName}.vhd")
},
DataDisks =
{
new VirtualMachineDataDisk(1, DiskCreateOptionType.Empty)
{
Name = "mydatadisk1",
DiskSizeGB = 100,
VhdUri = new Uri($"https://{storageName}.blob.core.windows.net/vhds/mydatadisk1.vhd")
},
new VirtualMachineDataDisk(2, DiskCreateOptionType.Empty)
{
DiskSizeGB = 50,
Name = "mydatadisk2",
VhdUri = new Uri($"https://{storageName}.blob.core.windows.net/vhds/mydatadisk2.vhd")
}
},
ImageReference = new ImageReference()
{
Publisher = "Canonical",
Offer = "UbuntuServer",
Sku = "16.04-LTS",
Version = "latest",
}
}
};
var linuxVM = (await resourceGroup.GetVirtualMachines()
.CreateOrUpdateAsync(WaitUntil.Completed, linuxVmName, vmData)).Value;
Utilities.Log("Created a Linux VM with un-managed OS and data disks: " + linuxVM.Id);
//=============================================================
// Deallocate the virtual machine
Utilities.Log("Deallocate VM: " + linuxVM.Id);
await linuxVM.DeallocateAsync(WaitUntil.Completed);
Utilities.Log("De-allocated VM: " + linuxVM.Id);
//=============================================================
// Migrate the virtual machine
Utilities.Log("Migrate VM: " + linuxVM.Id);
await linuxVM.ConvertToManagedDisksAsync(WaitUntil.Completed);
Utilities.Log("Migrated VM: " + linuxVM.Id);
}
catch(Exception e)
{
Console.WriteLine(e.ToString());
}
finally
{
try
{
if (_resourceGroupId is not null)
{
Console.WriteLine($"Deleting Resource Group: {_resourceGroupId}");
await client.GetResourceGroupResource(_resourceGroupId).DeleteAsync(WaitUntil.Completed);
Console.WriteLine($"Deleted Resource Group: {_resourceGroupId}");
}
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
public static async Task Main(string[] args)
{
try
{
//=================================================================
// Authenticate
var credential = new DefaultAzureCredential();
var subscriptionId = Environment.GetEnvironmentVariable("AZURE_SUBSCRIPTION_ID");
// you can also use `new ArmClient(credential)` here, and the default subscription will be the first subscription in your list of subscription
var client = new ArmClient(credential, subscriptionId);
await RunSample(client);
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
}