Skip to content

Commit c65e3e8

Browse files
committed
feat(datasource): Add Notification Channel data source
Signed-off-by: Federico Barcelona <[email protected]>
1 parent ea8c0e5 commit c65e3e8

File tree

6 files changed

+228
-1
lines changed

6 files changed

+228
-1
lines changed
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
1+
package sysdig
2+
3+
import (
4+
"regexp"
5+
"strconv"
6+
"time"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func dataSourceSysdigSecureNotificationChannel() *schema.Resource {
12+
timeout := 30 * time.Second
13+
14+
return &schema.Resource{
15+
Read: dataSourceSysdigNotificationChannelRead,
16+
17+
Timeouts: &schema.ResourceTimeout{
18+
Read: schema.DefaultTimeout(timeout),
19+
},
20+
21+
Schema: map[string]*schema.Schema{
22+
"name": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
},
26+
"enabled": {
27+
Type: schema.TypeBool,
28+
Computed: true,
29+
},
30+
"type": {
31+
Type: schema.TypeString,
32+
Computed: true,
33+
},
34+
"recipients": {
35+
Type: schema.TypeString,
36+
Computed: true,
37+
},
38+
"topics": {
39+
Type: schema.TypeString,
40+
Computed: true,
41+
},
42+
"api_key": {
43+
Type: schema.TypeString,
44+
Computed: true,
45+
},
46+
"routing_key": {
47+
Type: schema.TypeString,
48+
Computed: true,
49+
},
50+
"url": {
51+
Type: schema.TypeString,
52+
Computed: true,
53+
},
54+
"channel": {
55+
Type: schema.TypeString,
56+
Computed: true,
57+
},
58+
"account": {
59+
Type: schema.TypeString,
60+
Computed: true,
61+
},
62+
"service_key": {
63+
Type: schema.TypeString,
64+
Computed: true,
65+
Sensitive: true,
66+
},
67+
"service_name": {
68+
Type: schema.TypeString,
69+
Computed: true,
70+
},
71+
"notify_when_ok": {
72+
Type: schema.TypeBool,
73+
Computed: true,
74+
},
75+
"notify_when_resolved": {
76+
Type: schema.TypeBool,
77+
Computed: true,
78+
},
79+
"version": {
80+
Type: schema.TypeInt,
81+
Computed: true,
82+
},
83+
"send_test_notification": {
84+
Type: schema.TypeBool,
85+
Computed: true,
86+
},
87+
},
88+
}
89+
}
90+
91+
// Retrieves the information of a resource form the file and loads it in Terraform
92+
func dataSourceSysdigNotificationChannelRead(d *schema.ResourceData, meta interface{}) error {
93+
client := meta.(*SysdigClients).sysdigSecureClient
94+
95+
nc, err := client.GetNotificationChannelByName(d.Get("name").(string))
96+
if err != nil {
97+
return err
98+
}
99+
100+
d.SetId(strconv.Itoa(nc.ID))
101+
d.Set("version", nc.Version)
102+
d.Set("name", nc.Name)
103+
d.Set("enabled", nc.Enabled)
104+
d.Set("type", nc.Type)
105+
d.Set("recipients", nc.Options.EmailRecipients)
106+
d.Set("topics", nc.Options.SnsTopicARNs)
107+
d.Set("api_key", nc.Options.APIKey)
108+
d.Set("url", nc.Options.Url)
109+
d.Set("channel", nc.Options.Channel)
110+
d.Set("account", nc.Options.Account)
111+
d.Set("service_key", nc.Options.ServiceKey)
112+
d.Set("service_name", nc.Options.ServiceName)
113+
d.Set("routing_key", nc.Options.RoutingKey)
114+
d.Set("notify_when_ok", nc.Options.NotifyOnOk)
115+
d.Set("notify_when_resolved", nc.Options.NotifyOnResolve)
116+
d.Set("send_test_notification", nc.Options.SendTestNotification)
117+
118+
// When we receive a notification channel of type OpsGenie,
119+
// the API sends us the URL, but we are configuring the API
120+
// key in the file, so terraform identifies this as a change in
121+
// the resource and tries to update it remotely even if it
122+
// didn't change at all.
123+
// We need to extract the key from the url the API gives us
124+
// to avoid this Terraform's behaviour.
125+
if nc.Type == opsgenie {
126+
regex, err := regexp.Compile("apiKey=(.*)?$")
127+
if err != nil {
128+
return err
129+
}
130+
key := regex.FindStringSubmatch(nc.Options.Url)[1]
131+
d.Set("api_key", key)
132+
d.Set("url", "")
133+
}
134+
return nil
135+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package sysdig_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/draios/terraform-provider-sysdig/sysdig"
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/acctest"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
11+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
12+
)
13+
14+
func TestAccNotificationChannelDataSource(t *testing.T) {
15+
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
16+
17+
resource.ParallelTest(t, resource.TestCase{
18+
PreCheck: func() {
19+
if v := os.Getenv("SYSDIG_SECURE_API_TOKEN"); v == "" {
20+
t.Fatal("SYSDIG_SECURE_API_TOKEN must be set for acceptance tests")
21+
}
22+
},
23+
Providers: map[string]terraform.ResourceProvider{
24+
"sysdig": sysdig.Provider(),
25+
},
26+
Steps: []resource.TestStep{
27+
{
28+
Config: notificationChannelEmailWithNameAndDatasource(rText),
29+
},
30+
},
31+
})
32+
}
33+
34+
func notificationChannelEmailWithNameAndDatasource(name string) string {
35+
return fmt.Sprintf(`
36+
resource "sysdig_secure_notification_channel" "sample_email" {
37+
name = "%s"
38+
enabled = true
39+
type = "EMAIL"
40+
recipients = "[email protected]"
41+
notify_when_ok = false
42+
notify_when_resolved = false
43+
}
44+
45+
data "sysdig_secure_notification_channel" "sample_email" {
46+
name = sysdig_secure_notification_channel.sample_email.name
47+
}
48+
`, name)
49+
}

sysdig/provider.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ func Provider() terraform.ResourceProvider {
3535
},
3636
},
3737
ResourcesMap: map[string]*schema.Resource{
38+
"sysdig_user": resourceSysdigUser(),
39+
3840
"sysdig_secure_policy": resourceSysdigSecurePolicy(),
3941
"sysdig_secure_notification_channel": resourceSysdigSecureNotificationChannel(),
4042
"sysdig_secure_rule_container": resourceSysdigSecureRuleContainer(),
@@ -43,7 +45,6 @@ func Provider() terraform.ResourceProvider {
4345
"sysdig_secure_rule_process": resourceSysdigSecureRuleProcess(),
4446
"sysdig_secure_rule_syscall": resourceSysdigSecureRuleSyscall(),
4547
"sysdig_secure_rule_falco": resourceSysdigSecureRuleFalco(),
46-
"sysdig_user": resourceSysdigUser(),
4748
"sysdig_secure_team": resourceSysdigSecureTeam(),
4849

4950
"sysdig_monitor_alert_downtime": resourceSysdigMonitorAlertDowntime(),
@@ -52,6 +53,9 @@ func Provider() terraform.ResourceProvider {
5253
"sysdig_monitor_alert_anomaly": resourceSysdigMonitorAlertAnomaly(),
5354
"sysdig_monitor_alert_group_outlier": resourceSysdigMonitorAlertGroupOutlier(),
5455
},
56+
DataSourcesMap: map[string]*schema.Resource{
57+
"sysdig_secure_notification_channel": dataSourceSysdigSecureNotificationChannel(),
58+
},
5559
ConfigureFunc: providerConfigure,
5660
}
5761
}

sysdig/secure/client.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ type SysdigSecureClient interface {
2020

2121
CreateNotificationChannel(NotificationChannel) (NotificationChannel, error)
2222
GetNotificationChannelById(int) (NotificationChannel, error)
23+
GetNotificationChannelByName(string) (NotificationChannel, error)
2324
DeleteNotificationChannel(int) error
2425
UpdateNotificationChannel(NotificationChannel) (NotificationChannel, error)
2526

sysdig/secure/models.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,17 @@ func NotificationChannelFromJSON(body []byte) NotificationChannel {
101101
return result.NotificationChannel
102102
}
103103

104+
func NotificationChannelListFromJSON(body []byte) []NotificationChannel {
105+
var result notificationChannelListWrapper
106+
json.Unmarshal(body, &result)
107+
108+
return result.NotificationChannels
109+
}
110+
111+
type notificationChannelListWrapper struct {
112+
NotificationChannels []NotificationChannel `json:"notificationChannels"`
113+
}
114+
104115
type notificationChannelWrapper struct {
105116
NotificationChannel NotificationChannel `json:"notificationChannel"`
106117
}

sysdig/secure/notification_channels.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,33 @@ func (client *sysdigSecureClient) GetNotificationChannelById(id int) (nc Notific
3030
return
3131
}
3232

33+
func (client *sysdigSecureClient) GetNotificationChannelByName(name string) (nc NotificationChannel, err error) {
34+
response, err := client.doSysdigSecureRequest(http.MethodGet, client.GetNotificationChannelsUrl(), nil)
35+
if err != nil {
36+
return
37+
}
38+
defer response.Body.Close()
39+
40+
body, _ := ioutil.ReadAll(response.Body)
41+
42+
if response.StatusCode != http.StatusOK {
43+
err = errors.New(response.Status)
44+
return
45+
}
46+
47+
ncList := NotificationChannelListFromJSON(body)
48+
49+
for _, channel := range ncList {
50+
if channel.Name == name {
51+
nc = channel
52+
return
53+
}
54+
}
55+
56+
err = fmt.Errorf("Notification channel with Name: %s does not exist", name)
57+
return
58+
}
59+
3360
func (client *sysdigSecureClient) CreateNotificationChannel(ncRequest NotificationChannel) (nc NotificationChannel, err error) {
3461
response, err := client.doSysdigSecureRequest(http.MethodPost, client.GetNotificationChannelsUrl(), ncRequest.ToJSON())
3562
if err != nil {

0 commit comments

Comments
 (0)