Skip to content

Commit

Permalink
Add type checking of configuration override options during state refresh
Browse files Browse the repository at this point in the history
  • Loading branch information
janartodesk committed Nov 28, 2024
1 parent 45e8baf commit 5d4c008
Showing 1 changed file with 40 additions and 5 deletions.
45 changes: 40 additions & 5 deletions internal/resource/host/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/samber/lo"
"github.com/sendsmaily/terraform-provider-definednet/internal/definednet"
Expand Down Expand Up @@ -67,19 +68,44 @@ func (s *State) ApplyHost(ctx context.Context, host *definednet.Host) (diags dia
m.Enabled = types.BoolValue(true)

case "stats.listen":
m.Listen = types.StringValue(o.Value.(string))
v, err := convert[string](o.Value)
if err != nil {
diags.AddAttributeError(path.Root("metrics").AtMapKey("listen"), "Invalid Value", err.Error())
}

m.Listen = types.StringValue(v)

case "stats.path":
m.Path = types.StringValue(o.Value.(string))
v, err := convert[string](o.Value)
if err != nil {
diags.AddAttributeError(path.Root("metrics").AtMapKey("path"), "Invalid Value", err.Error())
}

m.Path = types.StringValue(v)

case "stats.namespace":
m.Namespace = types.StringValue(o.Value.(string))
v, err := convert[string](o.Value)
if err != nil {
diags.AddAttributeError(path.Root("metrics").AtMapKey("namespace"), "Invalid Value", err.Error())
}

m.Namespace = types.StringValue(v)

case "stats.subsystem":
m.Subsystem = types.StringValue(o.Value.(string))
v, err := convert[string](o.Value)
if err != nil {
diags.AddAttributeError(path.Root("metrics").AtMapKey("subsystem"), "Invalid Value", err.Error())
}

m.Subsystem = types.StringValue(v)

case "stats.message_metrics":
m.EnableExtraMetrics = types.BoolValue(o.Value.(bool))
v, err := convert[bool](o.Value)
if err != nil {
diags.AddAttributeError(path.Root("metrics").AtMapKey("enable_extra_metrics"), "Invalid Value", err.Error())
}

m.EnableExtraMetrics = types.BoolValue(v)
}

return m
Expand All @@ -91,3 +117,12 @@ func (s *State) ApplyHost(ctx context.Context, host *definednet.Host) (diags dia

return diags
}

func convert[T any](val any) (T, error) {
if val, ok := val.(T); ok {
return val, nil
}

var t T
return t, fmt.Errorf("unexpected type: wanted %T, got %T", t, val)
}

0 comments on commit 5d4c008

Please sign in to comment.