-
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 #2850 from MattMencel/azurerm_availability_set_dat…
…a_source add availability set data source
- Loading branch information
Showing
5 changed files
with
188 additions
and
0 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,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) | ||
|
||
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,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) | ||
} |
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,48 @@ | ||
--- | ||
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. |