From f84fdc15c252fa9090a908c3996124f400f86f08 Mon Sep 17 00:00:00 2001 From: rna-afk Date: Thu, 1 Jun 2023 12:02:53 -0400 Subject: [PATCH] add option to set ManagedBy field. Adding the option to set the ManagedBy field to the `azure_resource_group` resource. --- .../resource/resource_group_data_source.go | 9 ++++++ .../resource_group_data_source_test.go | 3 ++ .../resource/resource_group_resource.go | 17 ++++++++++ .../resource/resource_group_resource_test.go | 31 +++++++++++++++++++ website/docs/r/resource_group.html.markdown | 14 +++++---- 5 files changed, 68 insertions(+), 6 deletions(-) diff --git a/internal/services/resource/resource_group_data_source.go b/internal/services/resource/resource_group_data_source.go index 57164a5acaa61..b14e1778afd85 100644 --- a/internal/services/resource/resource_group_data_source.go +++ b/internal/services/resource/resource_group_data_source.go @@ -25,6 +25,10 @@ func dataSourceResourceGroup() *pluginsdk.Resource { "name": commonschema.ResourceGroupNameForDataSource(), "location": commonschema.LocationComputed(), "tags": tags.SchemaDataSource(), + "managed_by": { + Type: pluginsdk.TypeString, + Computed: true, + }, }, } } @@ -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) } diff --git a/internal/services/resource/resource_group_data_source_test.go b/internal/services/resource/resource_group_data_source_test.go index bbbf7d56bfb29..91ea8cf7663eb 100644 --- a/internal/services/resource/resource_group_data_source_test.go +++ b/internal/services/resource/resource_group_data_source_test.go @@ -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"), ), }, }) @@ -41,6 +42,8 @@ resource "azurerm_resource_group" "test" { tags = { env = "test" } + + managed_by = "test" } data "azurerm_resource_group" "test" { diff --git a/internal/services/resource/resource_group_resource.go b/internal/services/resource/resource_group_resource.go index a778cb212f334..c0ab91ebfd61f 100644 --- a/internal/services/resource/resource_group_resource.go +++ b/internal/services/resource/resource_group_resource.go @@ -8,6 +8,7 @@ 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" @@ -15,6 +16,7 @@ import ( "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" ) @@ -43,6 +45,12 @@ func resourceResourceGroup() *pluginsdk.Resource { "location": commonschema.Location(), "tags": tags.Schema(), + + "managed_by": { + Type: pluginsdk.TypeString, + Optional: true, + ValidateFunc: validation.StringIsNotEmpty, + }, }, } } @@ -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) } @@ -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) } diff --git a/internal/services/resource/resource_group_resource_test.go b/internal/services/resource/resource_group_resource_test.go index df64241d33921..4424d75e1bc6a 100644 --- a/internal/services/resource/resource_group_resource_test.go +++ b/internal/services/resource/resource_group_resource_test.go @@ -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{} @@ -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) +} diff --git a/website/docs/r/resource_group.html.markdown b/website/docs/r/resource_group.html.markdown index bb26ef805cfe4..3df2c2ff1b4f2 100644 --- a/website/docs/r/resource_group.html.markdown +++ b/website/docs/r/resource_group.html.markdown @@ -33,11 +33,13 @@ 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. @@ -45,15 +47,15 @@ In addition to the Arguments listed above - the following Attributes are exporte 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 +``` \ No newline at end of file