Files
llmgw/terraform-provider-llmgw/internal/provider/values.go
T
Ilker ManapandClaude Opus 5.5 11b5d44702 A Terraform provider for llmgw: models, quotas and guard attachments through the admin API
terraform-provider-llmgw/ is a module of its own, built on
terraform-plugin-framework, so the gateway does not carry its dependencies.
llmgw_model is a model added at runtime: scaling, class, owner and adapters
change in place, anything else replaces it; the definition is kept as
written rather than as the catalog filled it in. llmgw_quota and
llmgw_guard_attachment keep the scope as written beside the ID it resolved
to, and import by that ID. Data sources llmgw_models and llmgw_nodes.

The client lists every endpoint it calls and refuses any other; a test
checks each, and every field it sends and reads, against
docs/admin-api.openapi.json, so a change to the admin API that breaks the
provider fails here. Acceptance tests run Terraform against an admin API in
memory when TF_ACC is set. CI vets and tests the module.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
2026-10-05 00:04:35 +02:00

93 lines
2.2 KiB
Go

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())
}