From 8c1beaefbaf0d3870fce35c31b3d3a9c33edbda3 Mon Sep 17 00:00:00 2001 From: Vitaly Antonenko Date: Thu, 1 Aug 2024 18:47:50 +0300 Subject: [PATCH 1/2] fix(storagedirective): fix storage directive validator - Add validation logic to check for unknown or null values in the map elements. - Parse and validate storage directives using `juju/storage` package (use `jujustorage` name instead `storage`). --- internal/provider/validator_storagedirective.go | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/internal/provider/validator_storagedirective.go b/internal/provider/validator_storagedirective.go index bada4811..ab75d60c 100644 --- a/internal/provider/validator_storagedirective.go +++ b/internal/provider/validator_storagedirective.go @@ -8,7 +8,7 @@ import ( "fmt" "github.com/hashicorp/terraform-plugin-framework/schema/validator" - "github.com/juju/juju/storage" + jujustorage "github.com/juju/juju/storage" ) type stringIsStorageDirectiveValidator struct{} @@ -30,6 +30,13 @@ func (v stringIsStorageDirectiveValidator) ValidateMap(ctx context.Context, req return } + // If the value of any element is unknown or null, there is nothing to validate. + for _, element := range req.ConfigValue.Elements() { + if element.IsUnknown() || element.IsNull() { + return + } + } + var storageDirectives map[string]string resp.Diagnostics.Append(req.ConfigValue.ElementsAs(ctx, &storageDirectives, false)...) if resp.Diagnostics.HasError() { @@ -37,7 +44,7 @@ func (v stringIsStorageDirectiveValidator) ValidateMap(ctx context.Context, req } for label, directive := range storageDirectives { - _, err := storage.ParseConstraints(directive) + _, err := jujustorage.ParseConstraints(directive) if err != nil { resp.Diagnostics.AddAttributeError( req.Path, From 0c493deccd8289199543cb85c59840f0c40a8077 Mon Sep 17 00:00:00 2001 From: Vitaly Antonenko Date: Tue, 6 Aug 2024 18:58:14 +0300 Subject: [PATCH 2/2] chore: updated comment for validator --- internal/provider/validator_storagedirective.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/internal/provider/validator_storagedirective.go b/internal/provider/validator_storagedirective.go index ab75d60c..33f250d6 100644 --- a/internal/provider/validator_storagedirective.go +++ b/internal/provider/validator_storagedirective.go @@ -31,6 +31,9 @@ func (v stringIsStorageDirectiveValidator) ValidateMap(ctx context.Context, req } // If the value of any element is unknown or null, there is nothing to validate. + // Note tha the method is called twice by terraform. The first time it is called with + // unknown values, and the second time with known values. + // If the behavior changes, there is no need to validate all the values. for _, element := range req.ConfigValue.Elements() { if element.IsUnknown() || element.IsNull() { return