Skip to content

Commit

Permalink
feat: enhance error report and ignore removed resources on destroy (#107
Browse files Browse the repository at this point in the history
)

* feat: enhance error report and ignore removed resources on destroy
* fix(ci): Workaround Go 1.16 issue with go.sum dependencies
* chore: Update dependencies
  • Loading branch information
tembleking authored Jul 13, 2021
1 parent 463ceec commit 6a9f09c
Show file tree
Hide file tree
Showing 14 changed files with 227 additions and 83 deletions.
6 changes: 6 additions & 0 deletions .github/workflows/ci-master.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ jobs:
with:
go-version: ^1.15

- name: Workaround Go 1.16 issue with go.sum dependencies
run: go env -w GOFLAGS=-mod=mod

- name: Build project
run: GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -o terraform-provider-sysdig

Expand All @@ -69,6 +72,9 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- name: Workaround Go 1.16 issue with go.sum dependencies
run: go env -w GOFLAGS=-mod=mod

- name: Get dependencies
run: |
go get -u -v github.com/onsi/ginkgo/ginkgo
Expand Down
6 changes: 6 additions & 0 deletions .github/workflows/ci-pull-request.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@ jobs:
with:
go-version: ^1.15

- name: Workaround Go 1.16 issue with go.sum dependencies
run: go env -w GOFLAGS=-mod=mod

- name: Build project
run: GOOS=${{ matrix.os }} GOARCH=${{ matrix.arch }} go build -o terraform-provider-sysdig

Expand All @@ -69,6 +72,9 @@ jobs:
restore-keys: |
${{ runner.os }}-go-
- name: Workaround Go 1.16 issue with go.sum dependencies
run: go env -w GOFLAGS=-mod=mod

- name: Get dependencies
run: |
go get -u -v github.com/onsi/ginkgo/ginkgo
Expand Down
30 changes: 18 additions & 12 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,38 @@ require (
cloud.google.com/go v0.76.0 // indirect
cloud.google.com/go/storage v1.13.0 // indirect
github.com/agext/levenshtein v1.2.3 // indirect
github.com/agl/ed25519 v0.0.0-20170116200512-5312a6153412 // indirect
github.com/alcortesm/tgz v0.0.0-20161220082320-9c5fe88206d7 // indirect
github.com/apparentlymart/go-cidr v1.1.0 // indirect
github.com/aws/aws-sdk-go v1.37.10 // indirect
github.com/fatih/color v1.10.0 // indirect
github.com/fatih/color v1.12.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-cleanhttp v0.5.2 // indirect
github.com/hashicorp/go-cty v1.4.1-0.20200414143053-d3edf31b6320
github.com/hashicorp/go-getter v1.5.2 // indirect
github.com/hashicorp/go-multierror v1.1.0 // indirect
github.com/hashicorp/go-retryablehttp v0.6.8
github.com/hashicorp/go-cty v1.4.1-0.20200723130312-85980079f637
github.com/hashicorp/go-hclog v0.16.2 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-plugin v1.4.2 // indirect
github.com/hashicorp/go-retryablehttp v0.7.0
github.com/hashicorp/go-uuid v1.0.2 // indirect
github.com/hashicorp/hcl/v2 v2.8.2 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.4.3
github.com/hashicorp/yamux v0.0.0-20200609203250-aecfd211c9ce // indirect
github.com/hashicorp/hcl/v2 v2.10.0 // indirect
github.com/hashicorp/terraform-plugin-go v0.3.1 // indirect
github.com/hashicorp/terraform-plugin-sdk/v2 v2.7.0
github.com/hashicorp/yamux v0.0.0-20210707203944-259a57b3608c // indirect
github.com/jmespath/go-jmespath v0.4.0
github.com/keybase/go-crypto v0.0.0-20161004153544-93f5b35093ba // indirect
github.com/klauspost/compress v1.11.7 // indirect
github.com/mitchellh/copystructure v1.1.1 // indirect
github.com/mattn/go-isatty v0.0.13 // indirect
github.com/mitchellh/go-testing-interface v1.14.1 // indirect
github.com/mitchellh/go-wordwrap v1.0.1 // indirect
github.com/mitchellh/mapstructure v1.4.1 // indirect
github.com/oklog/run v1.1.0 // indirect
github.com/spf13/cast v1.3.1
github.com/ulikunitz/xz v0.5.10 // indirect
github.com/zclconf/go-cty v1.7.1 // indirect
github.com/zclconf/go-cty v1.9.0 // indirect
go.opencensus.io v0.22.6 // indirect
golang.org/x/crypto v0.0.0-20201221181555-eec23a3978ad // indirect
golang.org/x/net v0.0.0-20210614182718-04defd469f4e // indirect
golang.org/x/oauth2 v0.0.0-20210210192628-66670185b0cd // indirect
golang.org/x/sys v0.0.0-20210630005230-0f9fa26af87c // indirect
google.golang.org/api v0.40.0 // indirect
google.golang.org/genproto v0.0.0-20210212180131-e7f2df4ecc2d // indirect
google.golang.org/genproto v0.0.0-20210713002101-d411969a0d9a // indirect
)
117 changes: 117 additions & 0 deletions go.sum

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions sysdig/internal/client/common/helpers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package common

import (
"encoding/json"
"errors"
"net/http"
"strings"

"github.com/jmespath/go-jmespath"
"github.com/spf13/cast"
)

func errorFromResponse(response *http.Response) error {
var data interface{}
err := json.NewDecoder(response.Body).Decode(&data)
if err != nil {
return errors.New(response.Status)
}

search, err := jmespath.Search("[message, errors[].[reason, message]][][] | join(', ', @)", data)
if err != nil {
return errors.New(response.Status)
}

if searchArray, ok := search.([]interface{}); ok {
return errors.New(strings.Join(cast.ToStringSlice(searchArray), ", "))
}

return errors.New(cast.ToString(search))
}
30 changes: 12 additions & 18 deletions sysdig/internal/client/common/users.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package common
import (
"context"
"encoding/json"
"errors"
"fmt"
"io/ioutil"
"net/http"
Expand All @@ -16,13 +15,12 @@ func (client *sysdigCommonClient) GetUserById(ctx context.Context, id int) (u *U
}
defer response.Body.Close()

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}

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

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}

var userList struct {
Users []User `json:"users"`
}

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

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}

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

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}

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

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
return errors.New(response.Status)
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
return errorFromResponse(response)
}
return nil
}
Expand All @@ -117,11 +111,11 @@ func (client *sysdigCommonClient) GetCurrentUser(ctx context.Context) (u *User,
defer response.Body.Close()

if response.StatusCode != http.StatusOK {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}
body, _ := ioutil.ReadAll(response.Body)

body, _ := ioutil.ReadAll(response.Body)
user := UserFromJSON(body)
return &user, nil
}
Expand Down
6 changes: 3 additions & 3 deletions sysdig/internal/client/monitor/alerts.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func (c *sysdigMonitorClient) DeleteAlert(ctx context.Context, alertID int) erro
}
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
return errorFromResponse(response)
}

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

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

if response.StatusCode != 200 {
if response.StatusCode != http.StatusOK {
err = errorFromResponse(response)
return
}
Expand Down
2 changes: 1 addition & 1 deletion sysdig/internal/client/monitor/dashboards.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ func (client *sysdigMonitorClient) DeleteDashboard(ctx context.Context, ID int)
}
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
return errorFromResponse(response)
}

Expand Down
2 changes: 1 addition & 1 deletion sysdig/internal/client/monitor/notification_channels.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (client *sysdigMonitorClient) DeleteNotificationChannel(ctx context.Context
}
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
return errorFromResponse(response)
}
return nil
Expand Down
23 changes: 9 additions & 14 deletions sysdig/internal/client/monitor/teams.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,12 @@ func (client *sysdigMonitorClient) getUserIdbyEmail(ctx context.Context, userRol
}
defer response.Body.Close()

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK {
err = errors.New(response.Status)
err = errorFromResponse(response)
return nil, err
}

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

modifiedUserRoles := []UserRoles{}

for _, userRole := range userRoles {
ur := userRole
id, ok := usersMap[ur.Email]
Expand All @@ -52,13 +50,12 @@ func (client *sysdigMonitorClient) GetTeamById(ctx context.Context, id int) (t T
}
defer response.Body.Close()

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}

body, _ := ioutil.ReadAll(response.Body)
t = TeamFromJSON(body)

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

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK && response.StatusCode != http.StatusCreated {
err = errors.New(response.Status + " " + string(body))
err = errorFromResponse(response)
return
}

body, _ := ioutil.ReadAll(response.Body)
t = TeamFromJSON(body)
return
}
Expand All @@ -102,13 +98,12 @@ func (client *sysdigMonitorClient) UpdateTeam(ctx context.Context, tRequest Team
}
defer response.Body.Close()

body, _ := ioutil.ReadAll(response.Body)

if response.StatusCode != http.StatusOK {
err = errors.New(response.Status)
err = errorFromResponse(response)
return
}

body, _ := ioutil.ReadAll(response.Body)
t = TeamFromJSON(body)
return
}
Expand All @@ -120,7 +115,7 @@ func (client *sysdigMonitorClient) DeleteTeam(ctx context.Context, id int) error
}
defer response.Body.Close()

if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK {
if response.StatusCode != http.StatusNoContent && response.StatusCode != http.StatusOK && response.StatusCode != http.StatusNotFound {
return errorFromResponse(response)
}
return nil
Expand Down
Loading

0 comments on commit 6a9f09c

Please sign in to comment.