Logging and error handling improvements, bug fixes (#21)
- Uniformed logs and diagnostics: - Logging and adding to diagnostics is done by the highest level function (Create/Read/Update/Delete/Import) using `LogAndAddError` - Lower-level routines' signature changed to return error instead of writing to diagnostics - Standardize summary and details across services - Removed manual adding of relevant variables to details (they're in the context, TF adds them to logs) - Changed validators to be closer to official implementation - Fix logging wrong output after wait - Fix Argus checking wrong diagnostics - Fix Resource Manager not updating state after project update - Fix unnecessary pointer in LogAndAddError
This commit is contained in:
parent
29b8c91999
commit
4e8514df00
51 changed files with 1389 additions and 1092 deletions
|
|
@ -8,6 +8,7 @@ import (
|
|||
"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/core"
|
||||
)
|
||||
|
|
@ -38,47 +39,71 @@ func (v *Validator) ValidateString(ctx context.Context, req validator.StringRequ
|
|||
}
|
||||
|
||||
func UUID() *Validator {
|
||||
description := "value must be an UUID"
|
||||
|
||||
return &Validator{
|
||||
description: "validate string is UUID",
|
||||
description: description,
|
||||
validate: func(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
|
||||
if _, err := uuid.Parse(req.ConfigValue.ValueString()); err != nil {
|
||||
resp.Diagnostics.AddError("not a valid UUID", err.Error())
|
||||
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
|
||||
req.Path,
|
||||
description,
|
||||
req.ConfigValue.ValueString(),
|
||||
))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func IP() *Validator {
|
||||
description := "value must be an IP address"
|
||||
|
||||
return &Validator{
|
||||
description: "validate string is IP address",
|
||||
description: description,
|
||||
validate: func(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
|
||||
if net.ParseIP(req.ConfigValue.ValueString()) == nil {
|
||||
resp.Diagnostics.AddError("not a valid IP address", "")
|
||||
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: "validate string does not contain internal separator",
|
||||
description: description,
|
||||
validate: func(ctx context.Context, req validator.StringRequest, resp *validator.StringResponse) {
|
||||
if strings.Contains(req.ConfigValue.ValueString(), core.Separator) {
|
||||
resp.Diagnostics.AddError("Invalid character found.", fmt.Sprintf("The string should not contain a '%s'", core.Separator))
|
||||
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
|
||||
req.Path,
|
||||
description,
|
||||
req.ConfigValue.ValueString(),
|
||||
))
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
func SemanticMinorVersion() *Validator {
|
||||
func MinorVersionNumber() *Validator {
|
||||
description := "value must be a minor version number, without a leading 'v': '[MAJOR].[MINOR]'"
|
||||
|
||||
return &Validator{
|
||||
description: "validate string does not contain internal separator",
|
||||
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.AddError("Invalid version.", "The version should be a valid semantic version only containing major and minor version. The version should not contain a leading `v`. Got "+version)
|
||||
resp.Diagnostics.Append(validatordiag.InvalidAttributeValueDiagnostic(
|
||||
req.Path,
|
||||
description,
|
||||
req.ConfigValue.ValueString(),
|
||||
))
|
||||
}
|
||||
},
|
||||
}
|
||||
|
|
|
|||
|
|
@ -145,7 +145,7 @@ func TestNoSeparator(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
func TestSemanticMinorVersion(t *testing.T) {
|
||||
func TestMinorVersionNumber(t *testing.T) {
|
||||
tests := []struct {
|
||||
description string
|
||||
input string
|
||||
|
|
@ -195,7 +195,7 @@ func TestSemanticMinorVersion(t *testing.T) {
|
|||
for _, tt := range tests {
|
||||
t.Run(tt.description, func(t *testing.T) {
|
||||
r := validator.StringResponse{}
|
||||
SemanticMinorVersion().ValidateString(context.Background(), validator.StringRequest{
|
||||
MinorVersionNumber().ValidateString(context.Background(), validator.StringRequest{
|
||||
ConfigValue: types.StringValue(tt.input),
|
||||
}, &r)
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue