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>
210 lines
8.8 KiB
Go
210 lines
8.8 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/hashicorp/terraform-plugin-framework/datasource"
|
|
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
|
|
"github.com/hashicorp/terraform-plugin-framework/types"
|
|
|
|
"gitea.mynodes.xyz/ilker/llmgw/terraform-provider-llmgw/internal/client"
|
|
)
|
|
|
|
// --- llmgw_models ---
|
|
|
|
type modelsDataSource struct{ c *client.Client }
|
|
|
|
func newModelsDataSource() datasource.DataSource { return &modelsDataSource{} }
|
|
|
|
type modelsData struct {
|
|
Models []modelSummary `tfsdk:"models"`
|
|
}
|
|
|
|
type modelSummary struct {
|
|
Name types.String `tfsdk:"name"`
|
|
Description types.String `tfsdk:"description"`
|
|
Source types.String `tfsdk:"source"`
|
|
Aliases []string `tfsdk:"aliases"`
|
|
Profiles []string `tfsdk:"profiles"`
|
|
Class types.String `tfsdk:"class"`
|
|
Owner types.String `tfsdk:"owner"`
|
|
External types.String `tfsdk:"external"`
|
|
MinReplicas types.Int64 `tfsdk:"min_replicas"`
|
|
MaxReplicas types.Int64 `tfsdk:"max_replicas"`
|
|
ReadyReplicas types.Int64 `tfsdk:"ready_replicas"`
|
|
TotalReplicas types.Int64 `tfsdk:"total_replicas"`
|
|
DesiredReplicas types.Int64 `tfsdk:"desired_replicas"`
|
|
Parked types.Bool `tfsdk:"parked"`
|
|
Reason types.String `tfsdk:"reason"`
|
|
}
|
|
|
|
func (d *modelsDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_models"
|
|
}
|
|
|
|
func (d *modelsDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
|
str := func(desc string) schema.StringAttribute {
|
|
return schema.StringAttribute{Computed: true, Description: desc}
|
|
}
|
|
num := func(desc string) schema.Int64Attribute {
|
|
return schema.Int64Attribute{Computed: true, Description: desc}
|
|
}
|
|
resp.Schema = schema.Schema{
|
|
Description: "Every model in the catalog, from configuration and added at runtime, with its replicas now.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"models": schema.ListNestedAttribute{Computed: true, Description: "The models, in the catalog's order.",
|
|
NestedObject: schema.NestedAttributeObject{Attributes: map[string]schema.Attribute{
|
|
"name": str("The name clients ask for."),
|
|
"description": str("What it is for."),
|
|
"source": str("Where its weights are."),
|
|
"aliases": schema.ListAttribute{Computed: true, ElementType: types.StringType, Description: "Other names it answers to."},
|
|
"profiles": schema.ListAttribute{Computed: true, ElementType: types.StringType, Description: "Its profiles' names."},
|
|
"class": str("Its capacity class."),
|
|
"owner": str("Its owner, scope:ID; empty is shared."),
|
|
"external": str("Where an external model is served: the provider's host and its name for it."),
|
|
"min_replicas": num("Replicas kept running."),
|
|
"max_replicas": num("The most replicas."),
|
|
"ready_replicas": num("Replicas serving now."),
|
|
"total_replicas": num("Replicas in any state."),
|
|
"desired_replicas": num("Replicas it should have now."),
|
|
"parked": schema.BoolAttribute{Computed: true, Description: "It failed to start too often and is no longer retried."},
|
|
"reason": str("The last error, when parked."),
|
|
}},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (d *modelsDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
|
|
if c, ok := clientFrom(req.ProviderData); ok {
|
|
d.c = c
|
|
}
|
|
}
|
|
|
|
func (d *modelsDataSource) Read(ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse) {
|
|
ms, err := d.c.Models(ctx)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Listing the models", err.Error())
|
|
return
|
|
}
|
|
out := modelsData{Models: []modelSummary{}}
|
|
for _, m := range ms {
|
|
out.Models = append(out.Models, modelSummary{
|
|
Name: types.StringValue(m.Name), Description: types.StringValue(m.Description), Source: types.StringValue(m.Source),
|
|
Aliases: nonNil(m.Aliases), Profiles: nonNil(m.Profiles), Class: types.StringValue(m.Class),
|
|
Owner: types.StringValue(m.Owner), External: types.StringValue(m.External),
|
|
MinReplicas: types.Int64Value(m.Min), MaxReplicas: types.Int64Value(m.Max), ReadyReplicas: types.Int64Value(m.Ready),
|
|
TotalReplicas: types.Int64Value(m.Total), DesiredReplicas: types.Int64Value(m.Desired),
|
|
Parked: types.BoolValue(m.Parked), Reason: types.StringValue(m.Reason),
|
|
})
|
|
}
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &out)...)
|
|
}
|
|
|
|
// nonNil is an empty list for none, which reads better in an expression than
|
|
// a null one: length() of it is 0 rather than an error.
|
|
func nonNil(s []string) []string {
|
|
if s == nil {
|
|
return []string{}
|
|
}
|
|
return s
|
|
}
|
|
|
|
// --- llmgw_nodes ---
|
|
|
|
type nodesDataSource struct{ c *client.Client }
|
|
|
|
func newNodesDataSource() datasource.DataSource { return &nodesDataSource{} }
|
|
|
|
type nodesData struct {
|
|
Nodes []nodeSummary `tfsdk:"nodes"`
|
|
}
|
|
|
|
type nodeSummary struct {
|
|
ID types.String `tfsdk:"id"`
|
|
Hostname types.String `tfsdk:"hostname"`
|
|
Address types.String `tfsdk:"address"`
|
|
State types.String `tfsdk:"state"`
|
|
Labels map[string]string `tfsdk:"labels"`
|
|
GPUTotal types.Int64 `tfsdk:"gpu_total"`
|
|
GPUFree types.Int64 `tfsdk:"gpu_free"`
|
|
GPUClasses []string `tfsdk:"gpu_classes"`
|
|
MemoryGB types.Int64 `tfsdk:"memory_gb"`
|
|
CPUCount types.Int64 `tfsdk:"cpu_count"`
|
|
Engines []string `tfsdk:"engines"`
|
|
Fabrics []string `tfsdk:"fabrics"`
|
|
OS types.String `tfsdk:"os"`
|
|
AgentVersion types.String `tfsdk:"agent_version"`
|
|
Replicas types.Int64 `tfsdk:"replicas"`
|
|
}
|
|
|
|
func (d *nodesDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_nodes"
|
|
}
|
|
|
|
func (d *nodesDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
|
|
str := func(desc string) schema.StringAttribute {
|
|
return schema.StringAttribute{Computed: true, Description: desc}
|
|
}
|
|
num := func(desc string) schema.Int64Attribute {
|
|
return schema.Int64Attribute{Computed: true, Description: desc}
|
|
}
|
|
list := func(desc string) schema.ListAttribute {
|
|
return schema.ListAttribute{Computed: true, ElementType: types.StringType, Description: desc}
|
|
}
|
|
resp.Schema = schema.Schema{
|
|
Description: "Every node the control plane knows, with its cards and state now.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"nodes": schema.ListNestedAttribute{Computed: true, Description: "The nodes.",
|
|
NestedObject: schema.NestedAttributeObject{Attributes: map[string]schema.Attribute{
|
|
"id": str("The node's ID."),
|
|
"hostname": str("Its hostname."),
|
|
"address": str("Its management address."),
|
|
"state": str("ready, cordoned, draining, unreachable or lost."),
|
|
"labels": schema.MapAttribute{Computed: true, ElementType: types.StringType, Description: "Its labels."},
|
|
"gpu_total": num("Its GPUs."),
|
|
"gpu_free": num("GPUs not allocated."),
|
|
"gpu_classes": list("The classes of its GPUs, h100-80g."),
|
|
"memory_gb": num("Its memory, 0 when not reported."),
|
|
"cpu_count": num("Its CPUs, 0 when not reported."),
|
|
"engines": list("The inference engines installed."),
|
|
"fabrics": list("The fabrics its adapters are on."),
|
|
"os": str("Its operating system and architecture."),
|
|
"agent_version": str("The agent's version."),
|
|
"replicas": num("Replicas with a shard on it."),
|
|
}},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (d *nodesDataSource) Configure(_ context.Context, req datasource.ConfigureRequest, _ *datasource.ConfigureResponse) {
|
|
if c, ok := clientFrom(req.ProviderData); ok {
|
|
d.c = c
|
|
}
|
|
}
|
|
|
|
func (d *nodesDataSource) Read(ctx context.Context, _ datasource.ReadRequest, resp *datasource.ReadResponse) {
|
|
ns, err := d.c.Nodes(ctx)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Listing the nodes", err.Error())
|
|
return
|
|
}
|
|
out := nodesData{Nodes: []nodeSummary{}}
|
|
for _, n := range ns {
|
|
labels := n.Labels
|
|
if labels == nil {
|
|
labels = map[string]string{}
|
|
}
|
|
out.Nodes = append(out.Nodes, nodeSummary{
|
|
ID: types.StringValue(n.ID), Hostname: types.StringValue(n.Hostname), Address: types.StringValue(n.MgmtAddr),
|
|
State: types.StringValue(n.State), Labels: labels,
|
|
GPUTotal: types.Int64Value(n.GPUTotal), GPUFree: types.Int64Value(n.GPUFree), GPUClasses: nonNil(n.Classes),
|
|
MemoryGB: types.Int64Value(n.MemoryGB), CPUCount: types.Int64Value(n.CPUCount),
|
|
Engines: nonNil(n.Engines), Fabrics: nonNil(n.Fabrics), OS: types.StringValue(n.OS),
|
|
AgentVersion: types.StringValue(n.AgentVersion), Replicas: types.Int64Value(n.Replicas),
|
|
})
|
|
}
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &out)...)
|
|
}
|