-
Notifications
You must be signed in to change notification settings - Fork 4.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #702 from terraform-providers/dns-zone-data-source
New Data Source: `azurerm_dns_zone`
- Loading branch information
Showing
6 changed files
with
219 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |