Skip to content

Commit

Permalink
azurerm_monitor_activity_log_alert - fix action webhook (#19425)
Browse files Browse the repository at this point in the history
* change action to list

* fix activity log alert action webhook

* add action block in test
  • Loading branch information
teowa authored Nov 28, 2022
1 parent df438ff commit 12ddbbc
Show file tree
Hide file tree
Showing 2 changed files with 146 additions and 13 deletions.
15 changes: 2 additions & 13 deletions internal/services/monitor/monitor_activity_log_alert_resource.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package monitor

import (
"bytes"
"fmt"
"log"
"strings"
Expand Down Expand Up @@ -269,7 +268,7 @@ func resourceMonitorActivityLogAlert() *pluginsdk.Resource {
},

"action": {
Type: pluginsdk.TypeSet,
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
Expand All @@ -287,7 +286,6 @@ func resourceMonitorActivityLogAlert() *pluginsdk.Resource {
},
},
},
Set: resourceMonitorActivityLogAlertActionHash,
},

"description": {
Expand Down Expand Up @@ -331,11 +329,10 @@ func resourceMonitorActivityLogAlertCreateUpdate(d *pluginsdk.ResourceData, meta
description := d.Get("description").(string)
scopesRaw := d.Get("scopes").(*pluginsdk.Set).List()
criteriaRaw := d.Get("criteria").([]interface{})
actionRaw := d.Get("action").(*pluginsdk.Set).List()
actionRaw := d.Get("action").([]interface{})

t := d.Get("tags").(map[string]interface{})
expandedTags := tags.Expand(t)

parameters := insights.ActivityLogAlertResource{
Location: utils.String(azure.NormalizeLocation("Global")),
AlertRuleProperties: &insights.AlertRuleProperties{
Expand Down Expand Up @@ -746,11 +743,3 @@ func flattenMonitorActivityLogAlertAction(input *insights.ActionList) (result []
}
return result
}

func resourceMonitorActivityLogAlertActionHash(input interface{}) int {
var buf bytes.Buffer
if v, ok := input.(map[string]interface{}); ok {
buf.WriteString(fmt.Sprintf("%s-", v["action_group_id"].(string)))
}
return pluginsdk.HashString(buf.String())
}
144 changes: 144 additions & 0 deletions internal/services/monitor/monitor_activity_log_alert_resource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,28 @@ func TestAccMonitorActivityLogAlert_complete(t *testing.T) {
})
}

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

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

func TestAccMonitorActivityLogAlert_criteria(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_monitor_activity_log_alert", "test")
r := MonitorActivityLogAlertResource{}
Expand Down Expand Up @@ -357,6 +379,128 @@ resource "azurerm_monitor_activity_log_alert" "test" {
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomString, data.RandomInteger)
}

func (MonitorActivityLogAlertResource) basicWebhook(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_monitor_action_group" "test" {
name = "acctestActionGroup-%d"
resource_group_name = azurerm_resource_group.test.name
short_name = "acctestag"
}
resource "azurerm_monitor_action_group" "test2" {
name = "acctestActionGroup2-%d"
resource_group_name = azurerm_resource_group.test.name
short_name = "acctestag2"
}
resource "azurerm_storage_account" "test" {
name = "acctestsa%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_monitor_activity_log_alert" "test" {
name = "acctestActivityLogAlert-%d"
resource_group_name = azurerm_resource_group.test.name
scopes = [azurerm_resource_group.test.id]
criteria {
operation_name = "Microsoft.Storage/storageAccounts/write"
category = "Recommendation"
resource_id = azurerm_storage_account.test.id
}
action {
action_group_id = azurerm_monitor_action_group.test.id
webhook_properties = {
from = "terraform test"
}
}
action {
action_group_id = azurerm_monitor_action_group.test2.id
webhook_properties = {
to = "microsoft azure"
from = "terraform test"
}
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomString, data.RandomInteger)
}

func (MonitorActivityLogAlertResource) updateWebhook(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
features {}
}
resource "azurerm_resource_group" "test" {
name = "acctestRG-%d"
location = "%s"
}
resource "azurerm_monitor_action_group" "test" {
name = "acctestActionGroup-%d"
resource_group_name = azurerm_resource_group.test.name
short_name = "acctestag"
}
resource "azurerm_monitor_action_group" "test2" {
name = "acctestActionGroup2-%d"
resource_group_name = azurerm_resource_group.test.name
short_name = "acctestag2"
}
resource "azurerm_storage_account" "test" {
name = "acctestsa%s"
resource_group_name = azurerm_resource_group.test.name
location = azurerm_resource_group.test.location
account_tier = "Standard"
account_replication_type = "LRS"
}
resource "azurerm_monitor_activity_log_alert" "test" {
name = "acctestActivityLogAlert-%d"
resource_group_name = azurerm_resource_group.test.name
scopes = [azurerm_resource_group.test.id]
criteria {
operation_name = "Microsoft.Storage/storageAccounts/write"
category = "Recommendation"
resource_id = azurerm_storage_account.test.id
}
action {
action_group_id = azurerm_monitor_action_group.test2.id
webhook_properties = {
from = "terraform test"
to = "microsoft azure"
env = "test"
}
}
action {
action_group_id = azurerm_monitor_action_group.test.id
webhook_properties = {
from = "terraform test"
env = "test"
}
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomInteger, data.RandomString, data.RandomInteger)
}

func (MonitorActivityLogAlertResource) complete(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down

0 comments on commit 12ddbbc

Please sign in to comment.