provider/azurerm: Add DNS CName Record Resource

This commit is contained in:
stack72 2016-02-05 12:51:33 +00:00
parent 1357f150a5
commit 6b5e5710c8
7 changed files with 453 additions and 8 deletions

View File

@ -63,6 +63,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_dns_zone": resourceArmDnsZone(),
"azurerm_dns_a_record": resourceArmDnsARecord(),
"azurerm_dns_aaaa_record": resourceArmDnsAAAARecord(),
"azurerm_dns_cname_record": resourceArmDnsCNameRecord(),
"azurerm_sql_server": resourceArmSqlServer(),
"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 resourceArmDnsCNameRecord() *schema.Resource {
return &schema.Resource{
Create: resourceArmDnsCNameRecordCreate,
Read: resourceArmDnsCNameRecordRead,
Update: resourceArmDnsCNameRecordCreate,
Delete: resourceArmDnsCNameRecordDelete,
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 resourceArmDnsCNameRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
tags := d.Get("tags").(map[string]interface{})
expandedTags := expandTags(tags)
createCommand := &dns.CreateCNAMERecordSet{
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.CNAMERecord, len(recordStrings))
for i, v := range recordStrings {
records[i] = dns.CNAMERecord{
CNAME: v.(string),
}
}
createCommand.CNAMERecords = records
createRequest := rivieraClient.NewRequest()
createRequest.Command = createCommand
createResponse, err := createRequest.Execute()
if err != nil {
return fmt.Errorf("Error creating DNS CName Record: %s", err)
}
if !createResponse.IsSuccessful() {
return fmt.Errorf("Error creating DNS CName Record: %s", createResponse.Error)
}
readRequest := rivieraClient.NewRequest()
readRequest.Command = &dns.GetCNAMERecordSet{
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 CName Record: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Error reading DNS CName Record: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*dns.GetCNAMERecordSetResponse)
d.SetId(resp.ID)
return resourceArmDnsCNameRecordRead(d, meta)
}
func resourceArmDnsCNameRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
readRequest := rivieraClient.NewRequestForURI(d.Id())
readRequest.Command = &dns.GetCNAMERecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading DNS A Record: %s", err)
}
if !readResponse.IsSuccessful() {
log.Printf("[INFO] Error reading DNS A Record %q - removing from state", d.Id())
d.SetId("")
return fmt.Errorf("Error reading DNS A Record: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*dns.GetCNAMERecordSetResponse)
d.Set("ttl", resp.TTL)
if resp.CNAMERecords != nil {
records := make([]string, 0, len(resp.CNAMERecords))
for _, record := range resp.CNAMERecords {
records = append(records, record.CNAME)
}
if err := d.Set("records", records); err != nil {
return err
}
}
flattenAndSetTags(d, &resp.Tags)
return nil
}
func resourceArmDnsCNameRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
deleteRequest := rivieraClient.NewRequestForURI(d.Id())
deleteRequest.Command = &dns.DeleteRecordSet{
RecordSetType: "CNAME",
}
deleteResponse, err := deleteRequest.Execute()
if err != nil {
return fmt.Errorf("Error deleting DNS CName Record: %s", err)
}
if !deleteResponse.IsSuccessful() {
return fmt.Errorf("Error deleting DNS CName Record: %s", deleteResponse.Error)
}
return nil
}

View File

@ -0,0 +1,222 @@
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 TestAccAzureRMDnsCNameRecord_basic(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMDnsCNameRecord_basic, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsCNameRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsCNameRecordExists("azurerm_dns_cname_record.test"),
),
},
},
})
}
func TestAccAzureRMDnsCNameRecord_updateRecords(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMDnsCNameRecord_basic, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMDnsCNameRecord_updateRecords, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsCNameRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsCNameRecordExists("azurerm_dns_cname_record.test"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsCNameRecordExists("azurerm_dns_cname_record.test"),
),
},
},
})
}
func TestAccAzureRMDnsCNameRecord_withTags(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMDnsCNameRecord_withTags, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMDnsCNameRecord_withTagsUpdate, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsCNameRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsCNameRecordExists("azurerm_dns_cname_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_cname_record.test", "tags.#", "2"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsCNameRecordExists("azurerm_dns_cname_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_cname_record.test", "tags.#", "1"),
),
},
},
})
}
func testCheckAzureRMDnsCNameRecordExists(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.GetCNAMERecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetCNAMERecordSet: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Bad: GetCNAMERecordSet: %s", readResponse.Error)
}
return nil
}
}
func testCheckAzureRMDnsCNameRecordDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_dns_cname_record" {
continue
}
readRequest := conn.NewRequestForURI(rs.Primary.ID)
readRequest.Command = &dns.GetCNAMERecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetCNAMERecordSet: %s", err)
}
if readResponse.IsSuccessful() {
return fmt.Errorf("Bad: DNS CNAME Record still exists: %s", readResponse.Error)
}
}
return nil
}
var testAccAzureRMDnsCNameRecord_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_cname_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
records = ["contoso.com"]
}
`
var testAccAzureRMDnsCNameRecord_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_cname_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
records = ["contoso.co.uk"]
}
`
var testAccAzureRMDnsCNameRecord_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_cname_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
records = ["contoso.com"]
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMDnsCNameRecord_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_cname_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
records = ["contoso.com"]
tags {
environment = "staging"
}
}
`

View File

@ -34,7 +34,7 @@ resource "azurerm_dns_a_record" "test" {
The following arguments are supported:
* `name` - (Required) The name of the DNS Zone. Must be a valid domain name.
* `name` - (Required) The name of the DNS A Record.
* `resource_group_name` - (Required) Specifies the resource group where the resource exists. Changing this forces a new resource to be created.

View File

@ -34,7 +34,7 @@ resource "azurerm_dns_aaaa_record" "test" {
The following arguments are supported:
* `name` - (Required) The name of the DNS Zone. Must be a valid domain name.
* `name` - (Required) The name of the DNS AAAA Record.
* `resource_group_name` - (Required) Specifies the resource group where the resource exists. Changing this forces a new resource to be created.

View File

@ -0,0 +1,53 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_dns_cname_record"
sidebar_current: "docs-azurerm-resource-dns-cname-record"
description: |-
Create a DNS CNAME Record.
---
# azurerm\_dns\_cname\_record
Enables you to manage DNS CNAME 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_cname_record" "test" {
name = "test"
zone_name = "${azurerm_dns_zone.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
ttl = "300"
records = ["contoso.com"]
}
```
## 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.
* `records` - (Required) The target of the CNAME. Must be a single value.
* `tags` - (Optional) A mapping of tags to assign to the resource.
## Attributes Reference
The following attributes are exported:
* `id` - The DNS CName Record ID.

View File

@ -34,17 +34,21 @@
<a href="#">DNS Resources</a>
<ul class="nav nav-visible">
<li<%= sidebar_current("docs-azurerm-resource-dns-a-record") %>>
<a href="/docs/providers/azurerm/r/dns_a_record.html">azurerm_dns_a_record</a>
</li>
<li<%= sidebar_current("docs-azurerm-resource-dns-a-record") %>>
<a href="/docs/providers/azurerm/r/dns_a_record.html">azurerm_dns_a_record</a>
</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") %>>
<a href="/docs/providers/azurerm/r/dns_zone.html">azurerm_dns_zone</a>
</li>
<li<%= sidebar_current("docs-azurerm-resource-dns-cname-record") %>>
<a href="/docs/providers/azurerm/r/dns_cname_record.html">azurerm_dns_cname_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>
</ul>
</li>