-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into feat/add-validation-domain-ressource
- Loading branch information
Showing
238 changed files
with
1,010 additions
and
703 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package httperrors | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
|
||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
) | ||
|
||
// IsHTTPCodeError returns true if err is an http error with code statusCode | ||
func IsHTTPCodeError(err error, statusCode int) bool { | ||
if err == nil { | ||
return false | ||
} | ||
|
||
responseError := &scw.ResponseError{} | ||
if errors.As(err, &responseError) && responseError.StatusCode == statusCode { | ||
return true | ||
} | ||
return false | ||
} | ||
|
||
// Is404 returns true if err is an HTTP 404 error | ||
func Is404(err error) bool { | ||
notFoundError := &scw.ResourceNotFoundError{} | ||
return IsHTTPCodeError(err, http.StatusNotFound) || errors.As(err, ¬FoundError) | ||
} | ||
|
||
func Is412(err error) bool { | ||
preConditionFailedError := &scw.PreconditionFailedError{} | ||
return IsHTTPCodeError(err, http.StatusPreconditionFailed) || errors.As(err, &preConditionFailedError) | ||
} | ||
|
||
// Is403 returns true if err is an HTTP 403 error | ||
func Is403(err error) bool { | ||
permissionsDeniedError := &scw.PermissionsDeniedError{} | ||
return IsHTTPCodeError(err, http.StatusForbidden) || errors.As(err, &permissionsDeniedError) | ||
} | ||
|
||
// Is409 return true is err is an HTTP 409 error | ||
func Is409(err error) bool { | ||
// check transient error | ||
transientStateError := &scw.TransientStateError{} | ||
return IsHTTPCodeError(err, http.StatusConflict) || errors.As(err, &transientStateError) | ||
} | ||
|
||
// Is410 returns true if err is an HTTP 410 error | ||
func Is410(err error) bool { | ||
return IsHTTPCodeError(err, http.StatusGone) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package httperrors_test | ||
|
||
import ( | ||
"errors" | ||
"net/http" | ||
"testing" | ||
|
||
"github.com/scaleway/scaleway-sdk-go/scw" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestIsHTTPCodeError(t *testing.T) { | ||
assert.True(t, httperrors.IsHTTPCodeError(&scw.ResponseError{StatusCode: http.StatusBadRequest}, http.StatusBadRequest)) | ||
assert.False(t, httperrors.IsHTTPCodeError(nil, http.StatusBadRequest)) | ||
assert.False(t, httperrors.IsHTTPCodeError(&scw.ResponseError{StatusCode: http.StatusBadRequest}, http.StatusNotFound)) | ||
assert.False(t, httperrors.IsHTTPCodeError(errors.New("not an http error"), http.StatusNotFound)) | ||
} | ||
|
||
func TestIs404Error(t *testing.T) { | ||
assert.True(t, httperrors.Is404(&scw.ResponseError{StatusCode: http.StatusNotFound})) | ||
assert.False(t, httperrors.Is404(nil)) | ||
assert.False(t, httperrors.Is404(&scw.ResponseError{StatusCode: http.StatusBadRequest})) | ||
} | ||
|
||
func TestIs403Error(t *testing.T) { | ||
assert.True(t, httperrors.Is403(&scw.ResponseError{StatusCode: http.StatusForbidden})) | ||
assert.False(t, httperrors.Is403(nil)) | ||
assert.False(t, httperrors.Is403(&scw.ResponseError{StatusCode: http.StatusBadRequest})) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package verify | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/scaleway/scaleway-sdk-go/validation" | ||
) | ||
|
||
func IsEmail() schema.SchemaValidateFunc { | ||
return func(v interface{}, key string) (warnings []string, errors []error) { | ||
email, isString := v.(string) | ||
if !isString { | ||
return nil, []error{fmt.Errorf("invalid email for key '%s': not a string", key)} | ||
} | ||
|
||
if !validation.IsEmail(email) { | ||
return nil, []error{fmt.Errorf("invalid email for key '%s': '%s': should contain valid '@' character", key, email)} | ||
} | ||
|
||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package verify | ||
|
||
import ( | ||
"fmt" | ||
"net" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
func IsStandaloneIPorCIDR() schema.SchemaValidateFunc { | ||
return func(val interface{}, key string) (warns []string, errs []error) { | ||
ip, isString := val.(string) | ||
if !isString { | ||
return nil, []error{fmt.Errorf("invalid input for key '%s': not a string", key)} | ||
} | ||
|
||
// Check if it's a standalone IP address | ||
if net.ParseIP(ip) != nil { | ||
return | ||
} | ||
|
||
// Check if it's an IP with CIDR notation | ||
_, _, err := net.ParseCIDR(ip) | ||
if err != nil { | ||
errs = append(errs, fmt.Errorf("%q is not a valid IP address or CIDR notation: %s", key, ip)) | ||
} | ||
|
||
return | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package verify_test | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
func TestValidateStandaloneIPorCIDRWithValidIPReturnNothing(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
for _, ip := range []string{"192.168.1.1", "2001:0db8:85a3:0000:0000:8a2e:0370:7334", "10.0.0.0/24", "2001:0db8:85a3::8a2e:0370:7334/64"} { | ||
warnings, errors := verify.IsStandaloneIPorCIDR()(ip, "key") | ||
assert.Empty(warnings) | ||
assert.Empty(errors) | ||
} | ||
} | ||
|
||
func TestValidateStandaloneIPorCIDRWithInvalidIPReturnError(t *testing.T) { | ||
assert := assert.New(t) | ||
|
||
for _, ip := range []string{"10.0.0", "256.256.256.256", "2001::85a3::8a2e:0370:7334", "10.0.0.0/34"} { | ||
warnings, errors := verify.IsStandaloneIPorCIDR()(ip, "key") | ||
assert.Empty(warnings) | ||
assert.Len(errors, 1) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
package verify | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/scaleway/scaleway-sdk-go/validation" | ||
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality" | ||
) | ||
|
||
// IsUUIDorUUIDWithLocality validates the schema is a UUID or the combination of a locality and a UUID | ||
// e.g. "6ba7b810-9dad-11d1-80b4-00c04fd430c8" or "fr-par-1/6ba7b810-9dad-11d1-80b4-00c04fd430c8". | ||
func IsUUIDorUUIDWithLocality() schema.SchemaValidateFunc { | ||
return func(v interface{}, key string) ([]string, []error) { | ||
return IsUUID()(locality.ExpandID(v), key) | ||
} | ||
} | ||
|
||
// IsUUID validates the schema following the canonical UUID format | ||
// "6ba7b810-9dad-11d1-80b4-00c04fd430c8". | ||
func IsUUID() schema.SchemaValidateFunc { | ||
return func(v interface{}, key string) (warnings []string, errors []error) { | ||
uuid, isString := v.(string) | ||
if !isString { | ||
return nil, []error{fmt.Errorf("invalid UUID for key '%s': not a string", key)} | ||
} | ||
|
||
if !validation.IsUUID(uuid) { | ||
return nil, []error{fmt.Errorf("invalid UUID for key '%s': '%s' (%d): format should be 'xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx' (36) and contains valid hexadecimal characters", key, uuid, len(uuid))} | ||
} | ||
|
||
return | ||
} | ||
} | ||
|
||
func IsUUIDWithLocality() schema.SchemaValidateFunc { | ||
return func(v interface{}, key string) (warnings []string, errors []error) { | ||
uuid, isString := v.(string) | ||
if !isString { | ||
errors = []error{fmt.Errorf("invalid UUID for key '%s': not a string", key)} | ||
return | ||
} | ||
_, subUUID, err := locality.ParseLocalizedID(uuid) | ||
if err != nil { | ||
errors = []error{fmt.Errorf("invalid UUID with locality for key '%s': '%s' (%d): format should be 'locality/uuid'", key, uuid, len(uuid))} | ||
return | ||
} | ||
return IsUUID()(subUUID, key) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.