Skip to content

Commit

Permalink
Add data sources for PagerDuty and email notification channels (#147)
Browse files Browse the repository at this point in the history
* 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>
  • Loading branch information
3 people committed Feb 8, 2022
1 parent f824943 commit 50bad8a
Show file tree
Hide file tree
Showing 7 changed files with 309 additions and 1 deletion.
52 changes: 52 additions & 0 deletions sysdig/data_source_sysdig_monitor_notification_channel_email.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package sysdig

import (
"context"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceSysdigMonitorNotificationChannelEmail() *schema.Resource {
timeout := 5 * time.Minute

return &schema.Resource{
ReadContext: dataSourceSysdigMonitorNotificationChannelEmailRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(timeout),
},

Schema: createMonitorNotificationChannelSchema(map[string]*schema.Schema{
"recipients": {
Type: schema.TypeSet,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
},
}),
}
}

func dataSourceSysdigMonitorNotificationChannelEmailRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client, err := meta.(SysdigClients).sysdigMonitorClient()

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

nc, err := client.GetNotificationChannelByName(ctx, d.Get("name").(string))
if err != nil {
return diag.FromErr(err)
}

err = monitorNotificationChannelEmailToResourceData(&nc, d)
if err != nil {
return diag.FromErr(err)
}

d.SetId(strconv.Itoa(nc.ID))

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package sysdig_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/draios/terraform-provider-sysdig/sysdig"
)

func TestAccNotificationChannelEmailDataSource(t *testing.T) {
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
if v := os.Getenv("SYSDIG_MONITOR_API_TOKEN"); v == "" {
t.Fatal("SYSDIG_MONITOR_API_TOKEN must be set for acceptance tests")
}
},
ProviderFactories: map[string]func() (*schema.Provider, error){
"sysdig": func() (*schema.Provider, error) {
return sysdig.Provider(), nil
},
},

Steps: []resource.TestStep{
{
Config: notificationChannelEmail(rText),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_email.nc_email", "id", "sysdig_monitor_notification_channel_email.nc_email", "id"),
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_email.nc_email", "name", "sysdig_monitor_notification_channel_email.nc_email", "name"),
resource.TestCheckTypeSetElemAttr("data.sysdig_monitor_notification_channel_email.nc_email", "recipients.*", "root@localhost.com"),
),
},
},
})
}

func notificationChannelEmail(name string) string {
return fmt.Sprintf(`
resource "sysdig_monitor_notification_channel_email" "nc_email" {
name = "%s"
recipients = ["root@localhost.com"]
}
data "sysdig_monitor_notification_channel_email" "nc_email" {
name = sysdig_monitor_notification_channel_email.nc_email.name
}
`, name)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package sysdig

import (
"context"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

func dataSourceSysdigMonitorNotificationChannelPagerduty() *schema.Resource {
timeout := 5 * time.Minute

return &schema.Resource{
ReadContext: dataSourceSysdigMonitorNotificationChannelPagerdutyRead,

Timeouts: &schema.ResourceTimeout{
Read: schema.DefaultTimeout(timeout),
},

Schema: createMonitorNotificationChannelSchema(map[string]*schema.Schema{
"account": {
Type: schema.TypeString,
Computed: true,
},
"service_key": {
Type: schema.TypeString,
Computed: true,
},
"service_name": {
Type: schema.TypeString,
Computed: true,
},
}),
}
}

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

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

nc, err := client.GetNotificationChannelByName(ctx, d.Get("name").(string))
if err != nil {
return diag.FromErr(err)
}

err = monitorNotificationChannelPagerdutyToResourceData(&nc, d)
if err != nil {
return diag.FromErr(err)
}

d.SetId(strconv.Itoa(nc.ID))

return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package sysdig_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"

"github.com/draios/terraform-provider-sysdig/sysdig"
)

func TestAccNotificationChannelPagerdutyDataSource(t *testing.T) {
rText := acctest.RandStringFromCharSet(10, acctest.CharSetAlphaNum)

resource.ParallelTest(t, resource.TestCase{
PreCheck: func() {
if v := os.Getenv("SYSDIG_MONITOR_API_TOKEN"); v == "" {
t.Fatal("SYSDIG_MONITOR_API_TOKEN must be set for acceptance tests")
}
},
ProviderFactories: map[string]func() (*schema.Provider, error){
"sysdig": func() (*schema.Provider, error) {
return sysdig.Provider(), nil
},
},

Steps: []resource.TestStep{
{
Config: notificationChannelPagerduty(rText),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "name", "sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "name"),
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "account", "sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "account"),
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_key", "sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_key"),
resource.TestCheckResourceAttrPair("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_name", "sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_name"),
resource.TestCheckResourceAttr("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "account", "account"),
resource.TestCheckResourceAttr("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_key", "XXXXXXXXXX"),
resource.TestCheckResourceAttr("data.sysdig_monitor_notification_channel_pagerduty.nc_pagerduty", "service_name", "sysdig"),
),
},
},
})
}

func notificationChannelPagerduty(name string) string {
return fmt.Sprintf(`
resource "sysdig_monitor_notification_channel_pagerduty" "nc_pagerduty" {
name = "%s"
account = "account"
service_key = "XXXXXXXXXX"
service_name = "sysdig"
}
data "sysdig_monitor_notification_channel_pagerduty" "nc_pagerduty" {
name = sysdig_monitor_notification_channel_pagerduty.nc_pagerduty.name
}
`, name)
}
4 changes: 3 additions & 1 deletion sysdig/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,9 @@ func Provider() *schema.Provider {
"sysdig_current_user": dataSourceSysdigCurrentUser(),
"sysdig_user": dataSourceSysdigUser(),

"sysdig_fargate_workload_agent": dataSourceSysdigFargateWorkloadAgent(),
"sysdig_fargate_workload_agent": dataSourceSysdigFargateWorkloadAgent(),
"sysdig_monitor_notification_channel_pagerduty": dataSourceSysdigMonitorNotificationChannelPagerduty(),
"sysdig_monitor_notification_channel_email": dataSourceSysdigMonitorNotificationChannelEmail(),
},
ConfigureContextFunc: providerConfigure,
}
Expand Down
39 changes: 39 additions & 0 deletions website/docs/d/monitor_notification_channel_email.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
---
subcategory: "Sysdig Monitor"
layout: "sysdig"
page_title: "Sysdig: sysdig_monitor_notification_channel_email"
description: |-
Retrieves information about a Monitor notification channel of type Email
---

# Data Source: sysdig_monitor_notification_channel_email

Retrieves information about a Monitor notification channel of type Email

`~> **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.`

## Example Usage

```terraform
data "sysdig_monitor_notification_channel_email" "nc_email" {
name = "some notification channel name"
}
```

## Argument Reference

* `name` - (Required) The name of the Notification Channel to retrieve.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The Notification Channel ID.
* `name` - The Notification Channel Name.
* `recipients` - List of recipients that will receive the message.
* `enabled` - Whether the Notification Channel is active or not.
* `notify_when_ok` - Whether the Notification Channel sends a notification when the condition is no longer triggered.
* `notify_when_resolved` - Whether the Notification Channel sends a notification if it's manually acknowledged by a
user.
* `version` - The version of the Notification Channel.
* `send_test_notification` - Whether the Notification Channel has enabled the test notification.
42 changes: 42 additions & 0 deletions website/docs/d/monitor_notification_channel_pagerduty.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
subcategory: "Sysdig Monitor"
layout: "sysdig"
page_title: "Sysdig: sysdig_monitor_notification_channel_pagerduty"
description: |-
Retrieves information about a Monitor notification channel of type Pagerduty
---

# Data Source: sysdig_monitor_notification_channel_pagerduty

Retrieves information about a Monitor notification channel of type Pagerduty

`~> **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.`

## Example Usage

```terraform
data "sysdig_monitor_notification_channel_pagerduty" "nc_pagerduty" {
name = "some notification channel name"
}
```


## Argument Reference

* `name` - (Required) The name of the Notification Channel to retrieve.

## Attributes Reference

In addition to all arguments above, the following attributes are exported:

* `id` - The Notification Channel ID.
* `name` - The Notification Channel Name.
* `account` - Pagerduty account.
* `service_key` - Service Key for the Pagerduty account.
* `service_name` - Service name for the Pagerduty account.
* `enabled` - Whether the Notification Channel is active or not.
* `notify_when_ok` - Whether the Notification Channel sends a notification when the condition is no longer triggered.
* `notify_when_resolved` - Whether the Notification Channel sends a notification if it's manually acknowledged by a
user.
* `version` - The version of the Notification Channel.
* `send_test_notification` - Whether the Notification Channel has enabled the test notification.

0 comments on commit 50bad8a

Please sign in to comment.