Archived
ci / test (push) Failing after 5m55s
- DYNU_API_KEY env var fallback in provider config - dynu_domain resource: add 9 computed attributes (unicode_name, ipv4/ipv6 flags, wildcard aliases, allow_zone_transfer, dnssec, created_on, updated_on) - dynu_dns_records data source: add 6 type-specific fields (priority, weight, port, flags, tag, value) - Fix normalizeRecordContentForState for MX/SRV/NS/PTR (read host field) and CAA (read value field) to avoid plan/state inconsistency - Allow underscore-prefixed DNS labels in hostname validator (RFC 2782 SRV) - Preserve plan/state hostname across create/update/read to prevent inconsistent result errors when API returns full FQDN for node_name-style records - Update all version references from 0.1.0 to 0.2.0 Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01LFZyrmtKsezBwFWGTWsb2y
115 lines
3.2 KiB
Go
115 lines
3.2 KiB
Go
package provider
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
"strings"
|
|
|
|
"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/beatz174-bit/terraform-provider-dynu/internal/dynuclient"
|
|
)
|
|
|
|
var _ provider.Provider = &dynuProvider{}
|
|
|
|
type dynuProvider struct {
|
|
version string
|
|
}
|
|
|
|
type dynuProviderModel struct {
|
|
APIKey types.String `tfsdk:"api_key"`
|
|
BaseURL types.String `tfsdk:"base_url"`
|
|
}
|
|
|
|
type providerData struct {
|
|
client *dynuclient.Client
|
|
}
|
|
|
|
func New(version string) func() provider.Provider {
|
|
return func() provider.Provider {
|
|
return &dynuProvider{version: version}
|
|
}
|
|
}
|
|
|
|
func (p *dynuProvider) Metadata(_ context.Context, _ provider.MetadataRequest, resp *provider.MetadataResponse) {
|
|
resp.TypeName = "dynu"
|
|
resp.Version = p.version
|
|
}
|
|
|
|
func (p *dynuProvider) Schema(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {
|
|
resp.Schema = schema.Schema{
|
|
Description: "Terraform provider for Dynu DNS domains, records, and data sources.",
|
|
Attributes: map[string]schema.Attribute{
|
|
"api_key": schema.StringAttribute{
|
|
Optional: true,
|
|
Sensitive: true,
|
|
Description: "Dynu API key. Can also be set via the DYNU_API_KEY environment variable.",
|
|
},
|
|
"base_url": schema.StringAttribute{
|
|
Optional: true,
|
|
Description: "Override Dynu API base URL. Primarily intended for automated tests.",
|
|
},
|
|
},
|
|
}
|
|
}
|
|
|
|
func (p *dynuProvider) Configure(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
|
|
var data dynuProviderModel
|
|
|
|
resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)
|
|
if resp.Diagnostics.HasError() {
|
|
return
|
|
}
|
|
|
|
apiKey := resolveAPIKey(data.APIKey)
|
|
if apiKey == "" {
|
|
resp.Diagnostics.AddAttributeError(
|
|
path.Root("api_key"),
|
|
"Missing Dynu API key",
|
|
"Configure api_key in the provider block (for example, from var.dynu_api_key set in terraform.tfvars).",
|
|
)
|
|
return
|
|
}
|
|
|
|
providerData := &providerData{client: newDynuClient(apiKey, data.BaseURL)}
|
|
resp.DataSourceData = providerData
|
|
resp.ResourceData = providerData
|
|
}
|
|
|
|
func resolveAPIKey(configValue types.String) string {
|
|
if !configValue.IsNull() && !configValue.IsUnknown() {
|
|
if v := strings.TrimSpace(configValue.ValueString()); v != "" {
|
|
return v
|
|
}
|
|
}
|
|
return strings.TrimSpace(os.Getenv("DYNU_API_KEY"))
|
|
}
|
|
|
|
func (p *dynuProvider) DataSources(_ context.Context) []func() datasource.DataSource {
|
|
return []func() datasource.DataSource{
|
|
NewDomainsDataSource,
|
|
NewDomainDataSource,
|
|
NewDNSRecordsDataSource,
|
|
}
|
|
}
|
|
|
|
func (p *dynuProvider) Resources(_ context.Context) []func() resource.Resource {
|
|
return []func() resource.Resource{
|
|
NewDomainResource,
|
|
NewDNSRecordResource,
|
|
}
|
|
}
|
|
|
|
func newDynuClient(apiKey string, baseURL types.String) *dynuclient.Client {
|
|
if !baseURL.IsNull() && !baseURL.IsUnknown() && strings.TrimSpace(baseURL.ValueString()) != "" {
|
|
return dynuclient.New(apiKey, dynuclient.WithBaseURL(strings.TrimSpace(baseURL.ValueString())))
|
|
}
|
|
|
|
return dynuclient.New(apiKey)
|
|
}
|