Skip to content

Commit 13b2eb6

Browse files
authored
Support new Secure notification templates for Slack/Teams (#368)
* Support new Secure notification templates for Slack/Teams * Fixes after merge * Fix an issue where MS Teams doesn't have channel field * Fix issue with tests * Address review comments * Fix issue caught by acceptance test * Returning error after setting empty id
1 parent b757a31 commit 13b2eb6

13 files changed

+817
-16
lines changed

sysdig/data_source_sysdig_secure_notification_channel.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ const (
1919
NOTIFICATION_CHANNEL_TYPE_WEBHOOK = "WEBHOOK"
2020
NOTIFICATION_CHANNEL_TYPE_SLACK = "SLACK"
2121
NOTIFICATION_CHANNEL_TYPE_PAGERDUTY = "PAGER_DUTY"
22+
NOTIFICATION_CHANNEL_TYPE_MS_TEAMS = "MS_TEAMS"
23+
24+
NOTIFICATION_CHANNEL_TYPE_SLACK_TEMPLATE_KEY_V1 = "SLACK_SECURE_EVENT_NOTIFICATION_TEMPLATE_METADATA_v1"
25+
NOTIFICATION_CHANNEL_TYPE_SLACK_TEMPLATE_KEY_V2 = "SLACK_SECURE_EVENT_NOTIFICATION_TEMPLATE_METADATA_v2"
26+
NOTIFICATION_CHANNEL_TYPE_MS_TEAMS_TEMPLATE_KEY_V1 = "MS_TEAMS_SECURE_EVENT_NOTIFICATION_TEMPLATE_METADATA_v1"
27+
NOTIFICATION_CHANNEL_TYPE_MS_TEAMS_TEMPLATE_KEY_V2 = "MS_TEAMS_SECURE_EVENT_NOTIFICATION_TEMPLATE_METADATA_v2"
28+
29+
NOTIFICATION_CHANNEL_SECURE_EVENT_NOTIFICATION_CONTENT_SECTION = "SECURE_EVENT_NOTIFICATION_CONTENT"
2230
)
2331

2432
func dataSourceSysdigSecureNotificationChannel() *schema.Resource {

sysdig/internal/client/v2/model.go

Lines changed: 22 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,18 +63,29 @@ type usersWrapper struct {
6363
Users []User `json:"users"`
6464
}
6565

66+
type NotificationChannelTemplateConfigurationSection struct {
67+
SectionName string `json:"sectionName,omitempty"`
68+
ShouldShow bool `json:"shouldShow"`
69+
}
70+
71+
type NotificationChannelTemplateConfiguration struct {
72+
TemplateKey string `json:"templateKey,omitempty"`
73+
TemplateConfigurationSections []NotificationChannelTemplateConfigurationSection `json:"templateConfigurationSections,omitempty"`
74+
}
75+
6676
type NotificationChannelOptions struct {
67-
EmailRecipients []string `json:"emailRecipients,omitempty"` // Type: email
68-
SnsTopicARNs []string `json:"snsTopicARNs,omitempty"` // Type: SNS
69-
APIKey string `json:"apiKey,omitempty"` // Type: VictorOps
70-
RoutingKey string `json:"routingKey,omitempty"` // Type: VictorOps
71-
Url string `json:"url,omitempty"` // Type: OpsGenie, Webhook and Slack
72-
Channel string `json:"channel,omitempty"` // Type: Slack
73-
Account string `json:"account,omitempty"` // Type: PagerDuty
74-
ServiceKey string `json:"serviceKey,omitempty"` // Type: PagerDuty
75-
ServiceName string `json:"serviceName,omitempty"` // Type: PagerDuty
76-
AdditionalHeaders map[string]interface{} `json:"additionalHeaders,omitempty"` // Type: Webhook
77-
Region string `json:"region,omitempty"` // Type: OpsGenie
77+
EmailRecipients []string `json:"emailRecipients,omitempty"` // Type: email
78+
SnsTopicARNs []string `json:"snsTopicARNs,omitempty"` // Type: SNS
79+
APIKey string `json:"apiKey,omitempty"` // Type: VictorOps
80+
RoutingKey string `json:"routingKey,omitempty"` // Type: VictorOps
81+
Url string `json:"url,omitempty"` // Type: OpsGenie, Webhook and Slack
82+
Channel string `json:"channel,omitempty"` // Type: Slack
83+
Account string `json:"account,omitempty"` // Type: PagerDuty
84+
ServiceKey string `json:"serviceKey,omitempty"` // Type: PagerDuty
85+
ServiceName string `json:"serviceName,omitempty"` // Type: PagerDuty
86+
AdditionalHeaders map[string]interface{} `json:"additionalHeaders,omitempty"` // Type: Webhook
87+
Region string `json:"region,omitempty"` // Type: OpsGenie
88+
TemplateConfiguration []NotificationChannelTemplateConfiguration `json:"templateConfiguration,omitempty"`
7889

7990
NotifyOnOk bool `json:"notifyOnOk"`
8091
NotifyOnResolve bool `json:"notifyOnResolve"`

sysdig/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ func Provider() *schema.Provider {
114114
"sysdig_secure_notification_channel_webhook": resourceSysdigSecureNotificationChannelWebhook(),
115115
"sysdig_secure_notification_channel_slack": resourceSysdigSecureNotificationChannelSlack(),
116116
"sysdig_secure_notification_channel_pagerduty": resourceSysdigSecureNotificationChannelPagerduty(),
117+
"sysdig_secure_notification_channel_msteams": resourceSysdigSecureNotificationChannelMSTeams(),
117118
"sysdig_secure_rule_container": resourceSysdigSecureRuleContainer(),
118119
"sysdig_secure_rule_filesystem": resourceSysdigSecureRuleFilesystem(),
119120
"sysdig_secure_rule_network": resourceSysdigSecureRuleNetwork(),
@@ -147,6 +148,7 @@ func Provider() *schema.Provider {
147148
"sysdig_monitor_notification_channel_sns": resourceSysdigMonitorNotificationChannelSNS(),
148149
"sysdig_monitor_notification_channel_victorops": resourceSysdigMonitorNotificationChannelVictorOps(),
149150
"sysdig_monitor_notification_channel_webhook": resourceSysdigMonitorNotificationChannelWebhook(),
151+
"sysdig_monitor_notification_channel_msteams": resourceSysdigMonitorNotificationChannelMSTeams(),
150152
"sysdig_monitor_team": resourceSysdigMonitorTeam(),
151153
"sysdig_monitor_cloud_account": resourceSysdigMonitorCloudAccount(),
152154
},
Lines changed: 165 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,165 @@
1+
package sysdig
2+
3+
import (
4+
"context"
5+
"strconv"
6+
"time"
7+
8+
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
9+
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
11+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
12+
)
13+
14+
func resourceSysdigMonitorNotificationChannelMSTeams() *schema.Resource {
15+
timeout := 5 * time.Minute
16+
17+
return &schema.Resource{
18+
CreateContext: resourceSysdigMonitorNotificationChannelMSTeamsCreate,
19+
UpdateContext: resourceSysdigMonitorNotificationChannelMSTeamsUpdate,
20+
ReadContext: resourceSysdigMonitorNotificationChannelMSTeamsRead,
21+
DeleteContext: resourceSysdigMonitorNotificationChannelMSTeamsDelete,
22+
Importer: &schema.ResourceImporter{
23+
StateContext: schema.ImportStatePassthroughContext,
24+
},
25+
26+
Timeouts: &schema.ResourceTimeout{
27+
Create: schema.DefaultTimeout(timeout),
28+
Update: schema.DefaultTimeout(timeout),
29+
Read: schema.DefaultTimeout(timeout),
30+
Delete: schema.DefaultTimeout(timeout),
31+
},
32+
33+
Schema: createMonitorNotificationChannelSchema(map[string]*schema.Schema{
34+
"url": {
35+
Type: schema.TypeString,
36+
Required: true,
37+
},
38+
}),
39+
}
40+
}
41+
42+
func resourceSysdigMonitorNotificationChannelMSTeamsCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
43+
clients := meta.(SysdigClients)
44+
client, err := getMonitorNotificationChannelClient(clients)
45+
if err != nil {
46+
return diag.FromErr(err)
47+
}
48+
49+
teamID, err := client.CurrentTeamID(ctx)
50+
if err != nil {
51+
return diag.FromErr(err)
52+
}
53+
54+
notificationChannel, err := monitorNotificationChannelMSTeamsFromResourceData(d, teamID)
55+
if err != nil {
56+
return diag.FromErr(err)
57+
}
58+
59+
notificationChannel, err = client.CreateNotificationChannel(ctx, notificationChannel)
60+
if err != nil {
61+
return diag.FromErr(err)
62+
}
63+
64+
d.SetId(strconv.Itoa(notificationChannel.ID))
65+
66+
return resourceSysdigMonitorNotificationChannelMSTeamsRead(ctx, d, meta)
67+
}
68+
69+
func resourceSysdigMonitorNotificationChannelMSTeamsRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
70+
client, err := getMonitorNotificationChannelClient(meta.(SysdigClients))
71+
if err != nil {
72+
return diag.FromErr(err)
73+
}
74+
75+
id, err := strconv.Atoi(d.Id())
76+
if err != nil {
77+
return diag.FromErr(err)
78+
}
79+
80+
nc, err := client.GetNotificationChannelById(ctx, id)
81+
82+
if err != nil {
83+
d.SetId("")
84+
return diag.FromErr(err)
85+
}
86+
87+
err = monitorNotificationChannelMSTeamsToResourceData(&nc, d)
88+
if err != nil {
89+
return diag.FromErr(err)
90+
}
91+
92+
return nil
93+
}
94+
95+
func resourceSysdigMonitorNotificationChannelMSTeamsUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
96+
clients := meta.(SysdigClients)
97+
client, err := getMonitorNotificationChannelClient(clients)
98+
if err != nil {
99+
return diag.FromErr(err)
100+
}
101+
102+
teamID, err := client.CurrentTeamID(ctx)
103+
if err != nil {
104+
return diag.FromErr(err)
105+
}
106+
107+
nc, err := monitorNotificationChannelMSTeamsFromResourceData(d, teamID)
108+
if err != nil {
109+
return diag.FromErr(err)
110+
}
111+
112+
nc.Version = d.Get("version").(int)
113+
nc.ID, err = strconv.Atoi(d.Id())
114+
if err != nil {
115+
return diag.FromErr(err)
116+
}
117+
118+
_, err = client.UpdateNotificationChannel(ctx, nc)
119+
if err != nil {
120+
return diag.FromErr(err)
121+
}
122+
123+
return resourceSysdigMonitorNotificationChannelMSTeamsRead(ctx, d, meta)
124+
}
125+
126+
func resourceSysdigMonitorNotificationChannelMSTeamsDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
127+
client, err := getMonitorNotificationChannelClient(meta.(SysdigClients))
128+
if err != nil {
129+
return diag.FromErr(err)
130+
}
131+
132+
id, err := strconv.Atoi(d.Id())
133+
if err != nil {
134+
return diag.FromErr(err)
135+
}
136+
137+
err = client.DeleteNotificationChannel(ctx, id)
138+
if err != nil {
139+
return diag.FromErr(err)
140+
}
141+
142+
return nil
143+
}
144+
145+
func monitorNotificationChannelMSTeamsFromResourceData(d *schema.ResourceData, teamID int) (nc v2.NotificationChannel, err error) {
146+
nc, err = monitorNotificationChannelFromResourceData(d, teamID)
147+
if err != nil {
148+
return
149+
}
150+
151+
nc.Type = NOTIFICATION_CHANNEL_TYPE_MS_TEAMS
152+
nc.Options.Url = d.Get("url").(string)
153+
return
154+
}
155+
156+
func monitorNotificationChannelMSTeamsToResourceData(nc *v2.NotificationChannel, d *schema.ResourceData) (err error) {
157+
err = monitorNotificationChannelToResourceData(nc, d)
158+
if err != nil {
159+
return
160+
}
161+
162+
_ = d.Set("url", nc.Options.Url)
163+
164+
return
165+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//go:build tf_acc_sysdig_monitor || tf_acc_sysdig_common || tf_acc_ibm_monitor || tf_acc_ibm_common
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 TestAccMonitorNotificationChannelMSTeams(t *testing.T) {
17+
rText := func() string { return 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: monitorNotificationChannelMSTeamsWithName(rText()),
29+
},
30+
{
31+
Config: monitorNotificationChannelMSTeamsSharedWithCurrentTeam(rText()),
32+
},
33+
{
34+
ResourceName: "sysdig_monitor_notification_channel_msteams.sample-msteams",
35+
ImportState: true,
36+
ImportStateVerify: true,
37+
},
38+
},
39+
})
40+
}
41+
42+
func monitorNotificationChannelMSTeamsWithName(name string) string {
43+
return fmt.Sprintf(`
44+
resource "sysdig_monitor_notification_channel_msteams" "sample-msteams" {
45+
name = "Example Channel %s - MS Teams"
46+
enabled = true
47+
url = "https://hooks.msteams.cwom/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"
48+
notify_when_ok = true
49+
notify_when_resolved = true
50+
}`, name)
51+
}
52+
53+
func monitorNotificationChannelMSTeamsSharedWithCurrentTeam(name string) string {
54+
return fmt.Sprintf(`
55+
resource "sysdig_monitor_notification_channel_msteams" "sample-msteams" {
56+
name = "Example Channel %s - MS Teams"
57+
share_with_current_team = true
58+
enabled = true
59+
url = "https://hooks.msteams.cwom/services/XXXXXXXXX/XXXXXXXXX/XXXXXXXXXXXXXXXXXXXXXXXX"
60+
notify_when_ok = true
61+
notify_when_resolved = true
62+
}`, name)
63+
}

0 commit comments

Comments
 (0)