provider/azurerm: Add `azurerm_dns_txt_record` resource

This commit is contained in:
stack72 2016-02-06 18:42:54 +00:00
parent f57ce437cb
commit 37bc5a4c80
8 changed files with 555 additions and 4 deletions

6
Godeps/Godeps.json generated
View File

@ -526,15 +526,15 @@
},
{
"ImportPath": "github.com/jen20/riviera/azure",
"Rev": "ad1009032a2ff80c3d6ea094be49a2e98929a42b"
"Rev": "ba33b333fda56afaf01fcf87bf10e5ee17a0766f"
},
{
"ImportPath": "github.com/jen20/riviera/dns",
"Rev": "ad1009032a2ff80c3d6ea094be49a2e98929a42b"
"Rev": "ba33b333fda56afaf01fcf87bf10e5ee17a0766f"
},
{
"ImportPath": "github.com/jen20/riviera/sql",
"Rev": "ad1009032a2ff80c3d6ea094be49a2e98929a42b"
"Rev": "ba33b333fda56afaf01fcf87bf10e5ee17a0766f"
},
{
"ImportPath": "github.com/jmespath/go-jmespath",

View File

@ -64,6 +64,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_dns_a_record": resourceArmDnsARecord(),
"azurerm_dns_aaaa_record": resourceArmDnsAAAARecord(),
"azurerm_dns_cname_record": resourceArmDnsCNameRecord(),
"azurerm_dns_txt_record": resourceArmDnsTxtRecord(),
"azurerm_sql_server": resourceArmSqlServer(),
"azurerm_sql_database": resourceArmSqlDatabase(),
},

View File

@ -0,0 +1,194 @@
package azurerm
import (
"fmt"
"log"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jen20/riviera/dns"
)
func resourceArmDnsTxtRecord() *schema.Resource {
return &schema.Resource{
Create: resourceArmDnsTxtRecordCreate,
Read: resourceArmDnsTxtRecordRead,
Update: resourceArmDnsTxtRecordCreate,
Delete: resourceArmDnsTxtRecordDelete,
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{
"value": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
},
"ttl": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"tags": tagsSchema(),
},
}
}
func resourceArmDnsTxtRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
tags := d.Get("tags").(map[string]interface{})
expandedTags := expandTags(tags)
createCommand := &dns.CreateTXTRecordSet{
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,
}
txtRecords, recordErr := expandAzureRmDnsTxtRecords(d)
if recordErr != nil {
return fmt.Errorf("Error Building list of Azure RM Txt Records: %s", recordErr)
}
createCommand.TXTRecords = txtRecords
createRequest := rivieraClient.NewRequest()
createRequest.Command = createCommand
createResponse, err := createRequest.Execute()
if err != nil {
return fmt.Errorf("Error creating DNS TXT Record: %s", err)
}
if !createResponse.IsSuccessful() {
return fmt.Errorf("Error creating DNS TXT Record: %s", createResponse.Error)
}
readRequest := rivieraClient.NewRequest()
readRequest.Command = &dns.GetTXTRecordSet{
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 TXT Record: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Error reading DNS TXT Record: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*dns.GetTXTRecordSetResponse)
d.SetId(resp.ID)
return resourceArmDnsTxtRecordRead(d, meta)
}
func resourceArmDnsTxtRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
readRequest := rivieraClient.NewRequestForURI(d.Id())
readRequest.Command = &dns.GetTXTRecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading DNS TXT Record: %s", err)
}
if !readResponse.IsSuccessful() {
log.Printf("[INFO] Error reading DNS TXT Record %q - removing from state", d.Id())
d.SetId("")
return fmt.Errorf("Error reading DNS TXT Record: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*dns.GetTXTRecordSetResponse)
d.Set("ttl", resp.TTL)
if resp.TXTRecords != nil {
if err := d.Set("record", flattenAzureRmDnsTxtRecords(resp.TXTRecords)); err != nil {
log.Printf("[INFO] Error setting the Azure RM TXT Record State: %s", err)
return err
}
}
flattenAndSetTags(d, &resp.Tags)
return nil
}
func resourceArmDnsTxtRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
deleteRequest := rivieraClient.NewRequestForURI(d.Id())
deleteRequest.Command = &dns.DeleteRecordSet{
RecordSetType: "TXT",
}
deleteResponse, err := deleteRequest.Execute()
if err != nil {
return fmt.Errorf("Error deleting DNS TXT Record: %s", err)
}
if !deleteResponse.IsSuccessful() {
return fmt.Errorf("Error deleting DNS TXT Record: %s", deleteResponse.Error)
}
return nil
}
func expandAzureRmDnsTxtRecords(d *schema.ResourceData) ([]dns.TXTRecord, error) {
configs := d.Get("record").(*schema.Set).List()
txtRecords := make([]dns.TXTRecord, 0, len(configs))
for _, configRaw := range configs {
data := configRaw.(map[string]interface{})
txtRecord := dns.TXTRecord{
Value: data["value"].(string),
}
txtRecords = append(txtRecords, txtRecord)
}
return txtRecords, nil
}
func flattenAzureRmDnsTxtRecords(records []dns.TXTRecord) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(records))
for _, record := range records {
txtRecord := make(map[string]interface{})
txtRecord["value"] = record.Value
result = append(result, txtRecord)
}
return result
}

View File

@ -0,0 +1,257 @@
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 TestAccAzureRMDnsTxtRecord_basic(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMDnsTxtRecord_basic, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsTxtRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsTxtRecordExists("azurerm_dns_txt_record.test"),
),
},
},
})
}
func TestAccAzureRMDnsTxtRecord_updateRecords(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMDnsTxtRecord_basic, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMDnsTxtRecord_updateRecords, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsTxtRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsTxtRecordExists("azurerm_dns_txt_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_txt_record.test", "record.#", "2"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsTxtRecordExists("azurerm_dns_txt_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_txt_record.test", "record.#", "3"),
),
},
},
})
}
func TestAccAzureRMDnsTxtRecord_withTags(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMDnsTxtRecord_withTags, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMDnsTxtRecord_withTagsUpdate, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsTxtRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsTxtRecordExists("azurerm_dns_txt_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_txt_record.test", "tags.#", "2"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsTxtRecordExists("azurerm_dns_txt_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_txt_record.test", "tags.#", "1"),
),
},
},
})
}
func testCheckAzureRMDnsTxtRecordExists(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.GetTXTRecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetTXTRecordSet: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Bad: GetTXTRecordSet: %s", readResponse.Error)
}
return nil
}
}
func testCheckAzureRMDnsTxtRecordDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_dns_txt_record" {
continue
}
readRequest := conn.NewRequestForURI(rs.Primary.ID)
readRequest.Command = &dns.GetTXTRecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetTXTRecordSet: %s", err)
}
if readResponse.IsSuccessful() {
return fmt.Errorf("Bad: DNS TXT Record still exists: %s", readResponse.Error)
}
}
return nil
}
var testAccAzureRMDnsTxtRecord_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_txt_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
value = "Quick brown fox"
}
record {
value = "Another test txt string"
}
}
`
var testAccAzureRMDnsTxtRecord_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_txt_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
value = "Quick brown fox"
}
record {
value = "Another test txt string"
}
record {
value = "A wild 3rd record appears"
}
}
`
var testAccAzureRMDnsTxtRecord_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_txt_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
value = "Quick brown fox"
}
record {
value = "Another test txt string"
}
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMDnsTxtRecord_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_txt_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
value = "Quick brown fox"
}
record {
value = "Another test txt string"
}
tags {
environment = "staging"
}
}
`

View File

@ -3,7 +3,7 @@ package dns
import "github.com/jen20/riviera/azure"
type TXTRecord struct {
Value int `json:"value" mapstructure:"value"`
Value string `json:"value" mapstructure:"value"`
}
type CreateTXTRecordSetResponse struct {

View File

@ -0,0 +1,29 @@
package dns
import "github.com/jen20/riviera/azure"
type GetTXTRecordSetResponse struct {
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
Location string `mapstructure:"location"`
Tags map[string]*string `mapstructure:"tags"`
TTL *int `mapstructure:"TTL"`
TXTRecords []TXTRecord `mapstructure:"TXTRecords"`
}
type GetTXTRecordSet struct {
Name string `json:"-"`
ResourceGroupName string `json:"-"`
ZoneName string `json:"-"`
}
func (command GetTXTRecordSet) APIInfo() azure.APIInfo {
return azure.APIInfo{
APIVersion: apiVersion,
Method: "GET",
URLPathFunc: dnsRecordSetDefaultURLPathFunc(command.ResourceGroupName, command.ZoneName, "TXT", command.Name),
ResponseTypeFunc: func() interface{} {
return &GetTXTRecordSetResponse{}
},
}
}

View File

@ -0,0 +1,66 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_dns_txt_record"
sidebar_current: "docs-azurerm-resource-dns-txt-record"
description: |-
Create a DNS TXT Record.
---
# azurerm\_dns\_txt\_record
Enables you to manage DNS TXT 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_txt_record" "test" {
name = "test"
zone_name = "${azurerm_dns_zone.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
ttl = "300"
record {
value = "google-site-authenticator"
}
record {
value = "more site information here"
}
tags {
Environment = "Production"
}
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the DNS CNAME 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 txt record. Each `record` block supports fields documented below.
* `tags` - (Optional) A mapping of tags to assign to the resource.
The `record` block supports:
* `value` - (Required) The value of the record.
## Attributes Reference
The following attributes are exported:
* `id` - The DNS TXT 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-txt-record") %>>
<a href="/docs/providers/azurerm/r/dns_txt_record.html">azurerm_dns_txt_record</a>
</li>
<li<%= sidebar_current("docs-azurerm-resource-dns-zone") %>>
<a href="/docs/providers/azurerm/r/dns_zone.html">azurerm_dns_zone</a>
</li>