Skip to content

Commit

Permalink
Merge pull request #92 from iLert/feature/new-api-resources
Browse files Browse the repository at this point in the history
Feature/new api resources pt.1
  • Loading branch information
STLVRTX authored Aug 22, 2024
2 parents d40d8c1 + b99a242 commit 920f57a
Show file tree
Hide file tree
Showing 12 changed files with 256 additions and 24 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog

## 22.08.2024, Version 2.9.0

- feature/new-api-resources pt.1 in [#92](https://github.com/iLert/terraform-provider-ilert/pull/92)

## 24.07.2024, Version 2.8.6

- fix/microsoft-teams-webhook-validation [#90](https://github.com/iLert/terraform-provider-ilert/pull/90)
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.19

require (
github.com/hashicorp/terraform-plugin-sdk/v2 v2.25.0
github.com/iLert/ilert-go/v3 v3.8.3
github.com/iLert/ilert-go/v3 v3.9.0
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ github.com/hashicorp/yamux v0.1.1 h1:yrQxtgseBDrq9Y652vSRDvsKCJKOUD+GzTS4Y0Y8pvE
github.com/hashicorp/yamux v0.1.1/go.mod h1:CtWFDAQgb7dxtzFs4tWbplKIe2jSi3+5vKbgIO0SLnQ=
github.com/huandu/xstrings v1.3.1/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/huandu/xstrings v1.3.2/go.mod h1:y5/lhBue+AyNmUVz9RLU9xbLR0o4KIIExikq4ovT0aE=
github.com/iLert/ilert-go/v3 v3.8.3 h1:QY3aYHzQ4JDCP/36TMxwDKtsvMU1Rd7cPrVBN8VeVEA=
github.com/iLert/ilert-go/v3 v3.8.3/go.mod h1:xHJ8qdmthK4HExcQOd3V5JARed/EBKTdX86MqrJ1yJ0=
github.com/iLert/ilert-go/v3 v3.9.0 h1:i13Yv3JeK9ZEJYysR6hcHCDvMxx5xetuafVrdiJb6Ts=
github.com/iLert/ilert-go/v3 v3.9.0/go.mod h1:xHJ8qdmthK4HExcQOd3V5JARed/EBKTdX86MqrJ1yJ0=
github.com/imdario/mergo v0.3.11/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
github.com/imdario/mergo v0.3.12 h1:b6R2BslTbIEToALKP7LxUvijTsNI9TAe80pLWN2g/HU=
github.com/imdario/mergo v0.3.12/go.mod h1:jmQim1M+e3UYxmgPu/WyfjB3N3VflVyUjjjwH0dnCYA=
Expand Down
59 changes: 59 additions & 0 deletions ilert/resource_alert_action.go
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,22 @@ func resourceAlertAction() *schema.Resource {
},
},
},
"slack_webhook": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
MinItems: 1,
ForceNew: true,
ConflictsWith: removeStringsFromSlice(alertActionTypesAll, ilert.ConnectorTypes.SlackWebhook),
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"url": {
Type: schema.TypeString,
Required: true,
},
},
},
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -545,6 +561,15 @@ func resourceAlertAction() *schema.Resource {
},
},
"delay_sec": {
Type: schema.TypeInt,
Optional: true,
Deprecated: "The field delay_sec is deprecated! Please use escalation_ended_delay_sec instead for trigger_type 'alert_escalation_ended' or not_resolved_delay_sec for trigger_type 'alert_not_resolved'.",
},
"escalation_ended_delay_sec": {
Type: schema.TypeInt,
Optional: true,
},
"not_resolved_delay_sec": {
Type: schema.TypeInt,
Optional: true,
},
Expand Down Expand Up @@ -843,6 +868,16 @@ func buildAlertAction(d *schema.ResourceData) (*ilert.AlertAction, error) {
}
}

if val, ok := d.GetOk("slack_webhook"); ok {
vL := val.([]interface{})
if len(vL) > 0 {
v := vL[0].(map[string]interface{})
alertAction.Params = &ilert.AlertActionParamsSlackWebhook{
URL: v["url"].(string),
}
}
}

if val, ok := d.GetOk("alert_filter"); ok {
vL := val.([]interface{})
if len(vL) > 0 {
Expand Down Expand Up @@ -890,6 +925,22 @@ func buildAlertAction(d *schema.ResourceData) (*ilert.AlertAction, error) {
alertAction.DelaySec = val.(int)
}

if val, ok := d.GetOk("escalation_ended_delay_sec"); ok {
escalationEndedDelaySec := val.(int)
if escalationEndedDelaySec != 0 && (escalationEndedDelaySec < 30 || escalationEndedDelaySec > 7200) {
return nil, fmt.Errorf("[ERROR] Can't set 'escalation_ended_delay_sec', value must be either 0 or between 30 and 7200")
}
alertAction.EscalationEndedDelaySec = val.(int)
}

if val, ok := d.GetOk("not_resolved_delay_sec"); ok {
notResolvedDelaySec := val.(int)
if notResolvedDelaySec != 0 && (notResolvedDelaySec < 60 || notResolvedDelaySec > 7200) {
return nil, fmt.Errorf("[ERROR] Can't set 'not_resolved_delay_sec', value must be either 0 or between 60 and 7200")
}
alertAction.NotResolvedDelaySec = val.(int)
}

return alertAction, nil
}

Expand Down Expand Up @@ -1130,6 +1181,12 @@ func resourceAlertActionRead(ctx context.Context, d *schema.ResourceData, m inte
"url": result.AlertAction.Params.URL,
},
})
case ilert.ConnectorTypes.SlackWebhook:
d.Set("slack_webhook", []interface{}{
map[string]interface{}{
"url": result.AlertAction.Params.URL,
},
})
}

alertFilter, err := flattenAlertActionAlertFilter(result.AlertAction.AlertFilter)
Expand Down Expand Up @@ -1167,6 +1224,8 @@ func resourceAlertActionRead(ctx context.Context, d *schema.ResourceData, m inte
}

d.Set("delay_sec", result.AlertAction.DelaySec)
d.Set("escalation_ended_delay_sec", result.AlertAction.EscalationEndedDelaySec)
d.Set("not_resolved_delay_sec", result.AlertAction.NotResolvedDelaySec)

if val, ok := d.GetOk("alert_source"); ok && len(val.([]interface{})) == 1 {
if v, ok := d.GetOk("team"); !ok || len(v.([]interface{})) == 0 {
Expand Down
36 changes: 29 additions & 7 deletions ilert/resource_alert_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -562,6 +562,14 @@ func resourceAlertSource() *schema.Resource {
Optional: true,
ValidateFunc: validation.StringInSlice(ilert.AlertSourceAlertGroupingWindowsAll, false),
},
"score_threshold": {
Type: schema.TypeFloat,
Optional: true,
},
"event_filter": {
Type: schema.TypeString,
Optional: true,
},
},
CreateContext: resourceAlertSourceCreate,
ReadContext: resourceAlertSourceRead,
Expand Down Expand Up @@ -610,8 +618,8 @@ func buildAlertSource(d *schema.ResourceData) (*ilert.AlertSource, error) {
}
if val, ok := d.GetOk("alert_creation"); ok {
alertCreation := val.(string)
if _, ok := d.GetOk("alert_grouping_window"); !ok && alertCreation == ilert.AlertSourceAlertCreations.OneAlertGroupedPerWindow {
return nil, fmt.Errorf("[ERROR] Can't set alert creation type 'ONE_ALERT_GROUPED_PER_WINDOW' when alert grouping window is not set")
if _, ok := d.GetOk("alert_grouping_window"); !ok && (alertCreation == ilert.AlertSourceAlertCreations.OneAlertGroupedPerWindow || alertCreation == ilert.AlertSourceAlertCreations.IntelligentGrouping) {
return nil, fmt.Errorf("[ERROR] Can't set alert creation type 'ONE_ALERT_GROUPED_PER_WINDOW' or 'INTELLIGENT_GROUPING' when alert grouping window is not set")
}
alertSource.AlertCreation = alertCreation
}
Expand Down Expand Up @@ -901,13 +909,21 @@ func buildAlertSource(d *schema.ResourceData) (*ilert.AlertSource, error) {
}
}
if val, ok := d.GetOk("alert_grouping_window"); ok {
if alert_creation, ok := d.GetOk("alert_creation"); !ok || alert_creation.(string) != ilert.AlertSourceAlertCreations.OneAlertGroupedPerWindow {
return nil, fmt.Errorf("[ERROR] Can't set alert grouping window when alert creation is not set or not of type 'ONE_ALERT_GROUPED_PER_WINDOW'")
if alert_creation, ok := d.GetOk("alert_creation"); !ok || (alert_creation.(string) != ilert.AlertSourceAlertCreations.OneAlertGroupedPerWindow && alert_creation.(string) != ilert.AlertSourceAlertCreations.IntelligentGrouping) {
return nil, fmt.Errorf("[ERROR] Can't set alert grouping window when alert creation is not set or not of type 'ONE_ALERT_GROUPED_PER_WINDOW' or 'INTELLIGENT_GROUPING'")
}
alertGroupingWindow := val.(string)
alertSource.AlertGroupingWindow = alertGroupingWindow
}

if val, ok := d.GetOk("score_threshold"); ok {
alertSource.ScoreThreshold = val.(float64)
}

if val, ok := d.GetOk("event_filter"); ok {
alertSource.EventFilter = val.(string)
}

return alertSource, nil
}

Expand All @@ -923,7 +939,9 @@ func resourceAlertSourceCreate(ctx context.Context, d *schema.ResourceData, m in
log.Printf("[DEBUG] Creating ilert alert source %s\n", alertSource.Name)
result := &ilert.CreateAlertSourceOutput{}
err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
r, err := client.CreateAlertSource(&ilert.CreateAlertSourceInput{AlertSource: alertSource})
includes := make([]*string, 0)
includes = append(includes, ilert.String("summaryTemplate"), ilert.String("detailsTemplate"), ilert.String("routingTemplate"), ilert.String("textTemplate"), ilert.String("linkTemplates"), ilert.String("priorityTemplate"), ilert.String("eventFilter"))
r, err := client.CreateAlertSource(&ilert.CreateAlertSourceInput{AlertSource: alertSource, Include: includes})
if err != nil {
if _, ok := err.(*ilert.RetryableAPIError); ok {
log.Printf("[ERROR] Creating ilert alert source error '%s', so retry again", err.Error())
Expand Down Expand Up @@ -958,7 +976,7 @@ func resourceAlertSourceRead(ctx context.Context, d *schema.ResourceData, m inte
result := &ilert.GetAlertSourceOutput{}
err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutRead), func() *resource.RetryError {
includes := make([]*string, 0)
includes = append(includes, ilert.String("summaryTemplate"), ilert.String("detailsTemplate"), ilert.String("routingTemplate"), ilert.String("textTemplate"), ilert.String("linkTemplates"), ilert.String("priorityTemplate"))
includes = append(includes, ilert.String("summaryTemplate"), ilert.String("detailsTemplate"), ilert.String("routingTemplate"), ilert.String("textTemplate"), ilert.String("linkTemplates"), ilert.String("priorityTemplate"), ilert.String("eventFilter"))
r, err := client.GetAlertSource(&ilert.GetAlertSourceInput{AlertSourceID: ilert.Int64(alertSourceID), Include: includes})
if err != nil {
if _, ok := err.(*ilert.NotFoundAPIError); ok {
Expand Down Expand Up @@ -1006,6 +1024,8 @@ func resourceAlertSourceRead(ctx context.Context, d *schema.ResourceData, m inte
d.Set("email", result.AlertSource.IntegrationKey)
}
d.Set("alert_grouping_window", result.AlertSource.AlertGroupingWindow)
d.Set("score_threshold", result.AlertSource.ScoreThreshold)
d.Set("event_filter", result.AlertSource.EventFilter)

if result.AlertSource.Heartbeat != nil {
d.Set("heartbeat", []interface{}{
Expand Down Expand Up @@ -1173,7 +1193,9 @@ func resourceAlertSourceUpdate(ctx context.Context, d *schema.ResourceData, m in
}
log.Printf("[DEBUG] Updating alert source: %s", d.Id())
err = resource.RetryContext(ctx, d.Timeout(schema.TimeoutUpdate), func() *resource.RetryError {
_, err = client.UpdateAlertSource(&ilert.UpdateAlertSourceInput{AlertSource: alertSource, AlertSourceID: ilert.Int64(alertSourceID)})
includes := make([]*string, 0)
includes = append(includes, ilert.String("summaryTemplate"), ilert.String("detailsTemplate"), ilert.String("routingTemplate"), ilert.String("textTemplate"), ilert.String("linkTemplates"), ilert.String("priorityTemplate"), ilert.String("eventFilter"))
_, err = client.UpdateAlertSource(&ilert.UpdateAlertSourceInput{AlertSource: alertSource, AlertSourceID: ilert.Int64(alertSourceID), Include: includes})
if err != nil {
if _, ok := err.(*ilert.RetryableAPIError); ok {
time.Sleep(2 * time.Second)
Expand Down
2 changes: 1 addition & 1 deletion ilert/resource_connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (

func resourceConnector() *schema.Resource {
// remove types with no or one specific standalone connector
connectorTypesAll := removeStringsFromSlice(ilert.ConnectorTypesAll, ilert.ConnectorTypes.Email, ilert.ConnectorTypes.MicrosoftTeamsBot, ilert.ConnectorTypes.Slack, ilert.ConnectorTypes.Webhook, ilert.ConnectorTypes.AutomationRule, ilert.ConnectorTypes.Telegram, ilert.ConnectorTypes.DingTalkAction, ilert.ConnectorTypes.MicrosoftTeamsWebhook)
connectorTypesAll := removeStringsFromSlice(ilert.ConnectorTypesAll, ilert.ConnectorTypes.Email, ilert.ConnectorTypes.MicrosoftTeamsBot, ilert.ConnectorTypes.Slack, ilert.ConnectorTypes.Webhook, ilert.ConnectorTypes.AutomationRule, ilert.ConnectorTypes.Telegram, ilert.ConnectorTypes.DingTalkAction, ilert.ConnectorTypes.MicrosoftTeamsWebhook, ilert.ConnectorTypes.SlackWebhook)
return &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Expand Down
18 changes: 12 additions & 6 deletions ilert/resource_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,12 +301,18 @@ func resourceMetricRead(ctx context.Context, d *schema.ResourceData, m interface
}

d.Set("unit_label", result.Metric.UnitLabel)
d.Set("metadata", []interface{}{map[string]interface{}{
"query": result.Metric.Metadata.Query,
}})
d.Set("data_source", []interface{}{map[string]interface{}{
"id": int(result.Metric.DataSource.ID),
}})

if result.Metric.Metadata != nil {
d.Set("metadata", []interface{}{map[string]interface{}{
"query": result.Metric.Metadata.Query,
}})
}

if result.Metric.DataSource != nil {
d.Set("data_source", []interface{}{map[string]interface{}{
"id": int(result.Metric.DataSource.ID),
}})
}

return nil
}
Expand Down
Loading

0 comments on commit 920f57a

Please sign in to comment.