Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to set ManagedBy field. #22012

Merged
merged 1 commit into from
Jun 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions internal/services/resource/resource_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -25,6 +26,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 +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)
}
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
13 changes: 13 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,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)
}

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)
}
12 changes: 7 additions & 5 deletions website/docs/r/resource_group.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
```