diff --git a/internal/services/resource/resource_group_data_source.go b/internal/services/resource/resource_group_data_source.go index 57164a5acaa6..08f7e71f87cb 100644 --- a/internal/services/resource/resource_group_data_source.go +++ b/internal/services/resource/resource_group_data_source.go @@ -4,6 +4,7 @@ import ( "fmt" "time" + "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/internal/clients" @@ -25,6 +26,10 @@ func dataSourceResourceGroup() *pluginsdk.Resource { "name": commonschema.ResourceGroupNameForDataSource(), "location": commonschema.LocationComputed(), "tags": tags.SchemaDataSource(), + "managed_by": { + Type: pluginsdk.TypeString, + Computed: true, + }, }, } } @@ -50,5 +55,6 @@ func dataSourceResourceGroupRead(d *pluginsdk.ResourceData, meta interface{}) er d.Set("name", resp.Name) d.Set("location", location.NormalizeNilable(resp.Location)) + d.Set("managed_by", pointer.From(resp.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 bbbf7d56bfb2..91ea8cf7663e 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 a778cb212f33..17b3e9a31bc0 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,7 @@ func resourceResourceGroupRead(d *pluginsdk.ResourceData, meta interface{}) erro d.Set("name", resp.Name) d.Set("location", location.NormalizeNilable(resp.Location)) + d.Set("managed_by", pointer.From(resp.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 df64241d3392..4424d75e1bc6 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 bb26ef805cfe..517ac984e7b6 100644 --- a/website/docs/r/resource_group.html.markdown +++ b/website/docs/r/resource_group.html.markdown @@ -33,6 +33,8 @@ The following arguments are supported: --- +* `managed_by` - (Optional) The ID of the resource or application that manages this Resource Group. + * `tags` - (Optional) A mapping of tags which should be assigned to the Resource Group. ## Attributes Reference @@ -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