Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(alerts): allow ibm client for alert v2 and dashboard resources #391

Merged
merged 2 commits into from
Aug 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 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,8 @@ type Common interface {

type MonitorCommon interface {
AlertInterface
AlertV2Interface
DashboardInterface
}

type SecureCommon interface {
Expand Down
2 changes: 0 additions & 2 deletions sysdig/internal/client/v2/sysdig.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,7 @@ type SysdigCommon interface {
type SysdigMonitor interface {
SysdigCommon
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
19 changes: 17 additions & 2 deletions sysdig/resource_sysdig_monitor_dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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 {
Expand Down
24 changes: 10 additions & 14 deletions sysdig/resource_sysdig_monitor_dashboard_test.go
Original file line number Diff line number Diff line change
@@ -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"
Expand All @@ -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
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -771,7 +767,7 @@ resource "sysdig_monitor_dashboard" "dashboard" {
type = "timechart"
name = "example panel"
description = "description"

legend {
enabled = %[2]s
show_current = %[3]s
Expand All @@ -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"
Expand Down Expand Up @@ -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"
Expand Down
Loading
Loading