Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Azure DNS CAA record support #1450

Merged
merged 8 commits into from
Jun 26, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion azurerm/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/Azure/azure-sdk-for-go/services/containerservice/mgmt/2017-09-30/containerservice"
"github.com/Azure/azure-sdk-for-go/services/cosmos-db/mgmt/2015-04-08/documentdb"
"github.com/Azure/azure-sdk-for-go/services/datalake/store/mgmt/2016-11-01/account"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2016-04-01/dns"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-10-01/dns"
"github.com/Azure/azure-sdk-for-go/services/eventgrid/mgmt/2018-01-01/eventgrid"
"github.com/Azure/azure-sdk-for-go/services/eventhub/mgmt/2017-04-01/eventhub"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
Expand Down
2 changes: 1 addition & 1 deletion azurerm/data_source_dns_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"

"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2016-04-01/dns"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-10-01/dns"
"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2017-05-10/resources"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
Expand Down
54 changes: 54 additions & 0 deletions azurerm/import_arm_dns_caa_record_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package azurerm

import (
"testing"

"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
)

func TestAccAzureRMDnsCaaRecord_importBasic(t *testing.T) {
resourceName := "azurerm_dns_caa_record.test"

ri := acctest.RandInt()
config := testAccAzureRMDnsCaaRecord_basic(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsCaaRecordDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccAzureRMDnsCaaRecord_importWithTags(t *testing.T) {
resourceName := "azurerm_dns_caa_record.test"

ri := acctest.RandInt()
config := testAccAzureRMDnsCaaRecord_withTags(ri, testLocation())

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testCheckAzureRMDnsCaaRecordDestroy,
Steps: []resource.TestStep{
{
Config: config,
},
{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
},
},
})
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_data_lake_store": resourceArmDataLakeStore(),
"azurerm_dns_a_record": resourceArmDnsARecord(),
"azurerm_dns_aaaa_record": resourceArmDnsAAAARecord(),
"azurerm_dns_caa_record": resourceArmDnsCaaRecord(),
"azurerm_dns_cname_record": resourceArmDnsCNameRecord(),
"azurerm_dns_mx_record": resourceArmDnsMxRecord(),
"azurerm_dns_ns_record": resourceArmDnsNsRecord(),
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_dns_a_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/http"

"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2016-04-01/dns"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-10-01/dns"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_dns_a_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"testing"

"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2016-04-01/dns"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-10-01/dns"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_dns_aaaa_record.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"fmt"
"net/http"

"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2016-04-01/dns"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-10-01/dns"
"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)
Expand Down
2 changes: 1 addition & 1 deletion azurerm/resource_arm_dns_aaaa_record_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import (
"net/http"
"testing"

"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2016-04-01/dns"
"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-10-01/dns"
"github.com/hashicorp/terraform/helper/acctest"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
Expand Down
223 changes: 223 additions & 0 deletions azurerm/resource_arm_dns_caa_record.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
package azurerm

import (
"bytes"
"fmt"
"net/http"

"github.com/Azure/azure-sdk-for-go/services/dns/mgmt/2017-10-01/dns"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/helper/validation"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func resourceArmDnsCaaRecord() *schema.Resource {
return &schema.Resource{
Create: resourceArmDnsCaaRecordCreateOrUpdate,
Read: resourceArmDnsCaaRecordRead,
Update: resourceArmDnsCaaRecordCreateOrUpdate,
Delete: resourceArmDnsCaaRecordDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},

"resource_group_name": resourceGroupNameSchema(),

"zone_name": {
Type: schema.TypeString,
Required: true,
},

"record": {
Type: schema.TypeSet,
Required: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"flags": {
Type: schema.TypeInt,
Required: true,
},

"tag": {
Type: schema.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
"issue",
"issuewild",
"iodef",
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
},

"value": {
Type: schema.TypeString,
Required: true,
},
},
},
Set: resourceArmDnsCaaRecordHash,
},

"ttl": {
Type: schema.TypeInt,
Required: true,
},

"tags": tagsSchema(),
},
}
}

func resourceArmDnsCaaRecordCreateOrUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).dnsClient
ctx := meta.(*ArmClient).StopContext

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
zoneName := d.Get("zone_name").(string)
ttl := int64(d.Get("ttl").(int))
tags := d.Get("tags").(map[string]interface{})

records, err := expandAzureRmDnsCaaRecords(d)
if err != nil {
return err
}

parameters := dns.RecordSet{
Name: &name,
RecordSetProperties: &dns.RecordSetProperties{
Metadata: expandTags(tags),
TTL: &ttl,
CaaRecords: &records,
},
}

eTag := ""
ifNoneMatch := "" // set to empty to allow updates to records after creation
resp, err := client.CreateOrUpdate(ctx, resGroup, zoneName, name, dns.CAA, parameters, eTag, ifNoneMatch)
if err != nil {
return err
}

if resp.ID == nil {
return fmt.Errorf("Cannot read DNS CAA Record %s (resource group %s) ID", name, resGroup)
}

d.SetId(*resp.ID)

return resourceArmDnsCaaRecordRead(d, meta)
}

func resourceArmDnsCaaRecordRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).dnsClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

resGroup := id.ResourceGroup
name := id.Path["CAA"]
zoneName := id.Path["dnszones"]

resp, err := client.Get(ctx, resGroup, zoneName, name, dns.CAA)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading DNS CAA record %s: %v", name, err)
}

d.Set("name", name)
d.Set("resource_group_name", resGroup)
d.Set("zone_name", zoneName)
d.Set("ttl", resp.TTL)

if err := d.Set("record", flattenAzureRmDnsCaaRecords(resp.CaaRecords)); err != nil {
return err
}
flattenAndSetTags(d, resp.Metadata)

return nil
}

func resourceArmDnsCaaRecordDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).dnsClient
ctx := meta.(*ArmClient).StopContext

id, err := parseAzureResourceID(d.Id())
if err != nil {
return err
}

resGroup := id.ResourceGroup
name := id.Path["CAA"]
zoneName := id.Path["dnszones"]

resp, error := client.Delete(ctx, resGroup, zoneName, name, dns.CAA, "")
if resp.StatusCode != http.StatusOK {
return fmt.Errorf("Error deleting DNS CAA Record %s: %+v", name, error)
}

return nil
}

func flattenAzureRmDnsCaaRecords(records *[]dns.CaaRecord) []map[string]interface{} {
results := make([]map[string]interface{}, 0, len(*records))

if records != nil {
for _, record := range *records {
results = append(results, map[string]interface{}{
"flags": *record.Flags,
"tag": *record.Tag,
"value": *record.Value,
})
}
}

return results
}

func expandAzureRmDnsCaaRecords(d *schema.ResourceData) ([]dns.CaaRecord, error) {
recordStrings := d.Get("record").(*schema.Set).List()
records := make([]dns.CaaRecord, len(recordStrings))

for i, v := range recordStrings {
record := v.(map[string]interface{})
flags := int32(record["flags"].(int))
tag := record["tag"].(string)
value := record["value"].(string)

caaRecord := dns.CaaRecord{
Flags: &flags,
Tag: &tag,
Value: &value,
}

records[i] = caaRecord
}

return records, nil
}

func resourceArmDnsCaaRecordHash(v interface{}) int {
var buf bytes.Buffer

if m, ok := v.(map[string]interface{}); ok {
buf.WriteString(fmt.Sprintf("%d-", m["flags"].(int)))
buf.WriteString(fmt.Sprintf("%s-", m["tag"].(string)))
buf.WriteString(fmt.Sprintf("%s-", m["value"].(string)))
}

return hashcode.String(buf.String())
}
Loading