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

181 lines
6.6 KiB
Go

package provider
import (
"context"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"gitea.mynodes.xyz/ilker/llmgw/terraform-provider-llmgw/internal/client"
)
// The quota resource is one scope's capacity quota: what an organization,
// team or project is guaranteed of the cluster and the most it may hold.
//
// The scope is kept as written — team:Acme/ml — and beside it, as id, the
// scope the cluster resolved it to, team:ID. The quota is found again by the
// ID, so renaming the team does not lose it.
type quotaResource struct{ c *client.Client }
func newQuotaResource() resource.Resource { return &quotaResource{} }
type quotaModel struct {
ID types.String `tfsdk:"id"`
Scope types.String `tfsdk:"scope"`
Name types.String `tfsdk:"name"`
Guarantee *resourcesModel `tfsdk:"guarantee"`
Limit *resourcesModel `tfsdk:"limit"`
}
type resourcesModel struct {
GPUs types.Int64 `tfsdk:"gpus"`
CPUCores types.Int64 `tfsdk:"cpu_cores"`
MemoryGB types.Int64 `tfsdk:"memory_gb"`
}
func (r *quotaResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_quota"
}
func (r *quotaResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
amount := func(desc string) schema.SingleNestedAttribute {
return schema.SingleNestedAttribute{Optional: true, Description: desc,
Attributes: map[string]schema.Attribute{
"gpus": schema.Int64Attribute{Optional: true, Description: "GPUs; absent is not governed."},
"cpu_cores": schema.Int64Attribute{Optional: true, Description: "CPU cores of replicas without a GPU; absent is not governed."},
"memory_gb": schema.Int64Attribute{Optional: true, Description: "Host memory of those replicas, in GB; absent is not governed."},
}}
}
resp.Schema = schema.Schema{
Description: "A capacity quota on an organization, team or project. At least one of guarantee and limit must be set.",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{Computed: true,
Description: "The scope as the cluster records it, team:ID. Import by it.",
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}},
"scope": schema.StringAttribute{Required: true,
Description: "org:NAME, team:ORG/TEAM or project:ORG/TEAM/PROJECT, or the ID after the colon.",
PlanModifiers: []planmodifier.String{stringplanmodifier.RequiresReplace()}},
"name": schema.StringAttribute{Computed: true, Description: "The scope's path, Acme / ml.",
PlanModifiers: []planmodifier.String{stringplanmodifier.UseStateForUnknown()}},
"guarantee": amount("What is held for the scope: while it holds less, what others borrowed beyond their own guarantees is taken back for it."),
"limit": amount("The most the scope may hold."),
},
}
}
func (r *quotaResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
if c, ok := clientFrom(req.ProviderData); ok {
r.c = c
}
}
func amountOf(m *resourcesModel) client.Resources {
if m == nil {
return client.Resources{}
}
return client.Resources{GPUs: ptrOf(m.GPUs), CPUCores: ptrOf(m.CPUCores), MemoryGB: ptrOf(m.MemoryGB)}
}
// amountModel is the cluster's amount for the state: a block left out stays
// out while the cluster governs nothing in it.
func amountModel(had *resourcesModel, r client.Resources) *resourcesModel {
if had == nil && r.GPUs == nil && r.CPUCores == nil && r.MemoryGB == nil {
return nil
}
return &resourcesModel{GPUs: ptrOrNull(r.GPUs), CPUCores: ptrOrNull(r.CPUCores), MemoryGB: ptrOrNull(r.MemoryGB)}
}
func (r *quotaResource) put(ctx context.Context, plan *quotaModel) error {
id, err := r.c.SetQuota(ctx, client.QuotaRequest{Scope: plan.Scope.ValueString(),
Guarantee: amountOf(plan.Guarantee), Limit: amountOf(plan.Limit)})
if err != nil {
return err
}
plan.ID = types.StringValue(id)
plan.Name = types.StringNull()
qs, err := r.c.Quotas(ctx)
if err != nil {
return err
}
for _, q := range qs {
if q.Scope == id {
plan.Name = strOrNull(q.Name)
}
}
return nil
}
func (r *quotaResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var plan quotaModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
if err := r.put(ctx, &plan); err != nil {
resp.Diagnostics.AddError("Setting the quota on "+plan.Scope.ValueString(), err.Error())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *quotaResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var st quotaModel
resp.Diagnostics.Append(req.State.Get(ctx, &st)...)
if resp.Diagnostics.HasError() {
return
}
qs, err := r.c.Quotas(ctx)
if err != nil {
resp.Diagnostics.AddError("Reading the quotas", err.Error())
return
}
for _, q := range qs {
if q.Scope != st.ID.ValueString() {
continue
}
if st.Scope.IsNull() {
// Imported: the ID is the only name there is.
st.Scope = types.StringValue(q.Scope)
}
st.Name = strOrNull(q.Name)
st.Guarantee = amountModel(st.Guarantee, q.Guarantee)
st.Limit = amountModel(st.Limit, q.Limit)
resp.Diagnostics.Append(resp.State.Set(ctx, &st)...)
return
}
resp.State.RemoveResource(ctx)
}
func (r *quotaResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var plan quotaModel
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
if resp.Diagnostics.HasError() {
return
}
if err := r.put(ctx, &plan); err != nil {
resp.Diagnostics.AddError("Setting the quota on "+plan.Scope.ValueString(), err.Error())
return
}
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *quotaResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var st quotaModel
resp.Diagnostics.Append(req.State.Get(ctx, &st)...)
if resp.Diagnostics.HasError() {
return
}
if err := r.c.ClearQuota(ctx, st.ID.ValueString()); err != nil {
resp.Diagnostics.AddError("Removing the quota on "+st.Scope.ValueString(), err.Error())
}
}
func (r *quotaResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}