Skip to content

Commit

Permalink
Merge pull request #181 from sebastus/resource_arm_dns_aaaa_record
Browse files Browse the repository at this point in the history
resource_arm_dns_aaaa_record: switch dependency from riviera to azure-sdk-for-go
  • Loading branch information
tombuildsstuff authored Jul 26, 2017
2 parents 6026a72 + 901c114 commit 5fe3558
Show file tree
Hide file tree
Showing 3 changed files with 186 additions and 150 deletions.
31 changes: 26 additions & 5 deletions azurerm/import_arm_dns_aaaa_record_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package azurerm

import (
"fmt"
"testing"

"github.com/hashicorp/terraform/helper/acctest"
Expand All @@ -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,
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,
Expand Down
177 changes: 87 additions & 90 deletions azurerm/resource_arm_dns_aaaa_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,48 +2,48 @@ 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,
},

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,
},
Expand All @@ -53,124 +53,121 @@ func resourceArmDnsAAAARecord() *schema.Resource {
}
}

func resourceArmDnsAAAARecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
func resourceArmDnsAaaaRecordCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
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{})
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,
}

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

createRequest := rivieraClient.NewRequest()
createRequest.Command = createCommand

createResponse, err := createRequest.Execute()
records, err := expandAzureRmDnsAaaaRecords(d)
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)
return err
}

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: &dns.RecordSetProperties{
Metadata: expandTags(tags),
TTL: &ttl,
AaaaRecords: &records,
},
}

readResponse, err := readRequest.Execute()
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 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)

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: %+v", 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
}
Loading

0 comments on commit 5fe3558

Please sign in to comment.