ce7cb702218693d08ea6d83464d2457c9250027c

This commit is contained in:
ms2k 2026-11-12 20:46:28 +09:00
commit 34c5f8963c

View file

@ -0,0 +1,131 @@
package provider
import (
"context"
"os"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/path"
"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"
"github.com/quorate/terraform-provider-versitygw/internal/client"
)
var _ provider.Provider = (*Provider)(nil)
func New() provider.Provider {
return &Provider{}
}
type Provider struct{}
type providerModel struct {
Endpoint types.String `tfsdk:"endpoint"`
Region types.String `tfsdk:"region"`
AdminAccessKey types.String `tfsdk:"admin_access_key"`
AdminSecretKey types.String `tfsdk:"admin_secret_key"`
InsecureTLS types.Bool `tfsdk:"insecure_tls"`
}
func (p *Provider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = "versitygw"
}
func (p *Provider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
Attributes: map[string]schema.Attribute{
"endpoint": schema.StringAttribute{
Optional: true,
Description: "VersityGW admin API endpoint. May also be set with VERSITYGW_ENDPOINT.",
},
"region": schema.StringAttribute{
Optional: true,
Description: "SigV4 region. Defaults to us-east-1. May also be set with VERSITYGW_REGION.",
},
"admin_access_key": schema.StringAttribute{
Optional: true,
Sensitive: true,
Description: "VersityGW admin/root access key. May also be set with VERSITYGW_ADMIN_ACCESS_KEY.",
},
"admin_secret_key": schema.StringAttribute{
Optional: true,
Sensitive: true,
Description: "VersityGW admin/root secret key. May also be set with VERSITYGW_ADMIN_SECRET_KEY.",
},
"insecure_tls": schema.BoolAttribute{
Optional: true,
Description: "Disable TLS certificate verification for the admin endpoint. May also be set with VERSITYGW_INSECURE_TLS=true.",
},
},
}
}
func (p *Provider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
var cfg providerModel
resp.Diagnostics.Append(req.Config.Get(ctx, &cfg)...)
if resp.Diagnostics.HasError() {
return
}
endpoint := stringValue(cfg.Endpoint, os.Getenv("VERSITYGW_ENDPOINT"))
region := stringValue(cfg.Region, os.Getenv("VERSITYGW_REGION"))
if region == "" {
region = "us-east-1"
}
adminAccessKey := stringValue(cfg.AdminAccessKey, os.Getenv("VERSITYGW_ADMIN_ACCESS_KEY"))
adminSecretKey := stringValue(cfg.AdminSecretKey, os.Getenv("VERSITYGW_ADMIN_SECRET_KEY"))
insecureTLS := boolValue(cfg.InsecureTLS, os.Getenv("VERSITYGW_INSECURE_TLS") == "true")
if endpoint == "" {
resp.Diagnostics.AddAttributeError(path.Root("endpoint"), "Missing VersityGW endpoint", "Set endpoint in provider configuration or VERSITYGW_ENDPOINT.")
}
if adminAccessKey == "" {
resp.Diagnostics.AddAttributeError(path.Root("admin_access_key"), "Missing admin access key", "Set admin_access_key in provider configuration or VERSITYGW_ADMIN_ACCESS_KEY.")
}
if adminSecretKey == "" {
resp.Diagnostics.AddAttributeError(path.Root("admin_secret_key"), "Missing admin secret key", "Set admin_secret_key in provider configuration or VERSITYGW_ADMIN_SECRET_KEY.")
}
if resp.Diagnostics.HasError() {
return
}
api, err := client.New(client.Config{
Endpoint: endpoint,
Region: region,
AdminAccessKey: adminAccessKey,
AdminSecretKey: adminSecretKey,
InsecureTLS: insecureTLS,
})
if err != nil {
resp.Diagnostics.AddError("Invalid VersityGW provider configuration", err.Error())
return
}
resp.ResourceData = api
}
func (p *Provider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewUserResource,
}
}
func (p *Provider) DataSources(_ context.Context) []func() datasource.DataSource {
return nil
}
func stringValue(value types.String, fallback string) string {
if value.IsNull() || value.IsUnknown() {
return fallback
}
return value.ValueString()
}
func boolValue(value types.Bool, fallback bool) bool {
if value.IsNull() || value.IsUnknown() {
return fallback
}
return value.ValueBool()
}