-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
187 lines (165 loc) · 8.84 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
// 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.ResourceManager.Fluent;
using Microsoft.Azure.Management.ResourceManager.Fluent.Core;
using Microsoft.Azure.Management.Samples.Common;
using System;
using System.Collections.Generic;
namespace ManagePrivateDns
{
public class Program
{
private const string CustomDomainName = "private.contoso.com";
/**
* Azure private DNS sample for managing DNS zones.
* - Creates a private DNS zone (private.contoso.com)
* - Creates a virtual network
* - Link a virtual network
* - Creates test virtual machines
* - Creates an additional DNS record
* - Test the private DNS zone
*/
public static void RunSample(IAzure azure)
{
string rgName = SdkContext.RandomResourceName("rgNEMV_", 24);
string vnName = SdkContext.RandomResourceName("vnetwork1-", 24);
string subnetName = SdkContext.RandomResourceName("subnet1-", 24);
string linkName = SdkContext.RandomResourceName("vnlink1-", 24);
string vm1Name = SdkContext.RandomResourceName("vm1-", 24);
string vm2Name = SdkContext.RandomResourceName("vm2-", 24);
string rsName = SdkContext.RandomResourceName("recordset1-", 24);
try
{
var resourceGroup = azure.ResourceGroups.Define(rgName)
.WithRegion(Region.AsiaSouthEast)
.Create();
//============================================================
// Creates a private DNS zone
Utilities.Log("Creating private DNS zone " + CustomDomainName + "...");
var privateDnsZone = azure.PrivateDnsZones.Define(CustomDomainName)
.WithExistingResourceGroup(resourceGroup)
.Create();
Utilities.Log("Created private DNS zone " + privateDnsZone.Name);
Utilities.Print(privateDnsZone);
//============================================================
// Creates a virtual network
Utilities.Log("Creating virtual network " + vnName + "...");
INetwork virtualNetwork = azure.Networks.Define(vnName)
.WithRegion(Region.AsiaSouthEast)
.WithExistingResourceGroup(resourceGroup)
.WithAddressSpace("10.2.0.0/16")
.WithSubnet(subnetName, "10.2.0.0/24")
.Create();
Utilities.Log("Created virtual network " + virtualNetwork.Name);
//============================================================
// Link a virtual network
Utilities.Log("Creating virtual network link " + linkName + " within private zone " + privateDnsZone.Name + " ...");
privateDnsZone.Update()
.DefineVirtualNetworkLink(linkName)
.EnableAutoRegistration()
.WithReferencedVirtualNetworkId(virtualNetwork.Id)
.WithETagCheck()
.Attach()
.Apply();
Utilities.Log("Linked a virtual network " + virtualNetwork.Id);
Utilities.Print(privateDnsZone);
//============================================================
// Creates test virtual machines
Utilities.Log("Creating first virtual machine " + vm1Name + "...");
var virtualMachine1 = azure.VirtualMachines.Define(vm1Name)
.WithRegion(Region.AsiaSouthEast)
.WithExistingResourceGroup(resourceGroup)
.WithExistingPrimaryNetwork(virtualNetwork)
.WithSubnet(subnetName)
.WithPrimaryPrivateIPAddressDynamic()
.WithoutPrimaryPublicIPAddress()
.WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012Datacenter)
.WithAdminUsername("azureadmin")
.WithAdminPassword("Azure12345678")
.Create();
Utilities.Log("Created first virtual machine " + virtualMachine1.Name);
Utilities.Log("Starting first virtual machine " + virtualMachine1.Name + "...");
virtualMachine1.Start();
Utilities.Log("Started first virtual machine " + virtualMachine1.Name);
Utilities.Log("Creating second virtual machine " + vm2Name + "...");
var virtualMachine2 = azure.VirtualMachines.Define(vm2Name)
.WithRegion(Region.AsiaSouthEast)
.WithExistingResourceGroup(resourceGroup)
.WithExistingPrimaryNetwork(virtualNetwork)
.WithSubnet(subnetName)
.WithPrimaryPrivateIPAddressDynamic()
.WithoutPrimaryPublicIPAddress()
.WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012Datacenter)
.WithAdminUsername("Foo12")
.WithAdminPassword("BaR@12!Foo")
.Create();
Utilities.Log("Created second virtual machine " + virtualMachine2.Name);
Utilities.Log("Starting second virtual machine " + virtualMachine2.Name + "...");
virtualMachine2.Start();
Utilities.Log("Started second virtual machine " + virtualMachine2.Name);
//============================================================
// Creates an additional DNS record
Utilities.Log("Creating additional record set " + rsName + "...");
privateDnsZone.Update()
.DefineARecordSet(rsName)
.WithIPv4Address(virtualMachine1.GetPrimaryNetworkInterface().PrimaryPrivateIP)
.Attach()
.Apply();
Utilities.Log("Created additional record set " + rsName);
Utilities.Print(privateDnsZone);
//============================================================
// Test the private DNS zone
string script1 = "New-NetFirewallRule -DisplayName \"Allow ICMPv4-In\" -Protocol ICMPv4";
Utilities.Log("Preparing first command: " + script1);
string script2 = "ping " + virtualMachine1.ComputerName + "." + CustomDomainName;
Utilities.Log("Preparing second command: " + script2);
string script3 = "ping " + rsName + "." + CustomDomainName;
Utilities.Log("Preparing third command: " + script1);
Utilities.Log("Starting to run command...");
var result = virtualMachine2.RunPowerShellScript(new List<string> { script1, script2, script3 }, new List<RunCommandInputParameter>());
foreach (var info in result.Value)
{
Utilities.Log(info.Message);
}
}
finally
{
try
{
Utilities.Log("Deleting Resource Group: " + rgName);
azure.ResourceGroups.DeleteByName(rgName);
Utilities.Log("Deleted Resource Group: " + rgName);
}
catch (Exception)
{
Utilities.Log("Did not create any resources in Azure. No clean up is necessary");
}
}
}
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);
RunSample(azure);
}
catch (Exception e)
{
Utilities.Log(e);
}
}
}
}