Skip to content

Commit

Permalink
feat(alerts): allow ibm client for alert_v2 resources
Browse files Browse the repository at this point in the history
  • Loading branch information
dbonf committed Aug 7, 2023
1 parent 0bab7f1 commit a0ddb3e
Show file tree
Hide file tree
Showing 12 changed files with 62 additions and 55 deletions.
10 changes: 6 additions & 4 deletions sysdig/internal/client/v2/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@ import (
"encoding/json"
"errors"
"fmt"
"github.com/draios/terraform-provider-sysdig/buildinfo"
"github.com/hashicorp/go-retryablehttp"
"github.com/jmespath/go-jmespath"
"github.com/spf13/cast"
"io"
"log"
"net/http"
"net/http/httputil"
"strings"

"github.com/draios/terraform-provider-sysdig/buildinfo"
"github.com/hashicorp/go-retryablehttp"
"github.com/jmespath/go-jmespath"
"github.com/spf13/cast"
)

const (
Expand Down Expand Up @@ -48,6 +49,7 @@ type Common interface {

type MonitorCommon interface {
AlertInterface
AlertV2Interface
}

type SecureCommon interface {
Expand Down
1 change: 0 additions & 1 deletion sysdig/internal/client/v2/sysdig.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ type SysdigMonitor interface {
MonitorCommon
DashboardInterface
CloudAccountMonitorInterface
AlertV2Interface
}

type SysdigSecure interface {
Expand Down
21 changes: 20 additions & 1 deletion sysdig/resource_sysdig_monitor_alert_v2_common.go
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
package sysdig

import (
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
"regexp"
"strings"
"time"

v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
Expand Down Expand Up @@ -496,3 +497,21 @@ func updateScopedSegmentedConfigState(d *schema.ResourceData, config *v2.ScopedS

return nil
}

func getAlertV2Client(c SysdigClients) (v2.AlertV2Interface, error) {
var client v2.AlertV2Interface
var err error
switch c.GetClientType() {
case IBMMonitor:
client, err = c.ibmMonitorClient()
if err != nil {
return nil, err
}
default:
client, err = c.sysdigMonitorClientV2()
if err != nil {
return nil, err
}
}
return client, nil
}
5 changes: 3 additions & 2 deletions sysdig/resource_sysdig_monitor_alert_v2_downtime.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@ package sysdig

import (
"context"
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
"strconv"
"time"

v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -47,7 +48,7 @@ func resourceSysdigMonitorAlertV2Downtime() *schema.Resource {
}

func getAlertV2DowntimeClient(c SysdigClients) (v2.AlertV2DowntimeInterface, error) {
return c.sysdigMonitorClientV2()
return getAlertV2Client(c)
}

func resourceSysdigMonitorAlertV2DowntimeCreate(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
Expand Down
9 changes: 2 additions & 7 deletions sysdig/resource_sysdig_monitor_alert_v2_downtime_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//go:build tf_acc_sysdig_monitor
//go:build tf_acc_sysdig_monitor || tf_acc_ibm_monitor

package sysdig_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand All @@ -18,11 +17,7 @@ func TestAccAlertV2Downtime(t *testing.T) {
rText := func() string { return 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")
}
},
PreCheck: preCheckAnyEnv(t, SysdigMonitorApiTokenEnv, SysdigIBMMonitorAPIKeyEnv),
ProviderFactories: map[string]func() (*schema.Provider, error){
"sysdig": func() (*schema.Provider, error) {
return sysdig.Provider(), nil
Expand Down
5 changes: 3 additions & 2 deletions sysdig/resource_sysdig_monitor_alert_v2_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package sysdig
import (
"context"
"fmt"
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
"strconv"
"time"

v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -61,7 +62,7 @@ func resourceSysdigMonitorAlertV2Event() *schema.Resource {
}

func getAlertV2EventClient(c SysdigClients) (v2.AlertV2EventInterface, error) {
return c.sysdigMonitorClientV2()
return getAlertV2Client(c)
}

func resourceSysdigMonitorAlertV2EventCreate(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
Expand Down
9 changes: 2 additions & 7 deletions sysdig/resource_sysdig_monitor_alert_v2_event_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//go:build tf_acc_sysdig_monitor
//go:build tf_acc_sysdig_monitor || tf_acc_ibm_monitor

package sysdig_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand All @@ -18,11 +17,7 @@ func TestAccAlertV2Event(t *testing.T) {
rText := func() string { return 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")
}
},
PreCheck: preCheckAnyEnv(t, SysdigMonitorApiTokenEnv, SysdigIBMMonitorAPIKeyEnv),
ProviderFactories: map[string]func() (*schema.Provider, error){
"sysdig": func() (*schema.Provider, error) {
return sysdig.Provider(), nil
Expand Down
5 changes: 3 additions & 2 deletions sysdig/resource_sysdig_monitor_alert_v2_metric.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@ package sysdig
import (
"context"
"fmt"
v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"
"strconv"
"time"

v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -72,7 +73,7 @@ func resourceSysdigMonitorAlertV2Metric() *schema.Resource {
}

func getAlertV2MetricClient(c SysdigClients) (v2.AlertV2MetricInterface, error) {
return c.sysdigMonitorClientV2()
return getAlertV2Client(c)
}

func resourceSysdigMonitorAlertV2MetricCreate(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
Expand Down
9 changes: 2 additions & 7 deletions sysdig/resource_sysdig_monitor_alert_v2_metric_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//go:build tf_acc_sysdig_monitor
//go:build tf_acc_sysdig_monitor || tf_acc_ibm_monitor

package sysdig_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand All @@ -18,11 +17,7 @@ func TestAccAlertV2Metric(t *testing.T) {
rText := func() string { return 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")
}
},
PreCheck: preCheckAnyEnv(t, SysdigMonitorApiTokenEnv, SysdigIBMMonitorAPIKeyEnv),
ProviderFactories: map[string]func() (*schema.Provider, error){
"sysdig": func() (*schema.Provider, error) {
return sysdig.Provider(), nil
Expand Down
2 changes: 1 addition & 1 deletion sysdig/resource_sysdig_monitor_alert_v2_prometheus.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func resourceSysdigMonitorAlertV2Prometheus() *schema.Resource {
}

func getAlertV2PrometheusClient(c SysdigClients) (v2.AlertV2PrometheusInterface, error) {
return c.sysdigMonitorClientV2()
return getAlertV2Client(c)
}

func resourceSysdigMonitorAlertV2PrometheusCreate(ctx context.Context, d *schema.ResourceData, i interface{}) diag.Diagnostics {
Expand Down
9 changes: 2 additions & 7 deletions sysdig/resource_sysdig_monitor_alert_v2_prometheus_test.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
//go:build tf_acc_sysdig_monitor
//go:build tf_acc_sysdig_monitor || tf_acc_ibm_monitor

package sysdig_test

import (
"fmt"
"os"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/acctest"
Expand All @@ -18,11 +17,7 @@ func TestAccAlertV2Prometheus(t *testing.T) {
rText := func() string { return 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")
}
},
PreCheck: preCheckAnyEnv(t, SysdigMonitorApiTokenEnv, SysdigIBMMonitorAPIKeyEnv),
ProviderFactories: map[string]func() (*schema.Provider, error){
"sysdig": func() (*schema.Provider, error) {
return sysdig.Provider(), nil
Expand Down
32 changes: 18 additions & 14 deletions website/docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ optional.
For either options, the corresponding **URL** and **API Token** must be configured.
See options below.

Sysdig provider can also be used to interact with [IBM Cloud Monitoring](https://cloud.ibm.com/docs/monitoring?topic=monitoring-getting-started). For more details check `IBM Cloud Monitoring Authentication` example and configuration reference below.
Sysdig provider can also be used to interact with [IBM Cloud Monitoring](https://cloud.ibm.com/docs/monitoring?topic=monitoring-getting-started). For more details check `IBM Cloud Monitoring Authentication` example and configuration reference below.

Use the navigation to the left to read about the available resources.

Expand All @@ -41,7 +41,7 @@ terraform {
```terraform
provider "sysdig" {
sysdig_monitor_url = "https://app.sysdigcloud.com"
sysdig_monitor_api_token = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
sysdig_monitor_api_token = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
}
```

Expand All @@ -58,13 +58,13 @@ provider "sysdig" {

```terraform
provider "sysdig" {
sysdig_secure_url="https://secure.sysdig.com"
sysdig_secure_api_token = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
sysdig_monitor_url = "https://app.sysdigcloud.com"
sysdig_monitor_api_token = "xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
extra_headers = {
"Proxy-Authorization": "Basic xxxxxxxxxxxxxxxx"
}
Expand Down Expand Up @@ -114,9 +114,9 @@ optional.
When Monitor resources are to be created, this authentication must be in place.

* `sysdig_monitor_url` - (Required) This is the target Sysdig Monitor base API
endpoint. It's intended to be used with OnPrem installations.
endpoint. It's intended to be used with OnPrem installations.
<br/>By default, it points to `https://app.sysdigcloud.com`. [Find your Sysdig Saas region url](https://docs.sysdig.com/en/docs/administration/saas-regions-and-ip-ranges/#saas-regions-and-ip-ranges) (Sysdig Monitor endpoint)</br>
It can also be sourced from the `SYSDIG_MONITOR_URL` environment variable.<br/>Notice: it should not be ended with a
It can also be sourced from the `SYSDIG_MONITOR_URL` environment variable.<br/>Notice: it should not be ended with a
slash.<br/><br/>

* `sysdig_monitor_api_token` - (Required) The Sysdig Monitor API token.
Expand All @@ -136,7 +136,7 @@ When Secure resources are to be created, this authentication must be in place.

* `sysdig_secure_url` - (Required) This is the target Sysdig Secure base API
endpoint. It's intended to be used with OnPrem installations.
<br/>By default, it points to `https://secure.sysdig.com`.
<br/>By default, it points to `https://secure.sysdig.com`.
<br/>[Find your Sysdig Saas region url](https://docs.sysdig.com/en/docs/administration/saas-regions-and-ip-ranges/#saas-regions-and-ip-ranges) (Secure endpoint)
<br/> It can also be sourced from the `SYSDIG_SECURE_URL` environment variable.
<br/>Notice: it should not be ended with a slash.<br/><br/>
Expand All @@ -147,7 +147,7 @@ When Secure resources are to be created, this authentication must be in place.
<br/>Required if any `sysdig_secure_*` resource or data source is used.<br/><br/>

* `sysdig_secure_insecure_tls` - (Optional) Defines if the HTTP client can ignore
the use of invalid HTTPS certificates in the Secure API. It can be useful for
the use of invalid HTTPS certificates in the Secure API. It can be useful for
on-prem installations. It can also be sourced from the `SYSDIG_SECURE_INSECURE_TLS`
environment variable. By default, this is false.<br/><br/>

Expand All @@ -159,24 +159,24 @@ When IBM Cloud Monitoring resources are to be created, this authentication must
* `sysdig_monitor_url` - (Required) This is the target IBM Cloud Monitoring API
endpoint. It can also be sourced from the `SYSDIG_MONITOR_URL` environment variable. [Find your IBM Cloud Monitoring region url](https://cloud.ibm.com/docs/monitoring?topic=monitoring-endpoints#endpoints_monitoring).
<br/>Notice: it should not be ended with a slash.<br/><br/>
* `ibm_monitor_iam_url` - (Required) This is the target IAM endpoint used to issue IBM IAM token by consuming `ibm_monitor_api_key`.
* `ibm_monitor_iam_url` - (Required) This is the target IAM endpoint used to issue IBM IAM token by consuming `ibm_monitor_api_key`.
Provider will handle token expiration and refresh it when needed.
<br/>It can also be configured from the `SYSDIG_IBM_MONITOR_IAM_URL` environment variable.<br/><br/>
* `ibm_monitor_instance_id` (Required) This is the target instance ID (GUID format) of IBM instance which is hosting IBM Cloud Monitoring.
<br/>It can also be configured from the `SYSDIG_IBM_MONITOR_INSTANCE_ID` environment variable.
<br/><br/>
* `ibm_monitor_api_key` (Required) An API key is a unique code that is passed to an IBM IAM service to generate IAM token used for making HTTP request against IBM endpoints.
* `ibm_monitor_api_key` (Required) An API key is a unique code that is passed to an IBM IAM service to generate IAM token used for making HTTP request against IBM endpoints.
This argument can be used to specify any kind of IBM API keys (User API key, Service ID, ...).
<br/>It can also be configured from the `SYSDIG_IBM_MONITOR_API_KEY` environment variable.
<br/><br/>
* `sysdig_monitor_insecure_tls` - (Optional) Defines if the HTTP client can ignore
the use of invalid HTTPS certificates in the IBM Monitoring Cloud API.
<br/> It can also be sourced from the `SYSDIG_MONITOR_INSECURE_TLS`
environment variable. By default, this is false.<br/><br/>
* `sysdig_monitor_team_id` - (Optional) Use this argument to specify team in which you will be logged in.
* `sysdig_monitor_team_id` - (Optional) Use this argument to specify team in which you will be logged in.
If not specified, default team will be used. This argument has precedence over `sysdig_monitor_team_name` if both are specified.<br/>
It can also be configured from the `SYSDIG_MONITOR_TEAM_ID` environment variable.<br/><br/>
* `sysdig_monitor_team_name` - (Optional) This argument is the alternative way of specifying team in which you will be logged in.
* `sysdig_monitor_team_name` - (Optional) This argument is the alternative way of specifying team in which you will be logged in.
It has exactly the same meaning as `sysdig_monitor_team_id`, but instead of specifying team ID you are specifying a team name.</br>
It can also be configured from the `SYSDIG_MONITOR_TEAM_NAME` environment variable.<br/><br/>

Expand Down Expand Up @@ -234,8 +234,12 @@ When IBM Workload Protection resources are to be created, this authentication mu
> - `sysdig_monitor_alert_promql`
> - `sysdig_monitor_alert_anomaly`
> - `sysdig_monitor_alert_group_outlier`
> - `sysdig_monitor_alert_v2_downtime`
> - `sysdig_monitor_alert_v2_event`
> - `sysdig_monitor_alert_v2_metric`
> - `sysdig_monitor_alert_v2_prometheus`
> - `sysdig_secure_posture_zone`
>
>
> And data sources:
> - `sysdig_monitor_notification_channel_pagerduty`
> - `sysdig_monitor_notification_channel_email`
Expand Down

0 comments on commit a0ddb3e

Please sign in to comment.