-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathutils.go
More file actions
65 lines (56 loc) · 1.97 KB
/
utils.go
File metadata and controls
65 lines (56 loc) · 1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package utils
import (
"context"
"fmt"
"math"
"github.com/stackitcloud/stackit-cli/internal/pkg/utils"
"github.com/stackitcloud/stackit-sdk-go/services/dns"
)
type DNSClient interface {
GetZoneExecute(ctx context.Context, projectId, zoneId string) (*dns.ZoneResponse, error)
GetRecordSetExecute(ctx context.Context, projectId, zoneId, recordSetId string) (*dns.RecordSetResponse, error)
}
func GetZoneName(ctx context.Context, apiClient DNSClient, projectId, zoneId string) (string, error) {
resp, err := apiClient.GetZoneExecute(ctx, projectId, zoneId)
if err != nil {
return "", fmt.Errorf("get DNS zone: %w", err)
}
return *resp.Zone.Name, nil
}
func GetRecordSetName(ctx context.Context, apiClient DNSClient, projectId, zoneId, recordSetId string) (string, error) {
resp, err := apiClient.GetRecordSetExecute(ctx, projectId, zoneId, recordSetId)
if err != nil {
return "", fmt.Errorf("get DNS recordset: %w", err)
}
return *resp.Rrset.Name, nil
}
func GetRecordSetType(ctx context.Context, apiClient DNSClient, projectId, zoneId, recordSetId string) (*string, error) {
resp, err := apiClient.GetRecordSetExecute(ctx, projectId, zoneId, recordSetId)
if err != nil {
return utils.Ptr(""), fmt.Errorf("get DNS recordset: %w", err)
}
return resp.Rrset.Type, nil
}
func FormatTxtRecord(input string) (string, error) {
length := float64(len(input))
if length <= 255 {
return input, nil
}
// Max length with quotes and white spaces is 4096. Without the quotes and white spaces the max length is 4049
if length > 4049 {
return "", fmt.Errorf("max input length is 4049. The length of the input is %v", length)
}
result := ""
chunks := int(math.Ceil(length / 255))
for i := range chunks {
skip := 255 * i
if i == chunks-1 {
// Append the left record content
result += fmt.Sprintf("%q", input[0+skip:])
} else {
// Add 255 characters of the record data quoted to the result
result += fmt.Sprintf("%q ", input[0+skip:255+skip])
}
}
return result, nil
}