Skip to content

Commit 76a475a

Browse files
authored
feat(alerts): add change alert type (#393)
1 parent 02c7b6b commit 76a475a

22 files changed

+1048
-111
lines changed

sysdig/internal/client/v2/alerts_v2.go

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,16 @@ package v2
22

33
import (
44
"context"
5+
"errors"
56
"fmt"
67
"io"
78
"log"
89
"net/http"
910
"sync"
1011
)
1112

13+
var AlertV2NotFound = errors.New("alert not found")
14+
1215
type AlertV2Type string
1316
type AlertV2Severity string
1417
type AlertLinkV2Type string
@@ -22,6 +25,7 @@ const (
2225
AlertV2TypePrometheus AlertV2Type = "PROMETHEUS"
2326
AlertV2TypeManual AlertV2Type = "MANUAL"
2427
AlertV2TypeEvent AlertV2Type = "EVENT"
28+
AlertV2TypeChange AlertV2Type = "PERCENTAGE_OF_CHANGE"
2529

2630
AlertV2SeverityHigh AlertV2Severity = "high"
2731
AlertV2SeverityMedium AlertV2Severity = "medium"
@@ -43,6 +47,7 @@ type AlertV2Interface interface {
4347
AlertV2EventInterface
4448
AlertV2MetricInterface
4549
AlertV2DowntimeInterface
50+
AlertV2ChangeInterface
4651
}
4752

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

77+
type AlertV2ChangeInterface interface {
78+
Base
79+
CreateAlertV2Change(ctx context.Context, alert AlertV2Change) (AlertV2Change, error)
80+
UpdateAlertV2Change(ctx context.Context, alert AlertV2Change) (AlertV2Change, error)
81+
GetAlertV2Change(ctx context.Context, alertID int) (AlertV2Change, error)
82+
DeleteAlertV2Change(ctx context.Context, alertID int) error
83+
}
84+
7285
type AlertV2DowntimeInterface interface {
7386
Base
7487
CreateAlertV2Downtime(ctx context.Context, alert AlertV2Downtime) (AlertV2Downtime, error)
@@ -370,6 +383,82 @@ func (client *Client) DeleteAlertV2Downtime(ctx context.Context, alertID int) er
370383
return client.deleteAlertV2(ctx, alertID)
371384
}
372385

386+
func (client *Client) CreateAlertV2Change(ctx context.Context, alert AlertV2Change) (AlertV2Change, error) {
387+
err := client.addNotificationChannelType(ctx, alert.NotificationChannelConfigList)
388+
if err != nil {
389+
return AlertV2Change{}, err
390+
}
391+
392+
err = client.translateScopeSegmentLabels(ctx, &alert.Config.ScopedSegmentedConfig)
393+
if err != nil {
394+
return AlertV2Change{}, err
395+
}
396+
397+
payload, err := Marshal(alertV2ChangeWrapper{Alert: alert})
398+
if err != nil {
399+
return AlertV2Change{}, err
400+
}
401+
402+
body, err := client.createAlertV2(ctx, payload)
403+
if err != nil {
404+
return AlertV2Change{}, err
405+
}
406+
407+
wrapper, err := Unmarshal[alertV2ChangeWrapper](body)
408+
if err != nil {
409+
return AlertV2Change{}, err
410+
}
411+
412+
return wrapper.Alert, nil
413+
}
414+
415+
func (client *Client) UpdateAlertV2Change(ctx context.Context, alert AlertV2Change) (AlertV2Change, error) {
416+
err := client.addNotificationChannelType(ctx, alert.NotificationChannelConfigList)
417+
if err != nil {
418+
return AlertV2Change{}, err
419+
}
420+
421+
err = client.translateScopeSegmentLabels(ctx, &alert.Config.ScopedSegmentedConfig)
422+
if err != nil {
423+
return AlertV2Change{}, err
424+
}
425+
426+
payload, err := Marshal(alertV2ChangeWrapper{Alert: alert})
427+
if err != nil {
428+
return AlertV2Change{}, err
429+
}
430+
431+
body, err := client.updateAlertV2(ctx, alert.ID, payload)
432+
if err != nil {
433+
return AlertV2Change{}, err
434+
}
435+
436+
wrapper, err := Unmarshal[alertV2ChangeWrapper](body)
437+
if err != nil {
438+
return AlertV2Change{}, err
439+
}
440+
441+
return wrapper.Alert, nil
442+
}
443+
444+
func (client *Client) GetAlertV2Change(ctx context.Context, alertID int) (AlertV2Change, error) {
445+
body, err := client.getAlertV2(ctx, alertID)
446+
if err != nil {
447+
return AlertV2Change{}, err
448+
}
449+
450+
wrapper, err := Unmarshal[alertV2ChangeWrapper](body)
451+
if err != nil {
452+
return AlertV2Change{}, err
453+
}
454+
455+
return wrapper.Alert, nil
456+
}
457+
458+
func (client *Client) DeleteAlertV2Change(ctx context.Context, alertID int) error {
459+
return client.deleteAlertV2(ctx, alertID)
460+
}
461+
373462
func (client *Client) createAlertV2(ctx context.Context, alertJson io.Reader) (io.ReadCloser, error) {
374463
response, err := client.requester.Request(ctx, http.MethodPost, client.alertsV2URL(), alertJson)
375464
if err != nil {
@@ -419,6 +508,9 @@ func (client *Client) getAlertV2(ctx context.Context, alertID int) (io.ReadClose
419508
}
420509
defer response.Body.Close()
421510

511+
if response.StatusCode == http.StatusNotFound {
512+
return nil, AlertV2NotFound
513+
}
422514
if response.StatusCode != http.StatusOK {
423515
return nil, client.ErrorFromResponse(response)
424516
}

sysdig/internal/client/v2/model.go

Lines changed: 34 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,6 @@ type AlertV2Common struct {
495495
Version int `json:"version,omitempty"`
496496
Name string `json:"name"`
497497
Description string `json:"description,omitempty"`
498-
DurationSec int `json:"durationSec"`
499498
Type string `json:"type"`
500499
Group string `json:"group,omitempty"`
501500
Severity string `json:"severity"`
@@ -514,7 +513,8 @@ type AlertV2ConfigPrometheus struct {
514513

515514
type AlertV2Prometheus struct {
516515
AlertV2Common
517-
Config AlertV2ConfigPrometheus `json:"config"`
516+
DurationSec int `json:"durationSec"`
517+
Config AlertV2ConfigPrometheus `json:"config"`
518518
}
519519

520520
type alertV2PrometheusWrapper struct {
@@ -556,7 +556,8 @@ type AlertV2ConfigEvent struct {
556556

557557
type AlertV2Event struct {
558558
AlertV2Common
559-
Config AlertV2ConfigEvent `json:"config"`
559+
DurationSec int `json:"durationSec"`
560+
Config AlertV2ConfigEvent `json:"config"`
560561
}
561562

562563
type alertV2EventWrapper struct {
@@ -596,7 +597,8 @@ type AlertV2ConfigMetric struct {
596597

597598
type AlertV2Metric struct {
598599
AlertV2Common
599-
Config AlertV2ConfigMetric `json:"config"`
600+
DurationSec int `json:"durationSec"`
601+
Config AlertV2ConfigMetric `json:"config"`
600602
}
601603

602604
type alertV2MetricWrapper struct {
@@ -617,13 +619,40 @@ type AlertV2ConfigDowntime struct {
617619

618620
type AlertV2Downtime struct {
619621
AlertV2Common
620-
Config AlertV2ConfigDowntime `json:"config"`
622+
DurationSec int `json:"durationSec"`
623+
Config AlertV2ConfigDowntime `json:"config"`
621624
}
622625

623626
type alertV2DowntimeWrapper struct {
624627
Alert AlertV2Downtime `json:"alert"`
625628
}
626629

630+
type AlertV2ConfigChange struct {
631+
ScopedSegmentedConfig
632+
633+
ConditionOperator string `json:"conditionOperator"`
634+
Threshold float64 `json:"threshold"`
635+
WarningConditionOperator string `json:"warningConditionOperator,omitempty"`
636+
WarningThreshold *float64 `json:"warningThreshold,omitempty"`
637+
638+
GroupAggregation string `json:"groupAggregation"`
639+
TimeAggregation string `json:"timeAggregation"`
640+
Metric AlertMetricDescriptorV2 `json:"metric"`
641+
642+
ShorterRangeSec int `json:"shorterRangeSec"`
643+
LongerRangeSec int `json:"longerRangeSec"`
644+
}
645+
646+
type AlertV2Change struct {
647+
AlertV2Common
648+
DurationSec int `json:"durationSec"` // not really used but the api wants it set to 0 in POST/PUT
649+
Config AlertV2ConfigChange `json:"config"`
650+
}
651+
652+
type alertV2ChangeWrapper struct {
653+
Alert AlertV2Change `json:"alert"`
654+
}
655+
627656
type CloudAccountCredentialsMonitor struct {
628657
AccountId string `json:"accountId"`
629658
}

sysdig/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,7 @@ func Provider() *schema.Provider {
145145
"sysdig_monitor_alert_v2_metric": resourceSysdigMonitorAlertV2Metric(),
146146
"sysdig_monitor_alert_v2_downtime": resourceSysdigMonitorAlertV2Downtime(),
147147
"sysdig_monitor_alert_v2_prometheus": resourceSysdigMonitorAlertV2Prometheus(),
148+
"sysdig_monitor_alert_v2_change": resourceSysdigMonitorAlertV2Change(),
148149
"sysdig_monitor_dashboard": resourceSysdigMonitorDashboard(),
149150
"sysdig_monitor_notification_channel_email": resourceSysdigMonitorNotificationChannelEmail(),
150151
"sysdig_monitor_notification_channel_opsgenie": resourceSysdigMonitorNotificationChannelOpsGenie(),

0 commit comments

Comments
 (0)