Skip to content

Commit

Permalink
Merge pull request #1 from terraform-providers/andliu-private_dns_zones
Browse files Browse the repository at this point in the history
Changes from hashicorp#1404
  • Loading branch information
andyliuliming authored Jul 2, 2018
2 parents db23bf4 + f5a8e19 commit 6b89d9e
Show file tree
Hide file tree
Showing 4 changed files with 118 additions and 92 deletions.
142 changes: 83 additions & 59 deletions azurerm/resource_arm_dns_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package azurerm

import (
"fmt"
"strings"

"github.com/Azure/azure-sdk-for-go/services/preview/dns/mgmt/2018-03-01-preview/dns"
"github.com/hashicorp/terraform/helper/schema"
Expand All @@ -13,9 +12,9 @@ import (

func resourceArmDnsZone() *schema.Resource {
return &schema.Resource{
Create: resourceArmDnsZoneCreate,
Create: resourceArmDnsZoneCreateUpdate,
Read: resourceArmDnsZoneRead,
Update: resourceArmDnsZoneCreate,
Update: resourceArmDnsZoneCreateUpdate,
Delete: resourceArmDnsZoneDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
Expand Down Expand Up @@ -54,8 +53,7 @@ func resourceArmDnsZone() *schema.Resource {
ValidateFunc: validation.StringInSlice([]string{
string(dns.Private),
string(dns.Public),
}, true),
DiffSuppressFunc: ignoreCaseDiffSuppressFunc,
}, false),
},

"registration_virtual_network_ids": {
Expand All @@ -75,49 +73,26 @@ func resourceArmDnsZone() *schema.Resource {
}
}

func convertZoneType(zoneType string) dns.ZoneType {
for _, zType := range dns.PossibleZoneTypeValues() {
if strings.EqualFold(string(zType), zoneType) {
return zType
}
}
return dns.Public
}

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

name := d.Get("name").(string)
resGroup := d.Get("resource_group_name").(string)
location := "global"
zoneType := d.Get("zone_type").(string)

tags := d.Get("tags").(map[string]interface{})
registrationVirtualNetworks := d.Get("registration_virtual_network_ids").([]interface{})
registrationVNetSubResources := make([]dns.SubResource, 0, len(registrationVirtualNetworks))
for _, rvn := range registrationVirtualNetworks {
id := rvn.(string)
registrationVNetSubResources = append(registrationVNetSubResources, dns.SubResource{
ID: &id,
})
}
resolutionVirtualNetworks := d.Get("resolution_virtual_network_ids").([]interface{})
resolutionVNetSubResources := make([]dns.SubResource, 0, len(resolutionVirtualNetworks))
for _, rvn := range resolutionVirtualNetworks {
id := rvn.(string)
resolutionVNetSubResources = append(resolutionVNetSubResources, dns.SubResource{
ID: &id,
})
}

registrationVirtualNetworkIds := expandDnsZoneRegistrationVirtualNetworkIds(d)
resolutionVirtualNetworkIds := expandDnsZoneResolutionVirtualNetworkIds(d)

parameters := dns.Zone{
Location: &location,
Tags: expandTags(tags),
ZoneProperties: &dns.ZoneProperties{
ZoneType: convertZoneType(zoneType),
RegistrationVirtualNetworks: &registrationVNetSubResources,
ResolutionVirtualNetworks: &resolutionVNetSubResources,
ZoneType: dns.ZoneType(zoneType),
RegistrationVirtualNetworks: registrationVirtualNetworkIds,
ResolutionVirtualNetworks: resolutionVirtualNetworkIds,
},
}

Expand Down Expand Up @@ -164,34 +139,19 @@ func resourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
d.Set("max_number_of_record_sets", resp.MaxNumberOfRecordSets)
d.Set("zone_type", resp.ZoneType)

if resp.RegistrationVirtualNetworks != nil {
registrationVirtualNetworks := make([]string, 0, len(*resp.RegistrationVirtualNetworks))
for _, rvn := range *resp.RegistrationVirtualNetworks {
registrationVirtualNetworks = append(registrationVirtualNetworks, *rvn.ID)
}
if err := d.Set("registration_virtual_network_ids", registrationVirtualNetworks); err != nil {
return err
}
registrationVirtualNetworks := flattenDnsZoneRegistrationVirtualNetworkIDs(resp.RegistrationVirtualNetworks)
if err := d.Set("registration_virtual_network_ids", registrationVirtualNetworks); err != nil {
return err
}

if resp.ResolutionVirtualNetworks != nil {
resolutionVirtualNetworks := make([]string, 0, len(*resp.ResolutionVirtualNetworks))
for _, rvn := range *resp.ResolutionVirtualNetworks {
resolutionVirtualNetworks = append(resolutionVirtualNetworks, *rvn.ID)
}
if err := d.Set("resolution_virtual_network_ids", resolutionVirtualNetworks); err != nil {
return err
}
resolutionVirtualNetworks := flattenDnsZoneResolutionVirtualNetworkIDs(resp.ResolutionVirtualNetworks)
if err := d.Set("resolution_virtual_network_ids", resolutionVirtualNetworks); err != nil {
return err
}

if resp.NameServers != nil {
nameServers := make([]string, 0, len(*resp.NameServers))
for _, ns := range *resp.NameServers {
nameServers = append(nameServers, ns)
}
if err := d.Set("name_servers", nameServers); err != nil {
return err
}
nameServers := flattenDnsZoneNameservers(resp.NameServers)
if err := d.Set("name_servers", nameServers); err != nil {
return err
}

flattenAndSetTags(d, resp.Tags)
Expand Down Expand Up @@ -230,3 +190,67 @@ func resourceArmDnsZoneDelete(d *schema.ResourceData, meta interface{}) error {

return nil
}

func expandDnsZoneResolutionVirtualNetworkIds(d *schema.ResourceData) *[]dns.SubResource {
resolutionVirtualNetworks := d.Get("resolution_virtual_network_ids").([]interface{})

resolutionVNetSubResources := make([]dns.SubResource, 0, len(resolutionVirtualNetworks))
for _, rvn := range resolutionVirtualNetworks {
id := rvn.(string)
resolutionVNetSubResources = append(resolutionVNetSubResources, dns.SubResource{
ID: &id,
})
}

return &resolutionVNetSubResources
}

func flattenDnsZoneRegistrationVirtualNetworkIDs(input *[]dns.SubResource) []string {
registrationVirtualNetworks := make([]string, 0)

if input != nil {
for _, rvn := range *input {
registrationVirtualNetworks = append(registrationVirtualNetworks, *rvn.ID)
}
}

return registrationVirtualNetworks
}

func expandDnsZoneRegistrationVirtualNetworkIds(d *schema.ResourceData) *[]dns.SubResource {
registrationVirtualNetworks := d.Get("registration_virtual_network_ids").([]interface{})

registrationVNetSubResources := make([]dns.SubResource, 0)
for _, rvn := range registrationVirtualNetworks {
id := rvn.(string)
registrationVNetSubResources = append(registrationVNetSubResources, dns.SubResource{
ID: &id,
})
}

return &registrationVNetSubResources
}

func flattenDnsZoneResolutionVirtualNetworkIDs(input *[]dns.SubResource) []string {
resolutionVirtualNetworks := make([]string, 0)

if input != nil {
for _, rvn := range *input {
resolutionVirtualNetworks = append(resolutionVirtualNetworks, *rvn.ID)
}
}

return resolutionVirtualNetworks
}

func flattenDnsZoneNameservers(input *[]string) []string {
nameServers := make([]string, 0)

if input != nil {
for _, ns := range *input {
nameServers = append(nameServers, ns)
}
}

return nameServers
}
58 changes: 29 additions & 29 deletions azurerm/resource_arm_dns_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,55 +139,55 @@ func testCheckAzureRMDnsZoneDestroy(s *terraform.State) error {
func testAccAzureRMDnsZone_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
}
`, rInt, location, rInt)
}

func testAccAzureRMDnsZone_withVNets(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG_%d"
location = "%s"
name = "acctestRG_%d"
location = "%s"
}
resource "azurerm_virtual_network" "test" {
name = "acctestvnet%d"
location = "%s"
resource_group_name = "${azurerm_resource_group.test.name}"
address_space = ["10.0.0.0/16"]
dns_servers = ["168.63.129.16"]
}
name = "acctestvnet%d"
location = "%s"
resource_group_name = "${azurerm_resource_group.test.name}"
address_space = ["10.0.0.0/16"]
dns_servers = ["168.63.129.16"]
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_type = "Private"
registration_virtual_network_ids = ["${azurerm_virtual_network.test.id}"]
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
zone_type = "Private"
registration_virtual_network_ids = ["${azurerm_virtual_network.test.id}"]
}
`, rInt, location, rInt, location, rInt)
}

func testAccAzureRMDnsZone_withTags(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
environment = "Production"
cost_center = "MSFT"
}
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
environment = "Production"
cost_center = "MSFT"
}
}
`, rInt, location, rInt)
}
Expand All @@ -200,11 +200,11 @@ resource "azurerm_resource_group" "test" {
}
resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
environment = "staging"
}
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
environment = "staging"
}
}
`, rInt, location, rInt)
}
3 changes: 3 additions & 0 deletions website/docs/d/dns_zone.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,7 @@ in your subscription that matches `name` will be returned.
* `max_number_of_record_sets` - Maximum number of Records in the zone.
* `number_of_record_sets` - The number of records already in the zone.
* `name_servers` - A list of values that make up the NS record for the zone.
* `zone_type` - The type of this DNS zone, such as `Public` or `Private`.
* `registration_virtual_network_ids` - A list of Virtual Network ID's that register hostnames in this DNS zone.
* `resolution_virtual_network_ids` - A list of Virtual Network ID's that resolve records in this DNS zone.
* `tags` - A mapping of tags to assign to the EventHub Namespace.
7 changes: 3 additions & 4 deletions website/docs/r/dns_zone.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ resource "azurerm_dns_zone" "test_private" {
resource_group_name = "${azurerm_resource_group.test.name}"
zone_type = "Private"
}
```
## Argument Reference

Expand All @@ -39,11 +38,11 @@ The following arguments are supported:

* `resource_group_name` - (Required) Specifies the resource group where the resource exists. Changing this forces a new resource to be created.

* `zone_type` - (Required) Specifies the type of this DNS zone (Public or Private).
* `zone_type` - (Required) Specifies the type of this DNS zone. Possible values are `Public` or `Private` (Defaults to `Public`).

* `registration_virtual_network_ids` - (Optional) A list of IDs references to virtual networks that register hostnames in this DNS zone. This take effects when ZoneType is Private.
* `registration_virtual_network_ids` - (Optional) A list of Virtual Network ID's that register hostnames in this DNS zone. This field can only be set when `zone_type` is set to `Private`.

* `resolution_virtual_network_ids` - (Optional) A list of IDs references to virtual networks that resolve records in this DNS zone. This take effects when ZoneType is Private.
* `resolution_virtual_network_ids` - (Optional) A list of Virtual Network ID's that resolve records in this DNS zone. This field can only be set when `zone_type` is set to `Private`.

* `tags` - (Optional) A mapping of tags to assign to the resource.

Expand Down

0 comments on commit 6b89d9e

Please sign in to comment.