Archived
Remove unsupported location field from DNS record resource
This commit is contained in:
1 parent
1dbd948a22
commit
5eed12a594
9 files changed
+18
-87
No files matched your search
@@ -43,7 +43,6 @@ type dnsRecordResourceModel struct {
|
||||
Enabled types.Bool `tfsdk:"enabled"`
|
||||
Group types.String `tfsdk:"group"`
|
||||
Host types.String `tfsdk:"host"`
|
||||
Location types.String `tfsdk:"location"`
|
||||
NodeName types.String `tfsdk:"node_name"`
|
||||
DomainID types.Int64 `tfsdk:"domain_id"`
|
||||
DomainName types.String `tfsdk:"domain_name"`
|
||||
@@ -83,7 +82,6 @@ func (r *dnsRecordResource) Schema(_ context.Context, _ resource.SchemaRequest,
|
||||
"enabled": schema.BoolAttribute{Optional: true, Computed: true, Default: booldefault.StaticBool(true), Description: "Whether this DNS record is enabled/active."},
|
||||
"group": schema.StringAttribute{Optional: true, Computed: true, Description: "Dynu group value for this record."},
|
||||
"host": schema.StringAttribute{Optional: true, Computed: true, Description: "Host field for supported Dynu record types."},
|
||||
"location": schema.StringAttribute{Optional: true, Computed: true, Description: "Dynu location hint for A/AAAA records only."},
|
||||
"node_name": schema.StringAttribute{Optional: true, Computed: true, Description: "Node/label portion of the record."},
|
||||
"domain_id": schema.Int64Attribute{Computed: true, Description: "Dynu domain ID resolved from hostname."},
|
||||
"domain_name": schema.StringAttribute{Computed: true, Description: "Dynu root domain name resolved from hostname."},
|
||||
@@ -122,7 +120,6 @@ func (r *dnsRecordResource) ValidateConfig(ctx context.Context, req resource.Val
|
||||
}
|
||||
validateDNSRecordContentForTypeWithKnowledge(recordType, content, contentKnown, dynamicIntent, &resp.Diagnostics)
|
||||
validateDNSRecordTTL(config.TTL, &resp.Diagnostics)
|
||||
validateDNSRecordLocation(recordType, config.Location, &resp.Diagnostics)
|
||||
}
|
||||
|
||||
func (r *dnsRecordResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||
@@ -152,7 +149,6 @@ func (r *dnsRecordResource) Create(ctx context.Context, req resource.CreateReque
|
||||
State: boolPointerFromOptional(plan.Enabled),
|
||||
Group: stringFromOptional(plan.Group),
|
||||
Host: stringFromOptional(plan.Host),
|
||||
Location: stringFromOptional(plan.Location),
|
||||
}
|
||||
createReq = normalizeDNSRecordCreateRequestForType(createReq)
|
||||
if !validateDNSRecordContentForType(createReq.RecordType, createReq.Content, dynamicIntent, &resp.Diagnostics) {
|
||||
@@ -161,9 +157,6 @@ func (r *dnsRecordResource) Create(ctx context.Context, req resource.CreateReque
|
||||
if !validateRecordTTLSeconds(createReq.TTL, &resp.Diagnostics) {
|
||||
return
|
||||
}
|
||||
if !validateLocationForType(createReq.RecordType, createReq.Location, &resp.Diagnostics) {
|
||||
return
|
||||
}
|
||||
|
||||
record, err := r.clientProvider.client.CreateDNSRecord(ctx, domainID, createReq)
|
||||
if err != nil && dynamicIntent && isUnsupportedEmptyContentError(err) {
|
||||
@@ -256,7 +249,6 @@ func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateReque
|
||||
State: boolPointerFromOptional(preferKnownBool(plan.Enabled, state.Enabled)),
|
||||
Group: stringFromOptional(preferKnownString(plan.Group, state.Group)),
|
||||
Host: stringFromOptional(preferKnownString(plan.Host, state.Host)),
|
||||
Location: locationForUpdate(recordType, plan.Location, state.Location),
|
||||
}
|
||||
updateReq = normalizeDNSRecordUpdateRequestForType(updateReq)
|
||||
if !validateDNSRecordContentForType(updateReq.RecordType, updateReq.Content, dynamicIntent, &resp.Diagnostics) {
|
||||
@@ -265,9 +257,6 @@ func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateReque
|
||||
if !validateRecordTTLSeconds(updateReq.TTL, &resp.Diagnostics) {
|
||||
return
|
||||
}
|
||||
if !validateLocationForType(updateReq.RecordType, updateReq.Location, &resp.Diagnostics) {
|
||||
return
|
||||
}
|
||||
|
||||
if _, err := r.clientProvider.client.UpdateDNSRecord(ctx, domainID, recordID, updateReq); err != nil {
|
||||
if dynamicIntent && isUnsupportedEmptyContentError(err) {
|
||||
@@ -279,7 +268,6 @@ func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateReque
|
||||
State: updateReq.State,
|
||||
Group: updateReq.Group,
|
||||
Host: updateReq.Host,
|
||||
Location: updateReq.Location,
|
||||
}, &resp.Diagnostics); retryOK {
|
||||
updateReq.Content = retryReq.Content
|
||||
updateReq.Group = retryReq.Group
|
||||
@@ -349,7 +337,6 @@ func mapDNSRecordToState(record dynuclient.DNSRecord, dynamicIntent bool) dnsRec
|
||||
Enabled: types.BoolValue(record.State),
|
||||
Group: mapString(record.Group),
|
||||
Host: mapString(record.Host),
|
||||
Location: mapString(record.Location),
|
||||
NodeName: mapString(record.NodeName),
|
||||
DomainID: types.Int64Value(record.DomainID),
|
||||
DomainName: mapString(record.DomainName),
|
||||
@@ -583,33 +570,6 @@ func validateRecordTTLSeconds(ttl int64, diagnostics *diag.Diagnostics) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func validateDNSRecordLocation(recordType string, location types.String, diagnostics *diag.Diagnostics) {
|
||||
if location.IsNull() || location.IsUnknown() {
|
||||
return
|
||||
}
|
||||
validateLocationForType(recordType, location.ValueString(), diagnostics)
|
||||
}
|
||||
|
||||
func validateLocationForType(recordType string, location string, diagnostics *diag.Diagnostics) bool {
|
||||
if strings.TrimSpace(location) == "" {
|
||||
return true
|
||||
}
|
||||
normalizedType := strings.ToUpper(strings.TrimSpace(recordType))
|
||||
if normalizedType != "A" && normalizedType != "AAAA" {
|
||||
diagnostics.AddError("Invalid location", fmt.Sprintf("location is only supported for A and AAAA records, got %q.", normalizedType))
|
||||
return false
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func locationForUpdate(recordType string, planLocation types.String, stateLocation types.String) string {
|
||||
normalizedType := strings.ToUpper(strings.TrimSpace(recordType))
|
||||
if normalizedType != "A" && normalizedType != "AAAA" {
|
||||
return stringFromOptional(planLocation)
|
||||
}
|
||||
return stringFromOptional(preferKnownString(planLocation, stateLocation))
|
||||
}
|
||||
|
||||
func resolveDynamicIntent(recordType string, content types.String, dynamic types.Bool, diagnostics *diag.Diagnostics) (bool, bool) {
|
||||
normalizedType := strings.ToUpper(strings.TrimSpace(recordType))
|
||||
contentPtr := stringPointerFromOptionalContent(content)
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
package provider
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strings"
|
||||
"testing"
|
||||
|
||||
"github.com/dynu/terraform-provider-dynu/internal/dynuclient"
|
||||
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||
"github.com/hashicorp/terraform-plugin-framework/types"
|
||||
)
|
||||
|
||||
@@ -192,26 +194,15 @@ func TestValidateRecordTTLSeconds(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestValidateLocationForType(t *testing.T) {
|
||||
diags := diag.Diagnostics{}
|
||||
if !validateLocationForType("A", "us", &diags) || diags.HasError() {
|
||||
t.Fatal("expected location for A to be valid")
|
||||
func TestDNSRecordSchemaHasEnabledAndNoLocation(t *testing.T) {
|
||||
r := NewDNSRecordResource()
|
||||
resp := &resource.SchemaResponse{}
|
||||
r.Schema(context.Background(), resource.SchemaRequest{}, resp)
|
||||
if _, ok := resp.Schema.Attributes["enabled"]; !ok {
|
||||
t.Fatal("expected enabled attribute in schema")
|
||||
}
|
||||
diags = diag.Diagnostics{}
|
||||
if validateLocationForType("CNAME", "us", &diags) || !diags.HasError() {
|
||||
t.Fatal("expected location for CNAME to fail")
|
||||
}
|
||||
}
|
||||
|
||||
func TestLocationForUpdate(t *testing.T) {
|
||||
if got := locationForUpdate("A", types.StringNull(), types.StringValue("us")); got != "us" {
|
||||
t.Fatalf("expected A record update to preserve state location, got %q", got)
|
||||
}
|
||||
if got := locationForUpdate("CNAME", types.StringNull(), types.StringValue("us")); got != "" {
|
||||
t.Fatalf("expected non-A/AAAA type to ignore prior state location when omitted, got %q", got)
|
||||
}
|
||||
if got := locationForUpdate("TXT", types.StringValue("eu"), types.StringValue("us")); got != "eu" {
|
||||
t.Fatalf("expected explicit plan location to be returned as-is, got %q", got)
|
||||
if _, ok := resp.Schema.Attributes["location"]; ok {
|
||||
t.Fatal("expected location attribute to be removed from schema")
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user