Skip to content

Commit

Permalink
feat(monitor): resource for silence rule
Browse files Browse the repository at this point in the history
  • Loading branch information
dbonf committed Aug 11, 2023
1 parent 6b6343c commit e0b2d1e
Show file tree
Hide file tree
Showing 9 changed files with 562 additions and 4 deletions.
1 change: 1 addition & 0 deletions sysdig/internal/client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ type MonitorCommon interface {
AlertInterface
AlertV2Interface
DashboardInterface
SilenceRuleInterface
}

type SecureCommon interface {
Expand Down
13 changes: 13 additions & 0 deletions sysdig/internal/client/v2/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -714,3 +714,16 @@ type IdentityContext struct {
ServiceAccountID int `json:"serviceAccountId"`
ServiceAccountName string `json:"serviceAccountName"`
}

type SilenceRule struct {
Name string `json:"name"`
Enabled bool `json:"enabled"`
StartTs int64 `json:"startTs"`
DurationInSec int `json:"durationInSec"`
Scope string `json:"scope,omitempty"`
AlertIds []int `json:"alertIds,omitempty"`
NotificationChannelIds []int `json:"notificationChannelIds,omitempty"`

Version int `json:"version,omitempty"`
ID int `json:"id,omitempty"`
}
105 changes: 105 additions & 0 deletions sysdig/internal/client/v2/silence_rule.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
package v2

import (
"context"
"errors"
"fmt"
"net/http"
)

const (
silenceRulesPath = "%s/api/v1/silencingRules"
silenceRulePath = "%s/api/v1/silencingRules/%d"
)

var SilenceRuleNotFound = errors.New("silence rule not found")

type SilenceRuleInterface interface {
Base
GetSilenceRule(ctx context.Context, id int) (SilenceRule, error)
CreateSilenceRule(ctx context.Context, silenceRule SilenceRule) (SilenceRule, error)
UpdateSilenceRule(ctx context.Context, silenceRule SilenceRule) (SilenceRule, error)
DeleteSilenceRule(ctx context.Context, id int) error
}

func (client *Client) GetSilenceRule(ctx context.Context, id int) (SilenceRule, error) {
response, err := client.requester.Request(ctx, http.MethodGet, client.getSilenceRuleURL(id), nil)
if err != nil {
return SilenceRule{}, err
}
defer response.Body.Close()

if response.StatusCode == http.StatusNotFound {
return SilenceRule{}, SilenceRuleNotFound
}
if response.StatusCode != http.StatusOK {
return SilenceRule{}, client.ErrorFromResponse(response)
}

silenceRule, err := Unmarshal[SilenceRule](response.Body)
if err != nil {
return SilenceRule{}, err
}

return silenceRule, nil
}

func (client *Client) CreateSilenceRule(ctx context.Context, silenceRule SilenceRule) (SilenceRule, error) {
payload, err := Marshal(silenceRule)
if err != nil {
return SilenceRule{}, err
}

response, err := client.requester.Request(ctx, http.MethodPost, client.getSilenceRulesURL(), payload)
if err != nil {
return SilenceRule{}, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
return SilenceRule{}, client.ErrorFromResponse(response)
}

return Unmarshal[SilenceRule](response.Body)
}

func (client *Client) UpdateSilenceRule(ctx context.Context, silenceRule SilenceRule) (SilenceRule, error) {
payload, err := Marshal(silenceRule)
if err != nil {
return SilenceRule{}, err
}

response, err := client.requester.Request(ctx, http.MethodPut, client.getSilenceRuleURL(silenceRule.ID), payload)
if err != nil {
return SilenceRule{}, err
}
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
return SilenceRule{}, client.ErrorFromResponse(response)
}

return Unmarshal[SilenceRule](response.Body)
}

func (client *Client) DeleteSilenceRule(ctx context.Context, id int) error {
response, err := client.requester.Request(ctx, http.MethodDelete, client.getSilenceRuleURL(id), nil)
if err != nil {
return err
}
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
return client.ErrorFromResponse(response)
}

return nil
}

func (client *Client) getSilenceRulesURL() string {
return fmt.Sprintf(silenceRulesPath, client.config.url)
}

func (client *Client) getSilenceRuleURL(id int) string {
return fmt.Sprintf(silenceRulePath, client.config.url, id)
}
1 change: 1 addition & 0 deletions sysdig/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ func Provider() *schema.Provider {
"sysdig_secure_scanning_policy": resourceSysdigSecureScanningPolicy(),
"sysdig_secure_scanning_policy_assignment": resourceSysdigSecureScanningPolicyAssignment(),

"sysdig_monitor_silence_rule": resourceSysdigMonitorSilenceRule(),
"sysdig_monitor_alert_downtime": resourceSysdigMonitorAlertDowntime(),
"sysdig_monitor_alert_metric": resourceSysdigMonitorAlertMetric(),
"sysdig_monitor_alert_event": resourceSysdigMonitorAlertEvent(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestAccMonitorNotificationChannelTeamEmail(t *testing.T) {
func monitorNotificationChannelTeamEmailWithName(name string) string {
return fmt.Sprintf(`
resource "sysdig_monitor_team" "sample" {
name = "monitor-sample"
name = "monitor-sample-%s"
entrypoint {
type = "Explore"
}
Expand All @@ -58,13 +58,13 @@ resource "sysdig_monitor_notification_channel_team_email" "sample_team_email1" {
team_id = sysdig_monitor_team.sample.id
notify_when_ok = true
notify_when_resolved = true
}`, name)
}`, name, name)
}

func monitorNotificationChannelTeamEmailSharedWithCurrentTeam(name string) string {
return fmt.Sprintf(`
resource "sysdig_monitor_team" "sample" {
name = "monitor-sample"
name = "monitor-sample-%s"
entrypoint {
type = "Explore"
}
Expand All @@ -76,5 +76,5 @@ resource "sysdig_monitor_notification_channel_team_email" "sample_team_email2" {
notify_when_ok = true
notify_when_resolved = true
share_with_current_team = true
}`, name)
}`, name, name)
}
Loading

0 comments on commit e0b2d1e

Please sign in to comment.