Archived
Merge pull request #25 from beatz174-bit/codex/update-dns-record-content-schema-and-handling
Make dns_record content optional and omit it from write requests when unset
This commit is contained in:
@@ -111,7 +111,7 @@ type DNSRecord struct {
|
|||||||
type CreateDNSRecordRequest struct {
|
type CreateDNSRecordRequest struct {
|
||||||
NodeName string `json:"nodeName,omitempty"`
|
NodeName string `json:"nodeName,omitempty"`
|
||||||
RecordType string `json:"recordType"`
|
RecordType string `json:"recordType"`
|
||||||
Content string `json:"content"`
|
Content *string `json:"content,omitempty"`
|
||||||
TTL int64 `json:"ttl,omitempty"`
|
TTL int64 `json:"ttl,omitempty"`
|
||||||
State *bool `json:"state,omitempty"`
|
State *bool `json:"state,omitempty"`
|
||||||
Group string `json:"group,omitempty"`
|
Group string `json:"group,omitempty"`
|
||||||
@@ -121,7 +121,7 @@ type CreateDNSRecordRequest struct {
|
|||||||
type UpdateDNSRecordRequest struct {
|
type UpdateDNSRecordRequest struct {
|
||||||
NodeName string `json:"nodeName,omitempty"`
|
NodeName string `json:"nodeName,omitempty"`
|
||||||
RecordType string `json:"recordType"`
|
RecordType string `json:"recordType"`
|
||||||
Content string `json:"content"`
|
Content *string `json:"content,omitempty"`
|
||||||
TTL int64 `json:"ttl,omitempty"`
|
TTL int64 `json:"ttl,omitempty"`
|
||||||
State *bool `json:"state,omitempty"`
|
State *bool `json:"state,omitempty"`
|
||||||
Group string `json:"group,omitempty"`
|
Group string `json:"group,omitempty"`
|
||||||
@@ -312,7 +312,7 @@ func parseAPIException(payload []byte) error {
|
|||||||
type dnsRecordUpsertPayload struct {
|
type dnsRecordUpsertPayload struct {
|
||||||
NodeName string `json:"nodeName,omitempty"`
|
NodeName string `json:"nodeName,omitempty"`
|
||||||
RecordType string `json:"recordType"`
|
RecordType string `json:"recordType"`
|
||||||
Content string `json:"content,omitempty"`
|
Content *string `json:"content,omitempty"`
|
||||||
IPv4Address string `json:"ipv4Address,omitempty"`
|
IPv4Address string `json:"ipv4Address,omitempty"`
|
||||||
IPv6Address string `json:"ipv6Address,omitempty"`
|
IPv6Address string `json:"ipv6Address,omitempty"`
|
||||||
TTL int64 `json:"ttl,omitempty"`
|
TTL int64 `json:"ttl,omitempty"`
|
||||||
@@ -321,7 +321,7 @@ type dnsRecordUpsertPayload struct {
|
|||||||
Host string `json:"host,omitempty"`
|
Host string `json:"host,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
func buildDNSRecordUpsertPayload(recordType string, nodeName string, content string, ttl int64, state *bool, group string, host string) dnsRecordUpsertPayload {
|
func buildDNSRecordUpsertPayload(recordType string, nodeName string, content *string, ttl int64, state *bool, group string, host string) dnsRecordUpsertPayload {
|
||||||
payload := dnsRecordUpsertPayload{
|
payload := dnsRecordUpsertPayload{
|
||||||
NodeName: nodeName,
|
NodeName: nodeName,
|
||||||
RecordType: recordType,
|
RecordType: recordType,
|
||||||
@@ -334,12 +334,16 @@ func buildDNSRecordUpsertPayload(recordType string, nodeName string, content str
|
|||||||
|
|
||||||
switch strings.ToUpper(strings.TrimSpace(recordType)) {
|
switch strings.ToUpper(strings.TrimSpace(recordType)) {
|
||||||
case "A":
|
case "A":
|
||||||
payload.IPv4Address = content
|
if content != nil {
|
||||||
|
payload.IPv4Address = *content
|
||||||
|
}
|
||||||
case "AAAA":
|
case "AAAA":
|
||||||
payload.IPv6Address = content
|
if content != nil {
|
||||||
|
payload.IPv6Address = *content
|
||||||
|
}
|
||||||
case "CNAME":
|
case "CNAME":
|
||||||
if payload.Host == "" {
|
if payload.Host == "" && content != nil {
|
||||||
payload.Host = content
|
payload.Host = *content
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -95,7 +95,7 @@ func TestClientDNSRecordCRUD(t *testing.T) {
|
|||||||
created, err := client.CreateDNSRecord(context.Background(), 1001, dynuclient.CreateDNSRecordRequest{
|
created, err := client.CreateDNSRecord(context.Background(), 1001, dynuclient.CreateDNSRecordRequest{
|
||||||
NodeName: "api",
|
NodeName: "api",
|
||||||
RecordType: "TXT",
|
RecordType: "TXT",
|
||||||
Content: "created",
|
Content: stringPointer("created"),
|
||||||
TTL: 120,
|
TTL: 120,
|
||||||
State: &state,
|
State: &state,
|
||||||
Group: "integration",
|
Group: "integration",
|
||||||
@@ -118,7 +118,7 @@ func TestClientDNSRecordCRUD(t *testing.T) {
|
|||||||
updated, err := client.UpdateDNSRecord(context.Background(), 1001, created.ID, dynuclient.UpdateDNSRecordRequest{
|
updated, err := client.UpdateDNSRecord(context.Background(), 1001, created.ID, dynuclient.UpdateDNSRecordRequest{
|
||||||
NodeName: "api",
|
NodeName: "api",
|
||||||
RecordType: "TXT",
|
RecordType: "TXT",
|
||||||
Content: "updated",
|
Content: stringPointer("updated"),
|
||||||
TTL: 180,
|
TTL: 180,
|
||||||
State: &state,
|
State: &state,
|
||||||
})
|
})
|
||||||
@@ -145,7 +145,7 @@ func TestClientDNSRecordWriteAPIError(t *testing.T) {
|
|||||||
fake.SetAPIError("/dns/1001/record", fakedynu.APIError{HTTPStatus: 400, StatusCode: 400, Type: "Validation Exception", Message: "recordType invalid"})
|
fake.SetAPIError("/dns/1001/record", fakedynu.APIError{HTTPStatus: 400, StatusCode: 400, Type: "Validation Exception", Message: "recordType invalid"})
|
||||||
|
|
||||||
client := dynuclient.New("test-key", dynuclient.WithBaseURL(fake.BaseURL()), dynuclient.WithHTTPClient(fake.Client()))
|
client := dynuclient.New("test-key", dynuclient.WithBaseURL(fake.BaseURL()), dynuclient.WithHTTPClient(fake.Client()))
|
||||||
_, err := client.CreateDNSRecord(context.Background(), 1001, dynuclient.CreateDNSRecordRequest{RecordType: "", Content: "x"})
|
_, err := client.CreateDNSRecord(context.Background(), 1001, dynuclient.CreateDNSRecordRequest{RecordType: "", Content: stringPointer("x")})
|
||||||
if err == nil || !strings.Contains(err.Error(), "Validation Exception") {
|
if err == nil || !strings.Contains(err.Error(), "Validation Exception") {
|
||||||
t.Fatalf("expected validation API error, got %v", err)
|
t.Fatalf("expected validation API error, got %v", err)
|
||||||
}
|
}
|
||||||
@@ -179,7 +179,7 @@ func TestClientCreateDNSRecordSendsIPv4AddressForARecord(t *testing.T) {
|
|||||||
_, err := client.CreateDNSRecord(context.Background(), 1001, dynuclient.CreateDNSRecordRequest{
|
_, err := client.CreateDNSRecord(context.Background(), 1001, dynuclient.CreateDNSRecordRequest{
|
||||||
NodeName: "www",
|
NodeName: "www",
|
||||||
RecordType: "A",
|
RecordType: "A",
|
||||||
Content: "167.179.167.166",
|
Content: stringPointer("167.179.167.166"),
|
||||||
TTL: 300,
|
TTL: 300,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -216,7 +216,7 @@ func TestClientCreateDNSRecordNormalizesZoneStyleContent(t *testing.T) {
|
|||||||
record, err := client.CreateDNSRecord(context.Background(), 1001, dynuclient.CreateDNSRecordRequest{
|
record, err := client.CreateDNSRecord(context.Background(), 1001, dynuclient.CreateDNSRecordRequest{
|
||||||
NodeName: "www",
|
NodeName: "www",
|
||||||
RecordType: "A",
|
RecordType: "A",
|
||||||
Content: "167.179.167.166",
|
Content: stringPointer("167.179.167.166"),
|
||||||
TTL: 300,
|
TTL: 300,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -239,3 +239,48 @@ func TestClientDoRequestTopLevelAPIExceptionPayload(t *testing.T) {
|
|||||||
t.Fatalf("expected top-level API error, got %v", err)
|
t.Fatalf("expected top-level API error, got %v", err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestClientCreateDNSRecordOmitsContentWhenUnset(t *testing.T) {
|
||||||
|
var captured map[string]any
|
||||||
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
if r.Method != http.MethodPost || r.URL.Path != "/dns/1001/record" {
|
||||||
|
t.Fatalf("unexpected request %s %s", r.Method, r.URL.Path)
|
||||||
|
}
|
||||||
|
if err := json.NewDecoder(r.Body).Decode(&captured); err != nil {
|
||||||
|
t.Fatalf("failed to decode payload: %v", err)
|
||||||
|
}
|
||||||
|
_ = json.NewEncoder(w).Encode(map[string]any{
|
||||||
|
"statusCode": 200,
|
||||||
|
"id": 10,
|
||||||
|
"domainId": 1001,
|
||||||
|
"domainName": "example.com",
|
||||||
|
"nodeName": "www",
|
||||||
|
"hostname": "www.example.com",
|
||||||
|
"recordType": "A",
|
||||||
|
"ttl": 300,
|
||||||
|
"state": true,
|
||||||
|
})
|
||||||
|
}))
|
||||||
|
defer server.Close()
|
||||||
|
|
||||||
|
client := dynuclient.New("test-key", dynuclient.WithBaseURL(server.URL), dynuclient.WithHTTPClient(server.Client()))
|
||||||
|
_, err := client.CreateDNSRecord(context.Background(), 1001, dynuclient.CreateDNSRecordRequest{
|
||||||
|
NodeName: "www",
|
||||||
|
RecordType: "A",
|
||||||
|
TTL: 300,
|
||||||
|
})
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("CreateDNSRecord() error = %v", err)
|
||||||
|
}
|
||||||
|
|
||||||
|
if _, ok := captured["content"]; ok {
|
||||||
|
t.Fatalf("expected content to be omitted, got %#v", captured["content"])
|
||||||
|
}
|
||||||
|
if _, ok := captured["ipv4Address"]; ok {
|
||||||
|
t.Fatalf("expected ipv4Address to be omitted, got %#v", captured["ipv4Address"])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func stringPointer(value string) *string {
|
||||||
|
return &value
|
||||||
|
}
|
||||||
|
|||||||
@@ -9,6 +9,7 @@ import (
|
|||||||
|
|
||||||
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
|
"github.com/hashicorp/terraform-plugin-framework-validators/int64validator"
|
||||||
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
"github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator"
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource"
|
"github.com/hashicorp/terraform-plugin-framework/resource"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
|
||||||
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
|
||||||
@@ -21,6 +22,7 @@ var (
|
|||||||
_ resource.Resource = &dnsRecordResource{}
|
_ resource.Resource = &dnsRecordResource{}
|
||||||
_ resource.ResourceWithConfigure = &dnsRecordResource{}
|
_ resource.ResourceWithConfigure = &dnsRecordResource{}
|
||||||
_ resource.ResourceWithImportState = &dnsRecordResource{}
|
_ resource.ResourceWithImportState = &dnsRecordResource{}
|
||||||
|
_ resource.ResourceWithValidateConfig = &dnsRecordResource{}
|
||||||
)
|
)
|
||||||
|
|
||||||
type dnsRecordResource struct {
|
type dnsRecordResource struct {
|
||||||
@@ -64,7 +66,7 @@ func (r *dnsRecordResource) Schema(_ context.Context, _ resource.SchemaRequest,
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
"record_type": schema.StringAttribute{Required: true, Description: "DNS record type (A, AAAA, CNAME, TXT, etc.).", Validators: []validator.String{stringvalidator.LengthAtLeast(1)}},
|
"record_type": schema.StringAttribute{Required: true, Description: "DNS record type (A, AAAA, CNAME, TXT, etc.).", Validators: []validator.String{stringvalidator.LengthAtLeast(1)}},
|
||||||
"content": schema.StringAttribute{Required: true, Description: "Record content/value."},
|
"content": schema.StringAttribute{Optional: true, Description: "Record content/value."},
|
||||||
"ttl": schema.Int64Attribute{
|
"ttl": schema.Int64Attribute{
|
||||||
Optional: true,
|
Optional: true,
|
||||||
Computed: true,
|
Computed: true,
|
||||||
@@ -94,6 +96,31 @@ func (r *dnsRecordResource) Configure(_ context.Context, req resource.ConfigureR
|
|||||||
r.clientProvider = providerData
|
r.clientProvider = providerData
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *dnsRecordResource) ValidateConfig(ctx context.Context, req resource.ValidateConfigRequest, resp *resource.ValidateConfigResponse) {
|
||||||
|
var config dnsRecordResourceModel
|
||||||
|
resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)
|
||||||
|
if resp.Diagnostics.HasError() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
recordType, skip := knownNormalizedString(config.RecordType)
|
||||||
|
if skip {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if recordType == "A" || recordType == "AAAA" {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if config.Content.IsUnknown() {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if config.Content.IsNull() || strings.TrimSpace(config.Content.ValueString()) == "" {
|
||||||
|
resp.Diagnostics.AddError(
|
||||||
|
"Missing required content for DNS record type",
|
||||||
|
fmt.Sprintf("The %q record type requires a non-empty content value. Set the content attribute or use A/AAAA when content should be omitted.", recordType),
|
||||||
|
)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (r *dnsRecordResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
func (r *dnsRecordResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
|
||||||
var plan dnsRecordResourceModel
|
var plan dnsRecordResourceModel
|
||||||
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
|
||||||
@@ -110,20 +137,26 @@ func (r *dnsRecordResource) Create(ctx context.Context, req resource.CreateReque
|
|||||||
createReq := dynuclient.CreateDNSRecordRequest{
|
createReq := dynuclient.CreateDNSRecordRequest{
|
||||||
NodeName: recordNodeName(plan.NodeName, plan.Hostname, domainName),
|
NodeName: recordNodeName(plan.NodeName, plan.Hostname, domainName),
|
||||||
RecordType: strings.TrimSpace(plan.RecordType.ValueString()),
|
RecordType: strings.TrimSpace(plan.RecordType.ValueString()),
|
||||||
Content: strings.TrimSpace(plan.Content.ValueString()),
|
Content: stringPointerFromOptional(plan.Content),
|
||||||
TTL: int64FromOptional(plan.TTL),
|
TTL: int64FromOptional(plan.TTL),
|
||||||
State: boolPointerFromOptional(plan.State),
|
State: boolPointerFromOptional(plan.State),
|
||||||
Group: stringFromOptional(plan.Group),
|
Group: stringFromOptional(plan.Group),
|
||||||
Host: stringFromOptional(plan.Host),
|
Host: stringFromOptional(plan.Host),
|
||||||
}
|
}
|
||||||
|
if !validateDNSRecordContentForType(createReq.RecordType, createReq.Content, &resp.Diagnostics) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
record, err := r.clientProvider.client.CreateDNSRecord(ctx, domainID, createReq)
|
record, err := r.clientProvider.client.CreateDNSRecord(ctx, domainID, createReq)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
resp.Diagnostics.AddError(diagnosticSummary("Unable to create Dynu DNS record", err), err.Error())
|
addDNSRecordWriteDiagnostic("create", createReq.RecordType, createReq.Content, err, &resp.Diagnostics)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
state := mapDNSRecordToState(*record)
|
state := mapDNSRecordToState(*record)
|
||||||
|
if plan.Content.IsNull() || plan.Content.IsUnknown() {
|
||||||
|
state.Content = types.StringNull()
|
||||||
|
}
|
||||||
state.ID = types.StringValue(formatDNSRecordID(record.DomainID, record.ID))
|
state.ID = types.StringValue(formatDNSRecordID(record.DomainID, record.ID))
|
||||||
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
|
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
|
||||||
}
|
}
|
||||||
@@ -153,6 +186,9 @@ func (r *dnsRecordResource) Read(ctx context.Context, req resource.ReadRequest,
|
|||||||
}
|
}
|
||||||
|
|
||||||
nextState := mapDNSRecordToState(*record)
|
nextState := mapDNSRecordToState(*record)
|
||||||
|
if state.Content.IsNull() {
|
||||||
|
nextState.Content = types.StringNull()
|
||||||
|
}
|
||||||
nextState.ID = state.ID
|
nextState.ID = state.ID
|
||||||
resp.Diagnostics.Append(resp.State.Set(ctx, &nextState)...)
|
resp.Diagnostics.Append(resp.State.Set(ctx, &nextState)...)
|
||||||
}
|
}
|
||||||
@@ -183,15 +219,18 @@ func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateReque
|
|||||||
updateReq := dynuclient.UpdateDNSRecordRequest{
|
updateReq := dynuclient.UpdateDNSRecordRequest{
|
||||||
NodeName: recordNodeName(plan.NodeName, plan.Hostname, domainName),
|
NodeName: recordNodeName(plan.NodeName, plan.Hostname, domainName),
|
||||||
RecordType: strings.TrimSpace(plan.RecordType.ValueString()),
|
RecordType: strings.TrimSpace(plan.RecordType.ValueString()),
|
||||||
Content: strings.TrimSpace(plan.Content.ValueString()),
|
Content: stringPointerFromOptional(plan.Content),
|
||||||
TTL: int64FromOptional(plan.TTL),
|
TTL: int64FromOptional(plan.TTL),
|
||||||
State: boolPointerFromOptional(plan.State),
|
State: boolPointerFromOptional(plan.State),
|
||||||
Group: stringFromOptional(plan.Group),
|
Group: stringFromOptional(plan.Group),
|
||||||
Host: stringFromOptional(plan.Host),
|
Host: stringFromOptional(plan.Host),
|
||||||
}
|
}
|
||||||
|
if !validateDNSRecordContentForType(updateReq.RecordType, updateReq.Content, &resp.Diagnostics) {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
if _, err := r.clientProvider.client.UpdateDNSRecord(ctx, domainID, recordID, updateReq); err != nil {
|
if _, err := r.clientProvider.client.UpdateDNSRecord(ctx, domainID, recordID, updateReq); err != nil {
|
||||||
resp.Diagnostics.AddError(diagnosticSummary("Unable to update Dynu DNS record", err), err.Error())
|
addDNSRecordWriteDiagnostic("update", updateReq.RecordType, updateReq.Content, err, &resp.Diagnostics)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -202,6 +241,9 @@ func (r *dnsRecordResource) Update(ctx context.Context, req resource.UpdateReque
|
|||||||
}
|
}
|
||||||
|
|
||||||
nextState := mapDNSRecordToState(*record)
|
nextState := mapDNSRecordToState(*record)
|
||||||
|
if plan.Content.IsNull() || plan.Content.IsUnknown() {
|
||||||
|
nextState.Content = types.StringNull()
|
||||||
|
}
|
||||||
nextState.ID = types.StringValue(formatDNSRecordID(record.DomainID, record.ID))
|
nextState.ID = types.StringValue(formatDNSRecordID(record.DomainID, record.ID))
|
||||||
resp.Diagnostics.Append(resp.State.Set(ctx, &nextState)...)
|
resp.Diagnostics.Append(resp.State.Set(ctx, &nextState)...)
|
||||||
}
|
}
|
||||||
@@ -314,3 +356,49 @@ func stringFromOptional(value types.String) string {
|
|||||||
}
|
}
|
||||||
return strings.TrimSpace(value.ValueString())
|
return strings.TrimSpace(value.ValueString())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func stringPointerFromOptional(value types.String) *string {
|
||||||
|
if value.IsNull() || value.IsUnknown() {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
trimmed := strings.TrimSpace(value.ValueString())
|
||||||
|
return &trimmed
|
||||||
|
}
|
||||||
|
|
||||||
|
func validateDNSRecordContentForType(recordType string, content *string, diagnostics *diag.Diagnostics) bool {
|
||||||
|
normalizedType := strings.ToUpper(strings.TrimSpace(recordType))
|
||||||
|
if normalizedType == "A" || normalizedType == "AAAA" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
if content == nil || strings.TrimSpace(*content) == "" {
|
||||||
|
diagnostics.AddError(
|
||||||
|
"Missing required content for DNS record type",
|
||||||
|
fmt.Sprintf("The %q record type requires a non-empty content value. Set the content attribute or choose a type that supports omitted content (A/AAAA).", normalizedType),
|
||||||
|
)
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
func addDNSRecordWriteDiagnostic(operation string, recordType string, content *string, err error, diagnostics *diag.Diagnostics) {
|
||||||
|
detail := err.Error()
|
||||||
|
var apiErr *dynuclient.APIError
|
||||||
|
if errors.As(err, &apiErr) {
|
||||||
|
presence := "omitted"
|
||||||
|
if content != nil {
|
||||||
|
presence = fmt.Sprintf("set to %q", *content)
|
||||||
|
}
|
||||||
|
detail = fmt.Sprintf("%s. Dynu rejected this %s request for record type %q where content was %s.", err.Error(), operation, strings.ToUpper(strings.TrimSpace(recordType)), presence)
|
||||||
|
}
|
||||||
|
|
||||||
|
diagnostics.AddError(diagnosticSummary(fmt.Sprintf("Unable to %s Dynu DNS record", operation), err), detail)
|
||||||
|
}
|
||||||
|
|
||||||
|
func knownNormalizedString(value types.String) (string, bool) {
|
||||||
|
if value.IsNull() || value.IsUnknown() {
|
||||||
|
return "", true
|
||||||
|
}
|
||||||
|
return strings.ToUpper(strings.TrimSpace(value.ValueString())), false
|
||||||
|
}
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
package provider
|
package provider
|
||||||
|
|
||||||
import "testing"
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/terraform-plugin-framework/diag"
|
||||||
|
)
|
||||||
|
|
||||||
func TestParseDNSRecordID(t *testing.T) {
|
func TestParseDNSRecordID(t *testing.T) {
|
||||||
domainID, recordID, err := parseDNSRecordID("1001/55")
|
domainID, recordID, err := parseDNSRecordID("1001/55")
|
||||||
@@ -17,3 +21,37 @@ func TestParseDNSRecordIDInvalid(t *testing.T) {
|
|||||||
t.Fatal("expected parse error")
|
t.Fatal("expected parse error")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateDNSRecordContentForType(t *testing.T) {
|
||||||
|
nonEmpty := "hello"
|
||||||
|
blank := ""
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
name string
|
||||||
|
recordType string
|
||||||
|
content *string
|
||||||
|
wantValid bool
|
||||||
|
}{
|
||||||
|
{name: "A allows nil content", recordType: "A", content: nil, wantValid: true},
|
||||||
|
{name: "AAAA allows nil content", recordType: "AAAA", content: nil, wantValid: true},
|
||||||
|
{name: "TXT requires content", recordType: "TXT", content: nil, wantValid: false},
|
||||||
|
{name: "TXT rejects blank content", recordType: "TXT", content: &blank, wantValid: false},
|
||||||
|
{name: "TXT accepts non-empty content", recordType: "TXT", content: &nonEmpty, wantValid: true},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
diags := diag.Diagnostics{}
|
||||||
|
got := validateDNSRecordContentForType(tc.recordType, tc.content, &diags)
|
||||||
|
if got != tc.wantValid {
|
||||||
|
t.Fatalf("validateDNSRecordContentForType()=%v, want %v", got, tc.wantValid)
|
||||||
|
}
|
||||||
|
if tc.wantValid && diags.HasError() {
|
||||||
|
t.Fatalf("expected no error diagnostics, got %#v", diags)
|
||||||
|
}
|
||||||
|
if !tc.wantValid && !diags.HasError() {
|
||||||
|
t.Fatal("expected error diagnostics")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user