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_sentinel_alert_rule_nrt - support block event_grouping #20231

Merged
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
27 changes: 27 additions & 0 deletions internal/services/sentinel/sentinel_alert_rule_nrt_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-provider-azurerm/helpers/tf"
"github.com/hashicorp/terraform-provider-azurerm/helpers/validate"
"github.com/hashicorp/terraform-provider-azurerm/internal/clients"
"github.com/hashicorp/terraform-provider-azurerm/internal/features"
"github.com/hashicorp/terraform-provider-azurerm/internal/services/sentinel/parse"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/pluginsdk"
"github.com/hashicorp/terraform-provider-azurerm/internal/tf/validation"
Expand Down Expand Up @@ -96,6 +97,26 @@ func resourceSentinelAlertRuleNrt() *pluginsdk.Resource {
ValidateFunc: validation.StringIsNotEmpty,
},

"event_grouping": {
Type: pluginsdk.TypeList,
Required: features.FourPointOhBeta(),
Optional: !features.FourPointOhBeta(),
Computed: !features.FourPointOhBeta(), // the service will default it to `SingleAlert`.
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"aggregation_method": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(securityinsight.EventGroupingAggregationKindAlertPerResult),
string(securityinsight.EventGroupingAggregationKindSingleAlert),
}, false),
},
},
},
},

"tactics": {
Type: pluginsdk.TypeSet,
Optional: true,
Expand Down Expand Up @@ -367,6 +388,9 @@ func resourceSentinelAlertRuleNrtCreateUpdate(d *pluginsdk.ResourceData, meta in
if v, ok := d.GetOk("alert_rule_template_version"); ok {
param.NrtAlertRuleProperties.TemplateVersion = utils.String(v.(string))
}
if v, ok := d.GetOk("event_grouping"); ok {
param.NrtAlertRuleProperties.EventGroupingSettings = expandAlertRuleScheduledEventGroupingSetting(v.([]interface{}))
}
if v, ok := d.GetOk("alert_details_override"); ok {
param.NrtAlertRuleProperties.AlertDetailsOverride = expandAlertRuleAlertDetailsOverride(v.([]interface{}))
}
Expand Down Expand Up @@ -451,6 +475,9 @@ func resourceSentinelAlertRuleNrtRead(d *pluginsdk.ResourceData, meta interface{
d.Set("alert_rule_template_guid", prop.AlertRuleTemplateName)
d.Set("alert_rule_template_version", prop.TemplateVersion)

if err := d.Set("event_grouping", flattenAlertRuleScheduledEventGroupingSetting(prop.EventGroupingSettings)); err != nil {
return fmt.Errorf("setting `event_grouping`: %+v", err)
}
if err := d.Set("alert_details_override", flattenAlertRuleAlertDetailsOverride(prop.AlertDetailsOverride)); err != nil {
return fmt.Errorf("setting `alert_details_override`: %+v", err)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,28 @@ func TestAccSentinelAlertRuleNrt_withAlertRuleTemplateGuid(t *testing.T) {
})
}

func TestAccSentinelAlertRuleNrt_updateEventGroupingSetting(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_sentinel_alert_rule_nrt", "test")
r := SentinelAlertRuleNrtResource{}

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

func (t SentinelAlertRuleNrtResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.AlertRuleID(state.ID)
if err != nil {
Expand Down Expand Up @@ -251,6 +273,52 @@ resource "azurerm_sentinel_alert_rule_nrt" "test" {
`, r.template(data), data.RandomInteger)
}

func (r SentinelAlertRuleNrtResource) eventGroupingSetting(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_sentinel_alert_rule_nrt" "test" {
name = "acctest-SentinelAlertRule-NRT-%d"
log_analytics_workspace_id = azurerm_log_analytics_solution.test.workspace_resource_id
display_name = "Some Rule"
severity = "High"
query = <<QUERY
AzureActivity |
where OperationName == "Create or Update Virtual Machine" or OperationName =="Create Deployment" |
where ActivityStatus == "Succeeded" |
make-series dcount(ResourceId) default=0 on EventSubmissionTimestamp in range(ago(7d), now(), 1d) by Caller
QUERY

event_grouping {
aggregation_method = "SingleAlert"
}
}
`, r.template(data), data.RandomInteger)
}

func (r SentinelAlertRuleNrtResource) updateEventGroupingSetting(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_sentinel_alert_rule_nrt" "test" {
name = "acctest-SentinelAlertRule-NRT-%d"
log_analytics_workspace_id = azurerm_log_analytics_solution.test.workspace_resource_id
display_name = "Some Rule"
severity = "High"
query = <<QUERY
AzureActivity |
where OperationName == "Create or Update Virtual Machine" or OperationName =="Create Deployment" |
where ActivityStatus == "Succeeded" |
make-series dcount(ResourceId) default=0 on EventSubmissionTimestamp in range(ago(7d), now(), 1d) by Caller
QUERY

event_grouping {
aggregation_method = "AlertPerResult"
}
}
`, r.template(data), data.RandomInteger)
}

func (SentinelAlertRuleNrtResource) template(data acceptance.TestData) string {
return fmt.Sprintf(`
provider "azurerm" {
Expand Down
10 changes: 10 additions & 0 deletions website/docs/r/sentinel_alert_rule_nrt.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ The following arguments are supported:

* `entity_mapping` - (Optional) A list of `entity_mapping` blocks as defined below.

* `event_grouping` - (Optional) A `event_grouping` block as defined below.

-> **NOTE:** `event_grouping` will be required in the next major version of the AzureRM Provider.

* `incident` - (Optional) A `incident` block as defined below.

* `suppression_duration` - (Optional) If `suppression_enabled` is `true`, this is ISO 8601 timespan duration, which specifies the amount of time the query should stop running after alert is generated. Defaults to `PT5H`.
Expand Down Expand Up @@ -118,6 +122,12 @@ An `entity_mapping` block supports the following:

---

A `event_grouping` block supports the following:

* `aggregation_method` - (Required) The aggregation type of grouping the events. Possible values are `AlertPerResult` and `SingleAlert`.

---

A `field_mapping` block supports the following:

* `identifier` - (Required) The identifier of the entity.
Expand Down