provider/azurerm: Add `azurerm_dns_mx_record` resource

This commit is contained in:
stack72 2016-02-07 22:26:56 +00:00
parent 8ac4d2e080
commit 81b50330a1
6 changed files with 557 additions and 1 deletions

View File

@ -66,6 +66,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_dns_cname_record": resourceArmDnsCNameRecord(),
"azurerm_dns_txt_record": resourceArmDnsTxtRecord(),
"azurerm_dns_ns_record": resourceArmDnsNsRecord(),
"azurerm_dns_mx_record": resourceArmDnsMxRecord(),
"azurerm_dns_srv_record": resourceArmDnsSrvRecord(),
"azurerm_sql_server": resourceArmSqlServer(),
"azurerm_sql_database": resourceArmSqlDatabase(),

View File

@ -0,0 +1,212 @@
package azurerm
import (
"bytes"
"fmt"
"log"
"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,
Read: resourceArmDnsMxRecordRead,
Update: resourceArmDnsMxRecordCreate,
Delete: resourceArmDnsMxRecordDelete,
Schema: map[string]*schema.Schema{
"name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"resource_group_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"zone_name": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"record": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"preference": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
"exchange": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
Set: resourceArmDnsMxRecordHash,
},
"ttl": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"tags": tagsSchema(),
},
}
}
func resourceArmDnsMxRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
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()
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)
}
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),
}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading DNS MX Record: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Error reading DNS MX Record: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*dns.GetMXRecordSetResponse)
d.SetId(resp.ID)
return resourceArmDnsMxRecordRead(d, meta)
}
func resourceArmDnsMxRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
readRequest := rivieraClient.NewRequestForURI(d.Id())
readRequest.Command = &dns.GetMXRecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading DNS MX Record: %s", err)
}
if !readResponse.IsSuccessful() {
log.Printf("[INFO] Error reading DNS MX Record %q - removing from state", d.Id())
d.SetId("")
return fmt.Errorf("Error reading DNS MX Record: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*dns.GetMXRecordSetResponse)
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)
return err
}
flattenAndSetTags(d, &resp.Tags)
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",
}
deleteResponse, err := deleteRequest.Execute()
if err != nil {
return fmt.Errorf("Error deleting DNS MX Record: %s", err)
}
if !deleteResponse.IsSuccessful() {
return fmt.Errorf("Error deleting DNS MX Record: %s", deleteResponse.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),
}
records = append(records, mxrecord)
}
return records, nil
}
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,
})
}
return result
}
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)))
return hashcode.String(buf.String())
}

View File

@ -0,0 +1,267 @@
package azurerm
import (
"fmt"
"testing"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"github.com/jen20/riviera/dns"
)
func TestAccAzureRMDnsMxRecord_basic(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMDnsMxRecord_basic, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsMxRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsMxRecordExists("azurerm_dns_mx_record.test"),
),
},
},
})
}
func TestAccAzureRMDnsMxRecord_updateRecords(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMDnsMxRecord_basic, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMDnsMxRecord_updateRecords, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsMxRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsMxRecordExists("azurerm_dns_mx_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_mx_record.test", "record.#", "2"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsMxRecordExists("azurerm_dns_mx_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_mx_record.test", "record.#", "3"),
),
},
},
})
}
func TestAccAzureRMDnsMxRecord_withTags(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMDnsMxRecord_withTags, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMDnsMxRecord_withTagsUpdate, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsMxRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsMxRecordExists("azurerm_dns_mx_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_mx_record.test", "tags.#", "2"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsMxRecordExists("azurerm_dns_mx_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_mx_record.test", "tags.#", "1"),
),
},
},
})
}
func testCheckAzureRMDnsMxRecordExists(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]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
readRequest := conn.NewRequestForURI(rs.Primary.ID)
readRequest.Command = &dns.GetMXRecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetMXRecordSet: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Bad: GetMXRecordSet: %s", readResponse.Error)
}
return nil
}
}
func testCheckAzureRMDnsMxRecordDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_dns_mx_record" {
continue
}
readRequest := conn.NewRequestForURI(rs.Primary.ID)
readRequest.Command = &dns.GetMXRecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetMXRecordSet: %s", err)
}
if readResponse.IsSuccessful() {
return fmt.Errorf("Bad: DNS MX Record still exists: %s", readResponse.Error)
}
}
return nil
}
var testAccAzureRMDnsMxRecord_basic = `
resource "azurerm_resource_group" "test" {
name = "acctest_rg_%d"
location = "West US"
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_dns_mx_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
preference = "10"
exchange = "mail1.contoso.com"
}
record {
preference = "20"
exchange = "mail2.contoso.com"
}
}
`
var testAccAzureRMDnsMxRecord_updateRecords = `
resource "azurerm_resource_group" "test" {
name = "acctest_rg_%d"
location = "West US"
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_dns_mx_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
preference = "10"
exchange = "mail1.contoso.com"
}
record {
preference = "20"
exchange = "mail2.contoso.com"
}
record {
preference = "50"
exchange = "mail3.contoso.com"
}
}
`
var testAccAzureRMDnsMxRecord_withTags = `
resource "azurerm_resource_group" "test" {
name = "acctest_rg_%d"
location = "West US"
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_dns_mx_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
preference = "10"
exchange = "mail1.contoso.com"
}
record {
preference = "20"
exchange = "mail2.contoso.com"
}
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMDnsMxRecord_withTagsUpdate = `
resource "azurerm_resource_group" "test" {
name = "acctest_rg_%d"
location = "West US"
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_dns_mx_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
preference = "10"
exchange = "mail1.contoso.com"
}
record {
preference = "20"
exchange = "mail2.contoso.com"
}
tags {
environment = "staging"
}
}
`

View File

@ -92,7 +92,7 @@ func resourceArmDnsSrvRecordCreate(d *schema.ResourceData, meta interface{}) err
srvRecords, recordErr := expandAzureRmDnsSrvRecord(d)
if recordErr != nil {
return fmt.Errorf("Error Building Azure RM SOA Record: %s", recordErr)
return fmt.Errorf("Error Building Azure RM SRV Record: %s", recordErr)
}
createCommand.SRVRecords = srvRecords

View File

@ -0,0 +1,72 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_dns_mx_record"
sidebar_current: "docs-azurerm-resource-dns-mx-record"
description: |-
Manage a DNS MX Record.
---
# azurerm\_dns\_mx\_record
Enables you to manage DNS MX Records within Azure DNS.
## Example Usage
```
resource "azurerm_resource_group" "test" {
name = "acceptanceTestResourceGroup1"
location = "West US"
}
resource "azurerm_dns_zone" "test" {
name = "mydomain.com"
resource_group_name = "${azurerm_resource_group.test.name}"
}
resource "azurerm_dns_mx_record" "test" {
name = "test"
zone_name = "${azurerm_dns_zone.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
ttl = "300"
record {
preference = 10
exchange = "mail1.contoso.com"
}
record {
preference = 20
exchange = "mail2.contoso.com"
}
tags {
Environment = "Production"
}
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the DNS MX Record.
* `resource_group_name` - (Required) Specifies the resource group where the resource exists. Changing this forces a new resource to be created.
* `zone_name` - (Required) Specifies the DNS Zone where the resource exists. Changing this forces a new resource to be created.
* `TTL` - (Required) The Time To Live (TTL) of the DNS record.
* `record` - (Required) A list of values that make up the SRV record. Each `record` block supports fields documented below.
* `tags` - (Optional) A mapping of tags to assign to the resource.
The `record` block supports:
* `preference` - (Required) String representing the “preference” value of the MX records. Records with lower preference value take priority.
* `exchange` - (Required) The mail server responsible for the domain covered by the MX record.
## Attributes Reference
The following attributes are exported:
* `id` - The DNS MX Record ID.

View File

@ -46,6 +46,10 @@
<a href="/docs/providers/azurerm/r/dns_cname_record.html">azurerm_dns_cname_record</a>
</li>
<li<%= sidebar_current("docs-azurerm-resource-dns-mx-record") %>>
<a href="/docs/providers/azurerm/r/dns_mx_record.html">azurerm_dns_mx_record</a>
</li>
<li<%= sidebar_current("docs-azurerm-resource-dns-ns-record") %>>
<a href="/docs/providers/azurerm/r/dns_ns_record.html">azurerm_dns_ns_record</a>
</li>