From adc6ba49027ba1004d7944e75fd2d6534350ace4 Mon Sep 17 00:00:00 2001 From: Greg Oliver Date: Thu, 20 Jul 2017 14:37:14 -0700 Subject: [PATCH 1/4] resource_arm_dns_aaaa_record: switch dependency from riviera to azure-sdk-for-go --- azurerm/resource_arm_dns_aaaa_record.go | 176 ++++++++++--------- azurerm/resource_arm_dns_aaaa_record_test.go | 60 ++++--- 2 files changed, 124 insertions(+), 112 deletions(-) diff --git a/azurerm/resource_arm_dns_aaaa_record.go b/azurerm/resource_arm_dns_aaaa_record.go index 5246e9f4cae2..861a502a338a 100644 --- a/azurerm/resource_arm_dns_aaaa_record.go +++ b/azurerm/resource_arm_dns_aaaa_record.go @@ -2,18 +2,18 @@ package azurerm import ( "fmt" - "log" + "net/http" + "github.com/Azure/azure-sdk-for-go/arm/dns" "github.com/hashicorp/terraform/helper/schema" - "github.com/jen20/riviera/dns" ) func resourceArmDnsAAAARecord() *schema.Resource { return &schema.Resource{ - Create: resourceArmDnsAAAARecordCreate, - Read: resourceArmDnsAAAARecordRead, - Update: resourceArmDnsAAAARecordCreate, - Delete: resourceArmDnsAAAARecordDelete, + Create: resourceArmDnsAaaaRecordCreateOrUpdate, + Read: resourceArmDnsAaaaRecordRead, + Update: resourceArmDnsAaaaRecordCreateOrUpdate, + Delete: resourceArmDnsAaaaRecordDelete, Importer: &schema.ResourceImporter{ State: schema.ImportStatePassthrough, }, @@ -48,129 +48,133 @@ func resourceArmDnsAAAARecord() *schema.Resource { Required: true, }, + "etag": { + Type: schema.TypeString, + Computed: true, + }, + "tags": tagsSchema(), }, } } -func resourceArmDnsAAAARecordCreate(d *schema.ResourceData, meta interface{}) error { - client := meta.(*ArmClient) - rivieraClient := client.rivieraClient - - tags := d.Get("tags").(map[string]interface{}) - expandedTags := expandTags(tags) - - createCommand := &dns.CreateAAAARecordSet{ - Name: d.Get("name").(string), - Location: "global", - ResourceGroupName: d.Get("resource_group_name").(string), - ZoneName: d.Get("zone_name").(string), - TTL: d.Get("ttl").(int), - Tags: *expandedTags, - } +func resourceArmDnsAaaaRecordCreateOrUpdate(d *schema.ResourceData, meta interface{}) error { + dnsClient := meta.(*ArmClient).dnsClient - recordStrings := d.Get("records").(*schema.Set).List() - records := make([]dns.AAAARecord, len(recordStrings)) - for i, v := range recordStrings { - records[i] = dns.AAAARecord{ - IPv6Address: v.(string), - } - } - createCommand.AAAARecords = records + 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) - createRequest := rivieraClient.NewRequest() - createRequest.Command = createCommand + tags := d.Get("tags").(map[string]interface{}) + metadata := expandTags(tags) - createResponse, err := createRequest.Execute() - if err != nil { - return fmt.Errorf("Error creating DNS AAAA Record: %s", err) - } - if !createResponse.IsSuccessful() { - return fmt.Errorf("Error creating DNS AAAA Record: %s", createResponse.Error) + records, err := expandAzureRmDnsAaaaRecords(d) + props := dns.RecordSetProperties{ + Metadata: metadata, + TTL: &ttl, + AaaaRecords: &records, } - readRequest := rivieraClient.NewRequest() - readRequest.Command = &dns.GetAAAARecordSet{ - Name: d.Get("name").(string), - ResourceGroupName: d.Get("resource_group_name").(string), - ZoneName: d.Get("zone_name").(string), + parameters := dns.RecordSet{ + Name: &name, + RecordSetProperties: &props, } - readResponse, err := readRequest.Execute() + //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.AAAA, parameters, eTag, "") if err != nil { - return fmt.Errorf("Error reading DNS AAAA Record: %s", err) + return err } - if !readResponse.IsSuccessful() { - return fmt.Errorf("Error reading DNS AAAA Record: %s", readResponse.Error) + + if resp.ID == nil { + return fmt.Errorf("Cannot read DNS AAAA Record %s (resource group %s) ID", name, resGroup) } - resp := readResponse.Parsed.(*dns.GetAAAARecordSetResponse) - d.SetId(resp.ID) + d.SetId(*resp.ID) - return resourceArmDnsAAAARecordRead(d, meta) + return resourceArmDnsAaaaRecordRead(d, meta) } -func resourceArmDnsAAAARecordRead(d *schema.ResourceData, meta interface{}) error { - client := meta.(*ArmClient) - rivieraClient := client.rivieraClient +func resourceArmDnsAaaaRecordRead(d *schema.ResourceData, meta interface{}) error { + dnsClient := meta.(*ArmClient).dnsClient id, err := parseAzureResourceID(d.Id()) if err != nil { return err } - readRequest := rivieraClient.NewRequestForURI(d.Id()) - readRequest.Command = &dns.GetAAAARecordSet{} + resGroup := id.ResourceGroup + name := id.Path["AAAA"] + zoneName := id.Path["dnszones"] - readResponse, err := readRequest.Execute() + resp, err := dnsClient.Get(resGroup, zoneName, name, dns.AAAA) if err != nil { - return fmt.Errorf("Error reading DNS AAAA Record: %s", err) + return fmt.Errorf("Error reading DNS AAAA record %s: %v", name, err) } - if !readResponse.IsSuccessful() { - log.Printf("[INFO] Error reading DNS AAAA Record %q - removing from state", d.Id()) + if resp.StatusCode == http.StatusNotFound { d.SetId("") - return fmt.Errorf("Error reading DNS AAAA Record: %s", readResponse.Error) + return nil } - resp := readResponse.Parsed.(*dns.GetAAAARecordSetResponse) - - d.Set("name", resp.Name) - d.Set("resource_group_name", id.ResourceGroup) - d.Set("zone_name", id.Path["dnszones"]) + d.Set("name", name) + d.Set("resource_group_name", resGroup) + d.Set("zone_name", zoneName) d.Set("ttl", resp.TTL) + d.Set("etag", resp.Etag) - if resp.AAAARecords != nil { - records := make([]string, 0, len(resp.AAAARecords)) - for _, record := range resp.AAAARecords { - records = append(records, record.IPv6Address) - } + if err := d.Set("records", flattenAzureRmDnsAaaaRecords(resp.AaaaRecords)); err != nil { + return err + } + flattenAndSetTags(d, resp.Metadata) - if err := d.Set("records", records); err != nil { - return err - } + return nil +} + +func resourceArmDnsAaaaRecordDelete(d *schema.ResourceData, meta interface{}) error { + dnsClient := meta.(*ArmClient).dnsClient + + id, err := parseAzureResourceID(d.Id()) + if err != nil { + return err } - flattenAndSetTags(d, &resp.Tags) + resGroup := id.ResourceGroup + name := id.Path["AAAA"] + zoneName := id.Path["dnszones"] + + resp, error := dnsClient.Delete(resGroup, zoneName, name, dns.AAAA, "") + if resp.StatusCode != http.StatusOK { + return fmt.Errorf("Error deleting DNS AAAA Record %s: %s", name, error) + } return nil } -func resourceArmDnsAAAARecordDelete(d *schema.ResourceData, meta interface{}) error { - client := meta.(*ArmClient) - rivieraClient := client.rivieraClient +func flattenAzureRmDnsAaaaRecords(records *[]dns.AaaaRecord) []string { + results := make([]string, 0, len(*records)) - deleteRequest := rivieraClient.NewRequestForURI(d.Id()) - deleteRequest.Command = &dns.DeleteRecordSet{ - RecordSetType: "AAAA", + if records != nil { + for _, record := range *records { + results = append(results, *record.Ipv6Address) + } } - deleteResponse, err := deleteRequest.Execute() - if err != nil { - return fmt.Errorf("Error deleting DNS AAAA Record: %s", err) - } - if !deleteResponse.IsSuccessful() { - return fmt.Errorf("Error deleting DNS AAAA Record: %s", deleteResponse.Error) + return results +} + +func expandAzureRmDnsAaaaRecords(d *schema.ResourceData) ([]dns.AaaaRecord, error) { + recordStrings := d.Get("records").(*schema.Set).List() + records := make([]dns.AaaaRecord, len(recordStrings)) + + for i, v := range recordStrings { + ipv6 := v.(string) + records[i] = dns.AaaaRecord{ + Ipv6Address: &ipv6, + } } - return nil + return records, nil } diff --git a/azurerm/resource_arm_dns_aaaa_record_test.go b/azurerm/resource_arm_dns_aaaa_record_test.go index a0e3dafc7c18..71142620d6d0 100644 --- a/azurerm/resource_arm_dns_aaaa_record_test.go +++ b/azurerm/resource_arm_dns_aaaa_record_test.go @@ -2,12 +2,13 @@ package azurerm import ( "fmt" + "net/http" "testing" + "github.com/Azure/azure-sdk-for-go/arm/dns" "github.com/hashicorp/terraform/helper/acctest" "github.com/hashicorp/terraform/helper/resource" "github.com/hashicorp/terraform/terraform" - "github.com/jen20/riviera/dns" ) func TestAccAzureRMDnsAAAARecord_basic(t *testing.T) { @@ -17,12 +18,12 @@ func TestAccAzureRMDnsAAAARecord_basic(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testCheckAzureRMDnsAAAARecordDestroy, + CheckDestroy: testCheckAzureRMDnsAaaaRecordDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: config, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMDnsAAAARecordExists("azurerm_dns_aaaa_record.test"), + testCheckAzureRMDnsAaaaRecordExists("azurerm_dns_aaaa_record.test"), ), }, }, @@ -37,12 +38,12 @@ func TestAccAzureRMDnsAAAARecord_updateRecords(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testCheckAzureRMDnsAAAARecordDestroy, + CheckDestroy: testCheckAzureRMDnsAaaaRecordDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: preConfig, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMDnsAAAARecordExists("azurerm_dns_aaaa_record.test"), + testCheckAzureRMDnsAaaaRecordExists("azurerm_dns_aaaa_record.test"), resource.TestCheckResourceAttr( "azurerm_dns_aaaa_record.test", "records.#", "2"), ), @@ -51,7 +52,7 @@ func TestAccAzureRMDnsAAAARecord_updateRecords(t *testing.T) { resource.TestStep{ Config: postConfig, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMDnsAAAARecordExists("azurerm_dns_aaaa_record.test"), + testCheckAzureRMDnsAaaaRecordExists("azurerm_dns_aaaa_record.test"), resource.TestCheckResourceAttr( "azurerm_dns_aaaa_record.test", "records.#", "3"), ), @@ -68,12 +69,12 @@ func TestAccAzureRMDnsAAAARecord_withTags(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testCheckAzureRMDnsAAAARecordDestroy, + CheckDestroy: testCheckAzureRMDnsAaaaRecordDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: preConfig, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMDnsAAAARecordExists("azurerm_dns_aaaa_record.test"), + testCheckAzureRMDnsAaaaRecordExists("azurerm_dns_aaaa_record.test"), resource.TestCheckResourceAttr( "azurerm_dns_aaaa_record.test", "tags.%", "2"), ), @@ -82,7 +83,7 @@ func TestAccAzureRMDnsAAAARecord_withTags(t *testing.T) { resource.TestStep{ Config: postConfig, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMDnsAAAARecordExists("azurerm_dns_aaaa_record.test"), + testCheckAzureRMDnsAaaaRecordExists("azurerm_dns_aaaa_record.test"), resource.TestCheckResourceAttr( "azurerm_dns_aaaa_record.test", "tags.%", "1"), ), @@ -91,7 +92,7 @@ func TestAccAzureRMDnsAAAARecord_withTags(t *testing.T) { }) } -func testCheckAzureRMDnsAAAARecordExists(name string) resource.TestCheckFunc { +func testCheckAzureRMDnsAaaaRecordExists(name string) resource.TestCheckFunc { return func(s *terraform.State) error { // Ensure we have enough information in state to look up in API rs, ok := s.RootModule().Resources[name] @@ -99,42 +100,49 @@ func testCheckAzureRMDnsAAAARecordExists(name string) resource.TestCheckFunc { return fmt.Errorf("Not found: %s", name) } - conn := testAccProvider.Meta().(*ArmClient).rivieraClient - - readRequest := conn.NewRequestForURI(rs.Primary.ID) - readRequest.Command = &dns.GetAAAARecordSet{} + aaaaName := rs.Primary.Attributes["name"] + zoneName := rs.Primary.Attributes["zone_name"] + resourceGroup, hasResourceGroup := rs.Primary.Attributes["resource_group_name"] + if !hasResourceGroup { + return fmt.Errorf("Bad: no resource group found in state for DNS AAAA record: %s", aaaaName) + } - readResponse, err := readRequest.Execute() + conn := testAccProvider.Meta().(*ArmClient).dnsClient + resp, err := conn.Get(resourceGroup, zoneName, aaaaName, dns.AAAA) if err != nil { - return fmt.Errorf("Bad: GetAAAARecordSet: %s", err) + return fmt.Errorf("Bad: Get AAAA RecordSet: %v", err) } - if !readResponse.IsSuccessful() { - return fmt.Errorf("Bad: GetAAAARecordSet: %s", readResponse.Error) + + if resp.StatusCode == http.StatusNotFound { + return fmt.Errorf("Bad: DNS AAAA record %s (resource group: %s) does not exist", aaaaName, resourceGroup) } return nil } } -func testCheckAzureRMDnsAAAARecordDestroy(s *terraform.State) error { - conn := testAccProvider.Meta().(*ArmClient).rivieraClient +func testCheckAzureRMDnsAaaaRecordDestroy(s *terraform.State) error { + conn := testAccProvider.Meta().(*ArmClient).dnsClient for _, rs := range s.RootModule().Resources { if rs.Type != "azurerm_dns_aaaa_record" { continue } - readRequest := conn.NewRequestForURI(rs.Primary.ID) - readRequest.Command = &dns.GetAAAARecordSet{} + aaaaName := rs.Primary.Attributes["name"] + zoneName := rs.Primary.Attributes["zone_name"] + resourceGroup := rs.Primary.Attributes["resource_group_name"] + + resp, err := conn.Get(resourceGroup, zoneName, aaaaName, dns.AAAA) - readResponse, err := readRequest.Execute() if err != nil { - return fmt.Errorf("Bad: GetAAAARecordSet: %s", err) + return nil } - if readResponse.IsSuccessful() { - return fmt.Errorf("Bad: DNS AAAA Record still exists: %s", readResponse.Error) + if resp.StatusCode != http.StatusNotFound { + return fmt.Errorf("DNS AAAA record still exists:\n%#v", resp.RecordSetProperties) } + } return nil From d4a2051031e7c33d66207a30a335bd219ec578c1 Mon Sep 17 00:00:00 2001 From: Greg Oliver Date: Thu, 20 Jul 2017 20:41:00 -0700 Subject: [PATCH 2/4] fix casing of test function --- azurerm/import_arm_dns_aaaa_record_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/import_arm_dns_aaaa_record_test.go b/azurerm/import_arm_dns_aaaa_record_test.go index dbf38511015c..270de08ddcc7 100644 --- a/azurerm/import_arm_dns_aaaa_record_test.go +++ b/azurerm/import_arm_dns_aaaa_record_test.go @@ -17,7 +17,7 @@ func TestAccAzureRMDnsAAAARecord_importBasic(t *testing.T) { resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, - CheckDestroy: testCheckAzureRMDnsAAAARecordDestroy, + CheckDestroy: testCheckAzureRMDnsAaaaRecordDestroy, Steps: []resource.TestStep{ resource.TestStep{ Config: config, From 3f07dcc9fbc958f334a9a42a37dfb6dd516526dd Mon Sep 17 00:00:00 2001 From: Greg Oliver Date: Sat, 22 Jul 2017 17:19:35 -0700 Subject: [PATCH 3/4] response to PR feedback --- azurerm/resource_arm_dns_aaaa_record.go | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/azurerm/resource_arm_dns_aaaa_record.go b/azurerm/resource_arm_dns_aaaa_record.go index 861a502a338a..21e0e2a7e27d 100644 --- a/azurerm/resource_arm_dns_aaaa_record.go +++ b/azurerm/resource_arm_dns_aaaa_record.go @@ -48,11 +48,6 @@ func resourceArmDnsAAAARecord() *schema.Resource { Required: true, }, - "etag": { - Type: schema.TypeString, - Computed: true, - }, - "tags": tagsSchema(), }, } @@ -65,7 +60,6 @@ func resourceArmDnsAaaaRecordCreateOrUpdate(d *schema.ResourceData, meta interfa 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) @@ -84,7 +78,7 @@ func resourceArmDnsAaaaRecordCreateOrUpdate(d *schema.ResourceData, meta interfa //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.AAAA, parameters, eTag, "") + resp, err := dnsClient.CreateOrUpdate(resGroup, zoneName, name, dns.AAAA, parameters, "", "") if err != nil { return err } @@ -123,7 +117,6 @@ func resourceArmDnsAaaaRecordRead(d *schema.ResourceData, meta interface{}) erro d.Set("resource_group_name", resGroup) d.Set("zone_name", zoneName) d.Set("ttl", resp.TTL) - d.Set("etag", resp.Etag) if err := d.Set("records", flattenAzureRmDnsAaaaRecords(resp.AaaaRecords)); err != nil { return err @@ -147,7 +140,7 @@ func resourceArmDnsAaaaRecordDelete(d *schema.ResourceData, meta interface{}) er resp, error := dnsClient.Delete(resGroup, zoneName, name, dns.AAAA, "") if resp.StatusCode != http.StatusOK { - return fmt.Errorf("Error deleting DNS AAAA Record %s: %s", name, error) + return fmt.Errorf("Error deleting DNS AAAA Record %s: %+v", name, error) } return nil From 901c114b947220b98ccdf6dbc9d1201f149a23cc Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Wed, 26 Jul 2017 14:22:56 +0100 Subject: [PATCH 4/4] Refactoring / PR comments --- azurerm/import_arm_dns_aaaa_record_test.go | 29 ++++++- azurerm/resource_arm_dns_aaaa_record.go | 34 ++++---- azurerm/resource_arm_dns_aaaa_record_test.go | 84 +++++++++++--------- 3 files changed, 89 insertions(+), 58 deletions(-) diff --git a/azurerm/import_arm_dns_aaaa_record_test.go b/azurerm/import_arm_dns_aaaa_record_test.go index 270de08ddcc7..a7a34d83485f 100644 --- a/azurerm/import_arm_dns_aaaa_record_test.go +++ b/azurerm/import_arm_dns_aaaa_record_test.go @@ -1,7 +1,6 @@ package azurerm import ( - "fmt" "testing" "github.com/hashicorp/terraform/helper/acctest" @@ -12,18 +11,40 @@ func TestAccAzureRMDnsAAAARecord_importBasic(t *testing.T) { resourceName := "azurerm_dns_aaaa_record.test" ri := acctest.RandInt() - config := fmt.Sprintf(testAccAzureRMDnsAAAARecord_basic, ri, ri, ri) + config := testAccAzureRMDnsAAAARecord_basic(ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMDnsAaaaRecordDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + +func TestAccAzureRMDnsAAAARecord_importWithTags(t *testing.T) { + resourceName := "azurerm_dns_aaaa_record.test" - resource.TestStep{ + ri := acctest.RandInt() + config := testAccAzureRMDnsAAAARecord_withTags(ri) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMDnsAaaaRecordDestroy, + Steps: []resource.TestStep{ + { + Config: config, + }, + { ResourceName: resourceName, ImportState: true, ImportStateVerify: true, diff --git a/azurerm/resource_arm_dns_aaaa_record.go b/azurerm/resource_arm_dns_aaaa_record.go index 21e0e2a7e27d..a46d63f4b3af 100644 --- a/azurerm/resource_arm_dns_aaaa_record.go +++ b/azurerm/resource_arm_dns_aaaa_record.go @@ -19,31 +19,31 @@ func resourceArmDnsAAAARecord() *schema.Resource { }, Schema: map[string]*schema.Schema{ - "name": &schema.Schema{ + "name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "resource_group_name": &schema.Schema{ + "resource_group_name": { Type: schema.TypeString, Required: true, ForceNew: true, }, - "zone_name": &schema.Schema{ + "zone_name": { Type: schema.TypeString, Required: true, }, - "records": &schema.Schema{ + "records": { Type: schema.TypeSet, Required: true, Elem: &schema.Schema{Type: schema.TypeString}, Set: schema.HashString, }, - "ttl": &schema.Schema{ + "ttl": { Type: schema.TypeInt, Required: true, }, @@ -54,31 +54,31 @@ func resourceArmDnsAAAARecord() *schema.Resource { } func resourceArmDnsAaaaRecordCreateOrUpdate(d *schema.ResourceData, meta interface{}) error { - dnsClient := meta.(*ArmClient).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)) - tags := d.Get("tags").(map[string]interface{}) - metadata := expandTags(tags) records, err := expandAzureRmDnsAaaaRecords(d) - props := dns.RecordSetProperties{ - Metadata: metadata, - TTL: &ttl, - AaaaRecords: &records, + if err != nil { + return err } parameters := dns.RecordSet{ - Name: &name, - RecordSetProperties: &props, + Name: &name, + RecordSetProperties: &dns.RecordSetProperties{ + Metadata: expandTags(tags), + TTL: &ttl, + AaaaRecords: &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.AAAA, parameters, "", "") + eTag := "" + ifNoneMatch := "" // set to empty to allow updates to records after creation + resp, err := client.CreateOrUpdate(resGroup, zoneName, name, dns.AAAA, parameters, eTag, ifNoneMatch) if err != nil { return err } diff --git a/azurerm/resource_arm_dns_aaaa_record_test.go b/azurerm/resource_arm_dns_aaaa_record_test.go index 71142620d6d0..4c832225e306 100644 --- a/azurerm/resource_arm_dns_aaaa_record_test.go +++ b/azurerm/resource_arm_dns_aaaa_record_test.go @@ -12,18 +12,19 @@ import ( ) func TestAccAzureRMDnsAAAARecord_basic(t *testing.T) { + resourceName := "azurerm_dns_aaaa_record.test" ri := acctest.RandInt() - config := fmt.Sprintf(testAccAzureRMDnsAAAARecord_basic, ri, ri, ri) + config := testAccAzureRMDnsAAAARecord_basic(ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMDnsAaaaRecordDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: config, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMDnsAaaaRecordExists("azurerm_dns_aaaa_record.test"), + testCheckAzureRMDnsAaaaRecordExists(resourceName), ), }, }, @@ -31,30 +32,28 @@ func TestAccAzureRMDnsAAAARecord_basic(t *testing.T) { } func TestAccAzureRMDnsAAAARecord_updateRecords(t *testing.T) { + resourceName := "azurerm_dns_aaaa_record.test" ri := acctest.RandInt() - preConfig := fmt.Sprintf(testAccAzureRMDnsAAAARecord_basic, ri, ri, ri) - postConfig := fmt.Sprintf(testAccAzureRMDnsAAAARecord_updateRecords, ri, ri, ri) + preConfig := testAccAzureRMDnsAAAARecord_basic(ri) + postConfig := testAccAzureRMDnsAAAARecord_updateRecords(ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMDnsAaaaRecordDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: preConfig, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMDnsAaaaRecordExists("azurerm_dns_aaaa_record.test"), - resource.TestCheckResourceAttr( - "azurerm_dns_aaaa_record.test", "records.#", "2"), + testCheckAzureRMDnsAaaaRecordExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "records.#", "2"), ), }, - - resource.TestStep{ + { Config: postConfig, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMDnsAaaaRecordExists("azurerm_dns_aaaa_record.test"), - resource.TestCheckResourceAttr( - "azurerm_dns_aaaa_record.test", "records.#", "3"), + testCheckAzureRMDnsAaaaRecordExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "records.#", "3"), ), }, }, @@ -62,30 +61,28 @@ func TestAccAzureRMDnsAAAARecord_updateRecords(t *testing.T) { } func TestAccAzureRMDnsAAAARecord_withTags(t *testing.T) { + resourceName := "azurerm_dns_aaaa_record.test" ri := acctest.RandInt() - preConfig := fmt.Sprintf(testAccAzureRMDnsAAAARecord_withTags, ri, ri, ri) - postConfig := fmt.Sprintf(testAccAzureRMDnsAAAARecord_withTagsUpdate, ri, ri, ri) + preConfig := testAccAzureRMDnsAAAARecord_withTags(ri) + postConfig := testAccAzureRMDnsAAAARecord_withTagsUpdate(ri) resource.Test(t, resource.TestCase{ PreCheck: func() { testAccPreCheck(t) }, Providers: testAccProviders, CheckDestroy: testCheckAzureRMDnsAaaaRecordDestroy, Steps: []resource.TestStep{ - resource.TestStep{ + { Config: preConfig, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMDnsAaaaRecordExists("azurerm_dns_aaaa_record.test"), - resource.TestCheckResourceAttr( - "azurerm_dns_aaaa_record.test", "tags.%", "2"), + testCheckAzureRMDnsAaaaRecordExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "2"), ), }, - - resource.TestStep{ + { Config: postConfig, Check: resource.ComposeTestCheckFunc( - testCheckAzureRMDnsAaaaRecordExists("azurerm_dns_aaaa_record.test"), - resource.TestCheckResourceAttr( - "azurerm_dns_aaaa_record.test", "tags.%", "1"), + testCheckAzureRMDnsAaaaRecordExists(resourceName), + resource.TestCheckResourceAttr(resourceName, "tags.%", "1"), ), }, }, @@ -136,23 +133,26 @@ func testCheckAzureRMDnsAaaaRecordDestroy(s *terraform.State) error { resp, err := conn.Get(resourceGroup, zoneName, aaaaName, dns.AAAA) if err != nil { - return nil - } + if resp.StatusCode == http.StatusNotFound { + return nil + } - if resp.StatusCode != http.StatusNotFound { - return fmt.Errorf("DNS AAAA record still exists:\n%#v", resp.RecordSetProperties) + return err } + return fmt.Errorf("DNS AAAA record still exists:\n%#v", resp.RecordSetProperties) } return nil } -var testAccAzureRMDnsAAAARecord_basic = ` +func testAccAzureRMDnsAAAARecord_basic(rInt int) string { + return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "acctestRG_%d" location = "West US" } + resource "azurerm_dns_zone" "test" { name = "acctestzone%d.com" resource_group_name = "${azurerm_resource_group.test.name}" @@ -165,13 +165,16 @@ resource "azurerm_dns_aaaa_record" "test" { ttl = "300" records = ["2607:f8b0:4009:1803::1005", "2607:f8b0:4009:1803::1006"] } -` +`, rInt, rInt, rInt) +} -var testAccAzureRMDnsAAAARecord_updateRecords = ` +func testAccAzureRMDnsAAAARecord_updateRecords(rInt int) string { + return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "acctestRG_%d" location = "West US" } + resource "azurerm_dns_zone" "test" { name = "acctestzone%d.com" resource_group_name = "${azurerm_resource_group.test.name}" @@ -184,13 +187,16 @@ resource "azurerm_dns_aaaa_record" "test" { ttl = "300" records = ["2607:f8b0:4009:1803::1005", "2607:f8b0:4009:1803::1006", "::1"] } -` +`, rInt, rInt, rInt) +} -var testAccAzureRMDnsAAAARecord_withTags = ` +func testAccAzureRMDnsAAAARecord_withTags(rInt int) string { + return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "acctestRG_%d" location = "West US" } + resource "azurerm_dns_zone" "test" { name = "acctestzone%d.com" resource_group_name = "${azurerm_resource_group.test.name}" @@ -208,13 +214,16 @@ resource "azurerm_dns_aaaa_record" "test" { cost_center = "MSFT" } } -` +`, rInt, rInt, rInt) +} -var testAccAzureRMDnsAAAARecord_withTagsUpdate = ` +func testAccAzureRMDnsAAAARecord_withTagsUpdate(rInt int) string { + return fmt.Sprintf(` resource "azurerm_resource_group" "test" { name = "acctestRG_%d" location = "West US" } + resource "azurerm_dns_zone" "test" { name = "acctestzone%d.com" resource_group_name = "${azurerm_resource_group.test.name}" @@ -231,4 +240,5 @@ resource "azurerm_dns_aaaa_record" "test" { environment = "staging" } } -` +`, rInt, rInt, rInt) +}