Skip to content

Commit

Permalink
privat dns zone support.
Browse files Browse the repository at this point in the history
  • Loading branch information
andyliuliming committed Jun 17, 2018
1 parent 68305e1 commit 196082d
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 8 deletions.
38 changes: 38 additions & 0 deletions azurerm/data_source_dns_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,25 @@ func dataSourceArmDnsZone() *schema.Resource {

"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(),
Expand Down Expand Up @@ -94,6 +112,26 @@ func dataSourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
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))
for _, ns := range *ns {
Expand Down
23 changes: 23 additions & 0 deletions azurerm/import_arm_dns_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
72 changes: 64 additions & 8 deletions azurerm/resource_arm_dns_zone.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,25 @@ func resourceArmDnsZone() *schema.Resource {

"zone_type": {
Type: schema.TypeString,
Default: "Public",
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(),
Expand All @@ -66,12 +83,29 @@ func resourceArmDnsZoneCreate(d *schema.ResourceData, meta interface{}) error {
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),
ZoneType: dns.ZoneType(zone_type),
RegistrationVirtualNetworks: &registrationVNetSubResources,
ResolutionVirtualNetworks: &resolutionVNetSubResources,
},
}

Expand Down Expand Up @@ -118,12 +152,34 @@ func resourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
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)
Expand Down
44 changes: 44 additions & 0 deletions azurerm/resource_arm_dns_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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)
}

func testAccAzureRMDnsZone_withTags(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
Expand Down

0 comments on commit 196082d

Please sign in to comment.