Vigiar
1
0
Derivar
Você já tinha feito uma derivação do repositório terraform-provider-interwebao, anteriormente
0
terraform-provider-interwebao/internal/provider/provider.go
2026-11-28 15:28:02 +02:00

97 linhas
3,2 KiB
Go

// Package provider implements the Terraform provider on top of the public API.
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/interweb-ao/terraform-provider-interwebao/internal/brand"
"github.com/interweb-ao/terraform-provider-interwebao/internal/client"
)
var _ provider.Provider = &apiProvider{}
type apiProvider struct {
version string
}
type providerModel struct {
APIKey types.String `tfsdk:"api_key"`
}
// New returns the provider factory.
func New(version string) func() provider.Provider {
return func() provider.Provider {
return &apiProvider{version: version}
}
}
func (p *apiProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
resp.TypeName = brand.TypeName
resp.Version = p.version
}
func (p *apiProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
resp.Schema = schema.Schema{
MarkdownDescription: "Manage " + brand.Name + " VPS servers, domains and DNS records through the " + brand.Name +
" API. Documentation: " + brand.DocsURL,
Attributes: map[string]schema.Attribute{
"api_key": schema.StringAttribute{
MarkdownDescription: "API key (`sandbox_...` or `prod_...`). When not set, the provider reads the `" +
brand.APIKeyEnv + "` environment variable. Create keys in " + brand.PanelName + ".",
Optional: true,
Sensitive: true,
},
},
}
}
func (p *apiProvider) 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
}
if cfg.APIKey.IsUnknown() {
resp.Diagnostics.AddAttributeError(path.Root("api_key"), "Unknown API key",
"The API key must be known when the plan is made. Set it directly or use the "+brand.APIKeyEnv+" environment variable.")
return
}
key := os.Getenv(brand.APIKeyEnv)
if !cfg.APIKey.IsNull() && cfg.APIKey.ValueString() != "" {
key = cfg.APIKey.ValueString()
}
if key == "" {
resp.Diagnostics.AddAttributeError(path.Root("api_key"), "Missing API key",
"Set api_key in the provider block or the "+brand.APIKeyEnv+" environment variable. Create a key in "+brand.PanelName+" ("+brand.PanelURL+").")
return
}
c := client.New(key, "terraform-provider-"+brand.TypeName+"/"+p.version)
resp.DataSourceData = c
resp.ResourceData = c
}
func (p *apiProvider) Resources(_ context.Context) []func() resource.Resource {
return []func() resource.Resource{
NewVPSResource,
NewDomainResource,
NewDNSRecordResource,
}
}
func (p *apiProvider) DataSources(_ context.Context) []func() datasource.DataSource {
return []func() datasource.DataSource{
NewCatalogProductsDataSource,
NewCatalogProductDataSource,
NewCatalogTLDsDataSource,
NewDomainAvailabilityDataSource,
NewServersDataSource,
}
}