Skip to content

Commit

Permalink
monitor_activity_log_alert: add location attribute (#25389)
Browse files Browse the repository at this point in the history
* add location attribute to monitor_activity_log_alert resource

* remove location validation

* add acceptance test for activity log alert location

* change location to commonschema.LocationOptional

* fix tffmt

* Apply suggestions from code review

add review suggestions

Co-authored-by: stephybun <steph@hashicorp.com>

* fix stuff

---------

Co-authored-by: Joe Andary <joe.andary@ext.markant.com>
Co-authored-by: stephybun <steph@hashicorp.com>
  • Loading branch information
3 people committed Apr 16, 2024
1 parent ffd0273 commit f758db8
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 1 deletion.
21 changes: 20 additions & 1 deletion internal/services/monitor/monitor_activity_log_alert_resource.go
Expand Up @@ -9,12 +9,16 @@ import (
"strings"
"time"

"github.com/hashicorp/go-azure-helpers/lang/pointer"
"github.com/hashicorp/go-azure-helpers/lang/response"
"github.com/hashicorp/go-azure-helpers/resourcemanager/commonschema"
"github.com/hashicorp/go-azure-helpers/resourcemanager/location"
"github.com/hashicorp/go-azure-sdk/resource-manager/insights/2020-10-01/activitylogalertsapis"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-provider-azurerm/helpers/azure"
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/monitor/migration"
"github.com/hashicorp/terraform-provider-azurerm/internal/tags"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
Expand Down Expand Up @@ -57,6 +61,20 @@ func resourceMonitorActivityLogAlert() *pluginsdk.Resource {

"resource_group_name": commonschema.ResourceGroupName(),

"location": func() *pluginsdk.Schema {
if !features.FourPointOhBeta() {
return &pluginsdk.Schema{
Type: schema.TypeString,
Optional: true,
Default: "global",
ForceNew: true,
StateFunc: location.StateFunc,
DiffSuppressFunc: location.DiffSuppressFunc,
}
}
return commonschema.Location()
}(),

"scopes": {
Type: pluginsdk.TypeSet,
Required: true,
Expand Down Expand Up @@ -422,7 +440,7 @@ func resourceMonitorActivityLogAlertCreateUpdate(d *pluginsdk.ResourceData, meta

t := d.Get("tags").(map[string]interface{})
parameters := activitylogalertsapis.ActivityLogAlertResource{
Location: utils.String(azure.NormalizeLocation("Global")),
Location: pointer.To(location.Normalize(d.Get("location").(string))),
Properties: &activitylogalertsapis.AlertRuleProperties{
Enabled: utils.Bool(enabled),
Description: utils.String(description),
Expand Down Expand Up @@ -466,6 +484,7 @@ func resourceMonitorActivityLogAlertRead(d *pluginsdk.ResourceData, meta interfa
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {
d.Set("location", location.NormalizeNilable(model.Location))
if props := model.Properties; props != nil {
d.Set("enabled", props.Enabled)
d.Set("description", props.Description)
Expand Down
Expand Up @@ -315,6 +315,21 @@ func TestAccMonitorActivityLogAlert_ServiceHealth_basicAndDelete(t *testing.T) {
})
}

func TestAccMonitorActivityLogAlert_location(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_activity_log_alert", "test")
r := MonitorActivityLogAlertResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.location(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (MonitorActivityLogAlertResource) basic(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down Expand Up @@ -1143,6 +1158,30 @@ resource "azurerm_monitor_activity_log_alert" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomString, data.RandomInteger)
}

func (MonitorActivityLogAlertResource) location(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_monitor_activity_log_alert" "test" {
name = "acctestActivityLogAlert-%d"
resource_group_name = azurerm_resource_group.test.name
location = "westeurope"
scopes = [azurerm_resource_group.test.id]
criteria {
category = "Recommendation"
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger)
}

func (t MonitorActivityLogAlertResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := activitylogalertsapis.ParseActivityLogAlertID(state.ID)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions website/docs/r/monitor_activity_log_alert.html.markdown
Expand Up @@ -65,6 +65,7 @@ The following arguments are supported:

* `name` - (Required) The name of the activity log alert. Changing this forces a new resource to be created.
* `resource_group_name` - (Required) The name of the resource group in which to create the activity log alert instance. Changing this forces a new resource to be created.
* `location` - (Optional) The Azure Region where the activity log alert rule should exist. Changing this forces a new resource to be created. Defaults to `global`.
* `scopes` - (Required) The Scope at which the Activity Log should be applied. A list of strings which could be a resource group , or a subscription, or a resource ID (such as a Storage Account).
* `criteria` - (Required) A `criteria` block as defined below.
* `action` - (Optional) One or more `action` blocks as defined below.
Expand Down

0 comments on commit f758db8

Please sign in to comment.