@@ -2,13 +2,16 @@ package v2
22
33import (
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+
1215type AlertV2Type string
1316type AlertV2Severity string
1417type 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
4853type 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+
7285type 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+
373462func (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 }
0 commit comments