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>
140 lines
5.7 KiB
Go
140 lines
5.7 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
|
|
"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 guard attachment resource is a guardrail policy attached to a part of
|
|
// the tenancy tree, a key or a person. The policies themselves are defined
|
|
// in server.yaml's guard section, not here: what this manages is where they
|
|
// apply.
|
|
|
|
type guardAttachmentResource struct{ c *client.Client }
|
|
|
|
func newGuardAttachmentResource() resource.Resource { return &guardAttachmentResource{} }
|
|
|
|
type guardAttachmentModel struct {
|
|
ID types.String `tfsdk:"id"`
|
|
Target types.String `tfsdk:"target"`
|
|
Policy types.String `tfsdk:"policy"`
|
|
Scope types.String `tfsdk:"scope"`
|
|
ScopeID types.String `tfsdk:"scope_id"`
|
|
}
|
|
|
|
func (r *guardAttachmentResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
|
|
resp.TypeName = req.ProviderTypeName + "_guard_attachment"
|
|
}
|
|
|
|
func (r *guardAttachmentResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) {
|
|
replace := []planmodifier.String{stringplanmodifier.RequiresReplace()}
|
|
keep := []planmodifier.String{stringplanmodifier.UseStateForUnknown()}
|
|
resp.Schema = schema.Schema{
|
|
Description: "A guardrail policy from server.yaml's guard.policies, attached to an organization, team, project, key or person.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"id": schema.StringAttribute{Computed: true, PlanModifiers: keep,
|
|
Description: "scope:SCOPE-ID:policy. Import by it."},
|
|
"target": schema.StringAttribute{Required: true, PlanModifiers: replace,
|
|
Description: "org:NAME, team:ORG/TEAM, project:ORG/TEAM/PROJECT (or the ID after the colon), key:KEY-ID or user:USER-ID."},
|
|
"policy": schema.StringAttribute{Required: true, PlanModifiers: replace,
|
|
Description: "The policy's name in server.yaml's guard.policies."},
|
|
"scope": schema.StringAttribute{Computed: true, PlanModifiers: keep,
|
|
Description: "org, team, project, key or user."},
|
|
"scope_id": schema.StringAttribute{Computed: true, PlanModifiers: keep,
|
|
Description: "The ID the target resolved to."},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (r *guardAttachmentResource) Configure(_ context.Context, req resource.ConfigureRequest, _ *resource.ConfigureResponse) {
|
|
if c, ok := clientFrom(req.ProviderData); ok {
|
|
r.c = c
|
|
}
|
|
}
|
|
|
|
func attachmentID(a client.Attachment) string { return a.Scope + ":" + a.ScopeID + ":" + a.Policy }
|
|
|
|
func (r *guardAttachmentResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
|
var plan guardAttachmentModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
a, err := r.c.Attach(ctx, plan.Target.ValueString(), plan.Policy.ValueString())
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Attaching "+plan.Policy.ValueString()+" to "+plan.Target.ValueString(), err.Error())
|
|
return
|
|
}
|
|
plan.ID = types.StringValue(attachmentID(a))
|
|
plan.Scope = types.StringValue(a.Scope)
|
|
plan.ScopeID = types.StringValue(a.ScopeID)
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
|
|
}
|
|
|
|
func (r *guardAttachmentResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
|
|
var st guardAttachmentModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &st)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
g, err := r.c.Guard(ctx)
|
|
if err != nil {
|
|
resp.Diagnostics.AddError("Reading the guard attachments", err.Error())
|
|
return
|
|
}
|
|
for _, a := range g.Attachments {
|
|
if attachmentID(a) == st.ID.ValueString() {
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &st)...)
|
|
return
|
|
}
|
|
}
|
|
resp.State.RemoveResource(ctx)
|
|
}
|
|
|
|
// Update is never called: every attribute replaces the attachment.
|
|
func (r *guardAttachmentResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
|
|
var plan guardAttachmentModel
|
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
|
|
}
|
|
|
|
func (r *guardAttachmentResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
|
|
var st guardAttachmentModel
|
|
resp.Diagnostics.Append(req.State.Get(ctx, &st)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
// By the ID it resolved to, which still names it if the team has since
|
|
// been renamed.
|
|
err := r.c.Detach(ctx, st.Scope.ValueString()+":"+st.ScopeID.ValueString(), st.Policy.ValueString())
|
|
if err != nil && !client.IsNotFound(err) {
|
|
resp.Diagnostics.AddError("Detaching "+st.Policy.ValueString()+" from "+st.Target.ValueString(), err.Error())
|
|
}
|
|
}
|
|
|
|
func (r *guardAttachmentResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
|
|
// scope:SCOPE-ID:policy. The policy is after the last colon and the scope
|
|
// before the first; an ID between them is whatever is left.
|
|
scope, rest, ok1 := strings.Cut(req.ID, ":")
|
|
i := strings.LastIndex(rest, ":")
|
|
if !ok1 || i <= 0 || i == len(rest)-1 {
|
|
resp.Diagnostics.AddError("Not an attachment ID", fmt.Sprintf("%q is not scope:SCOPE-ID:policy, as id shows it", req.ID))
|
|
return
|
|
}
|
|
id, policy := rest[:i], rest[i+1:]
|
|
st := guardAttachmentModel{
|
|
ID: types.StringValue(req.ID), Target: types.StringValue(scope + ":" + id), Policy: types.StringValue(policy),
|
|
Scope: types.StringValue(scope), ScopeID: types.StringValue(id),
|
|
}
|
|
resp.Diagnostics.Append(resp.State.Set(ctx, &st)...)
|
|
}
|