From d4e68def15d0e8db5fb67e7c23f9d246760067ab Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Mon, 2 Jul 2018 10:16:18 +0200 Subject: [PATCH 1/3] Refactoring from PR comments: - Switching to use flatten/expand functions - Making the Resource fields consistent in the docs - Documenting the new fields in the Data Source --- azurerm/resource_arm_dns_zone.go | 139 +++++++++++++++----------- website/docs/d/dns_zone.html.markdown | 3 + website/docs/r/dns_zone.html.markdown | 7 +- 3 files changed, 88 insertions(+), 61 deletions(-) diff --git a/azurerm/resource_arm_dns_zone.go b/azurerm/resource_arm_dns_zone.go index 4338d7a10f88..14e2a2970a27 100644 --- a/azurerm/resource_arm_dns_zone.go +++ b/azurerm/resource_arm_dns_zone.go @@ -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" @@ -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, @@ -75,16 +74,7 @@ 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 @@ -92,32 +82,18 @@ func resourceArmDnsZoneCreate(d *schema.ResourceData, meta interface{}) error { 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: ®istrationVNetSubResources, - ResolutionVirtualNetworks: &resolutionVNetSubResources, + ZoneType: dns.ZoneType(zoneType), + RegistrationVirtualNetworks: registrationVirtualNetworkIds, + ResolutionVirtualNetworks: resolutionVirtualNetworkIds, }, } @@ -164,34 +140,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) @@ -230,3 +191,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 ®istrationVNetSubResources +} + +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 +} diff --git a/website/docs/d/dns_zone.html.markdown b/website/docs/d/dns_zone.html.markdown index 02f097d3b1ca..fee64f51e02c 100644 --- a/website/docs/d/dns_zone.html.markdown +++ b/website/docs/d/dns_zone.html.markdown @@ -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. diff --git a/website/docs/r/dns_zone.html.markdown b/website/docs/r/dns_zone.html.markdown index 57e96205df5e..baa2d357d913 100644 --- a/website/docs/r/dns_zone.html.markdown +++ b/website/docs/r/dns_zone.html.markdown @@ -29,7 +29,6 @@ resource "azurerm_dns_zone" "test_private" { resource_group_name = "${azurerm_resource_group.test.name}" zone_type = "Private" } - ``` ## Argument Reference @@ -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. From 16166938c61255f5671aa7e4a8824a25ea688558 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Mon, 2 Jul 2018 10:17:56 +0200 Subject: [PATCH 2/3] Making the spacing of the DNS tests consistent --- azurerm/resource_arm_dns_zone_test.go | 58 +++++++++++++-------------- 1 file changed, 29 insertions(+), 29 deletions(-) diff --git a/azurerm/resource_arm_dns_zone_test.go b/azurerm/resource_arm_dns_zone_test.go index b1dc811a11a1..60322b7fefb7 100644 --- a/azurerm/resource_arm_dns_zone_test.go +++ b/azurerm/resource_arm_dns_zone_test.go @@ -139,13 +139,13 @@ 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) } @@ -153,23 +153,23 @@ resource "azurerm_dns_zone" "test" { 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) } @@ -177,17 +177,17 @@ resource "azurerm_dns_zone" "test" { 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) } @@ -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) } From f5a8e19e764221c94a82342a63c4c65fd718f5a8 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Mon, 2 Jul 2018 11:44:52 +0200 Subject: [PATCH 3/3] Making the Zone Type case sensitive --- azurerm/resource_arm_dns_zone.go | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/azurerm/resource_arm_dns_zone.go b/azurerm/resource_arm_dns_zone.go index 14e2a2970a27..c0d4bc011b55 100644 --- a/azurerm/resource_arm_dns_zone.go +++ b/azurerm/resource_arm_dns_zone.go @@ -53,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": {