provider/azurerm: Add `azurerm_dns_srv_record` resource

This commit is contained in:
stack72 2016-02-07 21:49:02 +00:00
parent f9ffeae0f6
commit 8ac4d2e080
9 changed files with 614 additions and 22 deletions

6
Godeps/Godeps.json generated
View File

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

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_srv_record": resourceArmDnsSrvRecord(),
"azurerm_sql_server": resourceArmSqlServer(),
"azurerm_sql_database": resourceArmSqlDatabase(),
},

View File

@ -149,7 +149,7 @@ func resourceArmDnsNsRecordDelete(d *schema.ResourceData, meta interface{}) erro
deleteRequest := rivieraClient.NewRequestForURI(d.Id())
deleteRequest.Command = &dns.DeleteRecordSet{
RecordSetType: "TXT",
RecordSetType: "NS",
}
deleteResponse, err := deleteRequest.Execute()

View File

@ -0,0 +1,228 @@
package azurerm
import (
"bytes"
"fmt"
"log"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/jen20/riviera/dns"
)
func resourceArmDnsSrvRecord() *schema.Resource {
return &schema.Resource{
Create: resourceArmDnsSrvRecordCreate,
Read: resourceArmDnsSrvRecordRead,
Update: resourceArmDnsSrvRecordCreate,
Delete: resourceArmDnsSrvRecordDelete,
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{
"priority": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"weight": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"port": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"target": &schema.Schema{
Type: schema.TypeString,
Required: true,
},
},
},
Set: resourceArmDnsSrvRecordHash,
},
"ttl": &schema.Schema{
Type: schema.TypeInt,
Required: true,
},
"tags": tagsSchema(),
},
}
}
func resourceArmDnsSrvRecordCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
tags := d.Get("tags").(map[string]interface{})
expandedTags := expandTags(tags)
createCommand := &dns.CreateSRVRecordSet{
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,
}
srvRecords, recordErr := expandAzureRmDnsSrvRecord(d)
if recordErr != nil {
return fmt.Errorf("Error Building Azure RM SOA Record: %s", recordErr)
}
createCommand.SRVRecords = srvRecords
createRequest := rivieraClient.NewRequest()
createRequest.Command = createCommand
createResponse, err := createRequest.Execute()
if err != nil {
return fmt.Errorf("Error creating DNS SRV Record: %s", err)
}
if !createResponse.IsSuccessful() {
return fmt.Errorf("Error creating DNS SRV Record: %s", createResponse.Error)
}
readRequest := rivieraClient.NewRequest()
readRequest.Command = &dns.GetSRVRecordSet{
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 SRV Record: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Error reading DNS SRV Record: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*dns.GetSRVRecordSetResponse)
d.SetId(resp.ID)
return resourceArmDnsSrvRecordRead(d, meta)
}
func resourceArmDnsSrvRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
readRequest := rivieraClient.NewRequestForURI(d.Id())
readRequest.Command = &dns.GetSRVRecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Error reading DNS SRV Record: %s", err)
}
if !readResponse.IsSuccessful() {
log.Printf("[INFO] Error reading DNS SRV Record %q - removing from state", d.Id())
d.SetId("")
return fmt.Errorf("Error reading DNS SRV Record: %s", readResponse.Error)
}
resp := readResponse.Parsed.(*dns.GetSRVRecordSetResponse)
d.Set("ttl", resp.TTL)
if err := d.Set("record", flattenAzureRmDnsSrvRecord(resp.SRVRecords)); err != nil {
log.Printf("[INFO] Error setting the Azure RM SRV Record State: %s", err)
return err
}
flattenAndSetTags(d, &resp.Tags)
return nil
}
func resourceArmDnsSrvRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient)
rivieraClient := client.rivieraClient
deleteRequest := rivieraClient.NewRequestForURI(d.Id())
deleteRequest.Command = &dns.DeleteRecordSet{
RecordSetType: "SRV",
}
deleteResponse, err := deleteRequest.Execute()
if err != nil {
return fmt.Errorf("Error deleting DNS SRV Record: %s", err)
}
if !deleteResponse.IsSuccessful() {
return fmt.Errorf("Error deleting DNS SRV Record: %s", deleteResponse.Error)
}
return nil
}
func expandAzureRmDnsSrvRecord(d *schema.ResourceData) ([]dns.SRVRecord, error) {
config := d.Get("record").(*schema.Set).List()
records := make([]dns.SRVRecord, 0, len(config))
for _, pRaw := range config {
data := pRaw.(map[string]interface{})
srvRecord := dns.SRVRecord{
Priority: data["priority"].(int),
Weight: data["weight"].(int),
Port: data["port"].(int),
Target: data["target"].(string),
}
records = append(records, srvRecord)
}
return records, nil
}
func flattenAzureRmDnsSrvRecord(records []dns.SRVRecord) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(records))
for _, record := range records {
result = append(result, map[string]interface{}{
"priority": record.Priority,
"weight": record.Weight,
"port": record.Port,
"target": record.Target,
})
}
return result
}
func resourceArmDnsSrvRecordHash(v interface{}) int {
var buf bytes.Buffer
m := v.(map[string]interface{})
buf.WriteString(fmt.Sprintf("%d-", m["priority"].(int)))
buf.WriteString(fmt.Sprintf("%d-", m["weight"].(int)))
buf.WriteString(fmt.Sprintf("%d-", m["port"].(int)))
buf.WriteString(fmt.Sprintf("%s-", m["target"].(string)))
return hashcode.String(buf.String())
}

View File

@ -0,0 +1,285 @@
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 TestAccAzureRMDnsSrvRecord_basic(t *testing.T) {
ri := acctest.RandInt()
config := fmt.Sprintf(testAccAzureRMDnsSrvRecord_basic, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsSrvRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: config,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsSrvRecordExists("azurerm_dns_srv_record.test"),
),
},
},
})
}
func TestAccAzureRMDnsSrvRecord_updateRecords(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMDnsSrvRecord_basic, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMDnsSrvRecord_updateRecords, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsSrvRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsSrvRecordExists("azurerm_dns_srv_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_srv_record.test", "record.#", "2"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsSrvRecordExists("azurerm_dns_srv_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_srv_record.test", "record.#", "3"),
),
},
},
})
}
func TestAccAzureRMDnsSrvRecord_withTags(t *testing.T) {
ri := acctest.RandInt()
preConfig := fmt.Sprintf(testAccAzureRMDnsSrvRecord_withTags, ri, ri, ri)
postConfig := fmt.Sprintf(testAccAzureRMDnsSrvRecord_withTagsUpdate, ri, ri, ri)
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsSrvRecordDestroy,
Steps: []resource.TestStep{
resource.TestStep{
Config: preConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsSrvRecordExists("azurerm_dns_srv_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_srv_record.test", "tags.#", "2"),
),
},
resource.TestStep{
Config: postConfig,
Check: resource.ComposeTestCheckFunc(
testCheckAzureRMDnsSrvRecordExists("azurerm_dns_srv_record.test"),
resource.TestCheckResourceAttr(
"azurerm_dns_srv_record.test", "tags.#", "1"),
),
},
},
})
}
func testCheckAzureRMDnsSrvRecordExists(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.GetSRVRecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetSRVRecordSet: %s", err)
}
if !readResponse.IsSuccessful() {
return fmt.Errorf("Bad: GetSRVRecordSet: %s", readResponse.Error)
}
return nil
}
}
func testCheckAzureRMDnsSrvRecordDestroy(s *terraform.State) error {
conn := testAccProvider.Meta().(*ArmClient).rivieraClient
for _, rs := range s.RootModule().Resources {
if rs.Type != "azurerm_dns_srv_record" {
continue
}
readRequest := conn.NewRequestForURI(rs.Primary.ID)
readRequest.Command = &dns.GetSRVRecordSet{}
readResponse, err := readRequest.Execute()
if err != nil {
return fmt.Errorf("Bad: GetSRVRecordSet: %s", err)
}
if readResponse.IsSuccessful() {
return fmt.Errorf("Bad: DNS SRV Record still exists: %s", readResponse.Error)
}
}
return nil
}
var testAccAzureRMDnsSrvRecord_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_srv_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
priority = 1
weight = 5
port = 8080
target = "target1.contoso.com"
}
record {
priority = 2
weight = 25
port = 8080
target = "target2.contoso.com"
}
}
`
var testAccAzureRMDnsSrvRecord_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_srv_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
priority = 1
weight = 5
port = 8080
target = "target1.contoso.com"
}
record {
priority = 2
weight = 25
port = 8080
target = "target2.contoso.com"
}
record {
priority = 3
weight = 100
port = 8080
target = "target3.contoso.com"
}
}
`
var testAccAzureRMDnsSrvRecord_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_srv_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
priority = 1
weight = 5
port = 8080
target = "target1.contoso.com"
}
record {
priority = 2
weight = 25
port = 8080
target = "target2.contoso.com"
}
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`
var testAccAzureRMDnsSrvRecord_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_srv_record" "test" {
name = "myarecord%d"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_name = "${azurerm_dns_zone.test.name}"
ttl = "300"
record {
priority = 1
weight = 5
port = 8080
target = "target1.contoso.com"
}
record {
priority = 2
weight = 25
port = 8080
target = "target2.contoso.com"
}
tags {
environment = "staging"
}
}
`

View File

@ -3,12 +3,12 @@ package dns
import "github.com/jen20/riviera/azure"
type GetSOARecordSetResponse struct {
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
Location string `mapstructure:"location"`
Tags map[string]*string `mapstructure:"tags"`
TTL *int `mapstructure:"TTL"`
SOARecords []SOARecord `mapstructure:"SOARecords"`
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
Location string `mapstructure:"location"`
Tags map[string]*string `mapstructure:"tags"`
TTL *int `mapstructure:"TTL"`
SOARecord SOARecord `mapstructure:"SOARecord"`
}
type GetSOARecordSet struct {

View File

@ -11,32 +11,32 @@ type SOARecord struct {
RetryTime int `json:"retryTime" mapstructure:"retryTime"`
}
type CreateSOARecordSetResponse struct {
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
Location string `mapstructure:"location"`
Tags map[string]*string `mapstructure:"tags"`
TTL *int `mapstructure:"TTL"`
SOARecords []SOARecord `mapstructure:"SOARecords"`
type UpdateSOARecordSetResponse struct {
ID string `mapstructure:"id"`
Name string `mapstructure:"name"`
Location string `mapstructure:"location"`
Tags map[string]*string `mapstructure:"tags"`
TTL *int `mapstructure:"TTL"`
SOARecord SOARecord `mapstructure:"SOARecord"`
}
type CreateSOARecordSet struct {
type UpdateSOARecordSet struct {
Name string `json:"-"`
ResourceGroupName string `json:"-"`
ZoneName string `json:"-"`
Location string `json:"-" riviera:"location"`
Tags map[string]*string `json:"-" riviera:"tags"`
TTL int `json:"TTL"`
SOARecords []SOARecord `json:"SOARecords"`
SOARecord SOARecord `json:"SOARecord"`
}
func (command CreateSOARecordSet) APIInfo() azure.APIInfo {
func (command UpdateSOARecordSet) APIInfo() azure.APIInfo {
return azure.APIInfo{
APIVersion: apiVersion,
Method: "PUT",
Method: "PATCH",
URLPathFunc: dnsRecordSetDefaultURLPathFunc(command.ResourceGroupName, command.ZoneName, "SOA", command.Name),
ResponseTypeFunc: func() interface{} {
return &CreateSOARecordSetResponse{}
return &UpdateSOARecordSetResponse{}
},
}
}

View File

@ -0,0 +1,74 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_dns_srv_record"
sidebar_current: "docs-azurerm-resource-dns-srv-record"
description: |-
Manage a DNS SRV Record.
---
# azurerm\_dns\_srv\_record
Enables you to manage DNS SRV 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_srv_record" "test" {
name = "test"
zone_name = "${azurerm_dns_zone.test.name}"
resource_group_name = "${azurerm_resource_group.test.name}"
ttl = "300"
record {
priority = 1
weight = 5
port = 8080
target = "target1.contoso.com"
}
tags {
Environment = "Production"
}
}
```
## Argument Reference
The following arguments are supported:
* `name` - (Required) The name of the DNS SRV 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:
* `priority` - (Required) Priority of the SRV record.
* `weight` - (Required) Weight of the SRV record.
* `port` - (Required) Port the service is listening on.
* `target` - (Required) FQDN of the service.
## Attributes Reference
The following attributes are exported:
* `id` - The DNS SRV Record ID.

View File

@ -50,6 +50,10 @@
<a href="/docs/providers/azurerm/r/dns_ns_record.html">azurerm_dns_ns_record</a>
</li>
<li<%= sidebar_current("docs-azurerm-resource-dns-srv-record") %>>
<a href="/docs/providers/azurerm/r/dns_srv_record.html">azurerm_dns_srv_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>