Skip to content

Commit 50bad8a

Browse files
rmoeRyan Moetembleking
authored
Add data sources for PagerDuty and email notification channels (#147)
* Add data sources for PagerDuty and email notification channels Part of: #138 * ci: Add extra verification checks * docs: Add documentation for missing data sources * fix(docs): Add line break Co-authored-by: Ryan Moe <ryan.moe@ibm.com> Co-authored-by: Fede Barcelona <fede_rico_94@hotmail.com>
1 parent f824943 commit 50bad8a

7 files changed

+309
-1
lines changed
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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 dataSourceSysdigMonitorNotificationChannelEmail() *schema.Resource {
13+
timeout := 5 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigMonitorNotificationChannelEmailRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: createMonitorNotificationChannelSchema(map[string]*schema.Schema{
23+
"recipients": {
24+
Type: schema.TypeSet,
25+
Elem: &schema.Schema{Type: schema.TypeString},
26+
Computed: true,
27+
},
28+
}),
29+
}
30+
}
31+
32+
func dataSourceSysdigMonitorNotificationChannelEmailRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
33+
client, err := meta.(SysdigClients).sysdigMonitorClient()
34+
35+
if err != nil {
36+
return diag.FromErr(err)
37+
}
38+
39+
nc, err := client.GetNotificationChannelByName(ctx, d.Get("name").(string))
40+
if err != nil {
41+
return diag.FromErr(err)
42+
}
43+
44+
err = monitorNotificationChannelEmailToResourceData(&nc, d)
45+
if err != nil {
46+
return diag.FromErr(err)
47+
}
48+
49+
d.SetId(strconv.Itoa(nc.ID))
50+
51+
return nil
52+
}
Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
package sysdig_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
12+
"github.com/draios/terraform-provider-sysdig/sysdig"
13+
)
14+
15+
func TestAccNotificationChannelEmailDataSource(t *testing.T) {
16+
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() {
20+
if v := os.Getenv("SYSDIG_MONITOR_API_TOKEN"); v == "" {
21+
t.Fatal("SYSDIG_MONITOR_API_TOKEN must be set for acceptance tests")
22+
}
23+
},
24+
ProviderFactories: map[string]func() (*schema.Provider, error){
25+
"sysdig": func() (*schema.Provider, error) {
26+
return sysdig.Provider(), nil
27+
},
28+
},
29+
30+
Steps: []resource.TestStep{
31+
{
32+
Config: notificationChannelEmail(rText),
33+
Check: resource.ComposeAggregateTestCheckFunc(
34+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_email.nc_email", "id", "sysdig_monitor_notification_channel_email.nc_email", "id"),
35+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_email.nc_email", "name", "sysdig_monitor_notification_channel_email.nc_email", "name"),
36+
resource.TestCheckTypeSetElemAttr("data.sysdig_monitor_notification_channel_email.nc_email", "recipients.*", "root@localhost.com"),
37+
),
38+
},
39+
},
40+
})
41+
}
42+
43+
func notificationChannelEmail(name string) string {
44+
return fmt.Sprintf(`
45+
resource "sysdig_monitor_notification_channel_email" "nc_email" {
46+
name = "%s"
47+
recipients = ["root@localhost.com"]
48+
}
49+
50+
data "sysdig_monitor_notification_channel_email" "nc_email" {
51+
name = sysdig_monitor_notification_channel_email.nc_email.name
52+
}
53+
`, name)
54+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
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 dataSourceSysdigMonitorNotificationChannelPagerduty() *schema.Resource {
13+
timeout := 5 * time.Minute
14+
15+
return &schema.Resource{
16+
ReadContext: dataSourceSysdigMonitorNotificationChannelPagerdutyRead,
17+
18+
Timeouts: &schema.ResourceTimeout{
19+
Read: schema.DefaultTimeout(timeout),
20+
},
21+
22+
Schema: createMonitorNotificationChannelSchema(map[string]*schema.Schema{
23+
"account": {
24+
Type: schema.TypeString,
25+
Computed: true,
26+
},
27+
"service_key": {
28+
Type: schema.TypeString,
29+
Computed: true,
30+
},
31+
"service_name": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
},
35+
}),
36+
}
37+
}
38+
39+
func dataSourceSysdigMonitorNotificationChannelPagerdutyRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
40+
client, err := meta.(SysdigClients).sysdigMonitorClient()
41+
42+
if err != nil {
43+
return diag.FromErr(err)
44+
}
45+
46+
nc, err := client.GetNotificationChannelByName(ctx, d.Get("name").(string))
47+
if err != nil {
48+
return diag.FromErr(err)
49+
}
50+
51+
err = monitorNotificationChannelPagerdutyToResourceData(&nc, d)
52+
if err != nil {
53+
return diag.FromErr(err)
54+
}
55+
56+
d.SetId(strconv.Itoa(nc.ID))
57+
58+
return nil
59+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package sysdig_test
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
11+
12+
"github.com/draios/terraform-provider-sysdig/sysdig"
13+
)
14+
15+
func TestAccNotificationChannelPagerdutyDataSource(t *testing.T) {
16+
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)
17+
18+
resource.ParallelTest(t, resource.TestCase{
19+
PreCheck: func() {
20+
if v := os.Getenv("SYSDIG_MONITOR_API_TOKEN"); v == "" {
21+
t.Fatal("SYSDIG_MONITOR_API_TOKEN must be set for acceptance tests")
22+
}
23+
},
24+
ProviderFactories: map[string]func() (*schema.Provider, error){
25+
"sysdig": func() (*schema.Provider, error) {
26+
return sysdig.Provider(), nil
27+
},
28+
},
29+
30+
Steps: []resource.TestStep{
31+
{
32+
Config: notificationChannelPagerduty(rText),
33+
Check: resource.ComposeAggregateTestCheckFunc(
34+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "name", "sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "name"),
35+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "account", "sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "account"),
36+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_key", "sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_key"),
37+
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_name", "sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_name"),
38+
resource.TestCheckResourceAttr("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "account", "account"),
39+
resource.TestCheckResourceAttr("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_key", "XXXXXXXXXX"),
40+
resource.TestCheckResourceAttr("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_name", "sysdig"),
41+
),
42+
},
43+
},
44+
})
45+
}
46+
47+
func notificationChannelPagerduty(name string) string {
48+
return fmt.Sprintf(`
49+
resource "sysdig_monitor_notification_channel_pagerduty" "nc_pagerduty" {
50+
name = "%s"
51+
account = "account"
52+
service_key = "XXXXXXXXXX"
53+
service_name = "sysdig"
54+
}
55+
56+
data "sysdig_monitor_notification_channel_pagerduty" "nc_pagerduty" {
57+
name = sysdig_monitor_notification_channel_pagerduty.nc_pagerduty.name
58+
}
59+
`, name)
60+
}

sysdig/provider.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,9 @@ func Provider() *schema.Provider {
9595
"sysdig_current_user": dataSourceSysdigCurrentUser(),
9696
"sysdig_user": dataSourceSysdigUser(),
9797

98-
"sysdig_fargate_workload_agent": dataSourceSysdigFargateWorkloadAgent(),
98+
"sysdig_fargate_workload_agent": dataSourceSysdigFargateWorkloadAgent(),
99+
"sysdig_monitor_notification_channel_pagerduty": dataSourceSysdigMonitorNotificationChannelPagerduty(),
100+
"sysdig_monitor_notification_channel_email": dataSourceSysdigMonitorNotificationChannelEmail(),
99101
},
100102
ConfigureContextFunc: providerConfigure,
101103
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
---
2+
subcategory: "Sysdig Monitor"
3+
layout: "sysdig"
4+
page_title: "Sysdig: sysdig_monitor_notification_channel_email"
5+
description: |-
6+
Retrieves information about a Monitor notification channel of type Email
7+
---
8+
9+
# Data Source: sysdig_monitor_notification_channel_email
10+
11+
Retrieves information about a Monitor notification channel of type Email
12+
13+
`~> **Note:** Sysdig Terraform Provider is under rapid development at this point. If you experience any issue or discrepancy while using it, please make sure you have the latest version. If the issue persists, or you have a Feature Request to support an additional set of resources, please open a [new issue](https://github.com/sysdiglabs/terraform-provider-sysdig/issues/new) in the GitHub repository.`
14+
15+
## Example Usage
16+
17+
```terraform
18+
data "sysdig_monitor_notification_channel_email" "nc_email" {
19+
name = "some notification channel name"
20+
}
21+
```
22+
23+
## Argument Reference
24+
25+
* `name` - (Required) The name of the Notification Channel to retrieve.
26+
27+
## Attributes Reference
28+
29+
In addition to all arguments above, the following attributes are exported:
30+
31+
* `id` - The Notification Channel ID.
32+
* `name` - The Notification Channel Name.
33+
* `recipients` - List of recipients that will receive the message.
34+
* `enabled` - Whether the Notification Channel is active or not.
35+
* `notify_when_ok` - Whether the Notification Channel sends a notification when the condition is no longer triggered.
36+
* `notify_when_resolved` - Whether the Notification Channel sends a notification if it's manually acknowledged by a
37+
user.
38+
* `version` - The version of the Notification Channel.
39+
* `send_test_notification` - Whether the Notification Channel has enabled the test notification.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
---
2+
subcategory: "Sysdig Monitor"
3+
layout: "sysdig"
4+
page_title: "Sysdig: sysdig_monitor_notification_channel_pagerduty"
5+
description: |-
6+
Retrieves information about a Monitor notification channel of type Pagerduty
7+
---
8+
9+
# Data Source: sysdig_monitor_notification_channel_pagerduty
10+
11+
Retrieves information about a Monitor notification channel of type Pagerduty
12+
13+
`~> **Note:** Sysdig Terraform Provider is under rapid development at this point. If you experience any issue or discrepancy while using it, please make sure you have the latest version. If the issue persists, or you have a Feature Request to support an additional set of resources, please open a [new issue](https://github.com/sysdiglabs/terraform-provider-sysdig/issues/new) in the GitHub repository.`
14+
15+
## Example Usage
16+
17+
```terraform
18+
data "sysdig_monitor_notification_channel_pagerduty" "nc_pagerduty" {
19+
name = "some notification channel name"
20+
}
21+
```
22+
23+
24+
## Argument Reference
25+
26+
* `name` - (Required) The name of the Notification Channel to retrieve.
27+
28+
## Attributes Reference
29+
30+
In addition to all arguments above, the following attributes are exported:
31+
32+
* `id` - The Notification Channel ID.
33+
* `name` - The Notification Channel Name.
34+
* `account` - Pagerduty account.
35+
* `service_key` - Service Key for the Pagerduty account.
36+
* `service_name` - Service name for the Pagerduty account.
37+
* `enabled` - Whether the Notification Channel is active or not.
38+
* `notify_when_ok` - Whether the Notification Channel sends a notification when the condition is no longer triggered.
39+
* `notify_when_resolved` - Whether the Notification Channel sends a notification if it's manually acknowledged by a
40+
user.
41+
* `version` - The version of the Notification Channel.
42+
* `send_test_notification` - Whether the Notification Channel has enabled the test notification.

0 commit comments

Comments
 (0)