Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
Viktoras Makauskas committed Feb 19, 2024
2 parents d981551 + 94e4969 commit ecdc0c1
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ test:
.PHONY: testacc
testacc:
@echo "==> Running acceptance tests"
TF_ACC=1 go test ./castai/... '-run=^TestAcc' -v -timeout 16m
TF_ACC=1 go test ./castai/... '-run=^TestAcc' -v -timeout 20m

.PHONY: validate-terraform-examples
validate-terraform-examples:
Expand Down
3 changes: 2 additions & 1 deletion castai/resource_eks_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -201,9 +201,10 @@ func updateClusterSettings(ctx context.Context, data *schema.ResourceData, clien
}
err = sdk.StatusOk(response)
// In case of malformed user request return error to user right away.
if response.StatusCode() == 400 {
if response.StatusCode() == 400 && !sdk.IsCredentialsError(response) {
return backoff.Permanent(err)
}

return err
}, backoff.NewExponentialBackOff()); err != nil {
return fmt.Errorf("updating cluster configuration: %w", err)
Expand Down
2 changes: 1 addition & 1 deletion castai/resource_gke_cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ func updateGKEClusterSettings(ctx context.Context, data *schema.ResourceData, cl
}
err = sdk.StatusOk(response)
// In case of malformed user request return error to user right away.
if response.StatusCode() == 400 {
if response.StatusCode() == 400 && !sdk.IsCredentialsError(response) {
return backoff.Permanent(err)
}
return err
Expand Down
21 changes: 21 additions & 0 deletions castai/sdk/utils_response.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sdk

import (
"encoding/json"
"fmt"
"net/http"
)
Expand Down Expand Up @@ -32,3 +33,23 @@ func checkResponse(response Response, err error, expectedStatus int) error {

return nil
}

type ErrorResponse struct {
Message string `json:"message"`
FieldViolations []struct {
Field string `json:"field"`
Description string `json:"description"`
} `json:"fieldViolations"`
}

func IsCredentialsError(response Response) bool {
buf := response.GetBody()

var errResponse ErrorResponse
err := json.Unmarshal(buf, &errResponse)
if err != nil {
return false
}

return errResponse.Message == "Forbidden" && len(errResponse.FieldViolations) > 0 && errResponse.FieldViolations[0].Field == "credentials"
}
54 changes: 54 additions & 0 deletions castai/sdk/utils_response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package sdk

import (
"testing"

"github.com/stretchr/testify/require"
)

func Test_IsCredentialsError(t *testing.T) {
t.Run("credentials error", func(t *testing.T) {
t.Parallel()
r := require.New(t)

body := `{
"message": "Forbidden",
"fieldViolations":
[{"field": "credentials", "description": ""}]
}`
resp := ExternalClusterAPIReconcileClusterResponse{
Body: []byte(body),
}
result := IsCredentialsError(resp)
r.True(result)
})
t.Run("not credentials error", func(t *testing.T) {
t.Parallel()
r := require.New(t)

body := `{
"message": "something",
"fieldViolations":
[{"field": "credentials", "description": ""}]
}`
resp := ExternalClusterAPIReconcileClusterResponse{
Body: []byte(body),
}
result := IsCredentialsError(resp)
r.False(result)
})
t.Run("no fields in message", func(t *testing.T) {
t.Parallel()
r := require.New(t)

body := `{
"message": "something",
"fieldViolations":[]
}`
resp := ExternalClusterAPIReconcileClusterResponse{
Body: []byte(body),
}
result := IsCredentialsError(resp)
r.False(result)
})
}

0 comments on commit ecdc0c1

Please sign in to comment.