package provider import ( "context" "fmt" "time" "github.com/hashicorp/terraform-plugin-framework/diag" "github.com/hashicorp/terraform-plugin-framework/types" ) // Conversions between Terraform's values and the client's. // strOrNull is null for empty: the API leaves an unset field out, and a // configuration that does not set it holds null, not "". func strOrNull(s string) types.String { if s == "" { return types.StringNull() } return types.StringValue(s) } func intOrNull(n int64) types.Int64 { if n == 0 { return types.Int64Null() } return types.Int64Value(n) } func floatOrNull(f float64) types.Float64 { if f == 0 { return types.Float64Null() } return types.Float64Value(f) } func ptrOrNull(p *int64) types.Int64 { if p == nil { return types.Int64Null() } return types.Int64Value(*p) } func ptrOf(v types.Int64) *int64 { if v.IsNull() || v.IsUnknown() { return nil } n := v.ValueInt64() return &n } func listOf(ctx context.Context, l types.List, diags *diag.Diagnostics) []string { if l.IsNull() || l.IsUnknown() { return nil } var out []string diags.Append(l.ElementsAs(ctx, &out, false)...) return out } func mapOf(ctx context.Context, m types.Map, diags *diag.Diagnostics) map[string]string { if m.IsNull() || m.IsUnknown() { return nil } out := map[string]string{} diags.Append(m.ElementsAs(ctx, &out, false)...) return out } // duration reads a duration written as Go writes them, "30m" or "1h30m". func duration(v types.String, what string, diags *diag.Diagnostics) time.Duration { if v.IsNull() || v.IsUnknown() || v.ValueString() == "" { return 0 } d, err := time.ParseDuration(v.ValueString()) if err != nil || d < 0 { diags.AddError("Not a duration", fmt.Sprintf("%s is %q: write a duration like 30m or 1h30m", what, v.ValueString())) } return d } // keepDuration is the API's duration as the configuration wrote it when they // mean the same: "30m" and "30m0s" are one value, and showing a difference // between them on every plan would be noise. func keepDuration(had types.String, d time.Duration) types.String { if !had.IsNull() && !had.IsUnknown() { if was, err := time.ParseDuration(had.ValueString()); err == nil && was == d { return had } } return types.StringValue(d.String()) }