From 8c27990574568370ea3936bb0eec1d420edb92ee Mon Sep 17 00:00:00 2001 From: Stijn Tintel Date: Tue, 5 Jun 2018 19:14:15 +0300 Subject: [PATCH] Privat dns zone support. resolved https://github.com/terraform-providers/terraform-provider-azurerm/issues/793. --- azurerm/data_source_dns_zone.go | 44 +++++++++++++++ azurerm/import_arm_dns_zone_test.go | 23 ++++++++ azurerm/resource_arm_dns_zone.go | 79 +++++++++++++++++++++++++-- azurerm/resource_arm_dns_zone_test.go | 44 +++++++++++++++ 4 files changed, 184 insertions(+), 6 deletions(-) diff --git a/azurerm/data_source_dns_zone.go b/azurerm/data_source_dns_zone.go index 9ebe25686c655..05581e5fbcebd 100644 --- a/azurerm/data_source_dns_zone.go +++ b/azurerm/data_source_dns_zone.go @@ -43,6 +43,29 @@ func dataSourceArmDnsZone() *schema.Resource { Set: schema.HashString, }, + "zone_type": { + Type: schema.TypeString, + Default: nil, + Optional: true, + Computed: true, + }, + + "registration_virtual_networks": { + Type: schema.TypeList, + Default: nil, + Optional: true, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + + "resolution_virtual_networks": { + Type: schema.TypeList, + Default: nil, + Optional: true, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "tags": tagsForDataSourceSchema(), }, } @@ -87,6 +110,27 @@ func dataSourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error { if props := resp.ZoneProperties; props != nil { d.Set("number_of_record_sets", props.NumberOfRecordSets) d.Set("max_number_of_record_sets", props.MaxNumberOfRecordSets) + d.Set("zone_type", props.ZoneType) + + if rvns := props.RegistrationVirtualNetworks; rvns != nil { + registrationVNets := make([]string, 0, len(*rvns)) + for _, rvn := range *rvns { + registrationVNets = append(registrationVNets, *rvn.ID) + } + if err := d.Set("registration_virtual_networks", registrationVNets); err != nil { + return err + } + } + + if rvns := props.ResolutionVirtualNetworks; rvns != nil { + resolutionVNets := make([]string, 0, len(*rvns)) + for _, rvn := range *rvns { + resolutionVNets = append(resolutionVNets, *rvn.ID) + } + if err := d.Set("resolution_virtual_networks", resolutionVNets); err != nil { + return err + } + } if ns := props.NameServers; ns != nil { nameServers := make([]string, 0, len(*ns)) diff --git a/azurerm/import_arm_dns_zone_test.go b/azurerm/import_arm_dns_zone_test.go index c5e2a8ad0e4f3..58b6a943c94d7 100644 --- a/azurerm/import_arm_dns_zone_test.go +++ b/azurerm/import_arm_dns_zone_test.go @@ -30,6 +30,29 @@ func TestAccAzureRMDnsZone_importBasic(t *testing.T) { }) } +func TestAccAzureRMDnsZone_importBasicWithVNets(t *testing.T) { + resourceName := "azurerm_dns_zone.test" + + ri := acctest.RandInt() + config := testAccAzureRMDnsZone_withVNets(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMDnsZoneDestroy, + Steps: []resource.TestStep{ + { + Config: config, + }, + { + ResourceName: resourceName, + ImportState: true, + ImportStateVerify: true, + }, + }, + }) +} + func TestAccAzureRMDnsZone_importBasicWithTags(t *testing.T) { resourceName := "azurerm_dns_zone.test" diff --git a/azurerm/resource_arm_dns_zone.go b/azurerm/resource_arm_dns_zone.go index 3600c3233a2ef..a83c7969c9c9a 100644 --- a/azurerm/resource_arm_dns_zone.go +++ b/azurerm/resource_arm_dns_zone.go @@ -45,6 +45,29 @@ func resourceArmDnsZone() *schema.Resource { Set: schema.HashString, }, + "zone_type": { + Type: schema.TypeString, + Default: nil, + Optional: true, + Computed: true, + }, + + "registration_virtual_networks": { + Type: schema.TypeList, + Default: nil, + Optional: true, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + + "resolution_virtual_networks": { + Type: schema.TypeList, + Default: nil, + Optional: true, + Computed: true, + Elem: &schema.Schema{Type: schema.TypeString}, + }, + "tags": tagsSchema(), }, } @@ -57,12 +80,33 @@ func resourceArmDnsZoneCreate(d *schema.ResourceData, meta interface{}) error { name := d.Get("name").(string) resGroup := d.Get("resource_group_name").(string) location := "global" + zone_type := d.Get("zone_type").(string) tags := d.Get("tags").(map[string]interface{}) - + registrationVirtualNetworks := d.Get("registration_virtual_networks").([]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_networks").([]interface{}) + resolutionVNetSubResources := make([]dns.SubResource, 0, len(resolutionVirtualNetworks)) + for _, rvn := range resolutionVirtualNetworks { + id := rvn.(string) + resolutionVNetSubResources = append(resolutionVNetSubResources, dns.SubResource{ + ID: &id, + }) + } parameters := dns.Zone{ Location: &location, Tags: expandTags(tags), + ZoneProperties: &dns.ZoneProperties{ + ZoneType: dns.ZoneType(zone_type), + RegistrationVirtualNetworks: ®istrationVNetSubResources, + ResolutionVirtualNetworks: &resolutionVNetSubResources, + }, } etag := "" @@ -106,13 +150,36 @@ func resourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error { d.Set("resource_group_name", resGroup) d.Set("number_of_record_sets", resp.NumberOfRecordSets) d.Set("max_number_of_record_sets", resp.MaxNumberOfRecordSets) + d.Set("zone_type", resp.ZoneType) - nameServers := make([]string, 0, len(*resp.NameServers)) - for _, ns := range *resp.NameServers { - nameServers = append(nameServers, ns) + 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_networks", registrationVirtualNetworks); err != nil { + return err + } } - if err := d.Set("name_servers", nameServers); 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_networks", 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 + } } flattenAndSetTags(d, resp.Tags) diff --git a/azurerm/resource_arm_dns_zone_test.go b/azurerm/resource_arm_dns_zone_test.go index eed88bc3b0bb8..104a53fc5387c 100644 --- a/azurerm/resource_arm_dns_zone_test.go +++ b/azurerm/resource_arm_dns_zone_test.go @@ -30,6 +30,26 @@ func TestAccAzureRMDnsZone_basic(t *testing.T) { }) } +func TestAccAzureRMDnsZone_withVNets(t *testing.T) { + resourceName := "azurerm_dns_zone.test" + ri := acctest.RandInt() + config := testAccAzureRMDnsZone_withVNets(ri, testLocation()) + + resource.Test(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + CheckDestroy: testCheckAzureRMDnsZoneDestroy, + Steps: []resource.TestStep{ + { + Config: config, + Check: resource.ComposeTestCheckFunc( + testCheckAzureRMDnsZoneExists(resourceName), + ), + }, + }, + }) +} + func TestAccAzureRMDnsZone_withTags(t *testing.T) { resourceName := "azurerm_dns_zone.test" ri := acctest.RandInt() @@ -130,6 +150,30 @@ resource "azurerm_dns_zone" "test" { `, rInt, location, rInt) } +func testAccAzureRMDnsZone_withVNets(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + 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"] + } + +resource "azurerm_dns_zone" "test" { + name = "acctestzone%d.com" + resource_group_name = "${azurerm_resource_group.test.name}" + zone_type = "Private" + registration_virtual_networks = ["${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" {