Skip to content

Commit 6c7e5df

Browse files
committed
add function to format DNS TXT records
1 parent 6bdfff1 commit 6c7e5df

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

internal/cmd/dns/record-set/create/create.go

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"encoding/json"
66
"fmt"
7-
87
"github.com/goccy/go-yaml"
98
"github.com/stackitcloud/stackit-cli/internal/pkg/args"
109
"github.com/stackitcloud/stackit-cli/internal/pkg/errors"
@@ -16,6 +15,7 @@ import (
1615
dnsUtils "github.com/stackitcloud/stackit-cli/internal/pkg/services/dns/utils"
1716
"github.com/stackitcloud/stackit-cli/internal/pkg/spinner"
1817
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
18+
"math"
1919

2020
"github.com/spf13/cobra"
2121
"github.com/stackitcloud/stackit-sdk-go/services/dns"
@@ -31,6 +31,7 @@ const (
3131
typeFlag = "type"
3232

3333
defaultType = "A"
34+
txtType = "TXT"
3435
)
3536

3637
type inputModel struct {
@@ -152,7 +153,23 @@ func parseInput(p *print.Printer, cmd *cobra.Command) (*inputModel, error) {
152153
func buildRequest(ctx context.Context, model *inputModel, apiClient *dns.APIClient) dns.ApiCreateRecordSetRequest {
153154
records := make([]dns.RecordPayload, 0)
154155
for _, r := range model.Records {
155-
records = append(records, dns.RecordPayload{Content: utils.Ptr(r)})
156+
result := r
157+
if len(r) > 255 && model.Type == txtType {
158+
result = ""
159+
length := float64(len(r))
160+
chunks := int(math.Ceil(length / 255))
161+
for i := range chunks {
162+
skip := 255 * i
163+
if i == chunks-1 {
164+
// Append the left record content
165+
result += fmt.Sprintf("%q", r[0+skip:])
166+
} else {
167+
// Add 255 characters of the record data quoted to the result
168+
result += fmt.Sprintf("%q ", r[0+skip:255+skip])
169+
}
170+
}
171+
}
172+
records = append(records, dns.RecordPayload{Content: utils.Ptr(result)})
156173
}
157174

158175
req := apiClient.CreateRecordSet(ctx, model.ProjectId, model.ZoneId)

internal/cmd/dns/record-set/create/create_test.go

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package create
22

33
import (
44
"context"
5+
"fmt"
6+
"strings"
57
"testing"
68

79
"github.com/stackitcloud/stackit-cli/internal/pkg/globalflags"
@@ -23,6 +25,12 @@ var testClient = &dns.APIClient{}
2325
var testProjectId = uuid.NewString()
2426
var testZoneId = uuid.NewString()
2527

28+
var recordTxtOver255Char = []string{
29+
"foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoo",
30+
"foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoo",
31+
"foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar",
32+
}
33+
2634
func fixtureFlagValues(mods ...func(flagValues map[string]string)) map[string]string {
2735
flagValues := map[string]string{
2836
projectIdFlag: testProjectId,
@@ -326,6 +334,27 @@ func TestBuildRequest(t *testing.T) {
326334
Type: utils.Ptr(defaultType),
327335
}),
328336
},
337+
{
338+
description: "add TXT record with > 255 characters",
339+
model: fixtureInputModel(func(model *inputModel) {
340+
model.Type = txtType
341+
model.Records = []string{strings.Join(recordTxtOver255Char, "")}
342+
}),
343+
expectedRequest: testClient.CreateRecordSet(testCtx, testProjectId, testZoneId).
344+
CreateRecordSetPayload(dns.CreateRecordSetPayload{
345+
Name: utils.Ptr("example.com"),
346+
Records: &[]dns.RecordPayload{
347+
{
348+
Content: utils.Ptr(
349+
fmt.Sprintf("\"%s\"", strings.Join(recordTxtOver255Char, "\" \"")),
350+
),
351+
},
352+
},
353+
Type: utils.Ptr(txtType),
354+
Comment: utils.Ptr("comment"),
355+
Ttl: utils.Ptr(int64(3600)),
356+
}),
357+
},
329358
}
330359

331360
for _, tt := range tests {

0 commit comments

Comments
 (0)