Files
llmgw/terraform-provider-llmgw/internal/provider/provider.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

120 lines
4.2 KiB
Go

// Package provider is the llmgw Terraform provider: models, quotas and
// guardrail attachments managed through a control plane's admin API.
package provider
import (
"context"
"os"
"strings"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/provider/schema"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types"
"gitea.mynodes.xyz/ilker/llmgw/terraform-provider-llmgw/internal/client"
)
// DefaultEndpoint is the admin listener's default address.
const DefaultEndpoint = "http://127.0.0.1:9090"
// DefaultTokenFile is where the server writes the admin token under systemd.
const DefaultTokenFile = "/var/lib/llmgw/admin-token"
type llmgwProvider struct{ version string }
// New returns the provider, for providerserver.
func New(version string) func() provider.Provider {
return func() provider.Provider { return &llmgwProvider{version: version} }
}
type providerConfig struct {
Endpoint types.String `tfsdk:"endpoint"`
Token types.String `tfsdk:"token"`
TokenFile types.String `tfsdk:"token_file"`
Actor types.String `tfsdk:"actor"`
}
func (p *llmgwProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "llmgw"
resp.Version = p.version
}
func (p *llmgwProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manages an llmgw cluster through its control plane's admin API.",
Attributes: map[string]schema.Attribute{
"endpoint": schema.StringAttribute{
Optional: true,
Description: "The admin listener, " + DefaultEndpoint + " by default, or LLMGW_ADMIN_URL. " +
"It answers to an address, not a name; reach a remote one through an SSH tunnel.",
},
"token": schema.StringAttribute{
Optional: true,
Sensitive: true,
Description: "The admin token, or LLMGW_ADMIN_TOKEN. When neither is set it is read from token_file.",
},
"token_file": schema.StringAttribute{
Optional: true,
Description: "A file holding the admin token; " + DefaultTokenFile + " by default.",
},
"actor": schema.StringAttribute{
Optional: true,
Description: "Who changes are recorded as made by in the audit log; terraform by default.",
},
},
}
}
func (p *llmgwProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var cfg providerConfig
resp.Diagnostics.Append(req.Config.Get(ctx, &cfg)...)
if resp.Diagnostics.HasError() {
return
}
pick := func(v types.String, env, def string) string {
if !v.IsNull() && !v.IsUnknown() && v.ValueString() != "" {
return v.ValueString()
}
if s := os.Getenv(env); env != "" && s != "" {
return s
}
return def
}
endpoint := pick(cfg.Endpoint, "LLMGW_ADMIN_URL", DefaultEndpoint)
token := pick(cfg.Token, "LLMGW_ADMIN_TOKEN", "")
if token == "" {
file := pick(cfg.TokenFile, "", DefaultTokenFile)
b, err := os.ReadFile(file) //nolint:gosec // the file the configuration names
if err != nil {
resp.Diagnostics.AddError("No admin token",
"Set token or LLMGW_ADMIN_TOKEN, or make "+file+" readable (it is root's): "+err.Error())
return
}
token = strings.TrimSpace(string(b))
}
c, err := client.New(endpoint, token, pick(cfg.Actor, "", "terraform"))
if err != nil {
resp.Diagnostics.AddError("Cannot reach the admin API", err.Error())
return
}
resp.ResourceData = c
resp.DataSourceData = c
}
func (p *llmgwProvider) Resources(context.Context) []func() resource.Resource {
return []func() resource.Resource{newModelResource, newQuotaResource, newGuardAttachmentResource}
}
func (p *llmgwProvider) DataSources(context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{newModelsDataSource, newNodesDataSource}
}
// clientFrom takes the client Configure built, which is absent while
// Terraform validates a configuration before the provider is configured.
func clientFrom(data any) (*client.Client, bool) {
c, ok := data.(*client.Client)
return c, ok && c != nil
}