Skip to content

Commit

Permalink
add option to set ManagedBy field.
Browse files Browse the repository at this point in the history
Adding the option to set the ManagedBy field to
the `azure_resource_group` resource.
  • Loading branch information
rna-afk committed Jun 21, 2023
1 parent b29f31f commit f84fdc1
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 6 deletions.
9 changes: 9 additions & 0 deletions internal/services/resource/resource_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ func dataSourceResourceGroup() *pluginsdk.Resource {
"name": commonschema.ResourceGroupNameForDataSource(),
"location": commonschema.LocationComputed(),
"tags": tags.SchemaDataSource(),
"managed_by": {
Type: pluginsdk.TypeString,
Computed: true,
},
},
}
}
Expand All @@ -50,5 +54,10 @@ func dataSourceResourceGroupRead(d *pluginsdk.ResourceData, meta interface{}) er

d.Set("name", resp.Name)
d.Set("location", location.NormalizeNilable(resp.Location))
managedBy := ""
if v := resp.ManagedBy; v != nil {
managedBy = *v
}
d.Set("managed_by", managedBy)
return tags.FlattenAndSet(d, resp.Tags)
}
3 changes: 3 additions & 0 deletions internal/services/resource/resource_group_data_source_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ func TestAccDataSourceAzureRMResourceGroup_basic(t *testing.T) {
check.That(data.ResourceName).Key("location").HasValue(azure.NormalizeLocation(data.Locations.Primary)),
check.That(data.ResourceName).Key("tags.%").HasValue("1"),
check.That(data.ResourceName).Key("tags.env").HasValue("test"),
check.That(data.ResourceName).Key("managed_by").HasValue("test"),
),
},
})
Expand All @@ -41,6 +42,8 @@ resource "azurerm_resource_group" "test" {
tags = {
env = "test"
}
managed_by = "test"
}
data "azurerm_resource_group" "test" {
Expand Down
17 changes: 17 additions & 0 deletions internal/services/resource/resource_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,15 @@ import (
"time"

"github.com/Azure/azure-sdk-for-go/services/resources/mgmt/2020-06-01/resources" // nolint: staticcheck
"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/resource/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
"github.com/hashicorp/terraform-provider-azurerm/internal/timeouts"
"github.com/hashicorp/terraform-provider-azurerm/utils"
)
Expand Down Expand Up @@ -43,6 +45,12 @@ func resourceResourceGroup() *pluginsdk.Resource {
"location": commonschema.Location(),

"tags": tags.Schema(),

"managed_by": {
Type: pluginsdk.TypeString,
Optional: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
}
}
Expand Down Expand Up @@ -74,6 +82,10 @@ func resourceResourceGroupCreateUpdate(d *pluginsdk.ResourceData, meta interface
Tags: tags.Expand(t),
}

if v := d.Get("managed_by").(string); v != "" {
parameters.ManagedBy = pointer.To(v)
}

if _, err := client.CreateOrUpdate(ctx, name, parameters); err != nil {
return fmt.Errorf("creating Resource Group %q: %+v", name, err)
}
Expand Down Expand Up @@ -114,6 +126,11 @@ func resourceResourceGroupRead(d *pluginsdk.ResourceData, meta interface{}) erro

d.Set("name", resp.Name)
d.Set("location", location.NormalizeNilable(resp.Location))
managedBy := ""
if v := resp.ManagedBy; v != nil {
managedBy = *v
}
d.Set("managed_by", managedBy)
return tags.FlattenAndSet(d, resp.Tags)
}

Expand Down
31 changes: 31 additions & 0 deletions internal/services/resource/resource_group_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,22 @@ func TestAccResourceGroup_withTags(t *testing.T) {
})
}

func TestAccResourceGroup_withManagedBy(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_resource_group", "test")
testResource := ResourceGroupResource{}
assert := check.That(data.ResourceName)
data.ResourceTest(t, testResource, []acceptance.TestStep{
{
Config: testResource.withManagedByConfig(data),
Check: acceptance.ComposeTestCheckFunc(
assert.ExistsInAzure(testResource),
assert.Key("managed_by").HasValue("test"),
),
},
data.ImportStep(),
})
}

func TestAccResourceGroup_withNestedItemsAndFeatureFlag(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_resource_group", "test")
r := ResourceGroupResource{}
Expand Down Expand Up @@ -232,3 +248,18 @@ resource "azurerm_resource_group" "test" {
}
`, data.RandomInteger, data.Locations.Primary)
}

func (t ResourceGroupResource) withManagedByConfig(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
managed_by = "test"
}
`, data.RandomInteger, data.Locations.Primary)
}
14 changes: 8 additions & 6 deletions website/docs/r/resource_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,29 @@ The following arguments are supported:

---

* `managed_by` - (Optional) TODO.

* `tags` - (Optional) A mapping of tags which should be assigned to the Resource Group.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:
In addition to the Arguments listed above - the following Attributes are exported:

* `id` - The ID of the Resource Group.

## Timeouts

The `timeouts` block allows you to specify [timeouts](https://www.terraform.io/language/resources/syntax#operation-timeouts) for certain actions:

* `create` - (Defaults to 90 minutes) Used when creating the Resource Group.
* `create` - (Defaults to 1 hour and 30 minutes) Used when creating the Resource Group.
* `read` - (Defaults to 5 minutes) Used when retrieving the Resource Group.
* `update` - (Defaults to 90 minutes) Used when updating the Resource Group.
* `delete` - (Defaults to 90 minutes) Used when deleting the Resource Group.
* `update` - (Defaults to 1 hour and 30 minutes) Used when updating the Resource Group.
* `delete` - (Defaults to 1 hour and 30 minutes) Used when deleting the Resource Group.

## Import

Resource Groups can be imported using the `resource id`, e.g.

```shell
terraform import azurerm_resource_group.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/example
```
terraform import azurerm_resource_group.example /subscriptions/00000000-0000-0000-0000-000000000000/resourceGroups/group1
```

0 comments on commit f84fdc1

Please sign in to comment.