/
resource_dns_record.go
303 lines (260 loc) · 9.03 KB
/
resource_dns_record.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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
package provider
import (
"context"
"fmt"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/provider"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/nrdcg/porkbun"
)
// Ensure provider defined types fully satisfy framework interfaces
var (
_ resource.Resource = &porkbunDnsRecordResource{}
_ resource.ResourceWithImportState = &porkbunDnsRecordResource{}
)
func NewPorkbunDnsRecordResource() resource.Resource {
return &porkbunDnsRecordResource{}
}
type porkbunDnsRecordResource struct {
client *porkbun.Client
provider *provider.Provider
}
// porkbunDnsRecordResourceModel describes the data model
type porkbunDnsRecordResourceModel struct {
Id types.String `tfsdk:"id"`
Name types.String `tfsdk:"name"`
Type types.String `tfsdk:"type"`
Content types.String `tfsdk:"content"`
Ttl types.String `tfsdk:"ttl"`
Notes types.String `tfsdk:"notes"`
Prio types.String `tfsdk:"prio"`
Domain types.String `tfsdk:"domain"`
}
func (r *porkbunDnsRecordResource) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_dns_record"
}
func (r *porkbunDnsRecordResource) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Porkbun DNS Record resource",
Attributes: map[string]schema.Attribute{
"name": schema.StringAttribute{
Computed: true,
Optional: true,
MarkdownDescription: "The subdomain for the record itself without the base domain",
Default: stringdefault.StaticString(""),
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplaceIfConfigured(),
stringplanmodifier.UseStateForUnknown(),
},
},
"domain": schema.StringAttribute{
Required: true,
MarkdownDescription: "The base domain to to create the record on",
PlanModifiers: []planmodifier.String{
stringplanmodifier.RequiresReplaceIfConfigured(),
},
},
"id": schema.StringAttribute{
Computed: true,
Optional: false,
Required: false,
MarkdownDescription: "The Porkbun ID of the Record",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
// Todo: Validate that its not set to less than 600
"ttl": schema.StringAttribute{
Optional: true,
Computed: true,
MarkdownDescription: "The ttl of the record, the minimum is 600",
Default: stringdefault.StaticString("600"),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
Validators: []validator.String{
TtlAtLeast600(),
},
},
"type": schema.StringAttribute{
Required: true,
MarkdownDescription: "The type of DNS Record to create",
},
"notes": schema.StringAttribute{
Optional: true,
MarkdownDescription: "Notes to add to the record",
},
"prio": schema.StringAttribute{
Optional: true,
Computed: true,
MarkdownDescription: "The priority of the record",
Default: stringdefault.StaticString("0"),
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"content": schema.StringAttribute{
Optional: true,
MarkdownDescription: "The content of the record",
},
},
}
}
func (r *porkbunDnsRecordResource) Configure(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(*porkbun.Client)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Resource Configure Type",
fmt.Sprintf("Expected *http.Client, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (r porkbunDnsRecordResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data porkbunDnsRecordResourceModel
// attempts := req.Provider
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
record := porkbun.Record{
Name: data.Name.ValueString(),
Type: data.Type.ValueString(),
Content: data.Content.ValueString(),
TTL: data.Ttl.ValueString(), // Minimum is 600 according to porkbun docs
Prio: data.Prio.ValueString(), // Doesn't work on .com?
Notes: data.Notes.ValueString(),
}
id, err := r.client.CreateRecord(ctx, data.Domain.ValueString(), record)
if err != nil {
resp.Diagnostics.AddError(
"Error creating DNS Record",
fmt.Sprintf("Error: %s", err),
)
}
data.Id = types.StringValue(fmt.Sprint(id))
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r porkbunDnsRecordResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data porkbunDnsRecordResourceModel
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
getRecordsResult, err := r.getRecords(ctx, data.Domain.ValueString())
if err != nil {
resp.Diagnostics.AddError(
fmt.Sprintf(
`Could not retrieve records for %s.`,
data.Domain.ValueString(),
),
fmt.Sprintf("Error: %s", err.Error()),
)
}
tflog.Info(ctx, fmt.Sprintf("Found records: %s", getRecordsResult))
for _, record := range getRecordsResult {
tflog.Info(ctx, fmt.Sprintf("This record is: %s", record.ID))
if record.ID == data.Id.ValueString() {
// This is to handle if there's no subdomain
if data.Domain.ValueString() == record.Name {
data.Name = types.StringValue("")
} else {
// The API returns the full record as the name so we'll strip off the domain at the end to keep it consistent
data.Name = types.StringValue(strings.ReplaceAll(record.Name, fmt.Sprintf(".%s", data.Domain.ValueString()), ""))
}
}
}
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r porkbunDnsRecordResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data porkbunDnsRecordResourceModel
var recordId string
diags := req.Plan.Get(ctx, &data)
req.State.GetAttribute(ctx, path.Root("id"), &recordId)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
record := porkbun.Record{
Name: data.Name.ValueString(),
Type: data.Type.ValueString(),
Content: data.Content.ValueString(),
TTL: data.Ttl.ValueString(), // Minimum is 600 according to porkbun docs
Prio: data.Prio.ValueString(), // Doesn't work on .com?
Notes: data.Notes.ValueString(),
}
intId, err := strconv.Atoi(recordId)
if err != nil {
resp.Diagnostics.AddError(
"Error converting ID to a string",
fmt.Sprintf("Error: %s", err),
)
}
err = r.client.EditRecord(ctx, data.Domain.ValueString(), intId, record)
if err != nil {
resp.Diagnostics.AddError(
"Error updating the record",
fmt.Sprintf("Error %s", err),
)
}
if resp.Diagnostics.HasError() {
return
}
diags = resp.State.Set(ctx, &data)
resp.State.SetAttribute(ctx, path.Root("id"), &recordId)
resp.Diagnostics.Append(diags...)
}
func (r porkbunDnsRecordResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var state porkbunDnsRecordResourceModel
diags := req.State.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
intId, err := strconv.Atoi(state.Id.ValueString())
if err != nil {
resp.Diagnostics.AddError(
"Error converting ID to a string",
fmt.Sprintf("Error: %s", err),
)
}
err = r.client.DeleteRecord(ctx, state.Domain.ValueString(), intId)
if err != nil {
resp.Diagnostics.AddError(
"Error deleting record",
fmt.Sprintf("Error: %s", err),
)
}
if resp.Diagnostics.HasError() {
return
}
}
func (r porkbunDnsRecordResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
func (r porkbunDnsRecordResource) getRecords(ctx context.Context, domain string) ([]porkbun.Record, error) {
records, err := r.client.RetrieveRecords(ctx, domain)
if err != nil {
return []porkbun.Record{}, err
}
return records, nil
}