Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into ay/feat/client/5.18.0
Browse files Browse the repository at this point in the history
  • Loading branch information
1riatsila1 committed Dec 3, 2024
2 parents 98a3850 + f60a162 commit 02551d5
Show file tree
Hide file tree
Showing 40 changed files with 207 additions and 151 deletions.
31 changes: 11 additions & 20 deletions .golangci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ linters-settings:
funlen:
lines: 90
statements: 60
tagliatelle:
case:
rules:
json: snake

linters:
# See the dedicated "linters" documentation section.
Expand All @@ -26,32 +30,16 @@ linters:
disable:
- cyclop # we have gocyclo, apparently its better
- execinquery # deprecated

- contextcheck # we want this
- copyloopvar # if we update to later than go 1.22
- depguard # for banning specific dependencies
- dupl # we want this
- dupword # we want this
- errorlint # we want this
- forcetypeassert # we want this
- funlen # we want this
- goconst # we want this
- gocritic # we want this
- intrange # later version of go
# - ireturn # we want this
- lll # we want this
- maintidx # we want this

- mnd # we want this todo: also see gomnd
- gomnd # disabled in gl provider todo: also see mnd
- noctx # we want this
- nestif # we want this
- revive # we want this
- tagliatelle # we want this
- testifylint # we want this
- thelper # we want this
- usestdlibvars # we want this
- whitespace # we want this
- wrapcheck # we want this
- dupl # we want this
- contextcheck # we want this, but the "trivial" fix causes context cancelled errors in tests
- depguard # for banning specific dependencies

- canonicalheader # for checking copyright headers
- err113 # disabled in gl provider
Expand Down Expand Up @@ -90,6 +78,9 @@ issues:

- linters:
- stylecheck
- goconst
- maintidx
- testifylint
path: pkg/config-api-client

- linters:
Expand Down
4 changes: 2 additions & 2 deletions internal/provider/resources/resource_sensor.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ func (r *sensorResource) Schema(
"notes": schema.StringAttribute{
Description: "The address notes of the sensor.",
Optional: true,
// computed because goes from from nil -> "" when sensor becomes configured
// computed because goes from nil -> "" when sensor becomes configured
Computed: true,
},
"pcap_mode": schema.StringAttribute{
Description: "The packet capture mode of the agent.",
Optional: true,
// computed because goes from from nil -> "light" when sensor becomes configured
// computed because goes from nil -> "light" when sensor becomes configured
Computed: true,
},
},
Expand Down
99 changes: 75 additions & 24 deletions internal/provider/util/error_handling.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,11 @@ package util

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

config_api_client "github.com/aruba-uxi/terraform-provider-hpeuxi/pkg/config-api-client"
)
Expand All @@ -20,28 +22,17 @@ func GenerateErrorSummary(actionName string, entityName string) string {
func RaiseForStatus(response *http.Response, err error) (bool, string) {
if err != nil {
var detail string
var data map[string]interface{}

switch e := err.(type) {
case *url.Error:
detail = handleURLError(e)
case *config_api_client.GenericOpenAPIError:
if err := json.NewDecoder(response.Body).Decode(&data); err != nil {
detail = fmt.Sprintf(
"Unexpected error: there was an error decoding the API response body for "+
"%d status code response.",
response.StatusCode,
)
} else if message, ok := data["message"]; ok {
detail = message.(string)
if debugID, ok := data["debugId"]; ok {
detail += "\nDebugID: " + debugID.(string)
}
} else {
detail = "Unexpected error: " + e.Error()
}

var uErr *url.Error
var apiErr *config_api_client.GenericOpenAPIError

switch {
case errors.As(err, &uErr):
detail = handleURLError(uErr)
case errors.As(err, &apiErr):
detail = handleJSONError(response, apiErr)
default:
detail = "Unexpected error: " + e.Error()
detail = "Unexpected error: " + err.Error()
}

return true, detail
Expand All @@ -50,12 +41,72 @@ func RaiseForStatus(response *http.Response, err error) (bool, string) {
return false, ""
}

func handleJSONError(
response *http.Response,
apiErr *config_api_client.GenericOpenAPIError,
) string {
data, err := parseJSONResponse(response)
if err != nil {
return err.Error()
}

message := buildJSONErrorMsg(data, apiErr)
debugID := buildJSONDebugID(data)
parts := []string{message, debugID}

return strings.Join(parts, "\n")
}

func parseJSONResponse(response *http.Response) (map[string]any, error) {
var data map[string]interface{}

err := json.NewDecoder(response.Body).Decode(&data)
if err != nil {
return map[string]any{}, fmt.Errorf(
"Unexpected error: there was an error decoding the API response body for "+
"%d status code response.",
response.StatusCode,
)
}

return data, nil
}

func buildJSONErrorMsg(data map[string]any, apiErr *config_api_client.GenericOpenAPIError) string {
message, found := data["message"]
if !found {
return "Unexpected error: " + apiErr.Error()
}

messageStr, castOK := message.(string)
if !castOK {
return "Unexpected error: " + apiErr.Error()
}

return messageStr
}

func buildJSONDebugID(data map[string]any) string {
debugID, found := data["debugId"]
if !found {
return ""
}

debugIDStr, castOK := debugID.(string)
if !castOK {
return ""
}

return "DebugID: " + debugIDStr
}

func handleURLError(uErr *url.Error) string {
if uErr.Timeout() {
switch {
case uErr.Timeout():
return "Error: Request timed out. Please check your network."
} else if uErr.Temporary() {
case uErr.Temporary():
return "Error: Temporary network error. Please try again later."
} else {
default:
return fmt.Sprintf("URL Error: %v\n", uErr)
}
}
16 changes: 4 additions & 12 deletions test/live/resources/agent_group_assignment_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,18 +142,10 @@ func TestAgentGroupAssignmentResource(t *testing.T) {
},
},
CheckDestroy: func(s *terraform.State) error {
assert.Equal(t, util.GetGroupByName(groupName), nil)
assert.Equal(t, util.GetGroupByName(group2Name), nil)
assert.Equal(
t,
util.GetAgentGroupAssignment(resourceIDBeforeRecreate),
nil,
)
assert.Equal(
t,
util.GetAgentGroupAssignment(resourceIDAfterRecreate),
nil,
)
assert.Nil(t, util.GetGroupByName(groupName))
assert.Nil(t, util.GetGroupByName(group2Name))
assert.Nil(t, util.GetAgentGroupAssignment(resourceIDBeforeRecreate))
assert.Nil(t, util.GetAgentGroupAssignment(resourceIDAfterRecreate))

return nil
},
Expand Down
2 changes: 1 addition & 1 deletion test/live/resources/agent_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func TestAgentResource(t *testing.T) {
},
},
CheckDestroy: func(s *terraform.State) error {
assert.Equal(t, util.GetAgent(agentID), nil)
assert.Nil(t, util.GetAgent(agentID))

return nil
},
Expand Down
12 changes: 6 additions & 6 deletions test/live/resources/group_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -246,12 +246,12 @@ func TestGroupResource(t *testing.T) {
},
},
CheckDestroy: func(s *terraform.State) error {
assert.Equal(t, util.GetGroupByName(groupNameParent), nil)
assert.Equal(t, util.GetGroupByName(groupNameParentUpdated), nil)
assert.Equal(t, util.GetGroupByName(groupNameChild), nil)
assert.Equal(t, util.GetGroupByName(groupNameGrandChild), nil)
assert.Equal(t, util.GetGroupByName(groupNameGrandChildMovedToParent), nil)
assert.Equal(t, util.GetGroupByName(groupNameGrandChildMovedToRoot), nil)
assert.Nil(t, util.GetGroupByName(groupNameParent))
assert.Nil(t, util.GetGroupByName(groupNameParentUpdated))
assert.Nil(t, util.GetGroupByName(groupNameChild))
assert.Nil(t, util.GetGroupByName(groupNameGrandChild))
assert.Nil(t, util.GetGroupByName(groupNameGrandChildMovedToParent))
assert.Nil(t, util.GetGroupByName(groupNameGrandChildMovedToRoot))

return nil
},
Expand Down
Loading

0 comments on commit 02551d5

Please sign in to comment.