diff --git a/.golangci.yml b/.golangci.yml index 0ca01f12..f1b717e6 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -73,7 +73,6 @@ linters: - gochecknoinits # Checks that no init functions are present in Go code [fast: true, auto-fix: false] - gocognit # Computes and checks the cognitive complexity of functions [fast: true, auto-fix: false] - godox # Tool for detection of FIXME, TODO and other comment keywords [fast: true, auto-fix: false] - - err113 # Golang linter to check the errors handling expressions [fast: false, auto-fix: false] - gomnd # An analyzer to detect magic numbers. [fast: true, auto-fix: false] - lll # Reports long lines [fast: true, auto-fix: false] - maintidx # maintidx measures the maintainability index of each function. [fast: true, auto-fix: false] @@ -116,9 +115,8 @@ linters-settings: # Choose whether to use the extra rules. # Default: false extra-rules: true - + revive: - revive: ignore-generated-header: true severity: error rules: @@ -133,7 +131,7 @@ linters-settings: disabled: false arguments: # List Bypass rules - - [] + - [] # List force rules - [ # Default rules @@ -183,4 +181,4 @@ linters-settings: "VCDA", "NAT", "VPN", - ] + ] diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index cfa102db..42d86281 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -4,17 +4,29 @@ repos: hooks: - id: check-merge-conflict - id: trailing-whitespace + exclude: > + (?x)^( + docs/| + templates/| + )$ - id: end-of-file-fixer - id: forbid-submodules ## GOLANG -- repo: https://github.com/tekwizely/pre-commit-golang - rev: v1.0.0-rc.1 +- repo: https://github.com/golangci/golangci-lint + rev: v1.61.0 hooks: - - id: go-vet - - id: go-imports-repo - - id: golangci-lint-repo-pkg - - id: go-mod-tidy-repo + - id: golangci-lint + entry: bash -c 'exec golangci-lint run -j=6' + +- repo: local + hooks: + - id: gomod + name: gomod + entry: .pre-commit/go-mod + language: script + files: go.mod + pass_filenames: false ## GIT - repo: https://github.com/compilerla/conventional-pre-commit diff --git a/.pre-commit/go-mod b/.pre-commit/go-mod new file mode 100755 index 00000000..25d26ff0 --- /dev/null +++ b/.pre-commit/go-mod @@ -0,0 +1,6 @@ +#!/usr/bin/env bash +# +# go mod tidy will alter go.mod or go.sum if it finds any discrepancies +# + +go mod tidy diff --git a/go.mod b/go.mod index 8f6067f9..94fd0deb 100644 --- a/go.mod +++ b/go.mod @@ -23,10 +23,8 @@ require ( github.com/hashicorp/terraform-plugin-log v0.9.0 github.com/hashicorp/terraform-plugin-sdk/v2 v2.34.0 github.com/iancoleman/strcase v0.3.0 - github.com/kr/pretty v0.3.1 github.com/orange-cloudavenue/cloudavenue-sdk-go v0.11.1 github.com/orange-cloudavenue/common-go/utils v0.0.0-20240119163616-66b473d92339 - github.com/orange-cloudavenue/infrapi-sdk-go v0.1.4-0.20231005074857-89878ea119fb github.com/rs/zerolog v1.33.0 github.com/thanhpk/randstr v1.0.6 github.com/vmware/go-vcloud-director/v2 v2.25.0 @@ -44,7 +42,6 @@ require ( github.com/ProtonMail/go-crypto v1.1.0-alpha.2 // indirect github.com/agext/levenshtein v1.2.3 // indirect github.com/ajg/form v1.5.1 // indirect - github.com/antihax/optional v1.0.0 // indirect github.com/apparentlymart/go-textseg/v15 v15.0.0 // indirect github.com/araddon/dateparse v0.0.0-20210429162001-6b43995a97de // indirect github.com/armon/go-radix v1.0.0 // indirect @@ -85,6 +82,7 @@ require ( github.com/influxdata/influxdb-client-go/v2 v2.12.3 // indirect github.com/influxdata/line-protocol v0.0.0-20200327222509-2487e7298839 // indirect github.com/jmespath/go-jmespath v0.4.0 // indirect + github.com/kr/pretty v0.3.1 // indirect github.com/kr/text v0.2.0 // indirect github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.20 // indirect @@ -112,7 +110,6 @@ require ( go.abhg.dev/goldmark/frontmatter v0.2.0 // indirect golang.org/x/crypto v0.26.0 // indirect golang.org/x/mod v0.20.0 // indirect - golang.org/x/oauth2 v0.17.0 // indirect golang.org/x/sync v0.8.0 // indirect golang.org/x/sys v0.23.0 // indirect golang.org/x/text v0.17.0 // indirect diff --git a/go.sum b/go.sum index 46e4df9d..371be18a 100644 --- a/go.sum +++ b/go.sum @@ -28,8 +28,6 @@ github.com/agext/levenshtein v1.2.3 h1:YB2fHEn0UJagG8T1rrWknE3ZQzWM06O8AMAatNn7l github.com/agext/levenshtein v1.2.3/go.mod h1:JEDfjyjHDjOF/1e4FlBE/PkbqA9OfWu2ki2W0IB5558= github.com/ajg/form v1.5.1 h1:t9c7v8JUKu/XxOGBU0yjNpaMloxGEJhUkqFRq0ibGeU= github.com/ajg/form v1.5.1/go.mod h1:uL1WgH+h2mgNtvBq0339dVnzXdBETtL2LeUXaIv25UY= -github.com/antihax/optional v1.0.0 h1:xK2lYat7ZLaVVcIuj82J8kIro4V6kDe0AUDFboUCwcg= -github.com/antihax/optional v1.0.0/go.mod h1:uupD/76wgC+ih3iEmQUL+0Ugr19nfwCT1kdvxnR2qWY= github.com/apparentlymart/go-textseg/v12 v12.0.0/go.mod h1:S/4uRK2UtaQttw1GenVJEynmyUenKwP++x/+DdGV/Ec= github.com/apparentlymart/go-textseg/v15 v15.0.0 h1:uYvfpb3DyLSCGWnctWKGj857c6ew1u1fNQOlOtuGxQY= github.com/apparentlymart/go-textseg/v15 v15.0.0/go.mod h1:K8XmNZdhEBkdlyDdvbmmsvpAG721bKi0joRfFdHIWJ4= @@ -254,8 +252,6 @@ github.com/orange-cloudavenue/cloudavenue-sdk-go v0.11.1 h1:4RQp622DNMYwN3LsMqaT github.com/orange-cloudavenue/cloudavenue-sdk-go v0.11.1/go.mod h1:NwihPR1NUxAxUD98jNqh7EdyMihv/DRG1SPWLovgIQw= github.com/orange-cloudavenue/common-go/utils v0.0.0-20240119163616-66b473d92339 h1:DEKcWLGbEhu/I6kn9NAXhVCFrbPhR+Ef7oLmpLVnnPM= github.com/orange-cloudavenue/common-go/utils v0.0.0-20240119163616-66b473d92339/go.mod h1:11JAFfGWVmhoT4AAORKsIC5M6nI+uDGSEOScMzavgPA= -github.com/orange-cloudavenue/infrapi-sdk-go v0.1.4-0.20231005074857-89878ea119fb h1:1/Wc21Tp9RnDOUTjKBm9x3wi+UgUkDc2bv0fHJc5f2o= -github.com/orange-cloudavenue/infrapi-sdk-go v0.1.4-0.20231005074857-89878ea119fb/go.mod h1:pGa9mB6s+weCi5QtNe5nicp7yL0C/e+i+3wHRh4cjBE= github.com/peterhellberg/link v1.2.0 h1:UA5pg3Gp/E0F2WdX7GERiNrPQrM1K6CVJUUWfHa4t6c= github.com/peterhellberg/link v1.2.0/go.mod h1:gYfAh+oJgQu2SrZHg5hROVRQe1ICoK0/HHJTcE0edxc= github.com/pjbgf/sha1cd v0.3.0 h1:4D5XXmUUBUl/xQ6IjCkEAbqXskkq/4O7LmGn0AqMDs4= @@ -362,8 +358,6 @@ golang.org/x/net v0.25.0/go.mod h1:JkAGAh7GEvH74S6FOH42FLoXpXbE/aqXSrIQjXgsiwM= golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE= golang.org/x/net v0.28.0 h1:a9JDOJc5GMUJ0+UDqmLT86WiEy7iWyIhz8gz8E4e5hE= golang.org/x/net v0.28.0/go.mod h1:yqtgsTWOOnlGLG9GFRrK3++bGOUEkNBoHZc8MEDWPNg= -golang.org/x/oauth2 v0.17.0 h1:6m3ZPmLEFdVxKKWnKq4VqZ60gutO35zm+zrAHVmHyDQ= -golang.org/x/oauth2 v0.17.0/go.mod h1:OzPDGQiuQMguemayvdylqddI7qcD9lnSDb+1FiwQ5HA= golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= golang.org/x/sync v0.0.0-20220722155255-886fb9371eb4/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= diff --git a/internal/client/client_test.go b/internal/client/client_test.go index ce8701e5..277e4a20 100644 --- a/internal/client/client_test.go +++ b/internal/client/client_test.go @@ -6,8 +6,6 @@ import ( "os" "testing" - "github.com/kr/pretty" - clientca "github.com/orange-cloudavenue/cloudavenue-sdk-go" clientcloudavenue "github.com/orange-cloudavenue/cloudavenue-sdk-go/pkg/clients/cloudavenue" caverror "github.com/orange-cloudavenue/cloudavenue-sdk-go/pkg/errors" @@ -106,7 +104,6 @@ func TestCloudAvenueClient(t *testing.T) { _, err := c.New() if tt.wantErr { if err == nil { - t.Log(pretty.Sprint(err)) t.Errorf("expected error: %v", tt.expectedErr) return } diff --git a/internal/helpers/api_errors.go b/internal/helpers/api_errors.go deleted file mode 100644 index 7a1a3768..00000000 --- a/internal/helpers/api_errors.go +++ /dev/null @@ -1,97 +0,0 @@ -// Package helpers provides api errors helpers for the CloudAvenue Terraform Provider. -package helpers - -import ( - "errors" - "fmt" - "net/http" - - "github.com/hashicorp/terraform-plugin-framework/diag" - - cloudavenue "github.com/orange-cloudavenue/infrapi-sdk-go" -) - -// APIError is an error returned by the CloudAvenue API. -type APIError struct { - lastError error - statusCode int - cloudavenue.ApiError -} - -// Error returns the error message. -func (e *APIError) Error() string { - return e.lastError.Error() -} - -// Unwrap returns the underlying error. -func (e *APIError) Unwrap() error { - return e.lastError -} - -// GetSummary returns a summary of the error. -func (e *APIError) GetSummary() string { - return fmt.Sprintf("%s (HTTP Code => %d)", e.Reason, e.GetStatusCode()) -} - -// GetDetail returns a detailed description of the error. -func (e *APIError) GetDetail() string { - return e.Message -} - -// GetStatusCode returns the HTTP status code. -func (e *APIError) GetStatusCode() int { - return e.statusCode -} - -// GetTerraformDiagnostic returns a Terraform Diagnostic for the error. -func (e *APIError) GetTerraformDiagnostic() diag.Diagnostic { - var summary, detail string - if e.Reason != "" { - summary = e.GetSummary() - } else { - summary = fmt.Sprintf("HTTP response error: %d", e.GetStatusCode()) - } - if e.Message != "" { - detail = e.GetDetail() - } else { - detail = e.lastError.Error() - } - - if e.IsNotFound() { - return diag.NewWarningDiagnostic(summary, detail) - } - return diag.NewErrorDiagnostic(summary, detail) -} - -// IsNotFound returns true if the error is a 404 Not Found error. -func (e *APIError) IsNotFound() bool { - return e.GetStatusCode() == http.StatusNotFound -} - -// CheckAPIError checks the HTTP response for errors and returns an APIError with -// statusCode and model is the error is a cloudavenue.GenericSwaggerError type -// or only the error if not. -func CheckAPIError(err error, httpR *http.Response) *APIError { - if err == nil { - return nil - } - - if httpR != nil && httpR.StatusCode >= http.StatusBadRequest { - var apiErr cloudavenue.GenericSwaggerError - if errors.As(err, &apiErr) { - x := &APIError{ - lastError: err, - statusCode: httpR.StatusCode, - } - - if apiErr.Model() != nil { - if m, ok := apiErr.Model().(cloudavenue.ApiError); ok { - x.ApiError = m - } - } - - return x - } - } - return &APIError{lastError: err} -} diff --git a/internal/helpers/auth.go b/internal/helpers/auth.go deleted file mode 100644 index 8e9aafe1..00000000 --- a/internal/helpers/auth.go +++ /dev/null @@ -1,19 +0,0 @@ -// Package helpers provides auth helpers for the CloudAvenue Terraform Provider. -package helpers - -import ( - "context" - "errors" - - apiclient "github.com/orange-cloudavenue/infrapi-sdk-go" -) - -// GetAuthContextWithTO is a helper function to create the auth context with the token api and the terraform context with timeout. -func GetAuthContextWithTO(apiCtx, tfCtx context.Context) (context.Context, error) { - token, tokenIsAString := apiCtx.Value(apiclient.ContextAccessToken).(string) - if !tokenIsAString { - return nil, errors.New("token is not a string") - } - auth := context.WithValue(tfCtx, apiclient.ContextAccessToken, token) - return auth, nil -} diff --git a/internal/helpers/auth_test.go b/internal/helpers/auth_test.go deleted file mode 100644 index 2f2cf384..00000000 --- a/internal/helpers/auth_test.go +++ /dev/null @@ -1,26 +0,0 @@ -// Package helpers provides auth helpers for the CloudAvenue Terraform Provider. -package helpers - -import ( - "context" - "testing" - "time" - - apiclient "github.com/orange-cloudavenue/infrapi-sdk-go" -) - -func TestGetAuthContextWithTO(t *testing.T) { - name := "testGetAuthContextWithTO" - - apiCtx := context.WithValue(context.Background(), apiclient.ContextAccessToken, "token") - ctx := context.Background() - - tfCtx, cancel := context.WithTimeout(ctx, 10*time.Second) - defer cancel() - - newCtx, _ := GetAuthContextWithTO(apiCtx, tfCtx) - newToken, _ := newCtx.Value(apiclient.ContextAccessToken).(string) - if want := "token"; newToken != want { - t.Errorf("%s: got %s, want %s", name, newToken, want) - } -} diff --git a/internal/helpers/config.go b/internal/helpers/config.go deleted file mode 100644 index 30e3fe09..00000000 --- a/internal/helpers/config.go +++ /dev/null @@ -1,7 +0,0 @@ -// Package helpers provides configuration helpers for Terraform Provider. -package helpers - -const ( - // ForceNewDescription is the description for ForceNew. - ForceNewDescription = "Changes to this field will force a new resource to be created." -)