generated from hashicorp/terraform-provider-scaffolding-framework
/
zone_data_source.go
89 lines (73 loc) · 2.58 KB
/
zone_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
package provider
import (
"context"
"fmt"
"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 zoneDataSourceType struct{}
func (t zoneDataSourceType) 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,
Required: 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,
Computed: true,
},
"kind": {
MarkdownDescription: "Zone kind, one of \"Native\", \"Master\", \"Slave\".",
Type: types.StringType,
Computed: true,
},
},
}, nil
}
func (t zoneDataSourceType) NewDataSource(ctx context.Context, in tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
provider, diags := convertProviderType(in)
return zoneDataSource{
provider: provider,
}, diags
}
type zoneDataSourceData struct {
Id types.String `tfsdk:"id"`
ServerId types.String `tfsdk:"server_id"`
Name types.String `tfsdk:"name"`
Kind types.String `tfsdk:"kind"`
}
type zoneDataSource struct {
provider provider
}
func (d zoneDataSource) Read(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
var data zoneDataSourceData
diags := req.Config.Get(ctx, &data)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
tflog.Debug(ctx, "Reading zone", "id", data.Id.Value, "server_id", data.ServerId.Value)
zone, err := d.provider.client.GetZone(ctx, data.ServerId.Value, data.Id.Value)
if err != nil {
resp.Diagnostics.AddError("API Error", fmt.Sprintf("Unable to get zone '%s': %v", data.Id.Value, err))
return
}
data.Id = types.String{Value: zone.ID}
data.Name = types.String{Value: zone.Name}
data.Kind = types.String{Value: zone.Kind}
tflog.Debug(ctx, "Read zone", "id", zone.ID, "server_id", data.ServerId.Value, "name", zone.Name, "kind", zone.Kind)
diags = resp.State.Set(ctx, &data)
resp.Diagnostics.Append(diags...)
}