provider/azurerm: Add AzureRM AAAA DNS Record resource

This commit is contained in:
stack72 2016-02-05 12:14:09 +00:00
parent 761308cd2a
commit 1357f150a5
6 changed files with 450 additions and 1 deletions

View File

@ -62,6 +62,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_storage_queue": resourceArmStorageQueue(), "azurerm_storage_queue": resourceArmStorageQueue(),
"azurerm_dns_zone": resourceArmDnsZone(), "azurerm_dns_zone": resourceArmDnsZone(),
"azurerm_dns_a_record": resourceArmDnsARecord(), "azurerm_dns_a_record": resourceArmDnsARecord(),
"azurerm_dns_aaaa_record": resourceArmDnsAAAARecord(),
"azurerm_sql_server": resourceArmSqlServer(), "azurerm_sql_server": resourceArmSqlServer(),
"azurerm_sql_database": resourceArmSqlDatabase(), "azurerm_sql_database": resourceArmSqlDatabase(),
}, },

View File

@ -0,0 +1,165 @@
package azurerm
import (
"fmt"
"log"
"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,
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,
},
"records": &schema.Schema{
Type: schema.TypeSet,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"ttl": &schema.Schema{
Type: schema.TypeInt,
Required: 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,
}
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()
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)
}
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),
}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading DNS AAAA Record: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Error reading DNS AAAA Record: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*dns.GetAAAARecordSetResponse)
d.SetId(resp.ID)
return resourceArmDnsAAAARecordRead(d, meta)
}
func resourceArmDnsAAAARecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
readRequest := rivieraClient.NewRequestForURI(d.Id())
readRequest.Command = &dns.GetAAAARecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading DNS AAAA Record: %s", err)
}
if !readResponse.IsSuccessful() {
log.Printf("[INFO] Error reading DNS AAAA Record %q - removing from state", d.Id())
d.SetId("")
return fmt.Errorf("Error reading DNS AAAA Record: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*dns.GetAAAARecordSetResponse)
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", records); err != nil {
return err
}
}
flattenAndSetTags(d, &resp.Tags)
return nil
}
func resourceArmDnsAAAARecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
deleteRequest := rivieraClient.NewRequestForURI(d.Id())
deleteRequest.Command = &dns.DeleteRecordSet{
RecordSetType: "AAAA",
}
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 nil
}

View File

@ -0,0 +1,226 @@
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 TestAccAzureRMDnsAAAARecord_basic(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMDnsAAAARecord_basic, ri, ri, 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"),
),
},
},
})
}
func TestAccAzureRMDnsAAAARecord_updateRecords(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMDnsAAAARecord_basic, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMDnsAAAARecord_updateRecords, ri, ri, 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"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsAAAARecordExists("azurerm_dns_aaaa_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_aaaa_record.test", "records.#", "3"),
),
},
},
})
}
func TestAccAzureRMDnsAAAARecord_withTags(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMDnsAAAARecord_withTags, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMDnsAAAARecord_withTagsUpdate, ri, ri, 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"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsAAAARecordExists("azurerm_dns_aaaa_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_aaaa_record.test", "tags.#", "1"),
),
},
},
})
}
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]
if !ok {
return fmt.Errorf("Not found: %s", name)
}
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
readRequest := conn.NewRequestForURI(rs.Primary.ID)
readRequest.Command = &dns.GetAAAARecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetAAAARecordSet: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Bad: GetAAAARecordSet: %s", readResponse.Error)
}
return nil
}
}
func testCheckAzureRMDnsAAAARecordDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_dns_aaaa_record" {
continue
}
readRequest := conn.NewRequestForURI(rs.Primary.ID)
readRequest.Command = &dns.GetAAAARecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetAAAARecordSet: %s", err)
}
if readResponse.IsSuccessful() {
return fmt.Errorf("Bad: DNS AAAA Record still exists: %s", readResponse.Error)
}
}
return nil
}
var testAccAzureRMDnsAAAARecord_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_aaaa_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
records = ["2607:f8b0:4009:1803::1005", "2607:f8b0:4009:1803::1006"]
}
`
var testAccAzureRMDnsAAAARecord_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_aaaa_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
records = ["2607:f8b0:4009:1803::1005", "2607:f8b0:4009:1803::1006", "::1"]
}
`
var testAccAzureRMDnsAAAARecord_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_aaaa_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
records = ["2607:f8b0:4009:1803::1005", "2607:f8b0:4009:1803::1006"]
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMDnsAAAARecord_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_aaaa_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
records = ["2607:f8b0:4009:1803::1005", "2607:f8b0:4009:1803::1006"]
tags {
environment = "staging"
}
}
`

View File

@ -6,7 +6,7 @@ description: |-
Create a DNS A Record. Create a DNS A Record.
--- ---
# azurerm\_dns\_zone # azurerm\_dns\_a\_record
Enables you to manage DNS A Records within Azure DNS. Enables you to manage DNS A Records within Azure DNS.

View File

@ -0,0 +1,53 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_dns_aaaa_record"
sidebar_current: "docs-azurerm-resource-dns-aaaa-record"
description: |-
Create a DNS AAAA Record.
---
# azurerm\_dns\_aaaa\_record
Enables you to manage DNS AAAA 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_aaaa_record" "test" {
name = "test"
zone_name = "${azurerm_dns_zone.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
ttl = "300"
records = ["2607:f8b0:4009:1803::1005"]
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the DNS Zone. Must be a valid domain name.
* `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.
* `records` - (Required) List of IPv6 Addresses.
* `tags` - (Optional) A mapping of tags to assign to the resource.
## Attributes Reference
The following attributes are exported:
* `id` - The DNS AAAA Record ID.

View File

@ -38,6 +38,10 @@
<a href="/docs/providers/azurerm/r/dns_a_record.html">azurerm_dns_a_record</a> <a href="/docs/providers/azurerm/r/dns_a_record.html">azurerm_dns_a_record</a>
</li> </li>
<li<%= sidebar_current("docs-azurerm-resource-dns-aaaa-record") %>>
<a href="/docs/providers/azurerm/r/dns_aaaa_record.html">azurerm_dns_aaaa_record</a>
</li>
<li<%= sidebar_current("docs-azurerm-resource-dns-zone") %>> <li<%= sidebar_current("docs-azurerm-resource-dns-zone") %>>
<a href="/docs/providers/azurerm/r/dns_zone.html">azurerm_dns_zone</a> <a href="/docs/providers/azurerm/r/dns_zone.html">azurerm_dns_zone</a>
</li> </li>