Skip to content

Commit 6d8b71a

Browse files
authored
feat: Add context in CRUD functions and in the client (#45)
Context in Go allows us to cancel requests, kill goroutines and propagate the cancellation. Since TF SDK v2, the CRUD functions accept context for cancellation. This commit switches from the old deprecated CRUD functions to the new ones with context. Signed-off-by: Federico Barcelona <[email protected]>
1 parent 8644bb4 commit 6d8b71a

File tree

46 files changed

+1126
-819
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

46 files changed

+1126
-819
lines changed

sysdig/common/client.go

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

33
import (
4+
"context"
45
"crypto/tls"
56
"io"
67
"log"
@@ -9,11 +10,11 @@ import (
910
)
1011

1112
type SysdigCommonClient interface {
12-
CreateUser(User) (User, error)
13-
GetUserById(int) (User, error)
14-
DeleteUser(int) error
15-
UpdateUser(User) (User, error)
16-
GetCurrentUser() (User, error)
13+
CreateUser(context.Context, User) (User, error)
14+
GetUserById(context.Context, int) (User, error)
15+
DeleteUser(context.Context, int) error
16+
UpdateUser(context.Context, User) (User, error)
17+
GetCurrentUser(context.Context) (User, error)
1718
}
1819

1920
func WithExtraHeaders(client SysdigCommonClient, extraHeaders map[string]string) SysdigCommonClient {
@@ -43,8 +44,9 @@ type sysdigCommonClient struct {
4344
extraHeaders map[string]string
4445
}
4546

46-
func (client *sysdigCommonClient) doSysdigCommonRequest(method string, url string, payload io.Reader) (*http.Response, error) {
47+
func (client *sysdigCommonClient) doSysdigCommonRequest(ctx context.Context, method string, url string, payload io.Reader) (*http.Response, error) {
4748
request, _ := http.NewRequest(method, url, payload)
49+
request = request.WithContext(ctx)
4850
request.Header.Set("Authorization", "Bearer "+client.SysdigAPIToken)
4951
request.Header.Set("Content-Type", "application/json")
5052
if client.extraHeaders != nil {

sysdig/common/users.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package common
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io/ioutil"
78
"net/http"
89
)
910

10-
func (client *sysdigCommonClient) GetUserById(id int) (u User, err error) {
11-
response, err := client.doSysdigCommonRequest(http.MethodGet, client.GetUserUrl(id), nil)
11+
func (client *sysdigCommonClient) GetUserById(ctx context.Context, id int) (u User, err error) {
12+
response, err := client.doSysdigCommonRequest(ctx, http.MethodGet, client.GetUserUrl(id), nil)
1213
if err != nil {
1314
return
1415
}
@@ -26,8 +27,8 @@ func (client *sysdigCommonClient) GetUserById(id int) (u User, err error) {
2627
return
2728
}
2829

29-
func (client *sysdigCommonClient) CreateUser(uRequest User) (u User, err error) {
30-
response, err := client.doSysdigCommonRequest(http.MethodPost, client.GetUsersUrl(), uRequest.ToJSON())
30+
func (client *sysdigCommonClient) CreateUser(ctx context.Context, uRequest User) (u User, err error) {
31+
response, err := client.doSysdigCommonRequest(ctx, http.MethodPost, client.GetUsersUrl(), uRequest.ToJSON())
3132

3233
if err != nil {
3334
return
@@ -45,8 +46,8 @@ func (client *sysdigCommonClient) CreateUser(uRequest User) (u User, err error)
4546
return
4647
}
4748

48-
func (client *sysdigCommonClient) UpdateUser(uRequest User) (u User, err error) {
49-
response, err := client.doSysdigCommonRequest(http.MethodPut, client.GetUserUrl(uRequest.ID), uRequest.ToJSON())
49+
func (client *sysdigCommonClient) UpdateUser(ctx context.Context, uRequest User) (u User, err error) {
50+
response, err := client.doSysdigCommonRequest(ctx, http.MethodPut, client.GetUserUrl(uRequest.ID), uRequest.ToJSON())
5051
if err != nil {
5152
return
5253
}
@@ -63,8 +64,8 @@ func (client *sysdigCommonClient) UpdateUser(uRequest User) (u User, err error)
6364
return
6465
}
6566

66-
func (client *sysdigCommonClient) DeleteUser(id int) error {
67-
response, err := client.doSysdigCommonRequest(http.MethodDelete, client.GetUserUrl(id), nil)
67+
func (client *sysdigCommonClient) DeleteUser(ctx context.Context, id int) error {
68+
response, err := client.doSysdigCommonRequest(ctx, http.MethodDelete, client.GetUserUrl(id), nil)
6869
if err != nil {
6970
return err
7071
}
@@ -76,8 +77,8 @@ func (client *sysdigCommonClient) DeleteUser(id int) error {
7677
return nil
7778
}
7879

79-
func (client *sysdigCommonClient) GetCurrentUser() (u User, err error) {
80-
response, err := client.doSysdigCommonRequest(http.MethodGet, client.GetCurrentUserUrl(), nil)
80+
func (client *sysdigCommonClient) GetCurrentUser(ctx context.Context) (u User, err error) {
81+
response, err := client.doSysdigCommonRequest(ctx, http.MethodGet, client.GetCurrentUserUrl(), nil)
8182
if err != nil {
8283
return
8384
}

sysdig/data_source_sysdig_current_user.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package sysdig
22

33
import (
4+
"context"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
46
"strconv"
57
"time"
68

@@ -11,7 +13,7 @@ func dataSourceSysdigCurrentUser() *schema.Resource {
1113
timeout := 30 * time.Second
1214

1315
return &schema.Resource{
14-
Read: dataSourceSysdigCurrentUserRead,
16+
ReadContext: dataSourceSysdigCurrentUserRead,
1517

1618
Timeouts: &schema.ResourceTimeout{
1719
Read: schema.DefaultTimeout(timeout),
@@ -39,15 +41,15 @@ func dataSourceSysdigCurrentUser() *schema.Resource {
3941
}
4042

4143
// Retrieves the information of a resource form the file and loads it in Terraform
42-
func dataSourceSysdigCurrentUserRead(d *schema.ResourceData, meta interface{}) error {
44+
func dataSourceSysdigCurrentUserRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
4345
client, err := meta.(SysdigClients).sysdigCommonClient()
4446
if err != nil {
45-
return err
47+
return diag.FromErr(err)
4648
}
4749

48-
user, err := client.GetCurrentUser()
50+
user, err := client.GetCurrentUser(ctx)
4951
if err != nil {
50-
return err
52+
return diag.FromErr(err)
5153
}
5254

5355
d.SetId(strconv.Itoa(user.ID))

sysdig/data_source_sysdig_secure_notification_channel.go

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

33
import (
4+
"context"
5+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
46
"regexp"
57
"strconv"
68
"strings"
@@ -23,7 +25,7 @@ func dataSourceSysdigSecureNotificationChannel() *schema.Resource {
2325
timeout := 30 * time.Second
2426

2527
return &schema.Resource{
26-
Read: dataSourceSysdigNotificationChannelRead,
28+
ReadContext: dataSourceSysdigNotificationChannelRead,
2729

2830
Timeouts: &schema.ResourceTimeout{
2931
Read: schema.DefaultTimeout(timeout),
@@ -103,15 +105,15 @@ func dataSourceSysdigSecureNotificationChannel() *schema.Resource {
103105
}
104106

105107
// Retrieves the information of a resource form the file and loads it in Terraform
106-
func dataSourceSysdigNotificationChannelRead(d *schema.ResourceData, meta interface{}) error {
108+
func dataSourceSysdigNotificationChannelRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
107109
client, err := meta.(SysdigClients).sysdigSecureClient()
108110
if err != nil {
109-
return err
111+
return diag.FromErr(err)
110112
}
111113

112-
nc, err := client.GetNotificationChannelByName(d.Get("name").(string))
114+
nc, err := client.GetNotificationChannelByName(ctx, d.Get("name").(string))
113115
if err != nil {
114-
return err
116+
return diag.FromErr(err)
115117
}
116118

117119
d.SetId(strconv.Itoa(nc.ID))
@@ -142,7 +144,7 @@ func dataSourceSysdigNotificationChannelRead(d *schema.ResourceData, meta interf
142144
if nc.Type == NOTIFICATION_CHANNEL_TYPE_OPSGENIE {
143145
regex, err := regexp.Compile("apiKey=(.*)?$")
144146
if err != nil {
145-
return err
147+
return diag.FromErr(err)
146148
}
147149
key := regex.FindStringSubmatch(nc.Options.Url)[1]
148150
d.Set("api_key", key)

sysdig/monitor/alerts.go

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package monitor
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io/ioutil"
78
"net/http"
89
)
910

10-
func (c *sysdigMonitorClient) CreateAlert(alert Alert) (createdAlert Alert, err error) {
11-
response, err := c.doSysdigMonitorRequest(http.MethodPost, c.alertsURL(), alert.ToJSON())
11+
func (c *sysdigMonitorClient) CreateAlert(ctx context.Context, alert Alert) (createdAlert Alert, err error) {
12+
response, err := c.doSysdigMonitorRequest(ctx, http.MethodPost, c.alertsURL(), alert.ToJSON())
1213
if err != nil {
1314
return
1415
}
@@ -27,16 +28,16 @@ func (c *sysdigMonitorClient) CreateAlert(alert Alert) (createdAlert Alert, err
2728
return AlertFromJSON(body), nil
2829
}
2930

30-
func (c *sysdigMonitorClient) DeleteAlert(alertID int) error {
31-
response, err := c.doSysdigMonitorRequest(http.MethodDelete, c.alertURL(alertID), nil)
31+
func (c *sysdigMonitorClient) DeleteAlert(ctx context.Context, alertID int) error {
32+
response, err := c.doSysdigMonitorRequest(ctx, http.MethodDelete, c.alertURL(alertID), nil)
3233

3334
defer response.Body.Close()
3435

3536
return err
3637
}
3738

38-
func (c *sysdigMonitorClient) UpdateAlert(alert Alert) (updatedAlert Alert, err error) {
39-
response, err := c.doSysdigMonitorRequest(http.MethodPut, c.alertURL(alert.ID), alert.ToJSON())
39+
func (c *sysdigMonitorClient) UpdateAlert(ctx context.Context, alert Alert) (updatedAlert Alert, err error) {
40+
response, err := c.doSysdigMonitorRequest(ctx, http.MethodPut, c.alertURL(alert.ID), alert.ToJSON())
4041
if err != nil {
4142
return
4243
}
@@ -56,8 +57,8 @@ func (c *sysdigMonitorClient) UpdateAlert(alert Alert) (updatedAlert Alert, err
5657
return AlertFromJSON(body), nil
5758
}
5859

59-
func (c *sysdigMonitorClient) GetAlertById(alertID int) (alert Alert, err error) {
60-
response, err := c.doSysdigMonitorRequest(http.MethodGet, c.alertURL(alertID), nil)
60+
func (c *sysdigMonitorClient) GetAlertById(ctx context.Context, alertID int) (alert Alert, err error) {
61+
response, err := c.doSysdigMonitorRequest(ctx, http.MethodGet, c.alertURL(alertID), nil)
6162
if err != nil {
6263
return
6364
}

sysdig/monitor/client.go

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package monitor
22

33
import (
4+
"context"
45
"crypto/tls"
56
"io"
67
"log"
@@ -9,21 +10,21 @@ import (
910
)
1011

1112
type SysdigMonitorClient interface {
12-
CreateAlert(Alert) (Alert, error)
13-
DeleteAlert(int) error
14-
UpdateAlert(Alert) (Alert, error)
15-
GetAlertById(int) (Alert, error)
13+
CreateAlert(context.Context, Alert) (Alert, error)
14+
DeleteAlert(context.Context, int) error
15+
UpdateAlert(context.Context, Alert) (Alert, error)
16+
GetAlertById(context.Context, int) (Alert, error)
1617

17-
CreateTeam(Team) (Team, error)
18-
GetTeamById(int) (Team, error)
19-
UpdateTeam(Team) (Team, error)
20-
DeleteTeam(int) error
18+
CreateTeam(context.Context, Team) (Team, error)
19+
GetTeamById(context.Context, int) (Team, error)
20+
UpdateTeam(context.Context, Team) (Team, error)
21+
DeleteTeam(context.Context, int) error
2122

22-
CreateNotificationChannel(NotificationChannel) (NotificationChannel, error)
23-
GetNotificationChannelById(int) (NotificationChannel, error)
24-
GetNotificationChannelByName(string) (NotificationChannel, error)
25-
DeleteNotificationChannel(int) error
26-
UpdateNotificationChannel(NotificationChannel) (NotificationChannel, error)
23+
CreateNotificationChannel(context.Context, NotificationChannel) (NotificationChannel, error)
24+
GetNotificationChannelById(context.Context, int) (NotificationChannel, error)
25+
GetNotificationChannelByName(context.Context, string) (NotificationChannel, error)
26+
DeleteNotificationChannel(context.Context, int) error
27+
UpdateNotificationChannel(context.Context, NotificationChannel) (NotificationChannel, error)
2728
}
2829

2930
func WithExtraHeaders(client SysdigMonitorClient, extraHeaders map[string]string) SysdigMonitorClient {
@@ -53,8 +54,9 @@ type sysdigMonitorClient struct {
5354
extraHeaders map[string]string
5455
}
5556

56-
func (client *sysdigMonitorClient) doSysdigMonitorRequest(method string, url string, payload io.Reader) (*http.Response, error) {
57+
func (client *sysdigMonitorClient) doSysdigMonitorRequest(ctx context.Context, method string, url string, payload io.Reader) (*http.Response, error) {
5758
request, _ := http.NewRequest(method, url, payload)
59+
request = request.WithContext(ctx)
5860
request.Header.Set("Authorization", "Bearer "+client.SysdigMonitorAPIToken)
5961
request.Header.Set("Content-Type", "application/json")
6062
if client.extraHeaders != nil {

sysdig/monitor/notification_channels.go

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
package monitor
22

33
import (
4+
"context"
45
"errors"
56
"fmt"
67
"io/ioutil"
78
"net/http"
89
)
910

10-
func (client *sysdigMonitorClient) GetNotificationChannelById(id int) (nc NotificationChannel, err error) {
11-
response, err := client.doSysdigMonitorRequest(http.MethodGet, client.GetNotificationChannelUrl(id), nil)
11+
func (client *sysdigMonitorClient) GetNotificationChannelById(ctx context.Context, id int) (nc NotificationChannel, err error) {
12+
response, err := client.doSysdigMonitorRequest(ctx, http.MethodGet, client.GetNotificationChannelUrl(id), nil)
1213
if err != nil {
1314
return
1415
}
@@ -30,8 +31,8 @@ func (client *sysdigMonitorClient) GetNotificationChannelById(id int) (nc Notifi
3031
return
3132
}
3233

33-
func (client *sysdigMonitorClient) GetNotificationChannelByName(name string) (nc NotificationChannel, err error) {
34-
response, err := client.doSysdigMonitorRequest(http.MethodGet, client.GetNotificationChannelsUrl(), nil)
34+
func (client *sysdigMonitorClient) GetNotificationChannelByName(ctx context.Context, name string) (nc NotificationChannel, err error) {
35+
response, err := client.doSysdigMonitorRequest(ctx, http.MethodGet, client.GetNotificationChannelsUrl(), nil)
3536
if err != nil {
3637
return
3738
}
@@ -57,8 +58,8 @@ func (client *sysdigMonitorClient) GetNotificationChannelByName(name string) (nc
5758
return
5859
}
5960

60-
func (client *sysdigMonitorClient) CreateNotificationChannel(ncRequest NotificationChannel) (nc NotificationChannel, err error) {
61-
response, err := client.doSysdigMonitorRequest(http.MethodPost, client.GetNotificationChannelsUrl(), ncRequest.ToJSON())
61+
func (client *sysdigMonitorClient) CreateNotificationChannel(ctx context.Context, ncRequest NotificationChannel) (nc NotificationChannel, err error) {
62+
response, err := client.doSysdigMonitorRequest(ctx, http.MethodPost, client.GetNotificationChannelsUrl(), ncRequest.ToJSON())
6263
if err != nil {
6364
return
6465
}
@@ -75,8 +76,8 @@ func (client *sysdigMonitorClient) CreateNotificationChannel(ncRequest Notificat
7576
return
7677
}
7778

78-
func (client *sysdigMonitorClient) UpdateNotificationChannel(ncRequest NotificationChannel) (nc NotificationChannel, err error) {
79-
response, err := client.doSysdigMonitorRequest(http.MethodPut, client.GetNotificationChannelUrl(ncRequest.ID), ncRequest.ToJSON())
79+
func (client *sysdigMonitorClient) UpdateNotificationChannel(ctx context.Context, ncRequest NotificationChannel) (nc NotificationChannel, err error) {
80+
response, err := client.doSysdigMonitorRequest(ctx, http.MethodPut, client.GetNotificationChannelUrl(ncRequest.ID), ncRequest.ToJSON())
8081
if err != nil {
8182
return
8283
}
@@ -93,8 +94,8 @@ func (client *sysdigMonitorClient) UpdateNotificationChannel(ncRequest Notificat
9394
return
9495
}
9596

96-
func (client *sysdigMonitorClient) DeleteNotificationChannel(id int) error {
97-
response, err := client.doSysdigMonitorRequest(http.MethodDelete, client.GetNotificationChannelUrl(id), nil)
97+
func (client *sysdigMonitorClient) DeleteNotificationChannel(ctx context.Context, id int) error {
98+
response, err := client.doSysdigMonitorRequest(ctx, http.MethodDelete, client.GetNotificationChannelUrl(id), nil)
9899
if err != nil {
99100
return err
100101
}

0 commit comments

Comments
 (0)