From 57fbce99b0bbb9c79a04040f9b9fee98a6d3c86a Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Tue, 25 Jul 2017 14:23:28 +0100 Subject: [PATCH 1/2] Refactoring the ptr record --- azurerm/import_arm_dns_ptr_record_test.go | 26 ++++++++++++- azurerm/resource_arm_dns_ptr_record.go | 42 ++++++++++----------- azurerm/resource_arm_dns_ptr_record_test.go | 13 +++++-- website/docs/r/dns_ptr_record.html.markdown | 2 - 4 files changed, 52 insertions(+), 31 deletions(-) diff --git a/azurerm/import_arm_dns_ptr_record_test.go b/azurerm/import_arm_dns_ptr_record_test.go index d29353cbec16..3cc312e0b09f 100644 --- a/azurerm/import_arm_dns_ptr_record_test.go +++ b/azurerm/import_arm_dns_ptr_record_test.go @@ -18,11 +18,33 @@ func TestAccAzureRMDnsPtrRecord_importBasic(t *testing.T) { Providers: testAccProviders, CheckDestroy: testCheckAzureRMDnsPtrRecordDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMDnsPtrRecord_importWithTags(t *testing.T) { + resourceName := "azurerm_dns_ptr_record.test" - resource.TestStep{ + ri := acctest.RandInt() + config := testAccAzureRMDnsPtrRecord_withTags(ri) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMDnsPtrRecordDestroy, + Steps: []resource.TestStep{ + { + Config: config, + }, + { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, diff --git a/azurerm/resource_arm_dns_ptr_record.go b/azurerm/resource_arm_dns_ptr_record.go index 3ae7f0a3c4b9..66f28e773100 100644 --- a/azurerm/resource_arm_dns_ptr_record.go +++ b/azurerm/resource_arm_dns_ptr_record.go @@ -48,44 +48,36 @@ func resourceArmDnsPtrRecord() *schema.Resource { Required: true, }, - "etag": { - Type: schema.TypeString, - Computed: true, - }, - "tags": tagsSchema(), }, } } func resourceArmDnsPtrRecordCreateOrUpdate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*ArmClient) - dnsClient := client.dnsClient + client := meta.(*ArmClient).dnsClient name := d.Get("name").(string) resGroup := d.Get("resource_group_name").(string) zoneName := d.Get("zone_name").(string) ttl := int64(d.Get("ttl").(int)) - eTag := d.Get("etag").(string) - tags := d.Get("tags").(map[string]interface{}) - metadata := expandTags(tags) records, err := expandAzureRmDnsPtrRecords(d) - props := dns.RecordSetProperties{ - Metadata: metadata, - TTL: &ttl, - PtrRecords: &records, + if err != nil { + return err } parameters := dns.RecordSet{ - Name: &name, - RecordSetProperties: &props, + RecordSetProperties: &dns.RecordSetProperties{ + Metadata: expandTags(tags), + TTL: &ttl, + PtrRecords: &records, + }, } - //last parameter is set to empty to allow updates to records after creation - // (per SDK, set it to '*' to prevent updates, all other values are ignored) - resp, err := dnsClient.CreateOrUpdate(resGroup, zoneName, name, dns.PTR, parameters, eTag, "") + eTag := "" + ifNoneMatch := "" // set to empty to allow updates to records after creation + resp, err := client.CreateOrUpdate(resGroup, zoneName, name, dns.PTR, parameters, eTag, ifNoneMatch) if err != nil { return err } @@ -114,7 +106,7 @@ func resourceArmDnsPtrRecordRead(d *schema.ResourceData, meta interface{}) error resp, err := dnsClient.Get(resGroup, zoneName, name, dns.PTR) if err != nil { - return fmt.Errorf("Error reading DNS PTR record %s: %v", name, err) + return fmt.Errorf("Error reading DNS PTR record %s: %+v", name, err) } if resp.StatusCode == http.StatusNotFound { d.SetId("") @@ -148,9 +140,13 @@ func resourceArmDnsPtrRecordDelete(d *schema.ResourceData, meta interface{}) err name := id.Path["PTR"] zoneName := id.Path["dnszones"] - resp, error := dnsClient.Delete(resGroup, zoneName, name, dns.PTR, "") - if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Error deleting DNS PTR Record %s: %s", name, error) + resp, err := dnsClient.Delete(resGroup, zoneName, name, dns.PTR, "") + if err != nil { + if resp.StatusCode == http.StatusNotFound { + return nil + } + + return fmt.Errorf("Error deleting DNS PTR Record %s: %+v", name, err) } return nil diff --git a/azurerm/resource_arm_dns_ptr_record_test.go b/azurerm/resource_arm_dns_ptr_record_test.go index 1a20e3aaf63d..f472c26b469d 100644 --- a/azurerm/resource_arm_dns_ptr_record_test.go +++ b/azurerm/resource_arm_dns_ptr_record_test.go @@ -133,13 +133,14 @@ func testCheckAzureRMDnsPtrRecordDestroy(s *terraform.State) error { resp, err := conn.Get(resourceGroup, zoneName, ptrName, dns.PTR) if err != nil { - return nil - } + if resp.StatusCode != http.StatusNotFound { + return nil + } - if resp.StatusCode != http.StatusNotFound { - return fmt.Errorf("DNS PTR record still exists:\n%#v", resp.RecordSetProperties) + return err } + fmt.Errorf("DNS PTR record still exists:\n%#v", resp) } return nil @@ -151,6 +152,7 @@ resource "azurerm_resource_group" "test" { name = "acctestRG_%[1]d" location = "West US" } + resource "azurerm_dns_zone" "test" { name = "acctestzone%[1]d.com" resource_group_name = "${azurerm_resource_group.test.name}" @@ -172,6 +174,7 @@ resource "azurerm_resource_group" "test" { name = "acctestRG_%[1]d" location = "West US" } + resource "azurerm_dns_zone" "test" { name = "acctestzone%[1]d.com" resource_group_name = "${azurerm_resource_group.test.name}" @@ -193,6 +196,7 @@ resource "azurerm_resource_group" "test" { name = "acctestRG_%[1]d" location = "West US" } + resource "azurerm_dns_zone" "test" { name = "acctestzone%[1]d.com" resource_group_name = "${azurerm_resource_group.test.name}" @@ -219,6 +223,7 @@ resource "azurerm_resource_group" "test" { name = "acctestRG_%[1]d" location = "West US" } + resource "azurerm_dns_zone" "test" { name = "acctestzone%[1]d.com" resource_group_name = "${azurerm_resource_group.test.name}" diff --git a/website/docs/r/dns_ptr_record.html.markdown b/website/docs/r/dns_ptr_record.html.markdown index 4f689eb32533..4f355707f2ea 100644 --- a/website/docs/r/dns_ptr_record.html.markdown +++ b/website/docs/r/dns_ptr_record.html.markdown @@ -54,8 +54,6 @@ The following attributes are exported: * `id` - The DNS PTR Record ID. -* `etag` - The etag of the record set. - ## Import PTR records can be imported using the `resource id`, e.g. From df0e6e0367bfdd64fcbd7af34736e8d6b954b52c Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Wed, 26 Jul 2017 11:39:23 +0100 Subject: [PATCH 2/2] Refactoring the PTR record --- azurerm/resource_arm_dns_ptr_record.go | 9 +++++---- azurerm/resource_arm_dns_ptr_record_test.go | 4 ++-- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/azurerm/resource_arm_dns_ptr_record.go b/azurerm/resource_arm_dns_ptr_record.go index 66f28e773100..edba56b80e00 100644 --- a/azurerm/resource_arm_dns_ptr_record.go +++ b/azurerm/resource_arm_dns_ptr_record.go @@ -106,12 +106,13 @@ func resourceArmDnsPtrRecordRead(d *schema.ResourceData, meta interface{}) error resp, err := dnsClient.Get(resGroup, zoneName, name, dns.PTR) if err != nil { + if resp.StatusCode == http.StatusNotFound { + d.SetId("") + return nil + } + return fmt.Errorf("Error reading DNS PTR record %s: %+v", name, err) } - if resp.StatusCode == http.StatusNotFound { - d.SetId("") - return nil - } d.Set("name", name) d.Set("resource_group_name", resGroup) diff --git a/azurerm/resource_arm_dns_ptr_record_test.go b/azurerm/resource_arm_dns_ptr_record_test.go index f472c26b469d..17eb7fafa7be 100644 --- a/azurerm/resource_arm_dns_ptr_record_test.go +++ b/azurerm/resource_arm_dns_ptr_record_test.go @@ -133,14 +133,14 @@ func testCheckAzureRMDnsPtrRecordDestroy(s *terraform.State) error { resp, err := conn.Get(resourceGroup, zoneName, ptrName, dns.PTR) if err != nil { - if resp.StatusCode != http.StatusNotFound { + if resp.StatusCode == http.StatusNotFound { return nil } return err } - fmt.Errorf("DNS PTR record still exists:\n%#v", resp) + return fmt.Errorf("DNS PTR record still exists:\n%#v", resp) } return nil