// 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, newOrgResource, newTeamResource, newProjectResource, newKeyResource, newMembershipResource} } 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 }