generated from hashicorp/terraform-provider-scaffolding-framework
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zone_resource.go
265 lines (222 loc) · 7.44 KB
/
zone_resource.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
package provider
import (
"context"
"fmt"
"strings"
"github.com/gonzolino/terraform-provider-powerdns/internal/powerdns"
"github.com/hashicorp/terraform-plugin-framework/diag"
"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/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-log/tflog"
)
type zoneResourceType struct{}
func (t zoneResourceType) 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 Zone",
Attributes: map[string]tfsdk.Attribute{
"id": {
MarkdownDescription: "Opaque zone id, assigned by the server.",
Type: types.StringType,
Computed: true,
},
"server_id": {
MarkdownDescription: "The id of the server.",
Type: types.StringType,
Required: true,
},
"name": {
MarkdownDescription: "Name of the zone (e.g. \"example.com.\") MUST have a trailing dot.",
Type: types.StringType,
Required: true,
},
"kind": {
MarkdownDescription: "Zone kind, one of \"Native\", \"Master\", \"Slave\".",
Type: types.StringType,
Required: true,
},
},
}, nil
}
func (t zoneResourceType) NewResource(ctx context.Context, in provider.Provider) (resource.Resource, diag.Diagnostics) {
provider, diags := convertProviderType(in)
return zoneResource{
provider: provider,
}, diags
}
type zoneResourceData struct {
Id types.String `tfsdk:"id"`
ServerId types.String `tfsdk:"server_id"`
Name types.String `tfsdk:"name"`
Kind types.String `tfsdk:"kind"`
}
type zoneResource struct {
provider powerdnsProvider
}
func (r zoneResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var data zoneResourceData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
zone := &powerdns.Zone{}
zoneResourceDataToObject(ctx, data, zone)
tflog.Debug(ctx, "Creating zone", map[string]interface{}{
"server_id": data.ServerId.Value,
"name": zone.Name,
"kind": zone.Kind,
})
zone, err := r.provider.client.CreateZone(ctx, data.ServerId.Value, zone)
if err != nil {
resp.Diagnostics.AddError(
"API Error",
fmt.Sprintf("Unable to create zone '%s': %v",
zone.Name,
err))
return
}
zoneObjectToResourceData(ctx, zone, &data)
tflog.Debug(ctx, "Created zone", map[string]interface{}{
"id": data.Id.Value,
"server_id": data.ServerId.Value,
"name": data.Name.Value,
"kind": data.Kind.Value,
})
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r zoneResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var data zoneResourceData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
var id string
if !data.Id.Unknown && !data.Id.Null {
id = data.Id.Value
} else {
// If ID is not set, try to use name as id
id = data.Name.Value
}
tflog.Debug(ctx, "Reading zone", map[string]interface{}{
"id": id,
"server_id": data.ServerId.Value,
})
zone, err := r.provider.client.GetZone(ctx, data.ServerId.Value, id)
if err != nil {
resp.Diagnostics.AddError("API Error", fmt.Sprintf("Unable to get zone '%s': %v", id, err))
return
}
zoneObjectToResourceData(ctx, zone, &data)
tflog.Debug(ctx, "Read zone", map[string]interface{}{
"id": data.Id.Value,
"server_id": data.ServerId.Value,
"name": data.Name.Value,
"kind": data.Kind.Value,
})
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r zoneResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
var data zoneResourceData
diags := req.Plan.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
zone := &powerdns.Zone{}
zoneResourceDataToObject(ctx, data, zone)
var id string
if !data.Id.Unknown && !data.Id.Null {
id = data.Id.Value
} else {
// If ID is not set, try to use name as id
id = data.Name.Value
}
tflog.Debug(ctx, "Updating zone", map[string]interface{}{
"id": id,
"server_id": data.ServerId.Value,
"name": zone.Name,
"kind": zone.Kind,
"dnssec": zone.DNSSec,
"masters": zone.Masters,
})
if err := r.provider.client.UpdateZone(ctx, data.ServerId.Value, id, zone); err != nil {
resp.Diagnostics.AddError("API Error", fmt.Sprintf("Unable to update zone '%s': %v", id, err))
return
}
tflog.Debug(ctx, "Updated zone", map[string]interface{}{
"id": id,
"server_id": data.ServerId.Value,
})
tflog.Debug(ctx, "Reading zone", map[string]interface{}{
"id": id,
"server_id": data.ServerId.Value,
})
zone, err := r.provider.client.GetZone(ctx, data.ServerId.Value, id)
if err != nil {
resp.Diagnostics.AddError("API Error", fmt.Sprintf("Unable to get zone '%s': %v", id, err))
return
}
zoneObjectToResourceData(ctx, zone, &data)
tflog.Debug(ctx, "Read zone", map[string]interface{}{
"id": data.Id.Value,
"server_id": data.ServerId.Value,
"name": data.Name.Value,
"kind": data.Kind.Value,
})
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}
func (r zoneResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
var data zoneResourceData
diags := req.State.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Deleting zone", map[string]interface{}{
"id": data.Id.Value,
"server_id": data.ServerId.Value,
})
if err := r.provider.client.DeleteZone(ctx, data.ServerId.Value, data.Id.Value); err != nil {
resp.Diagnostics.AddError("API Error", fmt.Sprintf("Unable to delete zone '%s': %v", data.Id.Value, err))
return
}
tflog.Debug(ctx, "Deleted zone", map[string]interface{}{
"id": data.Id.Value,
"server_id": data.ServerId.Value,
})
resp.State.RemoveResource(ctx)
}
func (r zoneResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
splittedID := strings.Split(req.ID, "/")
if len(splittedID) != 2 {
resp.Diagnostics.AddError(
"Resource Import ID invalid",
fmt.Sprintf("ID '%s' should be in format 'server_id/zone_id'", req.ID),
)
return
}
serverID := splittedID[0]
zoneID := splittedID[1]
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("server_id"), serverID)...)
resp.Diagnostics.Append(resp.State.SetAttribute(ctx, path.Root("id"), zoneID)...)
// tfsdk.ResourceImportStateNotImplemented(ctx, "", resp)
// tfsdk.ResourceImportStatePassthroughID(ctx, tftypes.NewAttributePath().WithAttributeName("id"), req, resp)
}
func zoneResourceDataToObject(ctx context.Context, data zoneResourceData, zone *powerdns.Zone) {
zone.ID = data.Id.Value
zone.Name = data.Name.Value
zone.Kind = data.Kind.Value
}
func zoneObjectToResourceData(ctx context.Context, zone *powerdns.Zone, data *zoneResourceData) {
data.Id = types.String{Value: zone.ID}
data.Name = types.String{Value: zone.Name}
data.Kind = types.String{Value: zone.Kind}
}