Files
terraform-provider-giteavau…/internal/provider/provider.go
T
unkinben 646fa0f840 Initial terraform-provider-giteavaultsecret
Add a Terraform provider that manages the Gitea token secrets engine
(vault-plugin-secrets-gitea) on Vault/OpenBao, so terraform-vault can drive
the engine's mount, config, and roles declaratively.

- add the provider (source git.unkin.net/unkin/giteavaultsecret, prefix gitea_)
- add gitea_secret_backend (mount + config with seeded admin credentials)
- add gitea_secret_backend_role (username, scopes list, ttls, token_name_prefix)
- add the Vault API client plumbing, conversions, and unit tests
- add examples, a real terraform+Vault+mock-Gitea e2e, and Woodpecker pipelines
  releasing the provider zip to the artifactapi terraform registry

Claude-Session: https://claude.ai/code/session_015ur3i7D2azsMAWTSVABApv
2026-07-27 00:55:09 +10:00

101 lines
2.9 KiB
Go

package provider
import (
"context"
"os"
"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"
)
var _ provider.Provider = &giteaProvider{}
type giteaProvider struct {
version string
}
type giteaProviderModel struct {
Address types.String `tfsdk:"address"`
Token types.String `tfsdk:"token"`
}
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &giteaProvider{version: version}
}
}
func (p *giteaProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
// The provider's source address is git.unkin.net/unkin/giteavaultsecret, but
// its resources are prefixed "gitea_" (declare it in required_providers under
// the local name "gitea"), mirroring how google-beta ships google_*.
resp.TypeName = "gitea"
resp.Version = p.version
}
func (p *giteaProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "Manage the Gitea token secrets engine (config and roles) on HashiCorp Vault or OpenBao.",
Attributes: map[string]schema.Attribute{
"address": schema.StringAttribute{
Description: "Address of the Vault/OpenBao server. Falls back to the VAULT_ADDR environment variable.",
Optional: true,
},
"token": schema.StringAttribute{
Description: "Token used to authenticate to Vault/OpenBao. Falls back to the VAULT_TOKEN environment variable.",
Optional: true,
Sensitive: true,
},
},
}
}
func (p *giteaProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var config giteaProviderModel
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
if resp.Diagnostics.HasError() {
return
}
address := os.Getenv("VAULT_ADDR")
if !config.Address.IsNull() && config.Address.ValueString() != "" {
address = config.Address.ValueString()
}
token := os.Getenv("VAULT_TOKEN")
if !config.Token.IsNull() && config.Token.ValueString() != "" {
token = config.Token.ValueString()
}
if address == "" {
resp.Diagnostics.AddError(
"missing Vault address",
"Set the provider \"address\" attribute or the VAULT_ADDR environment variable.",
)
return
}
client, err := newVaultClient(address, token)
if err != nil {
resp.Diagnostics.AddError("failed to create Vault client", err.Error())
return
}
resp.DataSourceData = client
resp.ResourceData = client
}
func (p *giteaProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewSecretBackendResource,
NewSecretBackendRoleResource,
}
}
func (p *giteaProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return nil
}