-
Notifications
You must be signed in to change notification settings - Fork 4
/
Program.cs
679 lines (630 loc) · 37.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
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
// 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.Samples.Common;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager;
using Azure.ResourceManager.Network;
using Azure.ResourceManager.Network.Models;
using Azure.ResourceManager.Compute.Models;
using Azure.ResourceManager.Compute;
using System.Net.NetworkInformation;
namespace ManageInternetFacingLoadBalancer
{
public class Program
{
private static ResourceIdentifier? _resourceGroupId = null;
private static readonly string HttpProbe = "httpProbe";
private static readonly string HttpsProbe = "httpsProbe";
private static readonly string HttpLoadBalancingRule = "httpRule";
private static readonly string HttpsLoadBalancingRule = "httpsRule";
private static readonly string NatRule5000to22forVM1 = "nat5000to22forVM1";
private static readonly string NatRule5001to23forVM1 = "nat5001to23forVM1";
private static readonly string NatRule5002to22forVM2 = "nat5002to22forVM2";
private static readonly string NatRule5003to23forVM2 = "nat5003to23forVM2";
/**
* Azure Network sample for managing Internet facing load balancers -
*
* High-level ...
*
* - Create an Internet facing load balancer that receives network traffic on
* port 80 and 443 and sends load-balanced traffic to two virtual machines
*
* - Create NAT rules for SSH and TELNET access to virtual
* machines behind the load balancer
*
* - Create health probes
*
* Details ...
*
* Create an Internet facing load balancer with ...
* - A frontend public IP address
* - Two backend address pools which contain network interfaces for the virtual
* machines to receive HTTP and HTTPS network traffic from the load balancer
* - Two load balancing rules for HTTP and HTTPS to map public ports on the load
* balancer to ports in the backend address pool
* - Two probes which contain HTTP and HTTPS health probes used to check availability
* of virtual machines in the backend address pool
* - Two inbound NAT rules which contain rules that map a public port on the load
* balancer to a port for a specific virtual machine in the backend address pool
* - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23
*
* Create two network interfaces in the frontend subnet ...
* - And associate network interfaces to backend pools and NAT rules
*
* Create two virtual machines in the frontend subnet ...
* - And assign network interfaces
*
* Update an existing load balancer, configure TCP idle timeout
* Create another load balancer
* Remove an existing load balancer
*/
public static async Task RunSample(ArmClient client)
{
string rgName = Utilities.CreateRandomName("NetworkSampleRG");
string vnetName = Utilities.CreateRandomName("vnet");
string loadBalancerName1 = Utilities.CreateRandomName("intlb1");
string loadBalancerName2 = Utilities.CreateRandomName("intlb2");
string publicIpName1 = "pip1-" + loadBalancerName1;
string publicIpName2 = "pip2-" + loadBalancerName1;
string frontendName = loadBalancerName1 + "-FE1";
string backendPoolName1 = loadBalancerName1 + "-BAP1";
string backendPoolName2 = loadBalancerName1 + "-BAP2";
string networkInterfaceName1 = Utilities.CreateRandomName("nic1");
string networkInterfaceName2 = Utilities.CreateRandomName("nic2");
string availSetName = Utilities.CreateRandomName("av");
string vmName1 = Utilities.CreateRandomName("lVM1");
string vmName2 = Utilities.CreateRandomName("lVM2");
try
{
// Get default subscription
SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
// Create a resource group in the EastUS region
Utilities.Log($"Creating resource group...");
ArmOperation<ResourceGroupResource> rgLro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(AzureLocation.WestUS));
ResourceGroupResource resourceGroup = rgLro.Value;
_resourceGroupId = resourceGroup.Id;
Utilities.Log("Created a resource group with name: " + resourceGroup.Data.Name);
//=============================================================
// Create a virtual network with a frontend and a backend subnets
Utilities.Log("Creating virtual network with a frontend and a backend subnets...");
VirtualNetworkData vnetInput = new VirtualNetworkData()
{
Location = resourceGroup.Data.Location,
AddressPrefixes = { "172.16.0.0/16" },
Subnets =
{
new SubnetData() { Name = "Front-end", AddressPrefix = "172.16.1.0/24"},
new SubnetData() { Name = "Back-end", AddressPrefix = "172.16.3.0/24"},
},
};
var vnetLro = await resourceGroup.GetVirtualNetworks().CreateOrUpdateAsync(WaitUntil.Completed, vnetName, vnetInput);
VirtualNetworkResource vnet = vnetLro.Value;
Utilities.Log($"Created a virtual network: {vnet.Data.Name}");
//=============================================================
// Create a public IP address
Utilities.Log("Creating a public IP address...");
PublicIPAddressData publicIPInput1 = new PublicIPAddressData()
{
Location = resourceGroup.Data.Location,
Sku = new PublicIPAddressSku()
{
Name = PublicIPAddressSkuName.Standard,
Tier = PublicIPAddressSkuTier.Regional
},
PublicIPAllocationMethod = NetworkIPAllocationMethod.Static,
DnsSettings = new PublicIPAddressDnsSettings { DomainNameLabel = publicIpName1 },
};
_ = await resourceGroup.GetPublicIPAddresses().CreateOrUpdateAsync(WaitUntil.Completed, publicIpName1, publicIPInput1);
var publicIPLro1 = await resourceGroup.GetPublicIPAddresses().GetAsync(publicIpName1);
PublicIPAddressResource publicIP1 = publicIPLro1.Value;
Utilities.Log($"Created a public IP address: {publicIP1.Data.Name}");
//=============================================================
// Create an Internet facing load balancer
// Create a frontend IP address
// Two backend address pools which contain network interfaces for the virtual
// machines to receive HTTP and HTTPS network traffic from the load balancer
// Two load balancing rules for HTTP and HTTPS to map public ports on the load
// balancer to ports in the backend address pool
// Two probes which contain HTTP and HTTPS health probes used to check availability
// of virtual machines in the backend address pool
// Two inbound NAT rules which contain rules that map a public port on the load
// balancer to a port for a specific virtual machine in the backend address pool
// - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23
Utilities.Log("Creating a Internet facing load balancer with ...");
Utilities.Log("- A frontend IP address");
Utilities.Log("- Two backend address pools which contain network interfaces for the virtual\n"
+ " machines to receive HTTP and HTTPS network traffic from the load balancer");
Utilities.Log("- Two load balancing rules for HTTP and HTTPS to map public ports on the load\n"
+ " balancer to ports in the backend address pool");
Utilities.Log("- Two probes which contain HTTP and HTTPS health probes used to check availability\n"
+ " of virtual machines in the backend address pool");
Utilities.Log("- Two inbound NAT rules which contain rules that map a public port on the load\n"
+ " balancer to a port for a specific virtual machine in the backend address pool\n"
+ " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23");
var frontendIPConfigurationId = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Network/loadBalancers/{loadBalancerName1}/frontendIPConfigurations/{frontendName}");
var backendAddressPoolId1 = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Network/loadBalancers/{loadBalancerName1}/backendAddressPools/{backendPoolName1}");
var backendAddressPoolId2 = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Network/loadBalancers/{loadBalancerName1}/backendAddressPools/{backendPoolName2}");
LoadBalancerData loadBalancerInput = new LoadBalancerData()
{
Location = resourceGroup.Data.Location,
Sku = new LoadBalancerSku()
{
Name = LoadBalancerSkuName.Standard,
Tier = LoadBalancerSkuTier.Regional,
},
// Explicitly define the frontend
FrontendIPConfigurations =
{
new FrontendIPConfigurationData()
{
Name = frontendName,
PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic,
PublicIPAddress = new PublicIPAddressData()
{
Id = publicIP1.Id,
LinkedPublicIPAddress = new PublicIPAddressData()
{
Id = publicIP1.Id,
}
}
}
},
BackendAddressPools =
{
new BackendAddressPoolData()
{
Name = backendPoolName1
},
new BackendAddressPoolData()
{
Name = backendPoolName2
}
},
// Add two rules that uses above backend and probe
LoadBalancingRules =
{
new LoadBalancingRuleData()
{
Name = HttpLoadBalancingRule,
FrontendIPConfigurationId = frontendIPConfigurationId,
BackendAddressPoolId = backendAddressPoolId1,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 80,
BackendPort = 80,
EnableFloatingIP = false,
IdleTimeoutInMinutes = 15,
ProbeId = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Network/loadBalancers/{loadBalancerName1}/probes/{HttpProbe}"),
},
new LoadBalancingRuleData()
{
Name = HttpsLoadBalancingRule,
FrontendIPConfigurationId = frontendIPConfigurationId,
BackendAddressPoolId = backendAddressPoolId2,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 443,
BackendPort = 443,
EnableFloatingIP = false,
IdleTimeoutInMinutes = 15,
ProbeId = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Network/loadBalancers/{loadBalancerName1}/probes/{HttpsProbe}"),
},
},
// Add two probes one per rule
Probes =
{
new ProbeData()
{
Name = HttpProbe,
Protocol = ProbeProtocol.Http,
Port = 80,
IntervalInSeconds = 10,
NumberOfProbes = 2,
RequestPath = "/",
},
new ProbeData()
{
Name = HttpsProbe,
Protocol = ProbeProtocol.Https,
Port = 443,
IntervalInSeconds = 10,
NumberOfProbes = 2,
RequestPath = "/",
}
},
// Add two nat pools to enable direct VM connectivity for
// SSH to port 22 and TELNET to port 23
InboundNatRules =
{
new InboundNatRuleData()
{
Name = NatRule5000to22forVM1,
FrontendIPConfigurationId = frontendIPConfigurationId,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 5000,
BackendPort = 22,
IdleTimeoutInMinutes = 15,
EnableFloatingIP = false,
},
new InboundNatRuleData()
{
Name = NatRule5001to23forVM1,
FrontendIPConfigurationId = frontendIPConfigurationId,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 5001,
BackendPort = 23,
IdleTimeoutInMinutes = 15,
EnableFloatingIP = false,
},
new InboundNatRuleData()
{
Name = NatRule5002to22forVM2,
FrontendIPConfigurationId = frontendIPConfigurationId,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 5002,
BackendPort = 22,
IdleTimeoutInMinutes = 15,
EnableFloatingIP = false,
},
new InboundNatRuleData()
{
Name = NatRule5003to23forVM2,
FrontendIPConfigurationId = frontendIPConfigurationId,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 5003,
BackendPort = 23,
IdleTimeoutInMinutes = 15,
EnableFloatingIP = false,
}
},
};
var loadBalancerLro1 = await resourceGroup.GetLoadBalancers().CreateOrUpdateAsync(WaitUntil.Completed, loadBalancerName1, loadBalancerInput);
LoadBalancerResource loadBalancer1 = loadBalancerLro1.Value;
Utilities.Log($"Created a load balancer: {loadBalancer1.Data.Name}");
//=============================================================
// Create two network interfaces in the frontend subnet
// associate network interfaces to NAT rules, backend pools
Utilities.Log("Creating two network interfaces in the frontend subnet ...");
Utilities.Log("- And associating network interfaces to backend pools and NAT rules");
var nicInput1 = new NetworkInterfaceData()
{
Location = resourceGroup.Data.Location,
IPConfigurations =
{
new NetworkInterfaceIPConfigurationData()
{
Name = "default-config",
PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic,
Subnet = new SubnetData()
{
Id = vnet.Data.Subnets.First(item=>item.Name=="Front-end").Id
},
LoadBalancerBackendAddressPools =
{
new BackendAddressPoolData(){ Id = backendAddressPoolId1 },
new BackendAddressPoolData(){ Id = backendAddressPoolId2 },
},
LoadBalancerInboundNatRules =
{
new InboundNatRuleData(){ Id = loadBalancer1.Data.InboundNatRules.First(item => item.Name == NatRule5000to22forVM1).Id },
new InboundNatRuleData(){ Id = loadBalancer1.Data.InboundNatRules.First(item => item.Name == NatRule5001to23forVM1).Id },
}
}
}
};
var networkInterfaceLro1 = await resourceGroup.GetNetworkInterfaces().CreateOrUpdateAsync(WaitUntil.Completed, networkInterfaceName1, nicInput1);
NetworkInterfaceResource networkInterface1 = networkInterfaceLro1.Value;
Utilities.Log($"Created network interface: {networkInterface1.Data.Name}");
var nicInput2 = new NetworkInterfaceData()
{
Location = resourceGroup.Data.Location,
IPConfigurations =
{
new NetworkInterfaceIPConfigurationData()
{
Name = "default-config",
PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic,
Subnet = new SubnetData()
{
Id = vnet.Data.Subnets.First(item=>item.Name=="Front-end").Id
},
LoadBalancerBackendAddressPools =
{
new BackendAddressPoolData(){ Id = backendAddressPoolId1 },
new BackendAddressPoolData(){ Id = backendAddressPoolId2 },
},
LoadBalancerInboundNatRules =
{
new InboundNatRuleData(){ Id = loadBalancer1.Data.InboundNatRules.First(item => item.Name == NatRule5002to22forVM2).Id },
new InboundNatRuleData(){ Id = loadBalancer1.Data.InboundNatRules.First(item => item.Name == NatRule5003to23forVM2).Id },
}
}
}
};
var networkInterfaceLro2 = await resourceGroup.GetNetworkInterfaces().CreateOrUpdateAsync(WaitUntil.Completed, networkInterfaceName2, nicInput2);
NetworkInterfaceResource networkInterface2 = networkInterfaceLro2.Value;
Utilities.Log($"Created network interface: {networkInterface2.Data.Name}");
// Print network interface details
Utilities.Log("Created two network interfaces");
Utilities.Log("Network Interface ONE -");
Utilities.Log(networkInterface1.Data.IPConfigurations[0].LoadBalancerInboundNatRules[0].Id.Name);
Utilities.Log(networkInterface1.Data.IPConfigurations[0].LoadBalancerInboundNatRules[1].Id.Name);
Utilities.Log();
Utilities.Log("Network Interface TWO -");
Utilities.Log(networkInterface2.Data.IPConfigurations[0].LoadBalancerInboundNatRules[0].Id.Name);
Utilities.Log(networkInterface2.Data.IPConfigurations[0].LoadBalancerInboundNatRules[1].Id.Name);
//=============================================================
// Create an availability set
Utilities.Log("Creating an availability set ...");
AvailabilitySetData availabilitySetInput = new AvailabilitySetData(resourceGroup.Data.Location)
{
PlatformFaultDomainCount = 2,
PlatformUpdateDomainCount = 4,
};
var availabilitySetLro = await resourceGroup.GetAvailabilitySets().CreateOrUpdateAsync(WaitUntil.Completed, availSetName, availabilitySetInput);
AvailabilitySetResource availabilitySet = availabilitySetLro.Value;
Utilities.Log($"Created first availability set: {availabilitySet.Data.Name}");
//=============================================================
// Create two virtual machines and assign network interfaces
Utilities.Log("Creating two virtual machines in the frontend subnet ...");
Utilities.Log("- And assigning network interfaces");
// Create vm1
Utilities.Log("Creating a new virtual machine...");
VirtualMachineData vmInput1 = Utilities.GetDefaultVMInputData(resourceGroup, vmName1);
vmInput1.NetworkProfile.NetworkInterfaces.Add(new VirtualMachineNetworkInterfaceReference() { Id = networkInterface1.Id, Primary = true });
var vmLro1 = await resourceGroup.GetVirtualMachines().CreateOrUpdateAsync(WaitUntil.Completed, vmName1, vmInput1);
VirtualMachineResource vm1 = vmLro1.Value;
Utilities.Log($"Created virtual machine: {vm1.Data.Name}");
// Create vm2
Utilities.Log("Creating a new virtual machine...");
VirtualMachineData vmInput2 = Utilities.GetDefaultVMInputData(resourceGroup, vmName2);
vmInput2.NetworkProfile.NetworkInterfaces.Add(new VirtualMachineNetworkInterfaceReference() { Id = networkInterface2.Id, Primary = true });
var vmLro2 = await resourceGroup.GetVirtualMachines().CreateOrUpdateAsync(WaitUntil.Completed, vmName2, vmInput2);
VirtualMachineResource vm2 = vmLro2.Value;
Utilities.Log($"Created virtual machine: {vm2.Data.Name}");
//=============================================================
// Update a load balancer
// configure TCP idle timeout to 15 minutes
Utilities.Log("Updating the load balancer ...");
LoadBalancerData updateLoadBalancerInput = loadBalancer1.Data;
updateLoadBalancerInput.LoadBalancingRules.First(item => item.Name == HttpLoadBalancingRule).IdleTimeoutInMinutes = 15;
updateLoadBalancerInput.LoadBalancingRules.First(item => item.Name == HttpsLoadBalancingRule).IdleTimeoutInMinutes = 15;
loadBalancerLro1 = await resourceGroup.GetLoadBalancers().CreateOrUpdateAsync(WaitUntil.Completed, loadBalancerName1, loadBalancerInput);
loadBalancer1 = loadBalancerLro1.Value;
Utilities.Log("Update the load balancer with a TCP idle timeout to 15 minutes");
//=============================================================
// Create another public IP address
Utilities.Log("Creating another public IP address...");
PublicIPAddressData publicIPInput2 = new PublicIPAddressData()
{
Location = resourceGroup.Data.Location,
Sku = new PublicIPAddressSku()
{
Name = PublicIPAddressSkuName.Standard,
Tier = PublicIPAddressSkuTier.Regional
},
PublicIPAllocationMethod = NetworkIPAllocationMethod.Static,
DnsSettings = new PublicIPAddressDnsSettings { DomainNameLabel = publicIpName2 }
};
_ = await resourceGroup.GetPublicIPAddresses().CreateOrUpdateAsync(WaitUntil.Completed, publicIpName2, publicIPInput2);
var publicIPLro2 = await resourceGroup.GetPublicIPAddresses().GetAsync(publicIpName2);
PublicIPAddressResource publicIP2 = publicIPLro2.Value;
Utilities.Log($"Created another public IP address: {publicIP2.Data.Name}");
//=============================================================
// Create another Internet facing load balancer
// Create a frontend IP address
// Two backend address pools which contain network interfaces for the virtual
// machines to receive HTTP and HTTPS network traffic from the load balancer
// Two load balancing rules for HTTP and HTTPS to map public ports on the load
// balancer to ports in the backend address pool
// Two probes which contain HTTP and HTTPS health probes used to check availability
// of virtual machines in the backend address pool
// Two inbound NAT rules which contain rules that map a public port on the load
// balancer to a port for a specific virtual machine in the backend address pool
// - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23
Utilities.Log("Creating another Internet facing load balancer with ...");
Utilities.Log("- A frontend IP address");
Utilities.Log("- Two backend address pools which contain network interfaces for the virtual\n"
+ " machines to receive HTTP and HTTPS network traffic from the load balancer");
Utilities.Log("- Two load balancing rules for HTTP and HTTPS to map public ports on the load\n"
+ " balancer to ports in the backend address pool");
Utilities.Log("- Two probes which contain HTTP and HTTPS health probes used to check availability\n"
+ " of virtual machines in the backend address pool");
Utilities.Log("- Two inbound NAT rules which contain rules that map a public port on the load\n"
+ " balancer to a port for a specific virtual machine in the backend address pool\n"
+ " - this provides direct VM connectivity for SSH to port 22 and TELNET to port 23");
frontendIPConfigurationId = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Network/loadBalancers/{loadBalancerName2}/frontendIPConfigurations/{frontendName}");
backendAddressPoolId1 = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Network/loadBalancers/{loadBalancerName2}/backendAddressPools/{backendPoolName1}");
backendAddressPoolId2 = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Network/loadBalancers/{loadBalancerName2}/backendAddressPools/{backendPoolName2}");
loadBalancerInput = new LoadBalancerData()
{
Location = resourceGroup.Data.Location,
Sku = new LoadBalancerSku()
{
Name = LoadBalancerSkuName.Standard,
Tier = LoadBalancerSkuTier.Regional,
},
// Explicitly define the frontend
FrontendIPConfigurations =
{
new FrontendIPConfigurationData()
{
Name = frontendName,
PrivateIPAllocationMethod = NetworkIPAllocationMethod.Dynamic,
PublicIPAddress = new PublicIPAddressData()
{
Id = publicIP2.Id,
LinkedPublicIPAddress = new PublicIPAddressData()
{
Id = publicIP2.Id,
}
}
}
},
BackendAddressPools =
{
new BackendAddressPoolData()
{
Name = backendPoolName1
},
new BackendAddressPoolData()
{
Name = backendPoolName2
}
},
// Add two rules that uses above backend and probe
LoadBalancingRules =
{
new LoadBalancingRuleData()
{
Name = HttpLoadBalancingRule,
FrontendIPConfigurationId = frontendIPConfigurationId,
BackendAddressPoolId = backendAddressPoolId1,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 80,
BackendPort = 80,
EnableFloatingIP = false,
IdleTimeoutInMinutes = 15,
ProbeId = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Network/loadBalancers/{loadBalancerName2}/probes/{HttpProbe}"),
},
new LoadBalancingRuleData()
{
Name = HttpsLoadBalancingRule,
FrontendIPConfigurationId = frontendIPConfigurationId,
BackendAddressPoolId = backendAddressPoolId2,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 443,
BackendPort = 443,
EnableFloatingIP = false,
IdleTimeoutInMinutes = 15,
ProbeId = new ResourceIdentifier($"{resourceGroup.Id}/providers/Microsoft.Network/loadBalancers/{loadBalancerName2}/probes/{HttpsProbe}"),
},
},
// Add two probes one per rule
Probes =
{
new ProbeData()
{
Name = HttpProbe,
Protocol = ProbeProtocol.Http,
Port = 80,
IntervalInSeconds = 10,
NumberOfProbes = 2,
RequestPath = "/",
},
new ProbeData()
{
Name = HttpsProbe,
Protocol = ProbeProtocol.Https,
Port = 443,
IntervalInSeconds = 10,
NumberOfProbes = 2,
RequestPath = "/",
}
},
// Add two nat pools to enable direct VM connectivity for
// SSH to port 22 and TELNET to port 23
InboundNatRules =
{
new InboundNatRuleData()
{
Name = NatRule5000to22forVM1,
FrontendIPConfigurationId = frontendIPConfigurationId,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 5000,
BackendPort = 22,
IdleTimeoutInMinutes = 15,
EnableFloatingIP = false,
},
new InboundNatRuleData()
{
Name = NatRule5001to23forVM1,
FrontendIPConfigurationId = frontendIPConfigurationId,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 5001,
BackendPort = 23,
IdleTimeoutInMinutes = 15,
EnableFloatingIP = false,
},
new InboundNatRuleData()
{
Name = NatRule5002to22forVM2,
FrontendIPConfigurationId = frontendIPConfigurationId,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 5002,
BackendPort = 22,
IdleTimeoutInMinutes = 15,
EnableFloatingIP = false,
},
new InboundNatRuleData()
{
Name = NatRule5003to23forVM2,
FrontendIPConfigurationId = frontendIPConfigurationId,
Protocol = LoadBalancingTransportProtocol.Tcp,
FrontendPort = 5003,
BackendPort = 23,
IdleTimeoutInMinutes = 15,
EnableFloatingIP = false,
}
},
};
var loadBalancerLro2 = await resourceGroup.GetLoadBalancers().CreateOrUpdateAsync(WaitUntil.Completed, loadBalancerName2, loadBalancerInput);
LoadBalancerResource loadBalancer2 = loadBalancerLro2.Value;
Utilities.Log($"Created another balancer: {loadBalancer1.Data.Name}");
//=============================================================
// List load balancers
Utilities.Log("Walking through the list of load balancers");
await foreach (var loadBalancer in resourceGroup.GetLoadBalancers().GetAllAsync())
{
Utilities.Log(loadBalancer.Data.Name);
}
//=============================================================
// Remove a load balancer
Utilities.Log("Deleting load balancer " + loadBalancerName2
+ "(" + loadBalancer2.Id + ")");
await loadBalancer2.DeleteAsync(WaitUntil.Completed);
Utilities.Log("Deleted load balancer" + loadBalancerName2);
}
finally
{
try
{
if (_resourceGroupId is not null)
{
Utilities.Log($"Deleting Resource Group...");
await client.GetResourceGroupResource(_resourceGroupId).DeleteAsync(WaitUntil.Completed);
Utilities.Log($"Deleted Resource Group: {_resourceGroupId.Name}");
}
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
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);
await RunSample(client);
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
}