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

New Data Source: azurerm_dns_zone #702

Merged
merged 2 commits into from
Jan 16, 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
81 changes: 81 additions & 0 deletions azurerm/data_source_dns_zone.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package azurerm

import (
"fmt"

"github.com/hashicorp/terraform/helper/schema"
"github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils"
)

func dataSourceArmDnsZone() *schema.Resource {
return &schema.Resource{
Read: dataSourceArmDnsZoneRead,

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

"resource_group_name": resourceGroupNameForDataSourceSchema(),

"number_of_record_sets": {
Type: schema.TypeString,
Computed: true,
},

"max_number_of_record_sets": {
Type: schema.TypeString,
Computed: true,
},

"name_servers": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},

"tags": tagsForDataSourceSchema(),
},
}
}

func dataSourceArmDnsZoneRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*ArmClient).zonesClient

name := d.Get("name").(string)
resourceGroup := d.Get("resource_group_name").(string)

resp, err := client.Get(resourceGroup, name)
if err != nil {
if utils.ResponseWasNotFound(resp.Response) {
d.SetId("")
return nil
}
return fmt.Errorf("Error reading DNS Zone %q (Resource Group %q): %+v", name, resourceGroup, err)
}

d.SetId(*resp.ID)
d.Set("name", name)
d.Set("resource_group_name", resourceGroup)

if props := resp.ZoneProperties; props != nil {
d.Set("number_of_record_sets", props.NumberOfRecordSets)
d.Set("max_number_of_record_sets", props.MaxNumberOfRecordSets)

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

flattenAndSetTags(d, resp.Tags)

return nil
}
89 changes: 89 additions & 0 deletions azurerm/data_source_dns_zone_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package azurerm

import (
"fmt"
"testing"

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

func TestAccDataSourceAzureRMDNSZone_basic(t *testing.T) {
dataSourceName := "data.azurerm_dns_zone.test"
rInt := acctest.RandInt()
location := testLocation()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceDNSZone_basic(rInt, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"),
),
},
},
})
}

func TestAccDataSourceAzureRMDNSZone_tags(t *testing.T) {
dataSourceName := "data.azurerm_dns_zone.test"
rInt := acctest.RandInt()
location := testLocation()

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
Config: testAccDataSourceDNSZone_tags(rInt, location),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"),
resource.TestCheckResourceAttr(dataSourceName, "tags.hello", "world"),
),
},
},
})
}

func testAccDataSourceDNSZone_basic(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG_%d"
location = "%s"
}

resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
}

data "azurerm_dns_zone" "test" {
name = "${azurerm_dns_zone.test.name}"
resource_group_name = "${azurerm_dns_zone.test.resource_group_name}"
}
`, rInt, location, rInt)
}

func testAccDataSourceDNSZone_tags(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG_%d"
location = "%s"
}

resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
hello = "world"
}
}

data "azurerm_dns_zone" "test" {
name = "${azurerm_dns_zone.test.name}"
resource_group_name = "${azurerm_dns_zone.test.resource_group_name}"
}
`, rInt, location, rInt)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func Provider() terraform.ResourceProvider {
"azurerm_app_service_plan": dataSourceAppServicePlan(),
"azurerm_builtin_role_definition": dataSourceArmBuiltInRoleDefinition(),
"azurerm_client_config": dataSourceArmClientConfig(),
"azurerm_dns_zone": dataSourceArmDnsZone(),
"azurerm_eventhub_namespace": dataSourceEventHubNamespace(),
"azurerm_image": dataSourceArmImage(),
"azurerm_key_vault_access_policy": dataSourceArmKeyVaultAccessPolicy(),
Expand Down
10 changes: 5 additions & 5 deletions azurerm/resource_arm_dns_zone_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ func TestAccAzureRMDnsZone_withTags(t *testing.T) {
ri := acctest.RandInt()
location := testLocation()
preConfig := testAccAzureRMDnsZone_withTags(ri, location)
postConfig := testAccAzureRMDnsZone_withTagsUupdate(ri, location)
postConfig := testAccAzureRMDnsZone_withTagsUpdate(ri, location)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Expand Down Expand Up @@ -139,14 +139,14 @@ resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
environment = "Production"
cost_center = "MSFT"
environment = "Production"
cost_center = "MSFT"
}
}
`, rInt, location, rInt)
}

func testAccAzureRMDnsZone_withTagsUupdate(rInt int, location string) string {
func testAccAzureRMDnsZone_withTagsUpdate(rInt int, location string) string {
return fmt.Sprintf(`
resource "azurerm_resource_group" "test" {
name = "acctestRG_%d"
Expand All @@ -157,7 +157,7 @@ resource "azurerm_dns_zone" "test" {
name = "acctestzone%d.com"
resource_group_name = "${azurerm_resource_group.test.name}"
tags {
environment = "staging"
environment = "staging"
}
}
`, rInt, location, rInt)
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
<a href="/docs/providers/azurerm/d/client_config.html">azurerm_client_config</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-dns-zone") %>>
<a href="/docs/providers/azurerm/d/dns_zone.html">azurerm_dns_zone</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-eventhub-namespace") %>>
<a href="/docs/providers/azurerm/d/eventhub_namespace.html">azurerm_eventhub_namespace</a>
</li>
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/dns_zone.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
layout: "azurerm"
page_title: "Azure Resource Manager: azurerm_dns_zone"
sidebar_current: "docs-azurerm-datasource-dns-zone"
description: |-
Get information about a DNS Zone.

---

# Data Source: azurerm_dns_zone

Use this data source to obtain information about a DNS Zone.

## Example Usage

```hcl
data "azurerm_dns_zone" "test" {
name = "search-eventhubns"
resource_group_name = "search-service"
}

output "dns_zone_id" {
value = "${data.azurerm_dns_zone.test.id}"
}
```

## Argument Reference

* `name` - (Required) The name of the DNS Zone.
* `resource_group_name` - (Required) The Name of the Resource Group where the DNS Zone exists.

## Attributes Reference

* `id` - The ID of the DNS Zone.

* `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.
* `tags` - A mapping of tags to assign to the EventHub Namespace.