diff --git a/sysdig/internal/client/v2/client.go b/sysdig/internal/client/v2/client.go index 67346ad2..48ad48bf 100644 --- a/sysdig/internal/client/v2/client.go +++ b/sysdig/internal/client/v2/client.go @@ -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 ( @@ -48,6 +49,8 @@ type Common interface { type MonitorCommon interface { AlertInterface + AlertV2Interface + DashboardInterface } type SecureCommon interface { diff --git a/sysdig/internal/client/v2/sysdig.go b/sysdig/internal/client/v2/sysdig.go index 75df4221..0e896a0c 100644 --- a/sysdig/internal/client/v2/sysdig.go +++ b/sysdig/internal/client/v2/sysdig.go @@ -28,9 +28,7 @@ type SysdigCommon interface { type SysdigMonitor interface { SysdigCommon MonitorCommon - DashboardInterface CloudAccountMonitorInterface - AlertV2Interface } type SysdigSecure interface { diff --git a/sysdig/resource_sysdig_monitor_alert_v2_common.go b/sysdig/resource_sysdig_monitor_alert_v2_common.go index 08a9c00c..7b938ec3 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_common.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_common.go @@ -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" ) @@ -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 +} diff --git a/sysdig/resource_sysdig_monitor_alert_v2_downtime.go b/sysdig/resource_sysdig_monitor_alert_v2_downtime.go index 06f9dd90..54e058a9 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_downtime.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_downtime.go @@ -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" @@ -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 { diff --git a/sysdig/resource_sysdig_monitor_alert_v2_downtime_test.go b/sysdig/resource_sysdig_monitor_alert_v2_downtime_test.go index efb62019..13c6a3bc 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_downtime_test.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_downtime_test.go @@ -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" @@ -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 diff --git a/sysdig/resource_sysdig_monitor_alert_v2_event.go b/sysdig/resource_sysdig_monitor_alert_v2_event.go index 63103ae8..6f64f601 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_event.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_event.go @@ -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" @@ -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 { diff --git a/sysdig/resource_sysdig_monitor_alert_v2_event_test.go b/sysdig/resource_sysdig_monitor_alert_v2_event_test.go index a25a51f0..b56edd53 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_event_test.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_event_test.go @@ -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" @@ -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 diff --git a/sysdig/resource_sysdig_monitor_alert_v2_metric.go b/sysdig/resource_sysdig_monitor_alert_v2_metric.go index 907b6b60..9ea42693 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_metric.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_metric.go @@ -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" @@ -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 { diff --git a/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go b/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go index 8113e55f..38074fd6 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_metric_test.go @@ -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" @@ -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 diff --git a/sysdig/resource_sysdig_monitor_alert_v2_prometheus.go b/sysdig/resource_sysdig_monitor_alert_v2_prometheus.go index 89174b10..29dd41b6 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_prometheus.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_prometheus.go @@ -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 { diff --git a/sysdig/resource_sysdig_monitor_alert_v2_prometheus_test.go b/sysdig/resource_sysdig_monitor_alert_v2_prometheus_test.go index c14215d2..b62ab69d 100644 --- a/sysdig/resource_sysdig_monitor_alert_v2_prometheus_test.go +++ b/sysdig/resource_sysdig_monitor_alert_v2_prometheus_test.go @@ -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" @@ -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 diff --git a/sysdig/resource_sysdig_monitor_dashboard.go b/sysdig/resource_sysdig_monitor_dashboard.go index 32410c0f..938e04cd 100644 --- a/sysdig/resource_sysdig_monitor_dashboard.go +++ b/sysdig/resource_sysdig_monitor_dashboard.go @@ -4,10 +4,11 @@ import ( "context" "errors" "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" @@ -278,7 +279,21 @@ func resourceSysdigMonitorDashboard() *schema.Resource { } func getMonitorDashboardClient(c SysdigClients) (v2.DashboardInterface, error) { - return c.sysdigMonitorClientV2() + var client v2.DashboardInterface + 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 } func resourceSysdigDashboardCreate(ctx context.Context, data *schema.ResourceData, i interface{}) diag.Diagnostics { diff --git a/sysdig/resource_sysdig_monitor_dashboard_test.go b/sysdig/resource_sysdig_monitor_dashboard_test.go index e8ae364c..8e2a966d 100644 --- a/sysdig/resource_sysdig_monitor_dashboard_test.go +++ b/sysdig/resource_sysdig_monitor_dashboard_test.go @@ -1,13 +1,13 @@ -//go:build tf_acc_sysdig_monitor +//go:build tf_acc_sysdig_monitor || tf_acc_ibm_monitor package sysdig_test import ( "fmt" - v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2" - "os" "testing" + v2 "github.com/draios/terraform-provider-sysdig/sysdig/internal/client/v2" + "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" @@ -19,11 +19,7 @@ func TestAccDashboard(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 @@ -386,7 +382,7 @@ resource "sysdig_monitor_dashboard" "dashboard_2" { query { promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))" unit = "percent" - + format { display_format = "auto" input_format = "0-100" @@ -557,7 +553,7 @@ resource "sysdig_monitor_dashboard" "dashboard" { query { promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))" unit = "time" - + format { display_format = "auto" input_format = "ns" @@ -713,7 +709,7 @@ resource "sysdig_monitor_dashboard" "dashboard" { query { promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))" unit = "time" - + format { display_format = "auto" input_format = "ns" @@ -771,7 +767,7 @@ resource "sysdig_monitor_dashboard" "dashboard" { type = "timechart" name = "example panel" description = "description" - + legend { enabled = %[2]s show_current = %[3]s @@ -787,7 +783,7 @@ resource "sysdig_monitor_dashboard" "dashboard" { time_series_display_name_template = "{{host_hostname}}" type = "lines" } - + format { display_format = "auto" input_format = "0-100" @@ -823,7 +819,7 @@ resource "sysdig_monitor_dashboard" "dashboard" { query { promql = "avg(avg_over_time(sysdig_host_cpu_used_percent[$__interval]))" unit = "%[2]s" - + format { display_format = "%[3]s" input_format = "%[4]s" diff --git a/website/docs/index.md b/website/docs/index.md index 563af3f7..61b9f1a1 100644 --- a/website/docs/index.md +++ b/website/docs/index.md @@ -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. @@ -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" } ``` @@ -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" } @@ -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.
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)
- It can also be sourced from the `SYSDIG_MONITOR_URL` environment variable.
Notice: it should not be ended with a + It can also be sourced from the `SYSDIG_MONITOR_URL` environment variable.
Notice: it should not be ended with a slash.

* `sysdig_monitor_api_token` - (Required) The Sysdig Monitor API token. @@ -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. -
By default, it points to `https://secure.sysdig.com`. +
By default, it points to `https://secure.sysdig.com`.
[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)
It can also be sourced from the `SYSDIG_SECURE_URL` environment variable.
Notice: it should not be ended with a slash.

@@ -147,7 +147,7 @@ When Secure resources are to be created, this authentication must be in place.
Required if any `sysdig_secure_*` resource or data source is used.

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

@@ -159,13 +159,13 @@ 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).
Notice: it should not be ended with a slash.

-* `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.
It can also be configured from the `SYSDIG_IBM_MONITOR_IAM_URL` environment variable.

* `ibm_monitor_instance_id` (Required) This is the target instance ID (GUID format) of IBM instance which is hosting IBM Cloud Monitoring.
It can also be configured from the `SYSDIG_IBM_MONITOR_INSTANCE_ID` environment variable.

-* `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, ...).
It can also be configured from the `SYSDIG_IBM_MONITOR_API_KEY` environment variable.

@@ -173,10 +173,10 @@ When IBM Cloud Monitoring resources are to be created, this authentication must the use of invalid HTTPS certificates in the IBM Monitoring Cloud API.
It can also be sourced from the `SYSDIG_MONITOR_INSECURE_TLS` environment variable. By default, this is false.

-* `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.
It can also be configured from the `SYSDIG_MONITOR_TEAM_ID` environment variable.

-* `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.
It can also be configured from the `SYSDIG_MONITOR_TEAM_NAME` environment variable.

@@ -234,8 +234,13 @@ 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_monitor_dashboard` > - `sysdig_secure_posture_zone` -> +> > And data sources: > - `sysdig_monitor_notification_channel_pagerduty` > - `sysdig_monitor_notification_channel_email`