From 554dc2c2e22be04d3ddf94c1a20807492f808e6a Mon Sep 17 00:00:00 2001 From: Matt Mencel Date: Thu, 7 Feb 2019 14:23:36 -0600 Subject: [PATCH 1/6] add availability set data source corrections function rename and provider --- azurerm/data_source_availability_set.go | 74 ++++++++++++++++++++ azurerm/data_source_availability_set_test.go | 56 +++++++++++++++ azurerm/provider.go | 1 + 3 files changed, 131 insertions(+) create mode 100644 azurerm/data_source_availability_set.go create mode 100644 azurerm/data_source_availability_set_test.go diff --git a/azurerm/data_source_availability_set.go b/azurerm/data_source_availability_set.go new file mode 100644 index 000000000000..45d3efba5883 --- /dev/null +++ b/azurerm/data_source_availability_set.go @@ -0,0 +1,74 @@ +package azurerm + +import ( + "fmt" + + "github.com/hashicorp/terraform/helper/schema" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/utils" +) + +func dataSourceArmAvailabilitySet() *schema.Resource { + return &schema.Resource{ + Read: dataSourceArmAvailabilitySetRead, + Schema: map[string]*schema.Schema{ + "resource_group_name": resourceGroupNameForDataSourceSchema(), + + "name": { + Type: schema.TypeString, + Required: true, + ValidateFunc: validate.NoEmptyStrings, + }, + + "location": { + Type: schema.TypeString, + Computed: true, + }, + + "platform_update_domain_count": { + Type: schema.TypeString, + Computed: true, + }, + + "platform_fault_domain_count": { + Type: schema.TypeString, + Computed: true, + }, + + "managed": { + Type: schema.TypeString, + Computed: true, + }, + + "tags": { + Type: schema.TypeMap, + Computed: true, + }, + }, + } +} + +func dataSourceArmAvailabilitySetRead(d *schema.ResourceData, meta interface{}) error { + client := meta.(*ArmClient).availSetClient + ctx := meta.(*ArmClient).StopContext + + resGroup := d.Get("resource_group_name").(string) + name := d.Get("name").(string) + + resp, err := client.Get(ctx, resGroup, name) + if err != nil { + if utils.ResponseWasNotFound(resp.Response) { + return fmt.Errorf("Error: Availability Set %q (Resource Group %q) was not found", name, resGroup) + } + + return fmt.Errorf("Error making Read request on Availability Set %q (Resource Group %q): %+v", name, resGroup, err) + } + + d.SetId(*resp.ID) + d.Set("location", resp.Location) + d.Set("platform_update_domain_count", resp.PlatformUpdateDomainCount) + d.Set("platform_fault_domain_count", resp.PlatformFaultDomainCount) + flattenAndSetTags(d, resp.Tags) + + return nil +} diff --git a/azurerm/data_source_availability_set_test.go b/azurerm/data_source_availability_set_test.go new file mode 100644 index 000000000000..5ef514b476a1 --- /dev/null +++ b/azurerm/data_source_availability_set_test.go @@ -0,0 +1,56 @@ +package azurerm + +import ( + "fmt" + "testing" + + "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/tf" + + "github.com/hashicorp/terraform/helper/resource" +) + +func TestAccDataSourceAvailabilitySet_basic(t *testing.T) { + dataSourceName := "data.azurerm_availability_set.test" + ri := tf.AccRandTimeInt() + location := testLocation() + + resource.ParallelTest(t, resource.TestCase{ + PreCheck: func() { testAccPreCheck(t) }, + Providers: testAccProviders, + Steps: []resource.TestStep{ + { + Config: testAccDataSourceAvailabilitySet_basic(ri, location), + Check: resource.ComposeTestCheckFunc( + resource.TestCheckResourceAttrSet(dataSourceName, "location"), + resource.TestCheckResourceAttrSet(dataSourceName, "name"), + resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"), + resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"), + ), + }, + }, + }) +} + +func testAccDataSourceAvailabilitySet_basic(rInt int, location string) string { + return fmt.Sprintf(` +resource "azurerm_resource_group" "test" { + name = "acctestRG-%[1]d" + location = "%[2]s" +} + +resource "azurerm_availability_set" "test" { + name = "acctestavset-%[1]d" + location = "${azurerm_resource_group.test.location}" + resource_group_name = "${azurerm_resource_group.test.name}" + + tags { + "foo" = "bar" + } +} + +data "azurerm_availability_set" "test" { + resource_group_name = "${azurerm_resource_group.test.name}" + name = "${azurerm_availability_set.test.name}" +} +`, rInt, location) +} diff --git a/azurerm/provider.go b/azurerm/provider.go index 5af4de2cbf80..d7fe04774a1a 100644 --- a/azurerm/provider.go +++ b/azurerm/provider.go @@ -103,6 +103,7 @@ func Provider() terraform.ResourceProvider { "azurerm_app_service": dataSourceArmAppService(), "azurerm_application_insights": dataSourceArmApplicationInsights(), "azurerm_application_security_group": dataSourceArmApplicationSecurityGroup(), + "azurerm_availability_set": dataSourceArmAvailabilitySet(), "azurerm_azuread_application": dataSourceArmAzureADApplication(), "azurerm_azuread_service_principal": dataSourceArmActiveDirectoryServicePrincipal(), "azurerm_batch_account": dataSourceArmBatchAccount(), From 668d6b2f6199eb7bdeecc150f65fb04805b06b31 Mon Sep 17 00:00:00 2001 From: Matt Mencel Date: Thu, 7 Feb 2019 15:50:06 -0600 Subject: [PATCH 2/6] docs --- website/docs/d/availability_set.html.markdown | 42 +++++++++++++++++++ 1 file changed, 42 insertions(+) create mode 100644 website/docs/d/availability_set.html.markdown diff --git a/website/docs/d/availability_set.html.markdown b/website/docs/d/availability_set.html.markdown new file mode 100644 index 000000000000..ddbc467d040e --- /dev/null +++ b/website/docs/d/availability_set.html.markdown @@ -0,0 +1,42 @@ +--- +layout: "azurerm" +page_title: "Azure Resource Manager: azurerm_availability_set" +sidebar_current: "docs-azurerm-datasource-availability_set" +description: |- + Gets information about an existing Availability Set. +--- + +# Data Source: azurerm_availability_set + +Use this data source to access information about an existing Availability Set. + +## Example Usage + +```hcl +data "azurerm_availability_set" "test" { + name = "tf-appsecuritygroup" + resource_group_name = "my-resource-group" +} + +output "availability_set_id" { + value = "${data.azurerm_availability_set.test.id}" +} +``` + +## Argument Reference + +The following arguments are supported: + +* `name` - The name of the Availability Set. + +* `resource_group_name` - The name of the resource group in which the Availability Set exists. + +## Attributes Reference + +The following attributes are exported: + +* `id` - The ID of the Availability Set. + +* `location` - The supported Azure location where the Availability Set exists. + +* `tags` - A mapping of tags assigned to the resource. From 2c3a7c0f8ad7c2342c1caa664261469e2504c57e Mon Sep 17 00:00:00 2001 From: Matt Mencel Date: Fri, 8 Feb 2019 09:52:50 -0600 Subject: [PATCH 3/6] fixes --- azurerm/data_source_availability_set.go | 14 +++++++++++--- website/docs/d/availability_set.html.markdown | 6 ++++++ 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/azurerm/data_source_availability_set.go b/azurerm/data_source_availability_set.go index 45d3efba5883..38791b380395 100644 --- a/azurerm/data_source_availability_set.go +++ b/azurerm/data_source_availability_set.go @@ -2,6 +2,7 @@ package azurerm import ( "fmt" + "strings" "github.com/hashicorp/terraform/helper/schema" "github.com/terraform-providers/terraform-provider-azurerm/azurerm/helpers/validate" @@ -65,9 +66,16 @@ func dataSourceArmAvailabilitySetRead(d *schema.ResourceData, meta interface{}) } d.SetId(*resp.ID) - d.Set("location", resp.Location) - d.Set("platform_update_domain_count", resp.PlatformUpdateDomainCount) - d.Set("platform_fault_domain_count", resp.PlatformFaultDomainCount) + if location := resp.Location; location != nil { + d.Set("location", azureRMNormalizeLocation(*location)) + } + if resp.Sku != nil && resp.Sku.Name != nil { + d.Set("managed", strings.EqualFold(*resp.Sku.Name, "Aligned")) + } + if props := resp.AvailabilitySetProperties; props != nil { + d.Set("platform_update_domain_count", props.PlatformUpdateDomainCount) + d.Set("platform_fault_domain_count", props.PlatformFaultDomainCount) + } flattenAndSetTags(d, resp.Tags) return nil diff --git a/website/docs/d/availability_set.html.markdown b/website/docs/d/availability_set.html.markdown index ddbc467d040e..68aadbbca26f 100644 --- a/website/docs/d/availability_set.html.markdown +++ b/website/docs/d/availability_set.html.markdown @@ -39,4 +39,10 @@ The following attributes are exported: * `location` - The supported Azure location where the Availability Set exists. +* `managed` - Whether the availability set is managed or not. + +* `platform_fault_domain_count` - The number of fault domains that are used. + +* `platform_update_domain_count` - The number of update domains that are used. + * `tags` - A mapping of tags assigned to the resource. From c9cba823b15b47ee827bdf40d7ab781a24ec84bd Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Wed, 13 Feb 2019 12:16:11 +0100 Subject: [PATCH 4/6] d/availability_set: adding a link to the sidebar --- website/azurerm.erb | 4 ++++ website/docs/d/availability_set.html.markdown | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/website/azurerm.erb b/website/azurerm.erb index 3d1150a68e3c..7db9c3e5b3c9 100644 --- a/website/azurerm.erb +++ b/website/azurerm.erb @@ -79,6 +79,10 @@ azurerm_application_insights + > + azurerm_availability_set + + > azurerm_azuread_application diff --git a/website/docs/d/availability_set.html.markdown b/website/docs/d/availability_set.html.markdown index 68aadbbca26f..f29d5162c46f 100644 --- a/website/docs/d/availability_set.html.markdown +++ b/website/docs/d/availability_set.html.markdown @@ -1,7 +1,7 @@ --- layout: "azurerm" page_title: "Azure Resource Manager: azurerm_availability_set" -sidebar_current: "docs-azurerm-datasource-availability_set" +sidebar_current: "docs-azurerm-datasource-availability-set" description: |- Gets information about an existing Availability Set. --- From 1695b41448c0a733d1a76203c81a4d7b8a17cca9 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Wed, 13 Feb 2019 12:18:23 +0100 Subject: [PATCH 5/6] d/availability_set: making 'managed' a bool --- azurerm/data_source_availability_set.go | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/azurerm/data_source_availability_set.go b/azurerm/data_source_availability_set.go index 38791b380395..0b2905d271a9 100644 --- a/azurerm/data_source_availability_set.go +++ b/azurerm/data_source_availability_set.go @@ -37,14 +37,11 @@ func dataSourceArmAvailabilitySet() *schema.Resource { }, "managed": { - Type: schema.TypeString, + Type: schema.TypeBool, Computed: true, }, - "tags": { - Type: schema.TypeMap, - Computed: true, - }, + "tags": tagsForDataSourceSchema(), }, } } From db50550768740c8a39839ba25ae9d95e0e36a658 Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Wed, 13 Feb 2019 12:23:37 +0100 Subject: [PATCH 6/6] d/availability_set: fixing the test $ acctests azurerm TestAccDataSourceAvailabilitySet_basic === RUN TestAccDataSourceAvailabilitySet_basic === PAUSE TestAccDataSourceAvailabilitySet_basic === CONT TestAccDataSourceAvailabilitySet_basic --- PASS: TestAccDataSourceAvailabilitySet_basic (109.16s) PASS ok github.com/terraform-providers/terraform-provider-azurerm/azurerm 111.046s --- azurerm/data_source_availability_set_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/azurerm/data_source_availability_set_test.go b/azurerm/data_source_availability_set_test.go index 5ef514b476a1..5438303dd906 100644 --- a/azurerm/data_source_availability_set_test.go +++ b/azurerm/data_source_availability_set_test.go @@ -24,7 +24,7 @@ func TestAccDataSourceAvailabilitySet_basic(t *testing.T) { resource.TestCheckResourceAttrSet(dataSourceName, "location"), resource.TestCheckResourceAttrSet(dataSourceName, "name"), resource.TestCheckResourceAttrSet(dataSourceName, "resource_group_name"), - resource.TestCheckResourceAttr(dataSourceName, "tags.%", "0"), + resource.TestCheckResourceAttr(dataSourceName, "tags.%", "1"), ), }, },