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

azurerm_monitor_activity_log_alert - fix action webhook #19425

Merged
merged 3 commits into from
Nov 28, 2022
Merged
Show file tree
Hide file tree
Changes from 2 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
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())
}
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,100 @@ 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_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"
to = "microsoft azure"
}
}
}
`, data.RandomInteger, data.Locations.Primary, 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_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"
env = "test"
}
}
}
`, data.RandomInteger, data.Locations.Primary, data.RandomInteger, data.RandomString, data.RandomInteger)
}

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