Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

resource_arm_dns_mx_record: switch dependency from riviera to azure-sdk-for-go #185

Merged
merged 3 commits into from
Jul 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 27 additions & 4 deletions azurerm/import_arm_dns_mx_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,42 @@ func TestAccAzureRMDnsMxRecord_importBasic(t *testing.T) {
resourceName := "azurerm_dns_mx_record.test"

ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMDnsMxRecord_basic, ri, ri, ri)
config := testAccAzureRMDnsMxRecord_basic(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsMxRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
{
Config: config,
},

resource.TestStep{
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMDnsMxRecord_importWithTags(t *testing.T) {
resourceName := "azurerm_dns_mx_record.test"

ri := acctest.RandInt()
config := testAccAzureRMDnsMxRecord_withTags(ri)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsMxRecordDestroy,
Steps: []resource.TestStep{
{
Config: config,
},

{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
Expand Down
198 changes: 95 additions & 103 deletions azurerm/resource_arm_dns_mx_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,54 @@ package azurerm
import (
"bytes"
"fmt"
"log"
"net/http"
"strconv"

"github.com/Azure/azure-sdk-for-go/arm/dns"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jen20/riviera/dns"
)

func resourceArmDnsMxRecord() *schema.Resource {
return &schema.Resource{
Create: resourceArmDnsMxRecordCreate,
Create: resourceArmDnsMxRecordCreateOrUpdate,
Read: resourceArmDnsMxRecordRead,
Update: resourceArmDnsMxRecordCreate,
Update: resourceArmDnsMxRecordCreateOrUpdate,
Delete: resourceArmDnsMxRecordDelete,
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,
},

"record": &schema.Schema{
"record": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"preference": &schema.Schema{
"preference": {
// TODO: this should become an Int
Type: schema.TypeString,
Required: true,
},

"exchange": &schema.Schema{
"exchange": {
Type: schema.TypeString,
Required: true,
},
Expand All @@ -57,7 +59,7 @@ func resourceArmDnsMxRecord() *schema.Resource {
Set: resourceArmDnsMxRecordHash,
},

"ttl": &schema.Schema{
"ttl": {
Type: schema.TypeInt,
Required: true,
},
Expand All @@ -67,155 +69,145 @@ func resourceArmDnsMxRecord() *schema.Resource {
}
}

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

mxRecords, recordErr := expandAzureRmDnsMxRecord(d)
if recordErr != nil {
return fmt.Errorf("Error Building Azure RM MX Record: %s", recordErr)
}
createCommand.MXRecords = mxRecords

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

createResponse, err := createRequest.Execute()
records, err := expandAzureRmDnsMxRecords(d)
if err != nil {
return fmt.Errorf("Error creating DNS MX Record: %s", err)
}
if !createResponse.IsSuccessful() {
return fmt.Errorf("Error creating DNS MX Record: %s", createResponse.Error)
return err
}

readRequest := rivieraClient.NewRequest()
readRequest.Command = &dns.GetMXRecordSet{
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,
MxRecords: &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.MX, parameters, eTag, ifNoneMatch)
if err != nil {
return fmt.Errorf("Error reading DNS MX Record: %s", err)
return err
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Error reading DNS MX Record: %s", readResponse.Error)

if resp.ID == nil {
return fmt.Errorf("Cannot read DNS MX Record %s (resource group %s) ID", name, resGroup)
}

resp := readResponse.Parsed.(*dns.GetMXRecordSetResponse)
d.SetId(resp.ID)
d.SetId(*resp.ID)

return resourceArmDnsMxRecordRead(d, meta)
}

func resourceArmDnsMxRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
client := meta.(*ArmClient).dnsClient

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

readRequest := rivieraClient.NewRequestForURI(d.Id())
readRequest.Command = &dns.GetMXRecordSet{}
resGroup := id.ResourceGroup
name := id.Path["MX"]
zoneName := id.Path["dnszones"]

readResponse, err := readRequest.Execute()
resp, err := client.Get(resGroup, zoneName, name, dns.MX)
if err != nil {
return fmt.Errorf("Error reading DNS MX Record: %s", err)
return fmt.Errorf("Error reading DNS MX record %s: %v", name, err)
}
if !readResponse.IsSuccessful() {
log.Printf("[INFO] Error reading DNS MX Record %q - removing from state", d.Id())
if resp.StatusCode == http.StatusNotFound {
d.SetId("")
return fmt.Errorf("Error reading DNS MX Record: %s", readResponse.Error)
return nil
}

resp := readResponse.Parsed.(*dns.GetMXRecordSetResponse)

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 err := d.Set("record", flattenAzureRmDnsMxRecord(resp.MXRecords)); err != nil {
log.Printf("[INFO] Error setting the Azure RM MX Record State: %s", err)
if err := d.Set("record", flattenAzureRmDnsMxRecords(resp.MxRecords)); err != nil {
return err
}

flattenAndSetTags(d, &resp.Tags)
flattenAndSetTags(d, resp.Metadata)

return nil
}

func resourceArmDnsMxRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient

deleteRequest := rivieraClient.NewRequestForURI(d.Id())
deleteRequest.Command = &dns.DeleteRecordSet{
RecordSetType: "MX",
}
client := meta.(*ArmClient).dnsClient

deleteResponse, err := deleteRequest.Execute()
id, err := parseAzureResourceID(d.Id())
if err != nil {
return fmt.Errorf("Error deleting DNS MX Record: %s", err)
return err
}
if !deleteResponse.IsSuccessful() {
return fmt.Errorf("Error deleting DNS MX Record: %s", deleteResponse.Error)

resGroup := id.ResourceGroup
name := id.Path["MX"]
zoneName := id.Path["dnszones"]

resp, error := client.Delete(resGroup, zoneName, name, dns.MX, "")
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error deleting DNS MX Record %s: %+v", name, error)
}

return nil
}

func expandAzureRmDnsMxRecord(d *schema.ResourceData) ([]dns.MXRecord, error) {
config := d.Get("record").(*schema.Set).List()
records := make([]dns.MXRecord, 0, len(config))

for _, pRaw := range config {
data := pRaw.(map[string]interface{})

mxrecord := dns.MXRecord{
Preference: data["preference"].(string),
Exchange: data["exchange"].(string),
// flatten creates an array of map where preference is a string to suit
// the expectations of the ResourceData schema, so that this data can be
// managed by Terradata state.
func flattenAzureRmDnsMxRecords(records *[]dns.MxRecord) []map[string]interface{} {
results := make([]map[string]interface{}, 0, len(*records))

if records != nil {
for _, record := range *records {
preferenceI32 := *record.Preference
preference := strconv.Itoa(int(preferenceI32))
results = append(results, map[string]interface{}{
"preference": preference,
"exchange": *record.Exchange,
})
}

records = append(records, mxrecord)

}

return records, nil

return results
}

func flattenAzureRmDnsMxRecord(records []dns.MXRecord) []map[string]interface{} {

result := make([]map[string]interface{}, 0, len(records))
for _, record := range records {
result = append(result, map[string]interface{}{
"preference": record.Preference,
"exchange": record.Exchange,
})
// expand creates an array of dns.MxRecord, that is, the array needed
// by azure-sdk-for-go to manipulate azure resources, hence Preference
// is an int32
func expandAzureRmDnsMxRecords(d *schema.ResourceData) ([]dns.MxRecord, error) {
recordStrings := d.Get("record").(*schema.Set).List()
records := make([]dns.MxRecord, len(recordStrings))

for i, v := range recordStrings {
mxrecord := v.(map[string]interface{})
preference := mxrecord["preference"].(string)
i64, _ := strconv.ParseInt(preference, 10, 32)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should be able to update the schema and the set to be an int here - and then remove the strconv parse here?

Copy link
Contributor

@tombuildsstuff tombuildsstuff Jul 26, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added a TODO for this here

i32 := int32(i64)
exchange := mxrecord["exchange"].(string)

records[i] = dns.MxRecord{
Preference: &i32,
Exchange: &exchange,
}
}
return result

return records, nil
}

func resourceArmDnsMxRecordHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})

buf.WriteString(fmt.Sprintf("%s-", m["preference"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["exchange"].(string)))

Expand Down
Loading