Skip to content

Commit

Permalink
feat(alerts): add change alert type
Browse files Browse the repository at this point in the history
  • Loading branch information
dbonf committed Aug 9, 2023
1 parent 62ec437 commit 6c1cc4b
Show file tree
Hide file tree
Showing 22 changed files with 1,048 additions and 111 deletions.
92 changes: 92 additions & 0 deletions sysdig/internal/client/v2/alerts_v2.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@ package v2

import (
"context"
"errors"
"fmt"
"io"
"log"
"net/http"
"sync"
)

var AlertV2NotFound = errors.New("alert not found")

type AlertV2Type string
type AlertV2Severity string
type AlertLinkV2Type string
Expand All @@ -22,6 +25,7 @@ const (
AlertV2TypePrometheus AlertV2Type = "PROMETHEUS"
AlertV2TypeManual AlertV2Type = "MANUAL"
AlertV2TypeEvent AlertV2Type = "EVENT"
AlertV2TypeChange AlertV2Type = "PERCENTAGE_OF_CHANGE"

AlertV2SeverityHigh AlertV2Severity = "high"
AlertV2SeverityMedium AlertV2Severity = "medium"
Expand All @@ -43,6 +47,7 @@ type AlertV2Interface interface {
AlertV2EventInterface
AlertV2MetricInterface
AlertV2DowntimeInterface
AlertV2ChangeInterface
}

type AlertV2PrometheusInterface interface {
Expand All @@ -69,6 +74,14 @@ type AlertV2MetricInterface interface {
DeleteAlertV2Metric(ctx context.Context, alertID int) error
}

type AlertV2ChangeInterface interface {
Base
CreateAlertV2Change(ctx context.Context, alert AlertV2Change) (AlertV2Change, error)
UpdateAlertV2Change(ctx context.Context, alert AlertV2Change) (AlertV2Change, error)
GetAlertV2Change(ctx context.Context, alertID int) (AlertV2Change, error)
DeleteAlertV2Change(ctx context.Context, alertID int) error
}

type AlertV2DowntimeInterface interface {
Base
CreateAlertV2Downtime(ctx context.Context, alert AlertV2Downtime) (AlertV2Downtime, error)
Expand Down Expand Up @@ -370,6 +383,82 @@ func (client *Client) DeleteAlertV2Downtime(ctx context.Context, alertID int) er
return client.deleteAlertV2(ctx, alertID)
}

func (client *Client) CreateAlertV2Change(ctx context.Context, alert AlertV2Change) (AlertV2Change, error) {
err := client.addNotificationChannelType(ctx, alert.NotificationChannelConfigList)
if err != nil {
return AlertV2Change{}, err
}

err = client.translateScopeSegmentLabels(ctx, &alert.Config.ScopedSegmentedConfig)
if err != nil {
return AlertV2Change{}, err
}

payload, err := Marshal(alertV2ChangeWrapper{Alert: alert})
if err != nil {
return AlertV2Change{}, err
}

body, err := client.createAlertV2(ctx, payload)
if err != nil {
return AlertV2Change{}, err
}

wrapper, err := Unmarshal[alertV2ChangeWrapper](body)
if err != nil {
return AlertV2Change{}, err
}

return wrapper.Alert, nil
}

func (client *Client) UpdateAlertV2Change(ctx context.Context, alert AlertV2Change) (AlertV2Change, error) {
err := client.addNotificationChannelType(ctx, alert.NotificationChannelConfigList)
if err != nil {
return AlertV2Change{}, err
}

err = client.translateScopeSegmentLabels(ctx, &alert.Config.ScopedSegmentedConfig)
if err != nil {
return AlertV2Change{}, err
}

payload, err := Marshal(alertV2ChangeWrapper{Alert: alert})
if err != nil {
return AlertV2Change{}, err
}

body, err := client.updateAlertV2(ctx, alert.ID, payload)
if err != nil {
return AlertV2Change{}, err
}

wrapper, err := Unmarshal[alertV2ChangeWrapper](body)
if err != nil {
return AlertV2Change{}, err
}

return wrapper.Alert, nil
}

func (client *Client) GetAlertV2Change(ctx context.Context, alertID int) (AlertV2Change, error) {
body, err := client.getAlertV2(ctx, alertID)
if err != nil {
return AlertV2Change{}, err
}

wrapper, err := Unmarshal[alertV2ChangeWrapper](body)
if err != nil {
return AlertV2Change{}, err
}

return wrapper.Alert, nil
}

func (client *Client) DeleteAlertV2Change(ctx context.Context, alertID int) error {
return client.deleteAlertV2(ctx, alertID)
}

func (client *Client) createAlertV2(ctx context.Context, alertJson io.Reader) (io.ReadCloser, error) {
response, err := client.requester.Request(ctx, http.MethodPost, client.alertsV2URL(), alertJson)
if err != nil {
Expand Down Expand Up @@ -419,6 +508,9 @@ func (client *Client) getAlertV2(ctx context.Context, alertID int) (io.ReadClose
}
defer response.Body.Close()

if response.StatusCode == http.StatusNotFound {
return nil, AlertV2NotFound
}
if response.StatusCode != http.StatusOK {
return nil, client.ErrorFromResponse(response)
}
Expand Down
39 changes: 34 additions & 5 deletions sysdig/internal/client/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,6 @@ type AlertV2Common struct {
Version int `json:"version,omitempty"`
Name string `json:"name"`
Description string `json:"description,omitempty"`
DurationSec int `json:"durationSec"`
Type string `json:"type"`
Group string `json:"group,omitempty"`
Severity string `json:"severity"`
Expand All @@ -514,7 +513,8 @@ type AlertV2ConfigPrometheus struct {

type AlertV2Prometheus struct {
AlertV2Common
Config AlertV2ConfigPrometheus `json:"config"`
DurationSec int `json:"durationSec"`
Config AlertV2ConfigPrometheus `json:"config"`
}

type alertV2PrometheusWrapper struct {
Expand Down Expand Up @@ -556,7 +556,8 @@ type AlertV2ConfigEvent struct {

type AlertV2Event struct {
AlertV2Common
Config AlertV2ConfigEvent `json:"config"`
DurationSec int `json:"durationSec"`
Config AlertV2ConfigEvent `json:"config"`
}

type alertV2EventWrapper struct {
Expand Down Expand Up @@ -596,7 +597,8 @@ type AlertV2ConfigMetric struct {

type AlertV2Metric struct {
AlertV2Common
Config AlertV2ConfigMetric `json:"config"`
DurationSec int `json:"durationSec"`
Config AlertV2ConfigMetric `json:"config"`
}

type alertV2MetricWrapper struct {
Expand All @@ -617,13 +619,40 @@ type AlertV2ConfigDowntime struct {

type AlertV2Downtime struct {
AlertV2Common
Config AlertV2ConfigDowntime `json:"config"`
DurationSec int `json:"durationSec"`
Config AlertV2ConfigDowntime `json:"config"`
}

type alertV2DowntimeWrapper struct {
Alert AlertV2Downtime `json:"alert"`
}

type AlertV2ConfigChange struct {
ScopedSegmentedConfig

ConditionOperator string `json:"conditionOperator"`
Threshold float64 `json:"threshold"`
WarningConditionOperator string `json:"warningConditionOperator,omitempty"`
WarningThreshold *float64 `json:"warningThreshold,omitempty"`

GroupAggregation string `json:"groupAggregation"`
TimeAggregation string `json:"timeAggregation"`
Metric AlertMetricDescriptorV2 `json:"metric"`

ShorterRangeSec int `json:"shorterRangeSec"`
LongerRangeSec int `json:"longerRangeSec"`
}

type AlertV2Change struct {
AlertV2Common
DurationSec int `json:"durationSec"` // not really used but the api want it set to 0 in POST/PUT
Config AlertV2ConfigChange `json:"config"`
}

type alertV2ChangeWrapper struct {
Alert AlertV2Change `json:"alert"`
}

type CloudAccountCredentialsMonitor struct {
AccountId string `json:"accountId"`
}
Expand Down
1 change: 1 addition & 0 deletions sysdig/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func Provider() *schema.Provider {
"sysdig_monitor_alert_v2_metric": resourceSysdigMonitorAlertV2Metric(),
"sysdig_monitor_alert_v2_downtime": resourceSysdigMonitorAlertV2Downtime(),
"sysdig_monitor_alert_v2_prometheus": resourceSysdigMonitorAlertV2Prometheus(),
"sysdig_monitor_alert_v2_change": resourceSysdigMonitorAlertV2Change(),
"sysdig_monitor_dashboard": resourceSysdigMonitorDashboard(),
"sysdig_monitor_notification_channel_email": resourceSysdigMonitorNotificationChannelEmail(),
"sysdig_monitor_notification_channel_opsgenie": resourceSysdigMonitorNotificationChannelOpsGenie(),
Expand Down
Loading

0 comments on commit 6c1cc4b

Please sign in to comment.