/
Program.cs
254 lines (221 loc) · 12.6 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
// 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.Network.Fluent;
using Microsoft.Azure.Management.Network.Fluent.Models;
using Microsoft.Azure.Management.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
using System;
using System.Threading.Tasks;
namespace ManageVirtualNetworkAsync
{
public class Program
{
private static readonly string VNet1FrontEndSubnetName = "frontend";
private static readonly string VNet1BackEndSubnetName = "backend";
private static readonly string VNet1FrontEndSubnetNsgName = "frontendnsg";
private static readonly string VNet1BackEndSubnetNsgName = "backendnsg";
private static readonly string UserName = Utilities.CreateUsername();
private static readonly string SshKey = "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCfSPC2K7LZcFKEO+/t3dzmQYtrJFZNxOsbVgOVKietqHyvmYGHEC0J2wPdAqQ/63g/hhAEFRoyehM+rbeDri4txB3YFfnOK58jqdkyXzupWqXzOrlKY4Wz9SKjjN765+dqUITjKRIaAip1Ri137szRg71WnrmdP3SphTRlCx1Bk2nXqWPsclbRDCiZeF8QOTi4JqbmJyK5+0UqhqYRduun8ylAwKKQJ1NJt85sYIHn9f1Rfr6Tq2zS0wZ7DHbZL+zB5rSlAr8QyUdg/GQD+cmSs6LvPJKL78d6hMGk84ARtFo4A79ovwX/Fj01znDQkU6nJildfkaolH2rWFG/qttD azjava@javalib.Com";
private static readonly string ResourceGroupName = SdkContext.RandomResourceName("rgNEMV", 24);
/**
* 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(IAzure azure)
{
string vnetName1 = SdkContext.RandomResourceName("vnet1", 20);
string vnetName2 = SdkContext.RandomResourceName("vnet2", 20);
string frontEndVmName = SdkContext.RandomResourceName("fevm", 24);
string backEndVmName = SdkContext.RandomResourceName("bevm", 24);
string publicIpAddressLeafDnsForFrontEndVm = SdkContext.RandomResourceName("pip1", 24);
try
{
//============================================================
// Create a virtual network with specific address-space and two subnet
// Creates a network security group for backend subnet
Utilities.Log("Creating a network security group for virtual network backend subnet...");
Utilities.Log("Creating a network security group for virtual network frontend subnet...");
var backEndSubnetNsg = await azure.NetworkSecurityGroups
.Define(VNet1BackEndSubnetNsgName)
.WithRegion(Region.USEast)
.WithNewResourceGroup(ResourceGroupName)
.DefineRule("DenyInternetInComing")
.DenyInbound()
.FromAddress("INTERNET")
.FromAnyPort()
.ToAnyAddress()
.ToAnyPort()
.WithAnyProtocol()
.Attach()
.DefineRule("DenyInternetOutGoing")
.DenyOutbound()
.FromAnyAddress()
.FromAnyPort()
.ToAddress("INTERNET")
.ToAnyPort()
.WithAnyProtocol()
.Attach()
.CreateAsync();
Utilities.Log("Created network security group");
// Print the network security group
Utilities.PrintNetworkSecurityGroup(backEndSubnetNsg);
var frontEndSubnetNsg = await azure.NetworkSecurityGroups.Define(VNet1FrontEndSubnetNsgName)
.WithRegion(Region.USEast)
.WithExistingResourceGroup(ResourceGroupName)
.DefineRule("AllowHttpInComing")
.AllowInbound()
.FromAddress("INTERNET")
.FromAnyPort()
.ToAnyAddress()
.ToPort(80)
.WithProtocol(SecurityRuleProtocol.Tcp)
.Attach()
.DefineRule("DenyInternetOutGoing")
.DenyOutbound()
.FromAnyAddress()
.FromAnyPort()
.ToAddress("INTERNET")
.ToAnyPort()
.WithAnyProtocol()
.Attach()
.CreateAsync();
Utilities.Log("Created network security group");
// Print the network security group
Utilities.PrintNetworkSecurityGroup(frontEndSubnetNsg);
Utilities.Log("Creating virtual network #1...");
INetwork virtualNetwork1 = await azure.Networks.Define(vnetName1)
.WithRegion(Region.USEast)
.WithExistingResourceGroup(ResourceGroupName)
.WithAddressSpace("192.168.0.0/16")
.WithSubnet(VNet1FrontEndSubnetName, "192.168.1.0/24")
.DefineSubnet(VNet1BackEndSubnetName)
.WithAddressPrefix("192.168.2.0/24")
.WithExistingNetworkSecurityGroup(backEndSubnetNsg)
.Attach()
.CreateAsync();
Utilities.Log("Created a virtual network");
// Print the virtual network details
Utilities.PrintVirtualNetwork(virtualNetwork1);
//============================================================
// 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");
await virtualNetwork1.Update()
.UpdateSubnet(VNet1FrontEndSubnetName)
.WithExistingNetworkSecurityGroup(frontEndSubnetNsg)
.Parent()
.ApplyAsync();
Utilities.Log("Network security group rule associated with the frontend subnet");
// Print the virtual network details
Utilities.PrintVirtualNetwork(virtualNetwork1);
//============================================================
// Create a virtual machine in each subnet
// Creates the first virtual machine in frontend subnet
Utilities.Log("Creating a Linux virtual machine in the frontend subnet");
// Creates the second virtual machine in the backend subnet
Utilities.Log("Creating a Linux virtual machine in the backend subnet");
// Create a virtual network with default address-space and one default subnet
Utilities.Log("Creating virtual network #2...");
var t1 = DateTime.UtcNow;
var frontEndVM = await azure.VirtualMachines.Define(frontEndVmName)
.WithRegion(Region.USEast)
.WithExistingResourceGroup(ResourceGroupName)
.WithExistingPrimaryNetwork(virtualNetwork1)
.WithSubnet(VNet1FrontEndSubnetName)
.WithPrimaryPrivateIPAddressDynamic()
.WithNewPrimaryPublicIPAddress(publicIpAddressLeafDnsForFrontEndVm)
.WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
.WithRootUsername(UserName)
.WithSsh(SshKey)
.WithSize(VirtualMachineSizeTypes.Parse("Standard_D2a_v4"))
.CreateAsync();
var t2 = DateTime.UtcNow;
Utilities.Log("Created Linux VM: (took "
+ (t2 - t1).TotalSeconds + " seconds) " + frontEndVM);
// Print virtual machine details
Utilities.PrintVirtualMachine(frontEndVM);
t1 = DateTime.UtcNow;
var backEndVM = await azure.VirtualMachines.Define(backEndVmName)
.WithRegion(Region.USEast)
.WithExistingResourceGroup(ResourceGroupName)
.WithExistingPrimaryNetwork(virtualNetwork1)
.WithSubnet(VNet1BackEndSubnetName)
.WithPrimaryPrivateIPAddressDynamic()
.WithoutPrimaryPublicIPAddress()
.WithPopularLinuxImage(KnownLinuxVirtualMachineImage.UbuntuServer16_04_Lts)
.WithRootUsername(UserName)
.WithSsh(SshKey)
.WithSize(VirtualMachineSizeTypes.Parse("Standard_D2a_v4"))
.CreateAsync();
var t3 = DateTime.UtcNow;
Utilities.Log("Created Linux VM: (took "
+ (t3 - t1).TotalSeconds + " seconds) " + backEndVM.Id);
// Print virtual machine details
Utilities.PrintVirtualMachine(backEndVM);
var virtualNetwork2 = await azure.Networks.Define(vnetName2)
.WithRegion(Region.USEast)
.WithNewResourceGroup(ResourceGroupName)
.CreateAsync();
Utilities.Log("Created a virtual network");
// Print the virtual network details
Utilities.PrintVirtualNetwork(virtualNetwork2);
//============================================================
// List virtual networks
foreach (var virtualNetwork in await azure.Networks.ListByResourceGroupAsync(ResourceGroupName))
{
Utilities.PrintVirtualNetwork(virtualNetwork);
}
//============================================================
// Delete a virtual network
Utilities.Log("Deleting the virtual network");
await azure.Networks.DeleteByIdAsync(virtualNetwork2.Id);
Utilities.Log("Deleted the virtual network");
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + ResourceGroupName);
await azure.ResourceGroups.DeleteByNameAsync(ResourceGroupName);
Utilities.Log("Deleted Resource Group: " + ResourceGroupName);
}
catch (NullReferenceException)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
public static void Main(string[] args)
{
try
{
//=================================================================
// Authenticate
var 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);
RunSampleAsync(azure).GetAwaiter().GetResult();
}
catch (Exception ex)
{
Utilities.Log(ex);
}
}
}
}