Skip to content

Commit 6b7054d

Browse files
authored
feat(notification channel): add data sources for notification channels (#397)
* feat(notification channel): add data sources for monitor notification channels * feat(notification channel): add data sources for secure notification channels
1 parent 69618be commit 6b7054d

File tree

73 files changed

+3370
-57
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

73 files changed

+3370
-57
lines changed
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"strconv"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceSysdigMonitorNotificationChannelCustomWebhook() *schema.Resource {
13+
timeout := 5 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigMonitorNotificationChannelCustomWebhookRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: createMonitorNotificationChannelSchema(map[string]*schema.Schema{
23+
"url": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
"http_method": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
31+
"template": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
"allow_insecure_connections": {
36+
Type: schema.TypeBool,
37+
Computed: true,
38+
},
39+
"additional_headers": {
40+
Type: schema.TypeMap,
41+
Computed: true,
42+
},
43+
}),
44+
}
45+
}
46+
47+
func dataSourceSysdigMonitorNotificationChannelCustomWebhookRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
48+
client, err := getMonitorNotificationChannelClient(meta.(SysdigClients))
49+
if err != nil {
50+
return diag.FromErr(err)
51+
}
52+
53+
nc, err := client.GetNotificationChannelByName(ctx, d.Get("name").(string))
54+
if err != nil {
55+
return diag.FromErr(err)
56+
}
57+
58+
err = monitorNotificationChannelCustomWebhookToResourceData(&nc, d)
59+
if err != nil {
60+
return diag.FromErr(err)
61+
}
62+
63+
d.SetId(strconv.Itoa(nc.ID))
64+
65+
return nil
66+
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
//go:build tf_acc_sysdig_monitor || tf_acc_ibm_monitor
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
13+
"github.com/draios/terraform-provider-sysdig/sysdig"
14+
)
15+
16+
func TestAccMonitorNotificationChannelCustomWebhookDataSource(t *testing.T) {
17+
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: sysdigOrIBMMonitorPreCheck(t),
21+
ProviderFactories: map[string]func() (*schema.Provider, error){
22+
"sysdig": func() (*schema.Provider, error) {
23+
return sysdig.Provider(), nil
24+
},
25+
},
26+
Steps: []resource.TestStep{
27+
{
28+
Config: monitorNotificationChannelCustomWebhook(rText),
29+
Check: resource.ComposeAggregateTestCheckFunc(
30+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "id", "sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "id"),
31+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "name", "sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "name"),
32+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "url", "sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "url"),
33+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "http_method", "sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "http_method"),
34+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "template", "sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "template"),
35+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "allow_insecure_connections", "sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook", "allow_insecure_connections"),
36+
),
37+
},
38+
},
39+
})
40+
}
41+
42+
func monitorNotificationChannelCustomWebhook(name string) string {
43+
return fmt.Sprintf(`
44+
resource "sysdig_monitor_notification_channel_custom_webhook" "nc_custom_webhook" {
45+
name = "%s"
46+
url = "https://example.com/"
47+
http_method = "POST"
48+
template = "{\n \"code\": \"incident\",\n \"alert\": \"{{@alert_name}}\"\n}"
49+
allow_insecure_connections = true
50+
}
51+
52+
data "sysdig_monitor_notification_channel_custom_webhook" "nc_custom_webhook" {
53+
name = sysdig_monitor_notification_channel_custom_webhook.nc_custom_webhook.name
54+
}
55+
`, name)
56+
}

sysdig/data_source_sysdig_monitor_notification_channel_email_test.go

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package sysdig_test
44

55
import (
66
"fmt"
7-
"os"
87
"testing"
98

109
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
@@ -14,26 +13,19 @@ import (
1413
"github.com/draios/terraform-provider-sysdig/sysdig"
1514
)
1615

17-
func TestAccNotificationChannelEmailDataSource(t *testing.T) {
16+
func TestAccMonitorNotificationChannelEmailDataSource(t *testing.T) {
1817
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
1918

2019
resource.ParallelTest(t, resource.TestCase{
21-
PreCheck: func() {
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")
26-
}
27-
},
20+
PreCheck: sysdigOrIBMMonitorPreCheck(t),
2821
ProviderFactories: map[string]func() (*schema.Provider, error){
2922
"sysdig": func() (*schema.Provider, error) {
3023
return sysdig.Provider(), nil
3124
},
3225
},
33-
3426
Steps: []resource.TestStep{
3527
{
36-
Config: notificationChannelEmail(rText),
28+
Config: monitorNotificationChannelEmail(rText),
3729
Check: resource.ComposeAggregateTestCheckFunc(
3830
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_email.nc_email", "id", "sysdig_monitor_notification_channel_email.nc_email", "id"),
3931
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_email.nc_email", "name", "sysdig_monitor_notification_channel_email.nc_email", "name"),
@@ -44,7 +36,7 @@ func TestAccNotificationChannelEmailDataSource(t *testing.T) {
4436
})
4537
}
4638

47-
func notificationChannelEmail(name string) string {
39+
func monitorNotificationChannelEmail(name string) string {
4840
return fmt.Sprintf(`
4941
resource "sysdig_monitor_notification_channel_email" "nc_email" {
5042
name = "%s"
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"strconv"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceSysdigMonitorNotificationChannelGoogleChat() *schema.Resource {
13+
timeout := 5 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigMonitorNotificationChannelGoogleChatRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: createMonitorNotificationChannelSchema(map[string]*schema.Schema{
23+
"url": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
}),
28+
}
29+
}
30+
31+
func dataSourceSysdigMonitorNotificationChannelGoogleChatRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
32+
client, err := getMonitorNotificationChannelClient(meta.(SysdigClients))
33+
if err != nil {
34+
return diag.FromErr(err)
35+
}
36+
37+
nc, err := client.GetNotificationChannelByName(ctx, d.Get("name").(string))
38+
if err != nil {
39+
return diag.FromErr(err)
40+
}
41+
42+
err = monitorNotificationChannelGoogleChatToResourceData(&nc, d)
43+
if err != nil {
44+
return diag.FromErr(err)
45+
}
46+
47+
d.SetId(strconv.Itoa(nc.ID))
48+
49+
return nil
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
//go:build tf_acc_sysdig_monitor || tf_acc_ibm_monitor
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
13+
"github.com/draios/terraform-provider-sysdig/sysdig"
14+
)
15+
16+
func TestAccMonitorNotificationChannelGoogleChatDataSource(t *testing.T) {
17+
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
18+
19+
resource.ParallelTest(t, resource.TestCase{
20+
PreCheck: sysdigOrIBMMonitorPreCheck(t),
21+
ProviderFactories: map[string]func() (*schema.Provider, error){
22+
"sysdig": func() (*schema.Provider, error) {
23+
return sysdig.Provider(), nil
24+
},
25+
},
26+
Steps: []resource.TestStep{
27+
{
28+
Config: monitorNotificationChannelGoogleChat(rText),
29+
Check: resource.ComposeAggregateTestCheckFunc(
30+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_google_chat.nc_google_chat", "id", "sysdig_monitor_notification_channel_google_chat.nc_google_chat", "id"),
31+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_google_chat.nc_google_chat", "name", "sysdig_monitor_notification_channel_google_chat.nc_google_chat", "name"),
32+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_google_chat.nc_google_chat", "url", "sysdig_monitor_notification_channel_google_chat.nc_google_chat", "url"),
33+
),
34+
},
35+
},
36+
})
37+
}
38+
39+
func monitorNotificationChannelGoogleChat(name string) string {
40+
return fmt.Sprintf(`
41+
resource "sysdig_monitor_notification_channel_google_chat" "nc_google_chat" {
42+
name = "Example Channel %s - google chat"
43+
url = "https://chat.googleapis.com/v1/spaces/XXXXXX/messages?key=XXXXXXXXXXXXXXXXX"
44+
}
45+
46+
data "sysdig_monitor_notification_channel_google_chat" "nc_google_chat" {
47+
name = sysdig_monitor_notification_channel_google_chat.nc_google_chat.name
48+
}
49+
`, name)
50+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"strconv"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
)
11+
12+
func dataSourceSysdigMonitorNotificationChannelIBMEventNotification() *schema.Resource {
13+
timeout := 5 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigMonitorNotificationChannelIBMEventNotificationRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: createMonitorNotificationChannelSchema(map[string]*schema.Schema{
23+
"instance_id": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
}),
28+
}
29+
}
30+
31+
func dataSourceSysdigMonitorNotificationChannelIBMEventNotificationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
32+
client, err := getMonitorNotificationChannelClient(meta.(SysdigClients))
33+
if err != nil {
34+
return diag.FromErr(err)
35+
}
36+
37+
nc, err := client.GetNotificationChannelByName(ctx, d.Get("name").(string))
38+
if err != nil {
39+
return diag.FromErr(err)
40+
}
41+
42+
err = monitorNotificationChannelIBMEventNotificationToResourceData(&nc, d)
43+
if err != nil {
44+
return diag.FromErr(err)
45+
}
46+
47+
d.SetId(strconv.Itoa(nc.ID))
48+
49+
return nil
50+
}
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
//go:build tf_acc_sysdig_monitor || tf_acc_ibm_monitor
2+
3+
package sysdig_test
4+
5+
import (
6+
"fmt"
7+
"os"
8+
"testing"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
12+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
13+
14+
"github.com/draios/terraform-provider-sysdig/sysdig"
15+
)
16+
17+
func TestAccMonitorNotificationChannelIBMEventNotificationDataSource(t *testing.T) {
18+
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
19+
20+
ibmEventNotificationInstanceId := os.Getenv("IBM_EVENT_NOTIFICATION_INSTANCE_ID")
21+
if ibmEventNotificationInstanceId == "" {
22+
t.Skip("Skipping tests on sysdig_monitor_notification_channel_ibm_event_notification resource because IBM_EVENT_NOTIFICATION_INSTANCE_ID is not set")
23+
return
24+
}
25+
26+
resource.ParallelTest(t, resource.TestCase{
27+
PreCheck: preCheckAnyEnv(t, SysdigIBMMonitorAPIKeyEnv),
28+
ProviderFactories: map[string]func() (*schema.Provider, error){
29+
"sysdig": func() (*schema.Provider, error) {
30+
return sysdig.Provider(), nil
31+
},
32+
},
33+
Steps: []resource.TestStep{
34+
{
35+
Config: monitorNotificationChannelIBMEventNotification(rText, ibmEventNotificationInstanceId),
36+
Check: resource.ComposeAggregateTestCheckFunc(
37+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_ibm_event_notification.nc_ibm_event_notification", "id", "sysdig_monitor_notification_channel_ibm_event_notification.nc_ibm_event_notification", "id"),
38+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_ibm_event_notification.nc_ibm_event_notification", "name", "sysdig_monitor_notification_channel_ibm_event_notification.nc_ibm_event_notification", "name"),
39+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_ibm_event_notification.nc_ibm_event_notification", "instance_id", "sysdig_monitor_notification_channel_ibm_event_notification.nc_ibm_event_notification", "instance_id"),
40+
),
41+
},
42+
},
43+
})
44+
}
45+
46+
func monitorNotificationChannelIBMEventNotification(name, ibmEventNotificationInstanceId string) string {
47+
return fmt.Sprintf(`
48+
resource "sysdig_monitor_notification_channel_ibm_event_notification" "nc_ibm_event_notification" {
49+
name = "Example Channel %s - IBM Event Notification"
50+
instance_id = "%s"
51+
}
52+
53+
data "sysdig_monitor_notification_channel_ibm_event_notification" "nc_ibm_event_notification" {
54+
name = sysdig_monitor_notification_channel_ibm_event_notification.nc_ibm_event_notification.name
55+
}
56+
`, name, ibmEventNotificationInstanceId)
57+
}

0 commit comments

Comments
 (0)