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

monitor_activity_log_alert: add location attribute #25389

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 22 additions & 1 deletion internal/services/monitor/monitor_activity_log_alert_resource.go
Expand Up @@ -11,7 +11,9 @@ import (

"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"
Expand Down Expand Up @@ -57,6 +59,21 @@ func resourceMonitorActivityLogAlert() *pluginsdk.Resource {

"resource_group_name": commonschema.ResourceGroupName(),

"location": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Default: "global",
ValidateFunc: validation.Any(
location.EnhancedValidate,
validation.StringInSlice([]string{
"global",
}, false),
),
StateFunc: location.StateFunc,
DiffSuppressFunc: location.DiffSuppressFunc,
},
andaryjo marked this conversation as resolved.
Show resolved Hide resolved

"scopes": {
Type: pluginsdk.TypeSet,
Required: true,
Expand Down Expand Up @@ -396,6 +413,7 @@ func resourceMonitorActivityLogAlert() *pluginsdk.Resource {
func resourceMonitorActivityLogAlertCreateUpdate(d *pluginsdk.ResourceData, meta interface{}) error {
client := meta.(*clients.Client).Monitor.ActivityLogAlertsClient
subscriptionId := meta.(*clients.Client).Account.SubscriptionId
location := location.Normalize(d.Get("location").(string))
andaryjo marked this conversation as resolved.
Show resolved Hide resolved
ctx, cancel := timeouts.ForCreateUpdate(meta.(*clients.Client).StopContext, d)
defer cancel()

Expand All @@ -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: &location,
andaryjo marked this conversation as resolved.
Show resolved Hide resolved
Properties: &activitylogalertsapis.AlertRuleProperties{
Enabled: utils.Bool(enabled),
Description: utils.String(description),
Expand Down Expand Up @@ -466,6 +484,9 @@ func resourceMonitorActivityLogAlertRead(d *pluginsdk.ResourceData, meta interfa
d.Set("resource_group_name", id.ResourceGroupName)

if model := resp.Model; model != nil {

d.Set("location", location.Normalize(*model.Location))

andaryjo marked this conversation as resolved.
Show resolved Hide resolved
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_action_group", "test")
andaryjo marked this conversation as resolved.
Show resolved Hide resolved
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