/// <summary>
 /// Creates a RecordSet within a DNS zone.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Dns.IRecordSetOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='zoneName'>
 /// Required. The name of the zone without a terminating dot.
 /// </param>
 /// <param name='relativeRecordSetName'>
 /// Required. The name of the RecordSet, relative to the name of the
 /// zone.
 /// </param>
 /// <param name='recordType'>
 /// Required. The type of DNS record.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the CreateOrUpdate operation.
 /// </param>
 /// <returns>
 /// The response to a RecordSet CreateOrUpdate operation.
 /// </returns>
 public static RecordSetCreateOrUpdateResponse CreateOrUpdate(this IRecordSetOperations operations, string resourceGroupName, string zoneName, string relativeRecordSetName, RecordType recordType, RecordSetCreateOrUpdateParameters parameters)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IRecordSetOperations)s).CreateOrUpdateAsync(resourceGroupName, zoneName, relativeRecordSetName, recordType, parameters);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Creates a RecordSet within a DNS zone.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.Azure.Management.Dns.IRecordSetOperations.
 /// </param>
 /// <param name='resourceGroupName'>
 /// Required. The name of the resource group.
 /// </param>
 /// <param name='zoneName'>
 /// Required. The name of the zone without a terminating dot.
 /// </param>
 /// <param name='relativeRecordSetName'>
 /// Required. The name of the RecordSet, relative to the name of the
 /// zone.
 /// </param>
 /// <param name='recordType'>
 /// Required. The type of DNS record.
 /// </param>
 /// <param name='parameters'>
 /// Required. Parameters supplied to the CreateOrUpdate operation.
 /// </param>
 /// <returns>
 /// The response to a RecordSet CreateOrUpdate operation.
 /// </returns>
 public static Task<RecordSetCreateOrUpdateResponse> CreateOrUpdateAsync(this IRecordSetOperations operations, string resourceGroupName, string zoneName, string relativeRecordSetName, RecordType recordType, RecordSetCreateOrUpdateParameters parameters)
 {
     return operations.CreateOrUpdateAsync(resourceGroupName, zoneName, relativeRecordSetName, recordType, parameters, CancellationToken.None);
 }
        public void UpdateRecordSetPreconditionFailed()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                SingleRecordSetTestContext testContext = SetupSingleRecordSetTest();
                RecordSetCreateOrUpdateParameters createParameters = testContext.TestRecordSkeleton;
                createParameters.RecordSet.Properties.CnameRecord = new CnameRecord { Cname = "www.contoso.example.com" };

                RecordSetCreateOrUpdateResponse createResponse = testContext.DnsClient.RecordSets.CreateOrUpdate(
                    testContext.ResourceGroup.Name,
                    testContext.ZoneName,
                    testContext.RecordSetName,
                    RecordType.CNAME,
                    ifMatch: null,
                    ifNoneMatch: null,
                    parameters: createParameters);

                RecordSetCreateOrUpdateParameters updateParameters = new RecordSetCreateOrUpdateParameters { RecordSet = createResponse.RecordSet };
                updateParameters.RecordSet.ETag = "somegibberish";

                // expect Precondition Failed 412
                TestHelpers.AssertThrows<CloudException>(
                    () => testContext.DnsClient.RecordSets.CreateOrUpdate(
                        testContext.ResourceGroup.Name, 
                        testContext.ZoneName, 
                        testContext.RecordSetName, 
                        RecordType.CNAME,
                        ifMatch: null,
                        ifNoneMatch: null,
                        parameters: updateParameters),
                    exceptionAsserts: ex => ex.Error.Code == "PreconditionFailed");

                // expect Precondition Failed 412
                TestHelpers.AssertThrows<CloudException>(
                    () => testContext.DnsClient.RecordSets.Delete(
                        testContext.ResourceGroup.Name,
                        testContext.ZoneName,
                        testContext.RecordSetName,
                        RecordType.CNAME,
                        ifMatch: null,
                        ifNoneMatch: null),
                    exceptionAsserts: ex => ex.Error.Code == "PreconditionFailed");

                testContext.DnsClient.RecordSets.Delete(
                        testContext.ResourceGroup.Name,
                        testContext.ZoneName,
                        testContext.RecordSetName,
                        RecordType.CNAME,
                        ifMatch: null,
                        ifNoneMatch: null);

                testContext.DnsClient.Zones.Delete(testContext.ResourceGroup.Name, testContext.ZoneName, ifMatch: null, ifNoneMatch: null);
            }
        }
        public void UpdateSoa()
        {
            using (UndoContext context = UndoContext.Current)
            {
                context.Start();
                SingleRecordSetTestContext testContext = SetupSingleRecordSetTest();

                // SOA for the zone should already exist
                RecordSetGetResponse getresponse = testContext.DnsClient.RecordSets.Get(
                    testContext.ResourceGroup.Name,
                    testContext.ZoneName,
                    "@",
                    RecordType.SOA);

                Assert.Equal(HttpStatusCode.OK, getresponse.StatusCode);
                RecordSet soaResource = getresponse.RecordSet;
                Assert.NotNull(soaResource);
                Assert.NotNull(soaResource.Properties.SoaRecord);

                soaResource.Properties.SoaRecord.ExpireTime = 123;
                soaResource.Properties.SoaRecord.MinimumTtl = 1234;
                soaResource.Properties.SoaRecord.RefreshTime = 12345;
                soaResource.Properties.SoaRecord.RetryTime = 123456;

                var updateParameters = new RecordSetCreateOrUpdateParameters { RecordSet = soaResource };

                RecordSetCreateOrUpdateResponse updateResponse = testContext.DnsClient.RecordSets.CreateOrUpdate(
                    testContext.ResourceGroup.Name,
                    testContext.ZoneName,
                    "@",
                    RecordType.SOA,
                    ifMatch: null,
                    ifNoneMatch: null,
                    parameters: updateParameters);

                Assert.Equal(HttpStatusCode.OK, getresponse.StatusCode);
                Assert.True(
                    TestHelpers.AreEqual(soaResource, updateResponse.RecordSet, ignoreEtag: true),
                    "Response body of Update does not match expectations");

                getresponse = testContext.DnsClient.RecordSets.Get(
                    testContext.ResourceGroup.Name,
                    testContext.ZoneName,
                    "@",
                    RecordType.SOA);

                Assert.Equal(HttpStatusCode.OK, getresponse.StatusCode);
                Assert.True(
                    TestHelpers.AreEqual(updateResponse.RecordSet, getresponse.RecordSet),
                    "Response body of Get does not match expectations");

                // SOA will get deleted with the zone
                testContext.DnsClient.Zones.Delete(
                    testContext.ResourceGroup.Name,
                    testContext.ZoneName,
                    ifMatch: null, 
                    ifNoneMatch: null);
            }
        }