Skip to content

Commit

Permalink
Merge pull request #538 from anvial/fix-storagedirective-validator
Browse files Browse the repository at this point in the history
fix(storagedirective): fix storage directive validator
  • Loading branch information
anvial authored Aug 7, 2024
2 parents fa7cc97 + 0c493de commit 4bcd17c
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions internal/provider/validator_storagedirective.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand All @@ -30,14 +30,24 @@ func (v stringIsStorageDirectiveValidator) ValidateMap(ctx context.Context, req
return
}

// 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
}
}

var storageDirectives map[string]string
resp.Diagnostics.Append(req.ConfigValue.ElementsAs(ctx, &storageDirectives, false)...)
if resp.Diagnostics.HasError() {
return
}

for label, directive := range storageDirectives {
_, err := storage.ParseConstraints(directive)
_, err := jujustorage.ParseConstraints(directive)
if err != nil {
resp.Diagnostics.AddAttributeError(
req.Path,
Expand Down

0 comments on commit 4bcd17c

Please sign in to comment.