Move internal packages into internal folder (#33)
* Move internal packages into internal folder * Fix testutil imports
This commit is contained in:
parent
46be7cfafd
commit
f8c9e4c0af
83 changed files with 148 additions and 148 deletions
110
stackit/internal/validate/validate.go
Normal file
110
stackit/internal/validate/validate.go
Normal file
|
|
@ -0,0 +1,110 @@
|
|||
package validate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"net"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/hashicorp/terraform-plugin-framework-validators/helpers/validatordiag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
"github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core"
|
||||
)
|
||||
|
||||
type Validator struct {
|
||||
description string
|
||||
markdownDescription string
|
||||
validate ValidationFn
|
||||
}
|
||||
|
||||
type ValidationFn func(context.Context, validator.StringRequest, *validator.StringResponse)
|
||||
|
||||
var _ = validator.String(&Validator{})
|
||||
|
||||
func (v *Validator) Description(_ context.Context) string {
|
||||
return v.description
|
||||
}
|
||||
|
||||
func (v *Validator) MarkdownDescription(_ context.Context) string {
|
||||
return v.markdownDescription
|
||||
}
|
||||
|
||||
func (v *Validator) ValidateString(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) { // nolint:gocritic // function signature required by Terraform
|
||||
if req.ConfigValue.IsUnknown() || req.ConfigValue.IsNull() {
|
||||
return
|
||||
}
|
||||
v.validate(ctx, req, resp)
|
||||
}
|
||||
|
||||
func UUID() *Validator {
|
||||
description := "value must be an UUID"
|
||||
|
||||
return &Validator{
|
||||
description: description,
|
||||
validate: func(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
|
||||
if _, err := uuid.Parse(req.ConfigValue.ValueString()); err != nil {
|
||||
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
|
||||
req.Path,
|
||||
description,
|
||||
req.ConfigValue.ValueString(),
|
||||
))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func IP() *Validator {
|
||||
description := "value must be an IP address"
|
||||
|
||||
return &Validator{
|
||||
description: description,
|
||||
validate: func(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
|
||||
if net.ParseIP(req.ConfigValue.ValueString()) == nil {
|
||||
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
|
||||
req.Path,
|
||||
description,
|
||||
req.ConfigValue.ValueString(),
|
||||
))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func NoSeparator() *Validator {
|
||||
description := fmt.Sprintf("value must not contain identifier separator '%s'", core.Separator)
|
||||
|
||||
return &Validator{
|
||||
description: description,
|
||||
validate: func(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
|
||||
if strings.Contains(req.ConfigValue.ValueString(), core.Separator) {
|
||||
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
|
||||
req.Path,
|
||||
description,
|
||||
req.ConfigValue.ValueString(),
|
||||
))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func MinorVersionNumber() *Validator {
|
||||
description := "value must be a minor version number, without a leading 'v': '[MAJOR].[MINOR]'"
|
||||
|
||||
return &Validator{
|
||||
description: description,
|
||||
validate: func(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
|
||||
exp := `^\d+\.\d+?$`
|
||||
r := regexp.MustCompile(exp)
|
||||
version := req.ConfigValue.ValueString()
|
||||
if !r.MatchString(version) {
|
||||
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
|
||||
req.Path,
|
||||
description,
|
||||
req.ConfigValue.ValueString(),
|
||||
))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
210
stackit/internal/validate/validate_test.go
Normal file
210
stackit/internal/validate/validate_test.go
Normal file
|
|
@ -0,0 +1,210 @@
|
|||
package validate
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
func TestUUID(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input string
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"ok",
|
||||
"cae27bba-c43d-498a-861e-d11d241c4ff8",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"too short",
|
||||
"a-b-c-d",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"Empty",
|
||||
"",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"not UUID",
|
||||
"www-541-%",
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
r := validator.StringResponse{}
|
||||
UUID().ValidateString(context.Background(), validator.StringRequest{
|
||||
ConfigValue: types.StringValue(tt.input),
|
||||
}, &r)
|
||||
|
||||
if !tt.isValid && !r.Diagnostics.HasError() {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && r.Diagnostics.HasError() {
|
||||
t.Fatalf("Should not have failed: %v", r.Diagnostics.Errors())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestIP(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input string
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"ok IP4",
|
||||
"111.222.111.222",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ok IP6",
|
||||
"2001:0db8:85a3:08d3::0370:7344",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"too short",
|
||||
"0.1.2",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"Empty",
|
||||
"",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"Not an IP",
|
||||
"for-sure-not-an-IP",
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
r := validator.StringResponse{}
|
||||
IP().ValidateString(context.Background(), validator.StringRequest{
|
||||
ConfigValue: types.StringValue(tt.input),
|
||||
}, &r)
|
||||
|
||||
if !tt.isValid && !r.Diagnostics.HasError() {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && r.Diagnostics.HasError() {
|
||||
t.Fatalf("Should not have failed: %v", r.Diagnostics.Errors())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestNoSeparator(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input string
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"ok",
|
||||
"ABCD",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ok-2",
|
||||
"#$%&/()=.;-",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"Empty",
|
||||
"",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"not ok",
|
||||
"ab,",
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
r := validator.StringResponse{}
|
||||
NoSeparator().ValidateString(context.Background(), validator.StringRequest{
|
||||
ConfigValue: types.StringValue(tt.input),
|
||||
}, &r)
|
||||
|
||||
if !tt.isValid && !r.Diagnostics.HasError() {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && r.Diagnostics.HasError() {
|
||||
t.Fatalf("Should not have failed: %v", r.Diagnostics.Errors())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestMinorVersionNumber(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input string
|
||||
isValid bool
|
||||
}{
|
||||
{
|
||||
"ok",
|
||||
"1.20",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ok-2",
|
||||
"1.3",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"ok-3",
|
||||
"10.1",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"Empty",
|
||||
"",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"not ok",
|
||||
"afssfdfs",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"not ok-major-version",
|
||||
"1",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"not ok-patch-version",
|
||||
"1.20.1",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"not ok-version",
|
||||
"v1.20.1",
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
r := validator.StringResponse{}
|
||||
MinorVersionNumber().ValidateString(context.Background(), validator.StringRequest{
|
||||
ConfigValue: types.StringValue(tt.input),
|
||||
}, &r)
|
||||
|
||||
if !tt.isValid && !r.Diagnostics.HasError() {
|
||||
t.Fatalf("Should have failed")
|
||||
}
|
||||
if tt.isValid && r.Diagnostics.HasError() {
|
||||
t.Fatalf("Should not have failed: %v", r.Diagnostics.Errors())
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue