diff --git a/azurerm/data_source_dns_zone.go b/azurerm/data_source_dns_zone.go new file mode 100644 index 000000000000..0e8497c2cf72 --- /dev/null +++ b/azurerm/data_source_dns_zone.go @@ -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 +} diff --git a/azurerm/data_source_dns_zone_test.go b/azurerm/data_source_dns_zone_test.go new file mode 100644 index 000000000000..e62696a79e00 --- /dev/null +++ b/azurerm/data_source_dns_zone_test.go @@ -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) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 6c4b88abea7a..855f562c439e 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -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(), diff --git a/azurerm/resource_arm_dns_zone_test.go b/azurerm/resource_arm_dns_zone_test.go index 3b2b4f75d2d9..2f27d690e1a1 100644 --- a/azurerm/resource_arm_dns_zone_test.go +++ b/azurerm/resource_arm_dns_zone_test.go @@ -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) }, @@ -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" @@ -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) diff --git a/website/azurerm.erb b/website/azurerm.erb index 2f028d3cd309..6006a5544f46 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -35,6 +35,10 @@ azurerm_client_config +