/
ALIAS.go
118 lines (103 loc) · 3.36 KB
/
ALIAS.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
package datasources
import (
"context"
"fmt"
"github.com/SuperBuker/terraform-provider-dns-he-net/client/client"
"github.com/SuperBuker/terraform-provider-dns-he-net/client/models"
"github.com/SuperBuker/terraform-provider-dns-he-net/internal/tfmodels"
"github.com/hashicorp/terraform-plugin-framework/datasource"
"github.com/hashicorp/terraform-plugin-framework/datasource/schema"
)
// Ensure the implementation satisfies the expected interfaces.
var (
_ datasource.DataSource = &a{}
_ datasource.DataSourceWithConfigure = &alias{}
)
// NewALIAS initialises the ALIAS DataSource.
func NewALIAS() datasource.DataSource {
return &alias{}
}
// alias is the data source implementation.
type alias struct {
client *client.Client
}
// Metadata returns the data source type name.
func (alias) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_alias" // TODO: maybe rename
}
// Schema defines the schema for the data source.
func (alias) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) {
resp.Schema = schema.Schema{
Description: "DNS ALIAS record",
MarkdownDescription: "DNS ALIAS record",
Attributes: map[string]schema.Attribute{
"id": schema.Int64Attribute{
Description: "dns.he.net record id",
MarkdownDescription: "dns.he.net record id",
Required: true,
},
"zone_id": schema.Int64Attribute{
Description: "dns.he.net zone id",
MarkdownDescription: "dns.he.net zone id",
Required: true,
},
"domain": schema.StringAttribute{
Description: "Name of the DNS record",
MarkdownDescription: "Name of the DNS record",
Computed: true,
},
"ttl": schema.Int64Attribute{
Description: "Time-To-Live of the DNS record",
MarkdownDescription: "Time-To-Live of the DNS record",
Computed: true,
},
"data": schema.StringAttribute{
Description: "Value of the DNS record: a hostname",
MarkdownDescription: "Value of the DNS record: a hostname",
Computed: true,
},
},
}
}
// Configure adds the provider configured client to the data source.
func (d *alias) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) {
if cli, ok := configure(ctx, req, resp); ok {
d.client = cli
}
}
// Read refreshes the Terraform state with the latest data.
func (d alias) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) {
var state tfmodels.ALIAS
// Retrieve values from state
diags := req.Config.Get(ctx, &state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
// Retrieves record from dns.he.net, handles logging and errors
recordX, ok := readRecord(ctx, d.client, state.ID, state.ZoneID, "ALIAS", resp)
if !ok {
return
}
recordALIAS, ok := recordX.(models.ALIAS)
if !ok {
resp.Diagnostics.AddError(
"Unable to cast ALIAS record",
fmt.Sprintf("unexpected record type %T", recordX),
)
return
}
if err := state.SetRecord(recordALIAS); err != nil {
resp.Diagnostics.AddError(
"Unable to set ALIAS record",
err.Error(),
)
return
}
// Set state
diags = resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
return
}
}