Skip to content

Commit 6a9f09c

Browse files
authored
feat: enhance error report and ignore removed resources on destroy (#107)
* feat: enhance error report and ignore removed resources on destroy * fix(ci): Workaround Go 1.16 issue with go.sum dependencies * chore: Update dependencies
1 parent 463ceec commit 6a9f09c

File tree

14 files changed

+227
-83
lines changed

14 files changed

+227
-83
lines changed

.github/workflows/ci-master.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ jobs:
4545
with:
4646
go-version: ^1.15
4747

48+
- name: Workaround Go 1.16 issue with go.sum dependencies
49+
run: go env -w GOFLAGS=-mod=mod
50+
4851
- name: Build project
4952
run: GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -o terraform-provider-sysdig
5053

@@ -69,6 +72,9 @@ jobs:
6972
restore-keys: |
7073
${{ runner.os }}-go-
7174
75+
- name: Workaround Go 1.16 issue with go.sum dependencies
76+
run: go env -w GOFLAGS=-mod=mod
77+
7278
- name: Get dependencies
7379
run: |
7480
go get -u -v github.com/onsi/ginkgo/ginkgo

.github/workflows/ci-pull-request.yml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,9 @@ jobs:
4545
with:
4646
go-version: ^1.15
4747

48+
- name: Workaround Go 1.16 issue with go.sum dependencies
49+
run: go env -w GOFLAGS=-mod=mod
50+
4851
- name: Build project
4952
run: GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -o terraform-provider-sysdig
5053

@@ -69,6 +72,9 @@ jobs:
6972
restore-keys: |
7073
${{ runner.os }}-go-
7174
75+
- name: Workaround Go 1.16 issue with go.sum dependencies
76+
run: go env -w GOFLAGS=-mod=mod
77+
7278
- name: Get dependencies
7379
run: |
7480
go get -u -v github.com/onsi/ginkgo/ginkgo

go.mod

Lines changed: 18 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,38 @@ require (
66
cloud.google.com/go v0.76.0 // indirect
77
cloud.google.com/go/storage v1.13.0 // indirect
88
github.com/agext/levenshtein v1.2.3 // indirect
9+
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect
10+
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 // indirect
911
github.com/apparentlymart/go-cidr v1.1.0 // indirect
1012
github.com/aws/aws-sdk-go v1.37.10 // indirect
11-
github.com/fatih/color v1.10.0 // indirect
13+
github.com/fatih/color v1.12.0 // indirect
1214
github.com/hashicorp/errwrap v1.1.0 // indirect
1315
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
14-
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
15-
github.com/hashicorp/go-getter v1.5.2 // indirect
16-
github.com/hashicorp/go-multierror v1.1.0 // indirect
17-
github.com/hashicorp/go-retryablehttp v0.6.8
16+
github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637
17+
github.com/hashicorp/go-hclog v0.16.2 // indirect
18+
github.com/hashicorp/go-multierror v1.1.1 // indirect
19+
github.com/hashicorp/go-plugin v1.4.2 // indirect
20+
github.com/hashicorp/go-retryablehttp v0.7.0
1821
github.com/hashicorp/go-uuid v1.0.2 // indirect
19-
github.com/hashicorp/hcl/v2 v2.8.2 // indirect
20-
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3
21-
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
22+
github.com/hashicorp/hcl/v2 v2.10.0 // indirect
23+
github.com/hashicorp/terraform-plugin-go v0.3.1 // indirect
24+
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
25+
github.com/hashicorp/yamux v0.0.0-20210707203944-259a57b3608c // indirect
2226
github.com/jmespath/go-jmespath v0.4.0
27+
github.com/keybase/go-crypto v0.0.0-20161004153544-93f5b35093ba // indirect
2328
github.com/klauspost/compress v1.11.7 // indirect
24-
github.com/mitchellh/copystructure v1.1.1 // indirect
29+
github.com/mattn/go-isatty v0.0.13 // indirect
2530
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
2631
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
2732
github.com/mitchellh/mapstructure v1.4.1 // indirect
2833
github.com/oklog/run v1.1.0 // indirect
2934
github.com/spf13/cast v1.3.1
3035
github.com/ulikunitz/xz v0.5.10 // indirect
31-
github.com/zclconf/go-cty v1.7.1 // indirect
36+
github.com/zclconf/go-cty v1.9.0 // indirect
3237
go.opencensus.io v0.22.6 // indirect
33-
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
38+
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
3439
golang.org/x/oauth2 v0.0.0-20210210192628-66670185b0cd // indirect
40+
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
3541
google.golang.org/api v0.40.0 // indirect
36-
google.golang.org/genproto v0.0.0-20210212180131-e7f2df4ecc2d // indirect
42+
google.golang.org/genproto v0.0.0-20210713002101-d411969a0d9a // indirect
3743
)

go.sum

Lines changed: 117 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package common
2+
3+
import (
4+
"encoding/json"
5+
"errors"
6+
"net/http"
7+
"strings"
8+
9+
"github.com/jmespath/go-jmespath"
10+
"github.com/spf13/cast"
11+
)
12+
13+
func errorFromResponse(response *http.Response) error {
14+
var data interface{}
15+
err := json.NewDecoder(response.Body).Decode(&data)
16+
if err != nil {
17+
return errors.New(response.Status)
18+
}
19+
20+
search, err := jmespath.Search("[message, errors[].[reason, message]][][] | join(', ', @)", data)
21+
if err != nil {
22+
return errors.New(response.Status)
23+
}
24+
25+
if searchArray, ok := search.([]interface{}); ok {
26+
return errors.New(strings.Join(cast.ToStringSlice(searchArray), ", "))
27+
}
28+
29+
return errors.New(cast.ToString(search))
30+
}

sysdig/internal/client/common/users.go

Lines changed: 12 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package common
33
import (
44
"context"
55
"encoding/json"
6-
"errors"
76
"fmt"
87
"io/ioutil"
98
"net/http"
@@ -16,13 +15,12 @@ func (client *sysdigCommonClient) GetUserById(ctx context.Context, id int) (u *U
1615
}
1716
defer response.Body.Close()
1817

19-
body, _ := ioutil.ReadAll(response.Body)
20-
2118
if response.StatusCode != http.StatusOK {
22-
err = errors.New(response.Status)
19+
err = errorFromResponse(response)
2320
return
2421
}
2522

23+
body, _ := ioutil.ReadAll(response.Body)
2624
user := UserFromJSON(body)
2725
return &user, nil
2826
}
@@ -34,18 +32,16 @@ func (client *sysdigCommonClient) GetUserByEmail(ctx context.Context, email stri
3432
}
3533
defer response.Body.Close()
3634

37-
body, _ := ioutil.ReadAll(response.Body)
38-
3935
if response.StatusCode != http.StatusOK {
40-
err = errors.New(response.Status)
36+
err = errorFromResponse(response)
4137
return
4238
}
4339

4440
var userList struct {
4541
Users []User `json:"users"`
4642
}
4743

48-
err = json.Unmarshal(body, &userList)
44+
err = json.NewDecoder(response.Body).Decode(&userList)
4945
if err != nil {
5046
return
5147
}
@@ -67,13 +63,12 @@ func (client *sysdigCommonClient) CreateUser(ctx context.Context, uRequest *User
6763
}
6864
defer response.Body.Close()
6965

70-
body, _ := ioutil.ReadAll(response.Body)
71-
7266
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
73-
err = errors.New(response.Status)
67+
err = errorFromResponse(response)
7468
return
7569
}
7670

71+
body, _ := ioutil.ReadAll(response.Body)
7772
user := UserFromJSON(body)
7873
return &user, nil
7974
}
@@ -85,13 +80,12 @@ func (client *sysdigCommonClient) UpdateUser(ctx context.Context, uRequest *User
8580
}
8681
defer response.Body.Close()
8782

88-
body, _ := ioutil.ReadAll(response.Body)
89-
9083
if response.StatusCode != http.StatusOK {
91-
err = errors.New(response.Status)
84+
err = errorFromResponse(response)
9285
return
9386
}
9487

88+
body, _ := ioutil.ReadAll(response.Body)
9589
user := UserFromJSON(body)
9690
return &user, nil
9791
}
@@ -103,8 +97,8 @@ func (client *sysdigCommonClient) DeleteUser(ctx context.Context, id int) error
10397
}
10498
defer response.Body.Close()
10599

106-
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
107-
return errors.New(response.Status)
100+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
101+
return errorFromResponse(response)
108102
}
109103
return nil
110104
}
@@ -117,11 +111,11 @@ func (client *sysdigCommonClient) GetCurrentUser(ctx context.Context) (u *User,
117111
defer response.Body.Close()
118112

119113
if response.StatusCode != http.StatusOK {
120-
err = errors.New(response.Status)
114+
err = errorFromResponse(response)
121115
return
122116
}
123-
body, _ := ioutil.ReadAll(response.Body)
124117

118+
body, _ := ioutil.ReadAll(response.Body)
125119
user := UserFromJSON(body)
126120
return &user, nil
127121
}

sysdig/internal/client/monitor/alerts.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ func (c *sysdigMonitorClient) DeleteAlert(ctx context.Context, alertID int) erro
3333
}
3434
defer response.Body.Close()
3535

36-
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
36+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
3737
return errorFromResponse(response)
3838
}
3939

@@ -47,7 +47,7 @@ func (c *sysdigMonitorClient) UpdateAlert(ctx context.Context, alert Alert) (upd
4747
}
4848
defer response.Body.Close()
4949

50-
if response.StatusCode != 200 {
50+
if response.StatusCode != http.StatusOK {
5151
err = errorFromResponse(response)
5252
return
5353
}
@@ -66,7 +66,7 @@ func (c *sysdigMonitorClient) GetAlertById(ctx context.Context, alertID int) (al
6666
}
6767
defer response.Body.Close()
6868

69-
if response.StatusCode != 200 {
69+
if response.StatusCode != http.StatusOK {
7070
err = errorFromResponse(response)
7171
return
7272
}

sysdig/internal/client/monitor/dashboards.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ func (client *sysdigMonitorClient) DeleteDashboard(ctx context.Context, ID int)
7171
}
7272
defer response.Body.Close()
7373

74-
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
74+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
7575
return errorFromResponse(response)
7676
}
7777

sysdig/internal/client/monitor/notification_channels.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ func (client *sysdigMonitorClient) DeleteNotificationChannel(ctx context.Context
9797
}
9898
defer response.Body.Close()
9999

100-
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
100+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
101101
return errorFromResponse(response)
102102
}
103103
return nil

sysdig/internal/client/monitor/teams.go

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,12 @@ func (client *sysdigMonitorClient) getUserIdbyEmail(ctx context.Context, userRol
1616
}
1717
defer response.Body.Close()
1818

19-
body, _ := ioutil.ReadAll(response.Body)
20-
2119
if response.StatusCode != http.StatusOK {
22-
err = errors.New(response.Status)
20+
err = errorFromResponse(response)
2321
return nil, err
2422
}
2523

24+
body, _ := ioutil.ReadAll(response.Body)
2625
// Set User Id to UserRoles struct
2726
usersList := UsersListFromJSON(body)
2827
usersMap := make(map[string]int)
@@ -31,7 +30,6 @@ func (client *sysdigMonitorClient) getUserIdbyEmail(ctx context.Context, userRol
3130
}
3231

3332
modifiedUserRoles := []UserRoles{}
34-
3533
for _, userRole := range userRoles {
3634
ur := userRole
3735
id, ok := usersMap[ur.Email]
@@ -52,13 +50,12 @@ func (client *sysdigMonitorClient) GetTeamById(ctx context.Context, id int) (t T
5250
}
5351
defer response.Body.Close()
5452

55-
body, _ := ioutil.ReadAll(response.Body)
56-
5753
if response.StatusCode != http.StatusOK {
58-
err = errors.New(response.Status)
54+
err = errorFromResponse(response)
5955
return
6056
}
6157

58+
body, _ := ioutil.ReadAll(response.Body)
6259
t = TeamFromJSON(body)
6360

6461
return
@@ -78,13 +75,12 @@ func (client *sysdigMonitorClient) CreateTeam(ctx context.Context, tRequest Team
7875
}
7976
defer response.Body.Close()
8077

81-
body, _ := ioutil.ReadAll(response.Body)
82-
8378
if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
84-
err = errors.New(response.Status + " " + string(body))
79+
err = errorFromResponse(response)
8580
return
8681
}
8782

83+
body, _ := ioutil.ReadAll(response.Body)
8884
t = TeamFromJSON(body)
8985
return
9086
}
@@ -102,13 +98,12 @@ func (client *sysdigMonitorClient) UpdateTeam(ctx context.Context, tRequest Team
10298
}
10399
defer response.Body.Close()
104100

105-
body, _ := ioutil.ReadAll(response.Body)
106-
107101
if response.StatusCode != http.StatusOK {
108-
err = errors.New(response.Status)
102+
err = errorFromResponse(response)
109103
return
110104
}
111105

106+
body, _ := ioutil.ReadAll(response.Body)
112107
t = TeamFromJSON(body)
113108
return
114109
}
@@ -120,7 +115,7 @@ func (client *sysdigMonitorClient) DeleteTeam(ctx context.Context, id int) error
120115
}
121116
defer response.Body.Close()
122117

123-
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
118+
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
124119
return errorFromResponse(response)
125120
}
126121
return nil

0 commit comments

Comments
 (0)