Skip to content

Commit 345d46a

Browse files
authored
refactor: Move creation logics from Dashboard v3 to the client (#47)
Signed-off-by: Federico Barcelona <[email protected]>
1 parent 6fff856 commit 345d46a

File tree

5 files changed

+522
-352
lines changed

5 files changed

+522
-352
lines changed

sysdig/monitor/client.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package monitor
33
import (
44
"context"
55
"crypto/tls"
6+
"github.com/draios/terraform-provider-sysdig/sysdig/monitor/model"
67
"io"
78
"log"
89
"net/http"
@@ -26,9 +27,9 @@ type SysdigMonitorClient interface {
2627
DeleteNotificationChannel(context.Context, int) error
2728
UpdateNotificationChannel(context.Context, NotificationChannel) (NotificationChannel, error)
2829

29-
GetDashboardByID(context.Context, int) (Dashboard, error)
30-
CreateDashboard(context.Context, Dashboard) (Dashboard, error)
31-
UpdateDashboard(context.Context, Dashboard) (Dashboard, error)
30+
GetDashboardByID(context.Context, int) (*model.Dashboard, error)
31+
CreateDashboard(context.Context, *model.Dashboard) (*model.Dashboard, error)
32+
UpdateDashboard(context.Context, *model.Dashboard) (*model.Dashboard, error)
3233
DeleteDashboard(context.Context, int) error
3334
}
3435

sysdig/monitor/dashboards.go

Lines changed: 17 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,63 +5,65 @@ import (
55
"fmt"
66
"io/ioutil"
77
"net/http"
8+
9+
"github.com/draios/terraform-provider-sysdig/sysdig/monitor/model"
810
)
911

10-
func (client *sysdigMonitorClient) GetDashboardByID(ctx context.Context, ID int) (Dashboard, error) {
12+
func (client *sysdigMonitorClient) GetDashboardByID(ctx context.Context, ID int) (*model.Dashboard, error) {
1113
res, err := client.doSysdigMonitorRequest(ctx, http.MethodGet, client.getDashboardUrl(ID), nil)
1214
if err != nil {
13-
return Dashboard{}, err
15+
return nil, err
1416
}
1517
defer res.Body.Close()
1618

1719
body, err := ioutil.ReadAll(res.Body)
1820
if err != nil {
19-
return Dashboard{}, nil
21+
return nil, nil
2022
}
2123

2224
if res.StatusCode != http.StatusOK {
23-
return Dashboard{}, fmt.Errorf(string(body))
25+
return nil, fmt.Errorf(string(body))
2426
}
2527

26-
return DashboardFromJSON(body), nil
28+
return model.DashboardFromJSON(body), nil
2729
}
2830

29-
func (client *sysdigMonitorClient) CreateDashboard(ctx context.Context, dashboard Dashboard) (Dashboard, error) {
31+
func (client *sysdigMonitorClient) CreateDashboard(ctx context.Context, dashboard *model.Dashboard) (*model.Dashboard, error) {
3032
res, err := client.doSysdigMonitorRequest(ctx, http.MethodPost, client.getDashboardsUrl(), dashboard.ToJSON())
3133
if err != nil {
32-
return Dashboard{}, err
34+
return nil, err
3335
}
3436
defer res.Body.Close()
3537

3638
body, err := ioutil.ReadAll(res.Body)
3739
if err != nil {
38-
return Dashboard{}, err
40+
return nil, err
3941
}
4042

4143
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
42-
return Dashboard{}, fmt.Errorf(string(body))
44+
return nil, fmt.Errorf(string(body))
4345
}
4446

45-
return DashboardFromJSON(body), nil
47+
return model.DashboardFromJSON(body), nil
4648
}
4749

48-
func (client *sysdigMonitorClient) UpdateDashboard(ctx context.Context, dashboard Dashboard) (Dashboard, error) {
50+
func (client *sysdigMonitorClient) UpdateDashboard(ctx context.Context, dashboard *model.Dashboard) (*model.Dashboard, error) {
4951
res, err := client.doSysdigMonitorRequest(ctx, http.MethodPut, client.getDashboardUrl(dashboard.ID), dashboard.ToJSON())
5052
if err != nil {
51-
return Dashboard{}, err
53+
return nil, err
5254
}
5355
defer res.Body.Close()
5456

5557
body, err := ioutil.ReadAll(res.Body)
5658
if err != nil {
57-
return Dashboard{}, err
59+
return nil, err
5860
}
5961

6062
if res.StatusCode != http.StatusOK && res.StatusCode != http.StatusCreated {
61-
return Dashboard{}, fmt.Errorf(string(body))
63+
return nil, fmt.Errorf(string(body))
6264
}
6365

64-
return DashboardFromJSON(body), nil
66+
return model.DashboardFromJSON(body), nil
6567
}
6668

6769
func (client *sysdigMonitorClient) DeleteDashboard(ctx context.Context, ID int) error {

0 commit comments

Comments
 (0)