-
Notifications
You must be signed in to change notification settings - Fork 9.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[MS] provider/azurerm: Data Source for Azure Resource Group (#15022)
* Data Source support for Resource Group * Better message for mismatching locations. * Reuse existing read code * Adds documentation * Adds test * Adds a function for composing ID strings * Change location to computed.
- Loading branch information
1 parent
d587b68
commit b465b01
Showing
9 changed files
with
261 additions
and
2 deletions.
There are no files selected for viewing
44 changes: 44 additions & 0 deletions
44
builtin/providers/azurerm/data_source_arm_resource_group.go
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,44 @@ | ||
package azurerm | ||
|
||
import ( | ||
"github.com/hashicorp/terraform/helper/schema" | ||
) | ||
|
||
func dataSourceArmResourceGroup() *schema.Resource { | ||
return &schema.Resource{ | ||
Read: dataSourceArmResourceGroupRead, | ||
|
||
Schema: map[string]*schema.Schema{ | ||
"name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Required: true, | ||
}, | ||
|
||
"location": locationForDataSourceSchema(), | ||
"tags": tagsForDataSourceSchema(), | ||
}, | ||
} | ||
} | ||
|
||
func dataSourceArmResourceGroupRead(d *schema.ResourceData, meta interface{}) error { | ||
armClient := meta.(*ArmClient) | ||
|
||
resourceGroupName := d.Get("name").(string) | ||
resourceId := &ResourceID{ | ||
SubscriptionID: armClient.subscriptionId, | ||
ResourceGroup: resourceGroupName, | ||
} | ||
resourceIdString, err := composeAzureResourceID(resourceId) | ||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
d.SetId(resourceIdString) | ||
|
||
if err := resourceArmResourceGroupRead(d, meta); err != nil { | ||
return err | ||
} | ||
|
||
return nil | ||
} |
46 changes: 46 additions & 0 deletions
46
builtin/providers/azurerm/data_source_arm_resource_group_test.go
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,46 @@ | ||
package azurerm | ||
|
||
import ( | ||
"testing" | ||
|
||
"fmt" | ||
|
||
"github.com/hashicorp/terraform/helper/acctest" | ||
"github.com/hashicorp/terraform/helper/resource" | ||
) | ||
|
||
func TestAccDataSourceAzureRMResourceGroup_basic(t *testing.T) { | ||
ri := acctest.RandInt() | ||
name := fmt.Sprintf("acctestRg_%d", ri) | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: testAccDataSourceAzureRMResourceGroupBasic(name), | ||
Check: resource.ComposeTestCheckFunc( | ||
resource.TestCheckResourceAttr("data.azurerm_resource_group.test", "name", name), | ||
resource.TestCheckResourceAttr("data.azurerm_resource_group.test", "location", "westus2"), | ||
resource.TestCheckResourceAttr("data.azurerm_resource_group.test", "tags.%", "1"), | ||
resource.TestCheckResourceAttr("data.azurerm_resource_group.test", "tags.env", "test"), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccDataSourceAzureRMResourceGroupBasic(name string) string { | ||
return fmt.Sprintf(`resource "azurerm_resource_group" "test" { | ||
name = "%s" | ||
location = "West US 2" | ||
tags { | ||
env = "test" | ||
} | ||
} | ||
data "azurerm_resource_group" "test" { | ||
name = "${azurerm_resource_group.test.name}" | ||
} | ||
`, name) | ||
} |
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
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
39 changes: 39 additions & 0 deletions
39
website/source/docs/providers/azurerm/d/resource_group.html.markdown
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_resource_group" | ||
sidebar_current: "docs-azurerm-datasource-resource-group" | ||
description: |- | ||
Get information about the specified resource group. | ||
--- | ||
|
||
# azurerm\_resource\_group | ||
|
||
Use this data source to access the properties of an Azure resource group. | ||
|
||
## Example Usage | ||
|
||
```hcl | ||
data "azurerm_resource_group" "test" { | ||
name = "dsrg_test" | ||
} | ||
resource "azurerm_managed_disk" "test" { | ||
name = "managed_disk_name" | ||
location = "${data.azurerm_resource_group.test.location}" | ||
resource_group_name = "${data.azurerm_resource_group.test.name}" | ||
storage_account_type = "Standard_LRS" | ||
create_option = "Empty" | ||
disk_size_gb = "1" | ||
} | ||
``` | ||
|
||
## Argument Reference | ||
|
||
* `name` - (Required) Specifies the name of the resource group. | ||
|
||
~> **NOTE:** If the specified location doesn't match the actual resource group location, an error message with the actual location value will be shown. | ||
|
||
## Attributes Reference | ||
|
||
* `location` - The location of the resource group. | ||
* `tags` - A mapping of tags assigned to the resource group. |
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