-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathProgram.cs
395 lines (368 loc) · 19.5 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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License. See License.txt in the project root for license information.
using Azure.Core;
using Azure.Identity;
using Azure.ResourceManager;
using Azure.ResourceManager.Models;
using Azure.ResourceManager.Compute;
using Azure.ResourceManager.ManagedServiceIdentities;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager.Compute.Models;
using Azure.ResourceManager.Samples.Common;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Azure.ResourceManager.Network.Models;
using Azure.ResourceManager.Network;
using Azure;
using Azure.ResourceManager.Storage;
using Azure.ResourceManager.Storage.Models;
namespace ManageVirtualMachinesInParallelWithNetwork
{
public class Program
{
private const int FrontendVMCount = 10;
private const int BackendVMCount = 10;
private static readonly string UserName = Utilities.CreateUsername();
private static readonly string Password = Utilities.CreatePassword();
/**
* Create a virtual network with two Subnets ?frontend and backend
* Frontend allows HTTP in and denies Internet out
* Backend denies Internet in and Internet out
* Create m Linux virtual machines in the frontend
* Create m Windows virtual machines in the backend.
*/
public static async Task RunSample(ArmClient client)
{
string rgName = Utilities.CreateRandomName("rgNEPP");
string frontEndNSGName = Utilities.CreateRandomName("fensg");
string backEndNSGName = Utilities.CreateRandomName("bensg");
string networkName = Utilities.CreateRandomName("vnetCOMV");
string pipName = Utilities.CreateRandomName("pip1");
string linuxComputerName = Utilities.CreateRandomName("linuxComputer");
string networkConfigurationName = Utilities.CreateRandomName("networkconfiguration");
string storageAccountName = Utilities.CreateRandomName("stgCOMV");
string storageAccountSkuName = Utilities.CreateRandomName("stgSku");
var pipDnsLabelLinuxVM = Utilities.CreateRandomName("rgpip1");
var region = AzureLocation.EastUS;
// Create a resource group [Where all resources gets created]
var lro = await client.GetDefaultSubscription().GetResourceGroups().CreateOrUpdateAsync(Azure.WaitUntil.Completed, rgName, new ResourceGroupData(AzureLocation.EastUS));
var resourceGroup = lro.Value;
try
{
// Define a network security group for the front end of a subnet
// front end subnet contains two rules
// - ALLOW-SSH - allows SSH traffic into the front end subnet
// - ALLOW-WEB- allows HTTP traffic into the front end subnet
var frontEndNSGCollection = resourceGroup.GetNetworkSecurityGroups();
var frontEndNSGData = new NetworkSecurityGroupData()
{
Location = region,
SecurityRules =
{
new SecurityRuleData()
{
Priority = 100,
Description = "Allow SSH",
Protocol = "tcp",
SourcePortRange = "22"
},
new SecurityRuleData()
{
Priority = 101,
Description = "ALLOW-HTTP",
Protocol = "tcp",
SourcePortRange = "80"
}
}
};
var frontEndNSGCreatable = (await frontEndNSGCollection.CreateOrUpdateAsync(WaitUntil.Completed, frontEndNSGName, frontEndNSGData)).Value;
//============================================================
// Define a network security group for the back end of a subnet
// back end subnet contains two rules
// - ALLOW-SQL - allows SQL traffic only from the front end subnet
// - DENY-WEB - denies all outbound internet traffic from the back end subnet
var backEndNSGCollection = resourceGroup.GetNetworkSecurityGroups();
var backEndNSGData = new NetworkSecurityGroupData()
{
Location = region,
SecurityRules =
{
new SecurityRuleData()
{
Priority = 100,
Description = "ALLOW-SQL",
Protocol = "tcp",
SourcePortRange = "1433",
SourceAddressPrefix = "172.16.1.0/24"
},
new SecurityRuleData()
{
Priority = 200,
Description = "DENY-WEB",
}
}
};
var backEndNSGCreatable = (await frontEndNSGCollection.CreateOrUpdateAsync(WaitUntil.Completed, frontEndNSGName, frontEndNSGData)).Value;
Utilities.Log("Creating a security group for the front ends - allows SSH and HTTP");
Utilities.Log("Creating a security group for the back ends - allows SSH and denies all outbound internet traffic");
var networkSecurityGroups = new List<NetworkSecurityGroupResource>
{
frontEndNSGCreatable,
backEndNSGCreatable
};
NetworkSecurityGroupResource frontendNSG = networkSecurityGroups.First(n => n.Data.Name.Equals(frontEndNSGName, StringComparison.OrdinalIgnoreCase));
NetworkSecurityGroupResource backendNSG = networkSecurityGroups.First(n => n.Data.Name.Equals(backEndNSGName, StringComparison.OrdinalIgnoreCase));
Utilities.Log("Created a security group for the front end: " + frontendNSG.Id);
Utilities.PrintNetworkSecurityGroup(frontendNSG);
Utilities.Log("Created a security group for the back end: " + backendNSG.Id);
Utilities.PrintNetworkSecurityGroup(backendNSG);
// Create Network [Where all the virtual machines get added to]
var virtualNetworkCollection = resourceGroup.GetVirtualNetworks();
var data = new VirtualNetworkData()
{
Location = AzureLocation.EastUS,
Subnets =
{
new SubnetData()
{
AddressPrefixes =
{
"172.16.1.0/24"
},
Name = "Front-end"
},
new SubnetData()
{
AddressPrefixes =
{
"172.16.2.0/24"
},
Name = "Back-end"
}
},
AddressPrefixes =
{
"172.16.0.0/16"
}
};
var virtualNetworkLro = await virtualNetworkCollection.CreateOrUpdateAsync(WaitUntil.Completed, networkName, data);
var virtualNetwork = virtualNetworkLro.Value;
// Create a public IP address
Utilities.Log("Creating a Linux Public IP address...");
var publicAddressIPCollection = resourceGroup.GetPublicIPAddresses();
var publicIPAddressdata = new PublicIPAddressData()
{
Location = AzureLocation.EastUS,
Sku = new PublicIPAddressSku()
{
Name = PublicIPAddressSkuName.Standard,
},
PublicIPAddressVersion = NetworkIPVersion.IPv4,
PublicIPAllocationMethod = NetworkIPAllocationMethod.Static,
DnsSettings = new PublicIPAddressDnsSettings()
{
DomainNameLabel = pipDnsLabelLinuxVM
},
};
var publicIPAddressLro = await publicAddressIPCollection.CreateOrUpdateAsync(WaitUntil.Completed, pipName, publicIPAddressdata);
var publicIPAddress = publicIPAddressLro.Value;
Utilities.Log("Created a Linux Public IP address with name : " + publicIPAddress.Data.Name);
//Create a networkInterface
Utilities.Log("Created linux networkInterfaces");
var frontEndNetworkInterfaceData = new NetworkInterfaceData()
{
Location = AzureLocation.EastUS,
IPConfigurations =
{
new NetworkInterfaceIPConfigurationData()
{
Name = "internal",
Primary = true,
Subnet = new SubnetData
{
Name = "Front-end",
Id = new ResourceIdentifier($"{virtualNetwork.Data.Id}/subnets/Front-end")
},
PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic,
PublicIPAddress = publicIPAddress.Data,
}
}
};
var frontEndNetworkInterfaceName = Utilities.CreateRandomName("frontendnetworkInterface");
var frontEndNic = (await resourceGroup.GetNetworkInterfaces().CreateOrUpdateAsync(WaitUntil.Completed, frontEndNetworkInterfaceName, frontEndNetworkInterfaceData)).Value;
Utilities.Log("Created a Linux network interface with name : " + frontEndNic.Data.Name);
var backEndNetworkInterfaceData = new NetworkInterfaceData()
{
Location = AzureLocation.EastUS,
IPConfigurations =
{
new NetworkInterfaceIPConfigurationData()
{
Name = "internal",
Primary = true,
Subnet = new SubnetData
{
Name = "Back-end",
Id = new ResourceIdentifier($"{virtualNetwork.Data.Id}/subnets/Front-end")
},
PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic,
PublicIPAddress = publicIPAddress.Data,
}
}
};
var backEndnetworkInterfaceName = Utilities.CreateRandomName("backendnetworkInterface");
var backEndNic = (await resourceGroup.GetNetworkInterfaces().CreateOrUpdateAsync(WaitUntil.Completed, backEndnetworkInterfaceName, backEndNetworkInterfaceData)).Value;
Utilities.Log("Created a Linux network interface with name : " + backEndNic.Data.Name);
// Prepare Creatable Storage account definition [For storing VMs disk]
var storageAccountCollection = resourceGroup.GetStorageAccounts();
var acountData = new StorageAccountCreateOrUpdateContent(new StorageSku(storageAccountSkuName), StorageKind.Storage, region);
var creatableStorageAccount = (await storageAccountCollection.CreateOrUpdateAsync(WaitUntil.Completed, storageAccountName, acountData)).Value;
// Prepare a batch of Creatable Virtual Machines definitions
var virtualMachineCollection = resourceGroup.GetVirtualMachines();
List<VirtualMachineResource> frontendCreatableVirtualMachines = new List<VirtualMachineResource>();
for (int i = 0; i < FrontendVMCount; i++)
{
var backEndVmdata = new VirtualMachineData(AzureLocation.EastUS)
{
HardwareProfile = new VirtualMachineHardwareProfile()
{
VmSize = "Standard_D2a_v4"
},
OSProfile = new VirtualMachineOSProfile()
{
AdminUsername = UserName,
AdminPassword = Password,
ComputerName = linuxComputerName,
},
NetworkProfile = new VirtualMachineNetworkProfile()
{
NetworkInterfaces =
{
new VirtualMachineNetworkInterfaceReference()
{
Id = frontEndNic.Id,
Primary = true,
}
}
},
StorageProfile = new VirtualMachineStorageProfile()
{
OSDisk = new VirtualMachineOSDisk(DiskCreateOptionType.FromImage)
{
OSType = SupportedOperatingSystemType.Linux,
Caching = CachingType.ReadWrite,
ManagedDisk = new VirtualMachineManagedDisk()
{
StorageAccountType = StorageAccountType.StandardLrs
}
},
ImageReference = new ImageReference()
{
Publisher = "Canonical",
Offer = "UbuntuServer",
Sku = "16.04-LTS",
Version = "latest",
}
},
};
var virtualMachine_lro = await virtualMachineCollection.CreateOrUpdateAsync(WaitUntil.Completed, "VM-FE-" + i, backEndVmdata);
var virtualMachine = virtualMachine_lro.Value;
frontendCreatableVirtualMachines.Add(virtualMachine);
}
List<VirtualMachineResource> backendCreatableVirtualMachines = new List<VirtualMachineResource>();
for (int i = 0; i < BackendVMCount; i++)
{
var backEndVmdata = new VirtualMachineData(AzureLocation.EastUS)
{
HardwareProfile = new VirtualMachineHardwareProfile()
{
VmSize = "Standard_D2a_v4"
},
OSProfile = new VirtualMachineOSProfile()
{
AdminUsername = UserName,
AdminPassword = Password,
ComputerName = linuxComputerName,
},
NetworkProfile = new VirtualMachineNetworkProfile()
{
NetworkInterfaces =
{
new VirtualMachineNetworkInterfaceReference()
{
Id = backEndNic.Id,
Primary = true,
}
}
},
StorageProfile = new VirtualMachineStorageProfile()
{
OSDisk = new VirtualMachineOSDisk(DiskCreateOptionType.FromImage)
{
OSType = SupportedOperatingSystemType.Linux,
Caching = CachingType.ReadWrite,
ManagedDisk = new VirtualMachineManagedDisk()
{
StorageAccountType = StorageAccountType.StandardLrs
}
},
ImageReference = new ImageReference()
{
Publisher = "Canonical",
Offer = "UbuntuServer",
Sku = "16.04-LTS",
Version = "latest",
}
},
};
var virtualMachine_lro = await virtualMachineCollection.CreateOrUpdateAsync(WaitUntil.Completed, "VM-FE-" + i, backEndVmdata);
var virtualMachine = virtualMachine_lro.Value;
backendCreatableVirtualMachines.Add(virtualMachine);
}
var startTime = DateTimeOffset.Now.UtcDateTime;
Utilities.Log("Creating the virtual machines");
List<VirtualMachineResource> allCreatableVirtualMachines = new List<VirtualMachineResource>();
allCreatableVirtualMachines.AddRange(frontendCreatableVirtualMachines);
allCreatableVirtualMachines.AddRange(backendCreatableVirtualMachines);
var endTime = DateTimeOffset.Now.UtcDateTime;
Utilities.Log("Created virtual machines");
foreach (var virtualMachine in allCreatableVirtualMachines)
{
if (virtualMachine != null)
Utilities.Log(virtualMachine.Id);
}
Utilities.Log($"Virtual machines create: took {(endTime - startTime).TotalSeconds } seconds");
}
finally
{
Utilities.Log($"Deleting resource group : {rgName}");
await resourceGroup.DeleteAsync(WaitUntil.Completed);
Utilities.Log($"Deleted resource group : {rgName}");
}
}
public static async Task Main(string[] args)
{
try
{
//=============================================================
// Authenticate
var clientId = Environment.GetEnvironmentVariable("CLIENT_ID");
var clientSecret = Environment.GetEnvironmentVariable("CLIENT_SECRET");
var tenantId = Environment.GetEnvironmentVariable("TENANT_ID");
var subscription = Environment.GetEnvironmentVariable("SUBSCRIPTION_ID");
ClientSecretCredential credential = new ClientSecretCredential(tenantId, clientId, clientSecret);
ArmClient client = new ArmClient(credential, subscription);
// Print selected subscription
Utilities.Log("Selected subscription: " + client.GetSubscriptions().Id);
await RunSample(client);
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
}