|
| 1 | +// Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +// Licensed under the MIT License. See License.txt in the project root for license information. |
| 3 | + |
| 4 | +using Microsoft.Azure.Management.Compute.Fluent; |
| 5 | +using Microsoft.Azure.Management.Compute.Fluent.Models; |
| 6 | +using Microsoft.Azure.Management.Fluent; |
| 7 | +using Microsoft.Azure.Management.Network.Fluent; |
| 8 | +using Microsoft.Azure.Management.ResourceManager.Fluent; |
| 9 | +using Microsoft.Azure.Management.ResourceManager.Fluent.Core; |
| 10 | +using Microsoft.Azure.Management.Samples.Common; |
| 11 | +using System; |
| 12 | +using System.Collections.Generic; |
| 13 | + |
| 14 | +namespace ManagePrivateDns |
| 15 | +{ |
| 16 | + public class Program |
| 17 | + { |
| 18 | + private const string CustomDomainName = "private.contoso.com"; |
| 19 | + |
| 20 | + /** |
| 21 | + * Azure private DNS sample for managing DNS zones. |
| 22 | + * - Creates a private DNS zone (private.contoso.com) |
| 23 | + * - Creates a virtual network |
| 24 | + * - Link a virtual network |
| 25 | + * - Creates test virtual machines |
| 26 | + * - Creates an additional DNS record |
| 27 | + * - Test the private DNS zone |
| 28 | + */ |
| 29 | + public static void RunSample(IAzure azure) |
| 30 | + { |
| 31 | + string rgName = SdkContext.RandomResourceName("rgNEMV_", 24); |
| 32 | + string vnName = SdkContext.RandomResourceName("vnetwork1-", 24); |
| 33 | + string subnetName = SdkContext.RandomResourceName("subnet1-", 24); |
| 34 | + string linkName = SdkContext.RandomResourceName("vnlink1-", 24); |
| 35 | + string vm1Name = SdkContext.RandomResourceName("vm1-", 24); |
| 36 | + string vm2Name = SdkContext.RandomResourceName("vm2-", 24); |
| 37 | + string rsName = SdkContext.RandomResourceName("recordset1-", 24); |
| 38 | + |
| 39 | + try |
| 40 | + { |
| 41 | + var resourceGroup = azure.ResourceGroups.Define(rgName) |
| 42 | + .WithRegion(Region.AsiaSouthEast) |
| 43 | + .Create(); |
| 44 | + |
| 45 | + //============================================================ |
| 46 | + // Creates a private DNS zone |
| 47 | + |
| 48 | + Utilities.Log("Creating private DNS zone " + CustomDomainName + "..."); |
| 49 | + var privateDnsZone = azure.PrivateDnsZones.Define(CustomDomainName) |
| 50 | + .WithExistingResourceGroup(resourceGroup) |
| 51 | + .Create(); |
| 52 | + |
| 53 | + Utilities.Log("Created private DNS zone " + privateDnsZone.Name); |
| 54 | + Utilities.Print(privateDnsZone); |
| 55 | + |
| 56 | + //============================================================ |
| 57 | + // Creates a virtual network |
| 58 | + |
| 59 | + Utilities.Log("Creating virtual network " + vnName + "..."); |
| 60 | + INetwork virtualNetwork = azure.Networks.Define(vnName) |
| 61 | + .WithRegion(Region.AsiaSouthEast) |
| 62 | + .WithExistingResourceGroup(resourceGroup) |
| 63 | + .WithAddressSpace("10.2.0.0/16") |
| 64 | + .WithSubnet(subnetName, "10.2.0.0/24") |
| 65 | + .Create(); |
| 66 | + Utilities.Log("Created virtual network " + virtualNetwork.Name); |
| 67 | + |
| 68 | + //============================================================ |
| 69 | + // Link a virtual network |
| 70 | + |
| 71 | + Utilities.Log("Creating virtual network link " + linkName + " within private zone " + privateDnsZone.Name + " ..."); |
| 72 | + privateDnsZone.Update() |
| 73 | + .DefineVirtualNetworkLink(linkName) |
| 74 | + .EnableAutoRegistration() |
| 75 | + .WithReferencedVirtualNetworkId(virtualNetwork.Id) |
| 76 | + .WithETagCheck() |
| 77 | + .Attach() |
| 78 | + .Apply(); |
| 79 | + Utilities.Log("Linked a virtual network " + virtualNetwork.Id); |
| 80 | + Utilities.Print(privateDnsZone); |
| 81 | + |
| 82 | + //============================================================ |
| 83 | + // Creates test virtual machines |
| 84 | + |
| 85 | + Utilities.Log("Creating first virtual machine " + vm1Name + "..."); |
| 86 | + var virtualMachine1 = azure.VirtualMachines.Define(vm1Name) |
| 87 | + .WithRegion(Region.AsiaSouthEast) |
| 88 | + .WithExistingResourceGroup(resourceGroup) |
| 89 | + .WithExistingPrimaryNetwork(virtualNetwork) |
| 90 | + .WithSubnet(subnetName) |
| 91 | + .WithPrimaryPrivateIPAddressDynamic() |
| 92 | + .WithoutPrimaryPublicIPAddress() |
| 93 | + .WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012Datacenter) |
| 94 | + .WithAdminUsername("azureadmin") |
| 95 | + .WithAdminPassword("Azure12345678") |
| 96 | + .Create(); |
| 97 | + Utilities.Log("Created first virtual machine " + virtualMachine1.Name); |
| 98 | + Utilities.Log("Starting first virtual machine " + virtualMachine1.Name + "..."); |
| 99 | + virtualMachine1.Start(); |
| 100 | + Utilities.Log("Started first virtual machine " + virtualMachine1.Name); |
| 101 | + |
| 102 | + Utilities.Log("Creating second virtual machine " + vm2Name + "..."); |
| 103 | + var virtualMachine2 = azure.VirtualMachines.Define(vm2Name) |
| 104 | + .WithRegion(Region.AsiaSouthEast) |
| 105 | + .WithExistingResourceGroup(resourceGroup) |
| 106 | + .WithExistingPrimaryNetwork(virtualNetwork) |
| 107 | + .WithSubnet(subnetName) |
| 108 | + .WithPrimaryPrivateIPAddressDynamic() |
| 109 | + .WithoutPrimaryPublicIPAddress() |
| 110 | + .WithPopularWindowsImage(KnownWindowsVirtualMachineImage.WindowsServer2012Datacenter) |
| 111 | + .WithAdminUsername("Foo12") |
| 112 | + .WithAdminPassword("BaR@12!Foo") |
| 113 | + .Create(); |
| 114 | + Utilities.Log("Created second virtual machine " + virtualMachine2.Name); |
| 115 | + Utilities.Log("Starting second virtual machine " + virtualMachine2.Name + "..."); |
| 116 | + virtualMachine2.Start(); |
| 117 | + Utilities.Log("Started second virtual machine " + virtualMachine2.Name); |
| 118 | + |
| 119 | + //============================================================ |
| 120 | + // Creates an additional DNS record |
| 121 | + Utilities.Log("Creating additional record set " + rsName + "..."); |
| 122 | + privateDnsZone.Update() |
| 123 | + .DefineARecordSet(rsName) |
| 124 | + .WithIPv4Address(virtualMachine1.GetPrimaryNetworkInterface().PrimaryPrivateIP) |
| 125 | + .Attach() |
| 126 | + .Apply(); |
| 127 | + Utilities.Log("Created additional record set " + rsName); |
| 128 | + Utilities.Print(privateDnsZone); |
| 129 | + |
| 130 | + //============================================================ |
| 131 | + // Test the private DNS zone |
| 132 | + |
| 133 | + string script1 = "New-NetFirewallRule -DisplayName \"Allow ICMPv4-In\" -Protocol ICMPv4"; |
| 134 | + Utilities.Log("Preparing first command: " + script1); |
| 135 | + string script2 = "ping " + virtualMachine1.ComputerName + "." + CustomDomainName; |
| 136 | + Utilities.Log("Preparing second command: " + script2); |
| 137 | + string script3 = "ping " + rsName + "." + CustomDomainName; |
| 138 | + Utilities.Log("Preparing third command: " + script1); |
| 139 | + |
| 140 | + Utilities.Log("Starting to run command..."); |
| 141 | + var result = virtualMachine2.RunPowerShellScript(new List<string> { script1, script2, script3 }, new List<RunCommandInputParameter>()); |
| 142 | + foreach (var info in result.Value) |
| 143 | + { |
| 144 | + Utilities.Log(info.Message); |
| 145 | + } |
| 146 | + } |
| 147 | + finally |
| 148 | + { |
| 149 | + try |
| 150 | + { |
| 151 | + Utilities.Log("Deleting Resource Group: " + rgName); |
| 152 | + azure.ResourceGroups.DeleteByName(rgName); |
| 153 | + Utilities.Log("Deleted Resource Group: " + rgName); |
| 154 | + } |
| 155 | + catch (Exception) |
| 156 | + { |
| 157 | + Utilities.Log("Did not create any resources in Azure. No clean up is necessary"); |
| 158 | + } |
| 159 | + } |
| 160 | + } |
| 161 | + |
| 162 | + public static void Main(string[] args) |
| 163 | + { |
| 164 | + try |
| 165 | + { |
| 166 | + //================================================================= |
| 167 | + // Authenticate |
| 168 | + var credentials = SdkContext.AzureCredentialsFactory.FromFile(Environment.GetEnvironmentVariable("AZURE_AUTH_LOCATION")); |
| 169 | + |
| 170 | + var azure = Azure |
| 171 | + .Configure() |
| 172 | + .WithLogLevel(HttpLoggingDelegatingHandler.Level.Basic) |
| 173 | + .Authenticate(credentials) |
| 174 | + .WithDefaultSubscription(); |
| 175 | + |
| 176 | + // Print selected subscription |
| 177 | + Utilities.Log("Selected subscription: " + azure.SubscriptionId); |
| 178 | + |
| 179 | + RunSample(azure); |
| 180 | + } |
| 181 | + catch (Exception e) |
| 182 | + { |
| 183 | + Utilities.Log(e); |
| 184 | + } |
| 185 | + } |
| 186 | + } |
| 187 | +} |
0 commit comments