/
data_dns_record_set.go
91 lines (83 loc) · 2.45 KB
/
data_dns_record_set.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package dns
import (
"context"
"fmt"
"sort"
"github.com/apex/log"
"github.com/akamai/AkamaiOPEN-edgegrid-golang/v8/pkg/session"
"github.com/akamai/terraform-provider-akamai/v6/pkg/common/tf"
"github.com/akamai/terraform-provider-akamai/v6/pkg/meta"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceDNSRecordSet() *schema.Resource {
return &schema.Resource{
ReadContext: dataSourceDNSRecordSetRead,
Schema: map[string]*schema.Schema{
"zone": {
Type: schema.TypeString,
Required: true,
Description: "The domain zone, including any nested subdomains",
},
"host": {
Type: schema.TypeString,
Required: true,
Description: "The base credential hostname without the protocol",
},
"record_type": {
Type: schema.TypeString,
Required: true,
Description: "The DNS record type",
},
"rdata": {
Type: schema.TypeList,
Elem: &schema.Schema{Type: schema.TypeString},
Computed: true,
Description: "An array of data strings that represent multiple records within a set",
},
},
}
}
func dataSourceDNSRecordSetRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
meta := meta.Must(m)
logger := meta.Log("AkamaiDNS", "dataSourceDNSRecordSetRead")
// create a context with logging for api calls
ctx = session.ContextWithOptions(
ctx,
session.WithContextLog(logger),
)
zone, err := tf.GetStringValue("zone", d)
if err != nil {
return diag.FromErr(err)
}
host, err := tf.GetStringValue("host", d)
if err != nil {
return diag.FromErr(err)
}
recordType, err := tf.GetStringValue("record_type", d)
if err != nil {
return diag.FromErr(err)
}
logger.WithFields(log.Fields{
"zone": zone,
"host": host,
"recordtype": recordType,
}).Debug("Start Searching for records")
// Warning or Errors can be collected in a slice type
var diags diag.Diagnostics
rdata, err := inst.Client(meta).GetRdata(ctx, zone, host, recordType)
if err != nil {
return append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Failed retrieving recordset: %s", host),
Detail: err.Error(),
})
}
logger.WithField("rdata", rdata).Debug("Recordset found.")
sort.Strings(rdata)
if err := d.Set("rdata", rdata); err != nil {
return diag.FromErr(fmt.Errorf("%w: %s", tf.ErrValueSet, err.Error()))
}
d.SetId(host)
return nil
}