-
Notifications
You must be signed in to change notification settings - Fork 3
/
Program.cs
262 lines (234 loc) · 13.1 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
// 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.Resources.Models;
using Azure.ResourceManager.Samples.Common;
using Azure.ResourceManager.Resources;
using Azure.ResourceManager;
using Azure.ResourceManager.Network;
using Azure.ResourceManager.Network.Models;
namespace ManageVirtualNetworkAsync
{
public class Program
{
private static ResourceIdentifier? _resourceGroupId = null;
/**
* Azure Network sample for managing virtual networks.
* - Create a virtual network with Subnets
* - Update a virtual network
* - Create virtual machines in the virtual network subnets
* - Create another virtual network
* - List virtual networks
*/
public async static Task RunSampleAsync(ArmClient client)
{
string vnetName1 = Utilities.CreateRandomName("vnet1");
string vnetName2 = Utilities.CreateRandomName("vnet2");
string frontEndVmName = Utilities.CreateRandomName("fevm");
string backEndVmName = Utilities.CreateRandomName("bevm");
string publicIpAddressLeafDnsForFrontEndVm = Utilities.CreateRandomName("pip1");
try
{
// Get default subscription
SubscriptionResource subscription = await client.GetDefaultSubscriptionAsync();
// Create a resource group in the EastUS region
string rgName = Utilities.CreateRandomName("NetworkSampleRG");
Utilities.Log($"Creating resource group...");
ArmOperation<ResourceGroupResource> rgLro = await subscription.GetResourceGroups().CreateOrUpdateAsync(WaitUntil.Completed, rgName, new ResourceGroupData(AzureLocation.EastUS));
ResourceGroupResource resourceGroup = rgLro.Value;
_resourceGroupId = resourceGroup.Id;
Utilities.Log("Created a resource group with name: " + resourceGroup.Data.Name);
//============================================================
// Create a virtual network with specific address-space and two subnet
// Creates a network security group for backend subnet
// Create NSG for backend
Utilities.Log("Creating a network security group for virtual network backend subnet...");
string backendNsgName = Utilities.CreateRandomName("backEndNSG");
NetworkSecurityGroupData backendNsgInput = new NetworkSecurityGroupData()
{
Location = resourceGroup.Data.Location,
SecurityRules =
{
new SecurityRuleData()
{
Name = "DenyInternetInComing",
Protocol = SecurityRuleProtocol.Asterisk,
SourcePortRange = "*",
DestinationPortRange = "*",
SourceAddressPrefix = "INTERNET",
DestinationAddressPrefix = "*",
Access = SecurityRuleAccess.Deny,
Priority = 100,
Direction = SecurityRuleDirection.Inbound,
},
new SecurityRuleData()
{
Name = "DenyInternetOutGoing",
Protocol = SecurityRuleProtocol.Asterisk,
SourcePortRange = "*",
DestinationPortRange = "*",
SourceAddressPrefix = "*",
DestinationAddressPrefix = "internet",
Access = SecurityRuleAccess.Deny,
Priority = 200,
Direction = SecurityRuleDirection.Outbound,
}
}
};
var backendNsgLro = await resourceGroup.GetNetworkSecurityGroups().CreateOrUpdateAsync(WaitUntil.Completed, backendNsgName, backendNsgInput);
NetworkSecurityGroupResource backendNsg = backendNsgLro.Value;
Utilities.Log($"Created network security group: {backendNsg.Data.Name}");
// Create NSG for frontend
Utilities.Log("Creating a network security group for virtual network frontend subnet...");
string frontendNsgName = Utilities.CreateRandomName("frontEndNSG");
NetworkSecurityGroupData frontendNsgInput = new NetworkSecurityGroupData()
{
Location = resourceGroup.Data.Location,
SecurityRules =
{
new SecurityRuleData()
{
Name = "AllowHttpInComing",
Protocol = SecurityRuleProtocol.Tcp,
SourcePortRange = "*",
DestinationPortRange = "80",
SourceAddressPrefix = "INTERNET",
DestinationAddressPrefix = "*",
Access = SecurityRuleAccess.Allow,
Priority = 100,
Direction = SecurityRuleDirection.Inbound,
},
new SecurityRuleData()
{
Name = "DenyInternetOutGoing",
Protocol = SecurityRuleProtocol.Asterisk,
SourcePortRange = "*",
DestinationPortRange = "*",
SourceAddressPrefix = "*",
DestinationAddressPrefix = "internet",
Access = SecurityRuleAccess.Deny,
Priority = 200,
Direction = SecurityRuleDirection.Outbound,
}
}
};
var frontendNsgLro = await resourceGroup.GetNetworkSecurityGroups().CreateOrUpdateAsync(WaitUntil.Completed, frontendNsgName, frontendNsgInput);
NetworkSecurityGroupResource frontendNsg = frontendNsgLro.Value;
Utilities.Log($"Created network security group: {frontendNsg.Data.Name}");
// Create virtual network
Utilities.Log("Creating virtual network #1...");
string frontendSubnetName = Utilities.CreateRandomName("fesubnet");
string backendSubnetName = Utilities.CreateRandomName("besubnet");
VirtualNetworkData vnetInput1 = new VirtualNetworkData()
{
Location = resourceGroup.Data.Location,
AddressPrefixes = { "192.168.0.0/16" },
Subnets =
{
new SubnetData() { AddressPrefix = "192.168.1.0/24", Name = frontendSubnetName },
new SubnetData() { AddressPrefix = "192.168.2.0/24", Name = backendSubnetName, NetworkSecurityGroup = backendNsg.Data }
},
};
var vnetLro1 = await resourceGroup.GetVirtualNetworks().CreateOrUpdateAsync(WaitUntil.Completed, vnetName1, vnetInput1);
VirtualNetworkResource vnet1 = vnetLro1.Value;
Utilities.Log($"Created a virtual network: {vnet1.Data.Name}");
//============================================================
// Update a virtual network
// Update the virtual network frontend subnet by associating it with network security group
Utilities.Log("Associating network security group rule to frontend subnet");
vnetInput1 = new VirtualNetworkData()
{
Location = resourceGroup.Data.Location,
AddressPrefixes = { "192.168.0.0/16" },
Subnets =
{
new SubnetData() { AddressPrefix = "192.168.1.0/24", Name = frontendSubnetName, NetworkSecurityGroup = frontendNsg.Data },
new SubnetData() { AddressPrefix = "192.168.2.0/24", Name = backendSubnetName, NetworkSecurityGroup = backendNsg.Data }
},
};
vnetLro1 = await resourceGroup.GetVirtualNetworks().CreateOrUpdateAsync(WaitUntil.Completed, vnetName1, vnetInput1);
vnet1 = vnetLro1.Value;
Utilities.Log("Network security group rule associated with the frontend subnet");
//============================================================
// Create a virtual machine in each subnet
// Creates the first virtual machine in frontend subnet
Utilities.Log("Creating a virtual machine in the frontend subnet");
SubnetData feSubnet = vnet1.Data.Subnets.First(item => item.Name == frontendSubnetName);
var frontEndVM = await Utilities.CreateVirtualMachine(resourceGroup, feSubnet.Id, frontEndVmName);
Utilities.Log($"Created VM: {frontEndVM.Data.Name}");
// Creates the second virtual machine in the backend subnet
Utilities.Log("Creating a virtual machine in the backend subnet");
SubnetData beSubnet = vnet1.Data.Subnets.First(item => item.Name == backendSubnetName);
var backEndVM = await Utilities.CreateVirtualMachine(resourceGroup, beSubnet.Id, backEndVmName);
Utilities.Log($"Created VM: {backEndVM.Data.Name}");
// Create a virtual network with default address-space and one default subnet
Utilities.Log("Creating virtual network #2...");
VirtualNetworkData vnetInput2 = new VirtualNetworkData()
{
Location = resourceGroup.Data.Location,
AddressPrefixes = { "10.10.0.0/16" },
Subnets =
{
new SubnetData() { AddressPrefix = "10.10.1.0/24", Name = "default" },
},
};
var vnetLro2 = await resourceGroup.GetVirtualNetworks().CreateOrUpdateAsync(WaitUntil.Completed, vnetName2, vnetInput2);
var vnet2 = vnetLro2.Value;
Utilities.Log($"Created a virtual network: {vnet2.Data.Name}");
//============================================================
// List virtual networks
Utilities.Log($"Get all virtual network under {resourceGroup.Data.Name}");
await foreach (var virtualNetwork in resourceGroup.GetVirtualNetworks().GetAllAsync())
{
Utilities.Log("\t" + virtualNetwork.Data.Name);
}
//============================================================
// Delete a virtual network
Utilities.Log("Deleting the virtual network");
await vnet2.DeleteAsync(WaitUntil.Completed);
Utilities.Log("Deleted the virtual network");
}
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 RunSampleAsync(client);
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
}