Skip to content

Commit 63e69ba

Browse files
authored
Refactor dashboard (#323)
* refactor(dashboards): refactor dashboards to v2 client
1 parent d74a83e commit 63e69ba

File tree

7 files changed

+191
-188
lines changed

7 files changed

+191
-188
lines changed

sysdig/internal/client/monitor/client.go

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@ import (
99
"net/http/httputil"
1010

1111
"github.com/hashicorp/go-retryablehttp"
12-
13-
"github.com/draios/terraform-provider-sysdig/sysdig/internal/client/monitor/model"
1412
)
1513

1614
type SysdigMonitorClient interface {
@@ -37,11 +35,6 @@ type SysdigMonitorClient interface {
3735
GetLabelDescriptor(ctx context.Context, label string) (LabelDescriptorV3, error)
3836

3937
GetNotificationChannelById(context.Context, int) (NotificationChannel, error)
40-
41-
GetDashboardByID(context.Context, int) (*model.Dashboard, error)
42-
CreateDashboard(context.Context, *model.Dashboard) (*model.Dashboard, error)
43-
UpdateDashboard(context.Context, *model.Dashboard) (*model.Dashboard, error)
44-
DeleteDashboard(context.Context, int) error
4538
}
4639

4740
func WithExtraHeaders(client SysdigMonitorClient, extraHeaders map[string]string) SysdigMonitorClient {

sysdig/internal/client/monitor/dashboards.go

Lines changed: 0 additions & 87 deletions
This file was deleted.
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package v2
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"net/http"
7+
)
8+
9+
const (
10+
dashboardsPath = "%s/api/v3/dashboards"
11+
dashboardPath = "%s/api/v3/dashboards/%d"
12+
)
13+
14+
type DashboardInterface interface {
15+
GetDashboard(ctx context.Context, ID int) (*Dashboard, error)
16+
CreateDashboard(ctx context.Context, dashboard *Dashboard) (*Dashboard, error)
17+
UpdateDashboard(ctx context.Context, dashboard *Dashboard) (*Dashboard, error)
18+
DeleteDashboard(ctx context.Context, ID int) error
19+
}
20+
21+
func (client *Client) GetDashboard(ctx context.Context, ID int) (*Dashboard, error) {
22+
response, err := client.requester.Request(ctx, http.MethodGet, client.getDashboardURL(ID), nil)
23+
if err != nil {
24+
return nil, err
25+
}
26+
defer response.Body.Close()
27+
28+
if response.StatusCode != http.StatusOK {
29+
return nil, client.ErrorFromResponse(response)
30+
}
31+
32+
wrapper, err := Unmarshal[*dashboardWrapper](response.Body)
33+
if err != nil {
34+
return nil, err
35+
}
36+
37+
return wrapper.Dashboard, nil
38+
}
39+
40+
func (client *Client) CreateDashboard(ctx context.Context, dashboard *Dashboard) (*Dashboard, error) {
41+
payload, err := Marshal(dashboardWrapper{Dashboard: dashboard})
42+
if err != nil {
43+
return nil, err
44+
}
45+
46+
response, err := client.requester.Request(ctx, http.MethodPost, client.getDashboardsURL(), payload)
47+
if err != nil {
48+
return nil, err
49+
}
50+
defer response.Body.Close()
51+
52+
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
53+
return nil, client.ErrorFromResponse(response)
54+
}
55+
56+
wrapper, err := Unmarshal[*dashboardWrapper](response.Body)
57+
if err != nil {
58+
return nil, err
59+
}
60+
61+
return wrapper.Dashboard, nil
62+
}
63+
64+
func (client *Client) UpdateDashboard(ctx context.Context, dashboard *Dashboard) (*Dashboard, error) {
65+
payload, err := Marshal(dashboardWrapper{Dashboard: dashboard})
66+
if err != nil {
67+
return nil, err
68+
}
69+
70+
response, err := client.requester.Request(ctx, http.MethodPut, client.getDashboardURL(dashboard.ID), payload)
71+
if err != nil {
72+
return nil, err
73+
}
74+
defer response.Body.Close()
75+
76+
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
77+
return nil, client.ErrorFromResponse(response)
78+
}
79+
80+
wrapper, err := Unmarshal[*dashboardWrapper](response.Body)
81+
if err != nil {
82+
return nil, err
83+
}
84+
85+
return wrapper.Dashboard, nil
86+
}
87+
88+
func (client *Client) DeleteDashboard(ctx context.Context, ID int) error {
89+
response, err := client.requester.Request(ctx, http.MethodDelete, client.getDashboardURL(ID), nil)
90+
if err != nil {
91+
return err
92+
}
93+
defer response.Body.Close()
94+
95+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
96+
return client.ErrorFromResponse(response)
97+
}
98+
99+
return nil
100+
}
101+
102+
func (client *Client) getDashboardsURL() string {
103+
return fmt.Sprintf(dashboardsPath, client.config.url)
104+
}
105+
106+
func (client *Client) getDashboardURL(id int) string {
107+
return fmt.Sprintf(dashboardPath, client.config.url, id)
108+
}

sysdig/internal/client/monitor/model/dashboard.go renamed to sysdig/internal/client/v2/model_dashboard.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
package model
1+
package v2
22

33
import (
4-
"bytes"
5-
"encoding/json"
64
"errors"
75
"fmt"
8-
"io"
96
)
107

118
type Layout struct {
@@ -39,7 +36,7 @@ type Left struct {
3936
Unit string `json:"unit"`
4037
DisplayFormat string `json:"displayFormat"`
4138
Decimals interface{} `json:"decimals"`
42-
MinValue int `json:"minValue"`
39+
MinValue float64 `json:"minValue"`
4340
MaxValue interface{} `json:"maxValue"`
4441
MinInputFormat string `json:"minInputFormat"`
4542
MaxInputFormat string `json:"maxInputFormat"`
@@ -52,7 +49,7 @@ type Right struct {
5249
Unit string `json:"unit"`
5350
DisplayFormat string `json:"displayFormat"`
5451
Decimals interface{} `json:"decimals"`
55-
MinValue int `json:"minValue"`
52+
MinValue float64 `json:"minValue"`
5653
MaxValue interface{} `json:"maxValue"`
5754
MinInputFormat string `json:"minInputFormat"`
5855
MaxInputFormat string `json:"maxInputFormat"`
@@ -375,11 +372,11 @@ func (p *Panels) WithLayout(xPos, yPos, width, height int) (*Panels, error) {
375372
}
376373

377374
type NumberThresholds struct {
378-
Base Base `json:"base"`
379-
Values []interface{} `json:"values"`
375+
Base NumberThresholdBase `json:"base"`
376+
Values []interface{} `json:"values"`
380377
}
381378

382-
type Base struct {
379+
type NumberThresholdBase struct {
383380
DisplayText string `json:"displayText"`
384381
Severity string `json:"severity"`
385382
}
@@ -441,11 +438,6 @@ type dashboardWrapper struct {
441438
Dashboard *Dashboard `json:"dashboard"`
442439
}
443440

444-
func (db *Dashboard) ToJSON() io.Reader {
445-
payload, _ := json.Marshal(dashboardWrapper{db})
446-
return bytes.NewBuffer(payload)
447-
}
448-
449441
func (db *Dashboard) AddPanels(panels ...*Panels) {
450442
maxPanelID := 0
451443
for _, existingPanel := range db.Panels {
@@ -476,10 +468,3 @@ func (db *Dashboard) AsPublic(value bool) *Dashboard {
476468
db.Public = value
477469
return db
478470
}
479-
480-
func DashboardFromJSON(body []byte) *Dashboard {
481-
var result dashboardWrapper
482-
_ = json.Unmarshal(body, &result)
483-
484-
return result.Dashboard
485-
}

sysdig/internal/client/v2/sysdig.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ type SysdigCommon interface {
2424
type SysdigMonitor interface {
2525
SysdigCommon
2626
MonitorCommon
27+
DashboardInterface
2728
CloudAccountMonitorInterface
2829
}
2930

0 commit comments

Comments
 (0)