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_scheduled - support alert_rule_template_guid #9712

Merged
merged 8 commits into from
Dec 27, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
Expand Up @@ -60,6 +60,13 @@ func resourceSentinelAlertRuleScheduled() *schema.Resource {
ValidateFunc: validation.StringIsNotEmpty,
},

"alert_rule_template_guid": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ValidateFunc: validation.IsUUID,
},

"description": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -224,6 +231,10 @@ func resourceSentinelAlertRuleScheduledCreateUpdate(d *schema.ResourceData, meta
},
}

if v, ok := d.GetOk("alert_rule_template_guid"); ok {
param.ScheduledAlertRuleProperties.AlertRuleTemplateName = utils.String(v.(string))
}

// Service avoid concurrent update of this resource via checking the "etag" to guarantee it is the same value as last Read.
if !d.IsNewResource() {
resp, err := client.Get(ctx, workspaceID.ResourceGroup, "Microsoft.OperationalInsights", workspaceID.WorkspaceName, name)
Expand Down Expand Up @@ -307,6 +318,7 @@ func resourceSentinelAlertRuleScheduledRead(d *schema.ResourceData, meta interfa
d.Set("trigger_threshold", int(threshold))
d.Set("suppression_enabled", prop.SuppressionEnabled)
d.Set("suppression_duration", prop.SuppressionDuration)
d.Set("alert_rule_template_guid", prop.AlertRuleTemplateName)
}

return nil
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,21 @@ func TestAccSentinelAlertRuleScheduled_requiresImport(t *testing.T) {
})
}

func TestAccSentinelAlertRuleScheduled_withAlertRuleTemplateGuid(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_sentinel_alert_rule_scheduled", "test")
r := SentinelAlertRuleScheduledResource{}

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

func (t SentinelAlertRuleScheduledResource) Exists(ctx context.Context, clients *clients.Client, state *terraform.InstanceState) (*bool, error) {
id, err := parse.SentinelAlertRuleID(state.ID)
if err != nil {
Expand Down Expand Up @@ -172,6 +187,26 @@ resource "azurerm_sentinel_alert_rule_scheduled" "import" {
`, r.basic(data))
}

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

resource "azurerm_sentinel_alert_rule_scheduled" "test" {
name = "acctest-SentinelAlertRule-Sche-%d"
log_analytics_workspace_id = azurerm_log_analytics_workspace.test.id
display_name = "Some Rule"
severity = "Low"
alert_rule_template_guid = "65360bb0-8986-4ade-a89d-af3cf44d28aa"
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
}
`, r.template(data), data.RandomInteger)
}

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

---

* `alert_rule_template_guid` - (Optional) The GUID of the alert rule template which is used for this Sentinel Scheduled Alert Rule. Changing this forces a new Sentinel Scheduled Alert Rule to be created.

* `description` - (Optional) The description of this Sentinel Scheduled Alert Rule.

* `enabled` - (Optional) Should the Sentinel Scheduled Alert Rule be enabled? Defaults to `true`.
Expand Down