Skip to content

Commit 5f737ea

Browse files
authored
Refactor notification channels to use new v2 client (#291)
* feat(notificationchannels): refactor monitor notification channels to use v2 client
1 parent 50fd79a commit 5f737ea

28 files changed

+395
-239
lines changed

go.mod

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ require (
88
github.com/falcosecurity/kilt/runtimes/cloudformation v0.0.0-20211102134108-7c97c711c0d2
99
github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637
1010
github.com/hashicorp/go-retryablehttp v0.7.0
11-
github.com/hashicorp/terraform-plugin-sdk/v2 v2.24.0
11+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.26.1
1212
github.com/jmespath/go-jmespath v0.4.0
1313
github.com/rs/zerolog v1.26.1
1414
github.com/spf13/cast v1.4.1
@@ -17,7 +17,6 @@ require (
1717

1818
require (
1919
github.com/agext/levenshtein v1.2.3 // indirect
20-
github.com/apparentlymart/go-cidr v1.1.0 // indirect
2120
github.com/apparentlymart/go-textseg/v13 v13.0.0 // indirect
2221
github.com/containerd/stargz-snapshotter/estargz v0.10.1 // indirect
2322
github.com/davecgh/go-spew v1.1.1 // indirect
@@ -34,19 +33,19 @@ require (
3433
github.com/hashicorp/errwrap v1.1.0 // indirect
3534
github.com/hashicorp/go-checkpoint v0.5.0 // indirect
3635
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
37-
github.com/hashicorp/go-hclog v1.2.1 // indirect
36+
github.com/hashicorp/go-hclog v1.4.0 // indirect
3837
github.com/hashicorp/go-multierror v1.1.1 // indirect
39-
github.com/hashicorp/go-plugin v1.4.4 // indirect
38+
github.com/hashicorp/go-plugin v1.4.8 // indirect
4039
github.com/hashicorp/go-uuid v1.0.3 // indirect
4140
github.com/hashicorp/go-version v1.6.0 // indirect
42-
github.com/hashicorp/hc-install v0.4.0 // indirect
43-
github.com/hashicorp/hcl/v2 v2.14.1 // indirect
41+
github.com/hashicorp/hc-install v0.5.0 // indirect
42+
github.com/hashicorp/hcl/v2 v2.16.2 // indirect
4443
github.com/hashicorp/logutils v1.0.0 // indirect
45-
github.com/hashicorp/terraform-exec v0.17.3 // indirect
46-
github.com/hashicorp/terraform-json v0.14.0 // indirect
47-
github.com/hashicorp/terraform-plugin-go v0.14.0 // indirect
48-
github.com/hashicorp/terraform-plugin-log v0.7.0 // indirect
49-
github.com/hashicorp/terraform-registry-address v0.0.0-20220623143253-7d51757b572c // indirect
44+
github.com/hashicorp/terraform-exec v0.18.1 // indirect
45+
github.com/hashicorp/terraform-json v0.16.0 // indirect
46+
github.com/hashicorp/terraform-plugin-go v0.14.3 // indirect
47+
github.com/hashicorp/terraform-plugin-log v0.8.0 // indirect
48+
github.com/hashicorp/terraform-registry-address v0.1.0 // indirect
5049
github.com/hashicorp/terraform-svchost v0.0.0-20200729002733-f050f53b9734 // indirect
5150
github.com/hashicorp/yamux v0.0.0-20211028200310-0bc27b27de87 // indirect
5251
github.com/klauspost/compress v1.14.1 // indirect
@@ -68,15 +67,16 @@ require (
6867
github.com/vmihailenco/msgpack v4.0.4+incompatible // indirect
6968
github.com/vmihailenco/msgpack/v4 v4.3.12 // indirect
7069
github.com/vmihailenco/tagparser v0.1.1 // indirect
71-
github.com/zclconf/go-cty v1.11.0 // indirect
72-
golang.org/x/crypto v0.0.0-20220517005047-85d78b3ac167 // indirect
73-
golang.org/x/net v0.7.0 // indirect
74-
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c // indirect
75-
golang.org/x/sys v0.5.0 // indirect
76-
golang.org/x/text v0.7.0 // indirect
70+
github.com/zclconf/go-cty v1.13.1 // indirect
71+
golang.org/x/crypto v0.7.0 // indirect
72+
golang.org/x/mod v0.8.0 // indirect
73+
golang.org/x/net v0.8.0 // indirect
74+
golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4 // indirect
75+
golang.org/x/sys v0.6.0 // indirect
76+
golang.org/x/text v0.8.0 // indirect
7777
google.golang.org/appengine v1.6.7 // indirect
7878
google.golang.org/genproto v0.0.0-20220111164026-67b88f271998 // indirect
79-
google.golang.org/grpc v1.48.0 // indirect
79+
google.golang.org/grpc v1.51.0 // indirect
8080
google.golang.org/protobuf v1.28.1 // indirect
8181
gopkg.in/yaml.v3 v3.0.1 // indirect
8282
)

go.sum

Lines changed: 63 additions & 46 deletions
Large diffs are not rendered by default.

sysdig/data_source_sysdig_monitor_notification_channel_email.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,7 @@ func dataSourceSysdigMonitorNotificationChannelEmail() *schema.Resource {
3030
}
3131

3232
func dataSourceSysdigMonitorNotificationChannelEmailRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
33-
client, err := meta.(SysdigClients).sysdigMonitorClient()
34-
33+
client, err := getMonitorNotificationChannelClient(meta.(SysdigClients))
3534
if err != nil {
3635
return diag.FromErr(err)
3736
}

sysdig/data_source_sysdig_monitor_notification_channel_email_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build tf_acc_sysdig
1+
//go:build tf_acc_sysdig || tf_acc_ibm
22

33
package sysdig_test
44

@@ -19,8 +19,10 @@ func TestAccNotificationChannelEmailDataSource(t *testing.T) {
1919

2020
resource.ParallelTest(t, resource.TestCase{
2121
PreCheck: func() {
22-
if v := os.Getenv("SYSDIG_MONITOR_API_TOKEN"); v == "" {
23-
t.Fatal("SYSDIG_MONITOR_API_TOKEN must be set for acceptance tests")
22+
monitor := os.Getenv("SYSDIG_MONITOR_API_TOKEN")
23+
ibmMonitor := os.Getenv("SYSDIG_IBM_MONITOR_API_KEY")
24+
if monitor == "" && ibmMonitor == "" {
25+
t.Fatal("SYSDIG_MONITOR_API_TOKEN or SYSDIG_IBM_MONITOR_API_KEY must be set for acceptance tests")
2426
}
2527
},
2628
ProviderFactories: map[string]func() (*schema.Provider, error){

sysdig/data_source_sysdig_monitor_notification_channel_pagerduty.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ func dataSourceSysdigMonitorNotificationChannelPagerduty() *schema.Resource {
3737
}
3838

3939
func dataSourceSysdigMonitorNotificationChannelPagerdutyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
40-
client, err := meta.(SysdigClients).sysdigMonitorClient()
40+
client, err := getMonitorNotificationChannelClient(meta.(SysdigClients))
4141

4242
if err != nil {
4343
return diag.FromErr(err)

sysdig/data_source_sysdig_monitor_notification_channel_pagerduty_test.go

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
//go:build tf_acc_sysdig
1+
//go:build tf_acc_sysdig || tf_acc_ibm
22

33
package sysdig_test
44

@@ -19,8 +19,10 @@ func TestAccNotificationChannelPagerdutyDataSource(t *testing.T) {
1919

2020
resource.ParallelTest(t, resource.TestCase{
2121
PreCheck: func() {
22-
if v := os.Getenv("SYSDIG_MONITOR_API_TOKEN"); v == "" {
23-
t.Fatal("SYSDIG_MONITOR_API_TOKEN must be set for acceptance tests")
22+
monitor := os.Getenv("SYSDIG_MONITOR_API_TOKEN")
23+
ibmMonitor := os.Getenv("SYSDIG_IBM_MONITOR_API_KEY")
24+
if monitor == "" && ibmMonitor == "" {
25+
t.Fatal("SYSDIG_MONITOR_API_TOKEN or SYSDIG_IBM_MONITOR_API_KEY must be set for acceptance tests")
2426
}
2527
},
2628
ProviderFactories: map[string]func() (*schema.Provider, error){

sysdig/internal/client/monitor/client.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,11 +41,7 @@ type SysdigMonitorClient interface {
4141

4242
GetLabelDescriptor(ctx context.Context, label string) (LabelDescriptorV3, error)
4343

44-
CreateNotificationChannel(context.Context, NotificationChannel) (NotificationChannel, error)
4544
GetNotificationChannelById(context.Context, int) (NotificationChannel, error)
46-
GetNotificationChannelByName(context.Context, string) (NotificationChannel, error)
47-
DeleteNotificationChannel(context.Context, int) error
48-
UpdateNotificationChannel(context.Context, NotificationChannel) (NotificationChannel, error)
4945

5046
GetDashboardByID(context.Context, int) (*model.Dashboard, error)
5147
CreateDashboard(context.Context, *model.Dashboard) (*model.Dashboard, error)

sysdig/internal/client/monitor/notification_channels.go

Lines changed: 0 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -29,84 +29,6 @@ func (client *sysdigMonitorClient) GetNotificationChannelById(ctx context.Contex
2929
return
3030
}
3131

32-
func (client *sysdigMonitorClient) GetNotificationChannelByName(ctx context.Context, name string) (nc NotificationChannel, err error) {
33-
response, err := client.doSysdigMonitorRequest(ctx, http.MethodGet, client.GetNotificationChannelsUrl(), nil)
34-
if err != nil {
35-
return
36-
}
37-
defer response.Body.Close()
38-
39-
body, _ := io.ReadAll(response.Body)
40-
41-
if response.StatusCode != http.StatusOK {
42-
err = errorFromResponse(response)
43-
return
44-
}
45-
46-
ncList := NotificationChannelListFromJSON(body)
47-
48-
for _, channel := range ncList {
49-
if channel.Name == name {
50-
nc = channel
51-
return
52-
}
53-
}
54-
55-
err = fmt.Errorf("Notification channel with Name: %s does not exist", name)
56-
return
57-
}
58-
59-
func (client *sysdigMonitorClient) CreateNotificationChannel(ctx context.Context, ncRequest NotificationChannel) (nc NotificationChannel, err error) {
60-
response, err := client.doSysdigMonitorRequest(ctx, http.MethodPost, client.GetNotificationChannelsUrl(), ncRequest.ToJSON())
61-
if err != nil {
62-
return
63-
}
64-
defer response.Body.Close()
65-
66-
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
67-
err = errorFromResponse(response)
68-
return
69-
}
70-
71-
body, _ := io.ReadAll(response.Body)
72-
nc = NotificationChannelFromJSON(body)
73-
return
74-
}
75-
76-
func (client *sysdigMonitorClient) UpdateNotificationChannel(ctx context.Context, ncRequest NotificationChannel) (nc NotificationChannel, err error) {
77-
response, err := client.doSysdigMonitorRequest(ctx, http.MethodPut, client.GetNotificationChannelUrl(ncRequest.ID), ncRequest.ToJSON())
78-
if err != nil {
79-
return
80-
}
81-
defer response.Body.Close()
82-
83-
if response.StatusCode != http.StatusOK {
84-
err = errorFromResponse(response)
85-
return
86-
}
87-
88-
body, _ := io.ReadAll(response.Body)
89-
nc = NotificationChannelFromJSON(body)
90-
return
91-
}
92-
93-
func (client *sysdigMonitorClient) DeleteNotificationChannel(ctx context.Context, id int) error {
94-
response, err := client.doSysdigMonitorRequest(ctx, http.MethodDelete, client.GetNotificationChannelUrl(id), nil)
95-
if err != nil {
96-
return err
97-
}
98-
defer response.Body.Close()
99-
100-
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
101-
return errorFromResponse(response)
102-
}
103-
return nil
104-
}
105-
106-
func (client *sysdigMonitorClient) GetNotificationChannelsUrl() string {
107-
return fmt.Sprintf("%s/api/notificationChannels", client.URL)
108-
}
109-
11032
func (client *sysdigMonitorClient) GetNotificationChannelUrl(id int) string {
11133
return fmt.Sprintf("%s/api/notificationChannels/%d", client.URL, id)
11234
}

sysdig/internal/client/v2/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ const (
2525

2626
type Common interface {
2727
TeamInterface
28+
NotificationChannelInterface
2829
}
2930

3031
type Requester interface {

sysdig/internal/client/v2/model.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,3 +53,38 @@ type UsersList struct {
5353
type usersListWrapper struct {
5454
UsersList []UsersList `json:"users"`
5555
}
56+
57+
type NotificationChannelOptions struct {
58+
EmailRecipients []string `json:"emailRecipients,omitempty"` // Type: email
59+
SnsTopicARNs []string `json:"snsTopicARNs,omitempty"` // Type: SNS
60+
APIKey string `json:"apiKey,omitempty"` // Type: VictorOps
61+
RoutingKey string `json:"routingKey,omitempty"` // Type: VictorOps
62+
Url string `json:"url,omitempty"` // Type: OpsGenie, Webhook and Slack
63+
Channel string `json:"channel,omitempty"` // Type: Slack
64+
Account string `json:"account,omitempty"` // Type: PagerDuty
65+
ServiceKey string `json:"serviceKey,omitempty"` // Type: PagerDuty
66+
ServiceName string `json:"serviceName,omitempty"` // Type: PagerDuty
67+
AdditionalHeaders map[string]interface{} `json:"additionalHeaders,omitempty"` // Type: Webhook
68+
Region string `json:"region,omitempty"` // Type: OpsGenie
69+
70+
NotifyOnOk bool `json:"notifyOnOk"`
71+
NotifyOnResolve bool `json:"notifyOnResolve"`
72+
SendTestNotification bool `json:"sendTestNotification"`
73+
}
74+
75+
type NotificationChannel struct {
76+
ID int `json:"id,omitempty"`
77+
Version int `json:"version,omitempty"`
78+
Type string `json:"type"`
79+
Name string `json:"name"`
80+
Enabled bool `json:"enabled"`
81+
Options NotificationChannelOptions `json:"options"`
82+
}
83+
84+
type notificationChannelListWrapper struct {
85+
NotificationChannels []NotificationChannel `json:"notificationChannels"`
86+
}
87+
88+
type notificationChannelWrapper struct {
89+
NotificationChannel NotificationChannel `json:"notificationChannel"`
90+
}

0 commit comments

Comments
 (0)