generated from hashicorp/terraform-provider-scaffolding-framework
/
recordset_data_source.go
119 lines (102 loc) · 3.92 KB
/
recordset_data_source.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
type recordsetDataSourceType struct{}
func (t recordsetDataSourceType) GetSchema(ctx context.Context) (tfsdk.Schema, diag.Diagnostics) {
return tfsdk.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "PowerDNS Resource Record Set",
Attributes: map[string]tfsdk.Attribute{
"id": {
MarkdownDescription: "State ID for the record set (only needed for internal technical purposes).",
Type: types.StringType,
Computed: true,
},
"zone_id": {
MarkdownDescription: "ID of the zone this record set belongs to.",
Type: types.StringType,
Required: true,
},
"server_id": {
MarkdownDescription: "The id of the server.",
Type: types.StringType,
Required: true,
},
"name": {
MarkdownDescription: "Name for record set (e.g. \"www.powerdns.com.\")",
Type: types.StringType,
Required: true,
},
"type": {
MarkdownDescription: "Type of this record (e.g. \"A\", \"PTR\", \"MX\"). Required if record name is not unique.",
Type: types.StringType,
Required: true,
},
"ttl": {
MarkdownDescription: " DNS TTL of the records, in seconds.",
Type: types.Int64Type,
Computed: true,
},
"records": {
MarkdownDescription: "All records in this record set.",
Type: types.ListType{ElemType: types.StringType},
Computed: true,
},
},
}, nil
}
func (t recordsetDataSourceType) NewDataSource(ctx context.Context, in tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
provider, diags := convertProviderType(in)
return recordsetDataSource{
provider: provider,
}, diags
}
type recordsetDataSourceData struct {
Id types.String `tfsdk:"id"`
ZoneId types.String `tfsdk:"zone_id"`
ServerId types.String `tfsdk:"server_id"`
Name types.String `tfsdk:"name"`
Type types.String `tfsdk:"type"`
Ttl types.Int64 `tfsdk:"ttl"`
Records types.List `tfsdk:"records"`
}
type recordsetDataSource struct {
provider provider
}
func (d recordsetDataSource) Read(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
var data recordsetDataSourceData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Reading record set", "zone_id", data.ZoneId.Value, "server_id", data.ServerId.Value, "name", data.Name.Value, "type", data.Type.Value)
recordset, err := d.provider.client.GetRecordSet(ctx, data.ServerId.Value, data.ZoneId.Value, data.Name.Value, data.Type.Value)
if err != nil {
resp.Diagnostics.AddError("API Error", fmt.Sprintf("Unable to get record set '%s' (type '%s'): %v", data.Name.Value, data.Type.Value, err))
return
}
records := make([]attr.Value, len(recordset.Records))
for i, record := range recordset.Records {
records[i] = types.String{Value: record}
}
data.Id = types.String{Value: fmt.Sprintf("%s/%s/%s", data.ZoneId.Value, data.Name.Value, data.Type.Value)}
data.Name = types.String{Value: recordset.Name}
data.Type = types.String{Value: recordset.Type}
data.Ttl = types.Int64{Value: recordset.TTL}
data.Records = types.List{
ElemType: types.StringType,
Elems: records,
}
tflog.Debug(ctx, "Read record set", "zone_id", data.ZoneId.Value, "server_id", data.ServerId.Value,
"name", data.Name.Value, "type", data.Type.Value, "ttl", data.Ttl.Value, "records", data.Records)
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}