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

add availability set data source #2850

Merged
merged 7 commits into from
Feb 14, 2019
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
79 changes: 79 additions & 0 deletions azurerm/data_source_availability_set.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package azurerm

import (
"fmt"
"strings"

"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.TypeBool,
Computed: true,
},

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

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)
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)
MattMencel marked this conversation as resolved.
Show resolved Hide resolved

return nil
}
56 changes: 56 additions & 0 deletions azurerm/data_source_availability_set_test.go
Original file line number Diff line number Diff line change
@@ -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.%", "1"),
),
},
},
})
}

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)
}
1 change: 1 addition & 0 deletions azurerm/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
4 changes: 4 additions & 0 deletions website/azurerm.erb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@
<a href="/docs/providers/azurerm/d/application_insights.html">azurerm_application_insights</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-availability-set") %>>
<a href="/docs/providers/azurerm/d/availability_set.html">azurerm_availability_set</a>
</li>

<li<%= sidebar_current("docs-azurerm-datasource-azuread-application") %>>
<a href="/docs/providers/azurerm/d/azuread_application.html">azurerm_azuread_application</a>
</li>
Expand Down
48 changes: 48 additions & 0 deletions website/docs/d/availability_set.html.markdown
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
---
MattMencel marked this conversation as resolved.
Show resolved Hide resolved
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.

* `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.
MattMencel marked this conversation as resolved.
Show resolved Hide resolved