Skip to content

Commit

Permalink
fix(storagedirective): fix storage directive validator
Browse files Browse the repository at this point in the history
- 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`).
  • Loading branch information
anvial committed Aug 1, 2024
1 parent 84f794b commit 8c1beae
Showing 1 changed file with 9 additions and 2 deletions.
11 changes: 9 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,21 @@ 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() {
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 8c1beae

Please sign in to comment.