Beispiel #1
0
        public DnsZone CreateDnsZone(string name, string resourceGroupName, Hashtable[] tags)
        {
            ZoneCreateOrUpdateResponse response = this.DnsManagementClient.Zones.CreateOrUpdate(
                resourceGroupName,
                name,
                new ZoneCreateOrUpdateParameters
            {
                IfNoneMatch = "*",
                Zone        = new Management.Dns.Models.Zone
                {
                    Location   = DnsResourceLocation,
                    Name       = name,
                    Tags       = TagsConversionHelper.CreateTagDictionary(tags, validate: true),
                    ETag       = null,
                    Properties = new ZoneProperties
                    {
                        NumberOfRecordSets    = null,
                        MaxNumberOfRecordSets = null,
                    }
                }
            });

            return(new DnsZone
            {
                Name = response.Zone.Name,
                ResourceGroupName = resourceGroupName,
                Etag = response.Zone.ETag,
                Tags = TagsConversionHelper.CreateTagHashtable(response.Zone.Tags),
            });
        }
Beispiel #2
0
        public DnsZone UpdateDnsZone(DnsZone zone, bool overwrite)
        {
            ZoneCreateOrUpdateResponse response = this.DnsManagementClient.Zones.CreateOrUpdate(
                zone.ResourceGroupName,
                zone.Name,
                new ZoneCreateOrUpdateParameters
            {
                Zone = new Management.Dns.Models.Zone
                {
                    Location   = DnsResourceLocation,
                    Name       = zone.Name,
                    Tags       = TagsConversionHelper.CreateTagDictionary(zone.Tags, validate: true),
                    ETag       = overwrite ? "*" : zone.Etag,
                    Properties = new ZoneProperties
                    {
                    }
                }
            });

            return(new DnsZone
            {
                Name = response.Zone.Name,
                ResourceGroupName = zone.ResourceGroupName,
                Etag = response.Zone.ETag,
                Tags = TagsConversionHelper.CreateTagHashtable(response.Zone.Tags),
            });
        }
Beispiel #3
0
        public void UpdateZonePreconditionFailed()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                DnsManagementClient dnsClient = ResourceGroupHelper.GetDnsClient();

                string zoneName = TestUtilities.GenerateName("hydratestdnszone");
                string location = ResourceGroupHelper.GetResourceLocation(ResourceGroupHelper.GetResourcesClient(), "microsoft.network/dnszones");
                ResourceGroupExtended      resourceGroup  = ResourceGroupHelper.CreateResourceGroup();
                ZoneCreateOrUpdateResponse createresponse = ResourceGroupHelper.CreateZone(dnsClient, zoneName, location, resourceGroup);

                ZoneCreateOrUpdateParameters updateParameters = new ZoneCreateOrUpdateParameters {
                    Zone = createresponse.Zone
                };
                updateParameters.Zone.ETag = "somegibberish";

                // expect Precondition Failed 412
                TestHelpers.AssertThrows <CloudException>(
                    () => dnsClient.Zones.CreateOrUpdate(resourceGroup.Name, zoneName, updateParameters),
                    ex => ex.Error.Code == "PreconditionFailed");

                // expect Precondition Failed 412
                TestHelpers.AssertThrows <CloudException>(
                    () => dnsClient.Zones.Delete(resourceGroup.Name, zoneName, new ZoneDeleteParameters {
                    IfMatch = "somemoregib"
                }),
                    ex => ex.Error.Code == "PreconditionFailed");

                dnsClient.Zones.Delete(resourceGroup.Name, zoneName, new ZoneDeleteParameters());
            }
        }
Beispiel #4
0
        public void UpdateZonePreconditionFailed()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                DnsManagementClient dnsClient = ResourceGroupHelper.GetDnsClient();

                string zoneName = TestUtilities.GenerateName("hydratestdnszone.com");
                string location = ResourceGroupHelper.GetResourceLocation(ResourceGroupHelper.GetResourcesClient(), "microsoft.network/dnszones");
                ResourceGroupExtended      resourceGroup  = ResourceGroupHelper.CreateResourceGroup();
                ZoneCreateOrUpdateResponse createresponse = ResourceGroupHelper.CreateZone(dnsClient, zoneName, location, resourceGroup);

                ZoneCreateOrUpdateParameters updateParameters = new ZoneCreateOrUpdateParameters {
                    Zone = createresponse.Zone
                };
                updateParameters.Zone.ETag = "somegibberish";

                // expect Precondition Failed 412
                TestHelpers.AssertThrows <CloudException>(
                    () => dnsClient.Zones.CreateOrUpdate(resourceGroup.Name, zoneName, ifMatch: null, ifNoneMatch: null, parameters: updateParameters),
                    ex => ex.Error.Code == "PreconditionFailed");

                var result = dnsClient.Zones.Delete(resourceGroup.Name, zoneName, ifMatch: null, ifNoneMatch: null);
                Assert.Equal(HttpStatusCode.OK, result.StatusCode);

                result = dnsClient.Zones.Delete(resourceGroup.Name, "hiya.com", ifMatch: null, ifNoneMatch: null);
                Assert.Equal(HttpStatusCode.NoContent, result.StatusCode);
            }
        }
Beispiel #5
0
        public void CrudZoneSetsTheCurrentAndMaxRecordSetNumbersInResponse()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                DnsManagementClient dnsClient = ResourceGroupHelper.GetDnsClient();

                string zoneName = TestUtilities.GenerateName("hydratestdnszone");
                string location = ResourceGroupHelper.GetResourceLocation(ResourceGroupHelper.GetResourcesClient(), "microsoft.network/dnszones");
                ResourceGroupExtended resourceGroup = ResourceGroupHelper.CreateResourceGroup();

                // Create the zone clean
                ZoneCreateOrUpdateResponse createResponse = dnsClient.Zones.CreateOrUpdate(
                    resourceGroup.Name,
                    zoneName,
                    new ZoneCreateOrUpdateParameters
                {
                    Zone = new Microsoft.Azure.Management.Dns.Models.Zone
                    {
                        Location   = location,
                        Name       = zoneName,
                        Properties = new Microsoft.Azure.Management.Dns.Models.ZoneProperties
                        {
                            MaxNumberOfRecordSets = 42,                 // Test that specifying this value does not break Create (it must be ignored on server side).
                            NumberOfRecordSets    = 65,                 // Test that specifying this value does not break Create (it must be ignored on server side).
                        }
                    }
                });

                // Verify RecordSet numbers in the response.
                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);

                // Retrieve the zone after create
                ZoneGetResponse getResponse = dnsClient.Zones.Get(resourceGroup.Name, zoneName);

                // Verify RecordSet numbers in the response.
                Assert.Equal(HttpStatusCode.OK, getResponse.StatusCode);
                Assert.True(getResponse.Zone.Properties.NumberOfRecordSets == 2);

                Zone retrievedZone = getResponse.Zone;
                retrievedZone.Tags = new Dictionary <string, string> {
                    { "tag1", "value1" }
                };
                retrievedZone.Properties.NumberOfRecordSets    = null;
                retrievedZone.Properties.MaxNumberOfRecordSets = null;

                // Update the zone
                ZoneCreateOrUpdateResponse updateResponse = dnsClient.Zones.CreateOrUpdate(resourceGroup.Name, zoneName, new ZoneCreateOrUpdateParameters {
                    Zone = retrievedZone
                });

                // Verify RecordSet numbers in the response.
                Assert.Equal(HttpStatusCode.OK, updateResponse.StatusCode);

                // Delete the zone
                AzureOperationResponse deleteResponse = dnsClient.Zones.Delete(resourceGroup.Name, zoneName, new ZoneDeleteParameters());
                Assert.Equal(HttpStatusCode.OK, deleteResponse.StatusCode);
            }
        }
        public static ZoneCreateOrUpdateResponse CreateZone(DnsManagementClient dnsClient, string zoneName, string location, ResourceGroupExtended resourceGroup)
        {
            ZoneCreateOrUpdateResponse response = dnsClient.Zones.CreateOrUpdate(
                resourceGroup.Name,
                zoneName,
                new ZoneCreateOrUpdateParameters
            {
                Zone = new Microsoft.Azure.Management.Dns.Models.Zone
                {
                    Location   = location,
                    Name       = zoneName,
                    ETag       = null,
                    Properties = new Microsoft.Azure.Management.Dns.Models.ZoneProperties
                    {
                    }
                }
            });

            return(response);
        }
        protected async void AddZone()
        {
            InputDialog input = new InputDialog("Enter zone name", "Enter the name of the DNS zone to add", "");

            if (ActiveResourceGroup != null && input.ShowDialog() == true)
            {
                ZoneCreateOrUpdateParameters p = new ZoneCreateOrUpdateParameters();
                p.Zone            = new Zone("global");
                p.Zone.Properties = new ZoneProperties();
                try
                {
                    ZoneCreateOrUpdateResponse responseCreateZone = await _dnsManagementClient.Zones.CreateOrUpdateAsync(ActiveResourceGroup.Name, input.Value, p, null, null);

                    ReloadZones();
                } catch (Exception ex)
                {
                    Debug.WriteLine(ex.Message);
                    MessageBox.Show("Failed to add zone name!");
                }
            }
        }
        /*
         *   MAIN
         */
        static void Main(string[] args)
        {
            try
            {
                /*
                 *  validate args
                 */
                if (args.Length != 3)
                {
                    Console.WriteLine("Must provide the subscription ID, resource group and  zone name on the command line:");
                    Console.WriteLine(string.Format("e.g. {0} a11765aa-da85-55df-322c-f43434afcdb2 myRG mycontoso.com", System.AppDomain.CurrentDomain.FriendlyName));
                    PauseBeforeExit();
                    return;
                }
                string subID    = args[0];
                string rgName   = args[1];
                string zoneName = args[2];


                /*
                 *   Authorization
                 */

                //  get the JWT for the subscription, will be prompted for credentials
                Console.WriteLine(string.Format("Logging into subscription {0}...", subID));
                string jwt = JWTHelper.GetAuthToken(tenantId: JWTHelper.GetSubscriptionTenantId(subID), alwaysPrompt: true);

                //  make the credentials for your subscription ID
                TokenCloudCredentials tcCreds = new TokenCloudCredentials(subID, jwt);


                /*
                 *   Make sure we have a resource group as all ARM resources are in a resouce group
                 */

                //  get the resource management client
                ResourceManagementClient resourceClient = new ResourceManagementClient(tcCreds);

                //  check if the resource group already exists
                ResourceGroupExistsResult rgExists = resourceClient.ResourceGroups.CheckExistence(rgName);
                if (rgExists.Exists)
                {
                    Console.WriteLine(string.Format("ResourceGroup {0} already exists, but that's ok we'll reuse it...", rgName));
                }
                else
                {
                    Console.WriteLine(string.Format("Creating resouce group {0}...", rgName));
                    resourceClient.ResourceGroups.CreateOrUpdate(rgName, new ResourceGroup("northeurope"));
                }


                /*
                 *  Create a zone and some record sets
                 *  for Private Preview, zone name must be globally unique so it may already exist!
                 */

                //  get the DNS management client
                DnsManagementClient dnsClient = new DnsManagementClient(tcCreds);

                // check we're registered for Microsoft.Network namespace
                if (!IsProviderRegistered(resourceClient.Providers.List(null).Providers, "Microsoft.Network"))
                {
                    Console.WriteLine("Registering with Microsoft.Network namespace...");
                    resourceClient.Providers.Register("Microsoft.Network");
                }
                else
                {
                    Console.WriteLine("Already registered with Microsoft.Network namespace.");
                }

                //  create a DNS zone
                Console.WriteLine(string.Format("Creating zone and records for {0}...", zoneName));
                Zone z = new Zone("global");
                z.Properties = new ZoneProperties();
                z.Tags.Add("dept", "shopping");
                z.Tags.Add("env", "production");
                ZoneCreateOrUpdateResponse responseCreateZone = dnsClient.Zones.CreateOrUpdate(rgName, zoneName, new ZoneCreateOrUpdateParameters(z));

                // make some records (dnsClient.RecordSets will become dnsClient.RecordSetsets in future)
                RecordSet rsWwwA = new RecordSet("global");
                rsWwwA.Properties          = new RecordSetProperties(3600);
                rsWwwA.Properties.ARecords = new List <ARecord>();
                rsWwwA.Properties.ARecords.Add(new ARecord("1.2.3.4"));
                rsWwwA.Properties.ARecords.Add(new ARecord("1.2.3.5"));
                RecordSetCreateOrUpdateResponse responseCreateA = dnsClient.RecordSets.CreateOrUpdate(rgName, zoneName, "www", RecordType.A, new RecordSetCreateOrUpdateParameters(rsWwwA));

                RecordSet rsWwwAaaa = new RecordSet("global");
                rsWwwAaaa.Properties             = new RecordSetProperties(3600);
                rsWwwAaaa.Properties.AaaaRecords = new List <AaaaRecord>();
                rsWwwAaaa.Properties.AaaaRecords.Add(new AaaaRecord("1:1:1:1::1"));
                rsWwwAaaa.Properties.AaaaRecords.Add(new AaaaRecord("1:1:1:1::2"));
                RecordSetCreateOrUpdateResponse responseCreateAAAA = dnsClient.RecordSets.CreateOrUpdate(rgName, zoneName, "www", RecordType.AAAA, new RecordSetCreateOrUpdateParameters(rsWwwAaaa));

                // list the zones & record sets in the resource group
                ZoneListResponse zoneListResponse = dnsClient.Zones.List(rgName, new ZoneListParameters());
                foreach (Zone zone in zoneListResponse.Zones)
                {
                    RecordSetListResponse recordSets = dnsClient.RecordSets.ListAll(rgName, zone.Name, new RecordSetListParameters());
                    WriteRecordSetsToConsole(zone.Name, recordSets.RecordSets);
                }


                /*
                 *  ETAGs - set to a value to check record hasn't changed, set to * to make sure it exists
                 *
                 *  Also in RecordSetCreateOrUpdateParameters:
                 *      IfNoneMatch = *, only succesed if resource does not exist
                 */

                //  get the RecordSet for {Name=www, Type=A}
                RecordSetGetResponse getWwwA = dnsClient.RecordSets.Get(rgName, zoneName, "www", RecordType.A);
                string previousETag          = getWwwA.RecordSet.ETag;
                Console.WriteLine(string.Format("ETag for www.{0} is {1}", zoneName, previousETag));

                //  make a new record set, setting the ETag
                RecordSet newWwwA = new RecordSet("global");
                newWwwA.Properties          = new RecordSetProperties(3600);
                newWwwA.Properties.ARecords = new List <ARecord>();
                newWwwA.Properties.ARecords.Add(new ARecord("4.3.2.1"));
                newWwwA.Properties.ARecords.Add(new ARecord("5.3.2.1"));
                newWwwA.ETag = previousETag;

                // do two creates, second one will fail
                try
                {
                    Console.WriteLine("Doing first update - should succeed");
                    RecordSetCreateOrUpdateResponse responseETagUpdate1 = dnsClient.RecordSets.CreateOrUpdate(rgName, zoneName, "www", RecordType.A, new RecordSetCreateOrUpdateParameters(newWwwA));
                    Console.WriteLine(string.Format("Update set Etag to {0}", responseETagUpdate1.RecordSet.ETag));

                    Console.WriteLine("Doing second update - should fail because ETag changed!");
                    RecordSetCreateOrUpdateResponse responseETagUpdate2 = dnsClient.RecordSets.CreateOrUpdate(rgName, zoneName, "www", RecordType.A, new RecordSetCreateOrUpdateParameters(newWwwA));
                    Console.WriteLine(string.Format("Update set Etag to {0}", responseETagUpdate2.RecordSet.ETag));
                }
                catch (Hyak.Common.CloudException e)
                {
                    //  check if the precondition failed
                    if (e.Response.StatusCode == System.Net.HttpStatusCode.PreconditionFailed)
                    {
                        Console.WriteLine("The ETag precondition failed");
                    }
                    else
                    {
                        throw e;
                    }
                }

                // show records now
                WriteRecordSetsToConsole(zoneName, dnsClient.RecordSets.ListAll(rgName, zoneName, new RecordSetListParameters()).RecordSets);


                /*
                 *  End
                 */

                //  get one of the NS records
                RecordSetGetResponse getNS = dnsClient.RecordSets.Get(rgName, zoneName, "@", RecordType.NS);
                string firstNS             = getNS.RecordSet.Properties.NsRecords[0].Nsdname;

                //  show how to resolve record
                string url = string.Format("http://www.digwebinterface.com/?hostnames=www.{0}&type=&ns=self&nameservers={1}", zoneName, firstNS);
                Console.WriteLine(string.Format("To see the record resolve, goto: {0}", url));

                //  done
                PauseBeforeExit();

                // if we dare to delete the resource group :)
                // resourceClient.ResourceGroups.DeleteAsync(rgName);
            }
            catch (Exception e)
            {
                Console.WriteLine(string.Format("Exception Caught: {0}", e.Message));
                PauseBeforeExit();
            }
        }
Beispiel #9
0
        public void CrudZoneFullCycle()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                DnsManagementClient dnsClient = ResourceGroupHelper.GetDnsClient();

                string zoneName = TestUtilities.GenerateName("hydratestdnszone");
                string location = ResourceGroupHelper.GetResourceLocation(ResourceGroupHelper.GetResourcesClient(), "microsoft.network/dnszones");
                ResourceGroupExtended resourceGroup = ResourceGroupHelper.CreateResourceGroup();

                Action <Zone> assertZoneInvariants = zone =>
                {
                    Assert.Equal(zoneName, zone.Name);
                    Assert.False(string.IsNullOrEmpty(zone.ETag));
                };

                // Create the zone clean, verify response
                ZoneCreateOrUpdateResponse createResponse = dnsClient.Zones.CreateOrUpdate(
                    resourceGroup.Name,
                    zoneName,
                    new ZoneCreateOrUpdateParameters
                {
                    Zone = new Microsoft.Azure.Management.Dns.Models.Zone
                    {
                        Location = location,
                        Name     = zoneName,
                        Tags     = new Dictionary <string, string> {
                            { "tag1", "value1" }
                        },
                        ETag       = null,
                        Properties = new Microsoft.Azure.Management.Dns.Models.ZoneProperties
                        {
                        }
                    }
                });

                Assert.Equal(HttpStatusCode.Created, createResponse.StatusCode);
                assertZoneInvariants(createResponse.Zone);
                Assert.Equal(1, createResponse.Zone.Tags.Count);

                // Retrieve the zone after create, verify response
                var getresponse = dnsClient.Zones.Get(resourceGroup.Name, zoneName);

                Assert.Equal(HttpStatusCode.OK, getresponse.StatusCode);
                assertZoneInvariants(getresponse.Zone);
                Assert.Equal(1, getresponse.Zone.Tags.Count);

                // Call Update on the object returned by Create (important distinction from Get below)
                Zone createdZone = createResponse.Zone;
                createdZone.Tags = new Dictionary <string, string> {
                    { "tag1", "value1" }, { "tag2", "value2" }
                };

                ZoneCreateOrUpdateResponse updateResponse = dnsClient.Zones.CreateOrUpdate(resourceGroup.Name, zoneName, new ZoneCreateOrUpdateParameters {
                    Zone = createdZone
                });

                Assert.Equal(HttpStatusCode.OK, updateResponse.StatusCode);
                assertZoneInvariants(updateResponse.Zone);
                Assert.Equal(2, updateResponse.Zone.Tags.Count);

                // Retrieve the zone after create, verify response
                getresponse = dnsClient.Zones.Get(resourceGroup.Name, zoneName);

                Assert.Equal(HttpStatusCode.OK, getresponse.StatusCode);
                assertZoneInvariants(getresponse.Zone);
                Assert.Equal(2, getresponse.Zone.Tags.Count);

                // Call Update on the object returned by Get (important distinction from Create above)
                Zone retrievedZone = getresponse.Zone;
                retrievedZone.Tags = new Dictionary <string, string> {
                    { "tag1", "value1" }, { "tag2", "value2" }, { "tag3", "value3" }
                };

                updateResponse = dnsClient.Zones.CreateOrUpdate(resourceGroup.Name, zoneName, new ZoneCreateOrUpdateParameters {
                    Zone = retrievedZone
                });

                Assert.Equal(HttpStatusCode.OK, updateResponse.StatusCode);
                assertZoneInvariants(updateResponse.Zone);
                Assert.Equal(3, updateResponse.Zone.Tags.Count);

                // Delete the zone
                AzureOperationResponse deleteResponse = dnsClient.Zones.Delete(resourceGroup.Name, zoneName, new ZoneDeleteParameters());
                Assert.Equal(HttpStatusCode.OK, deleteResponse.StatusCode);
            }
        }