-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_autogen_subdomain_delegation.go
177 lines (153 loc) · 6.9 KB
/
resource_autogen_subdomain_delegation.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
package provider
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/nullstone-io/terraform-provider-ns/internal/server"
"github.com/nullstone-io/terraform-provider-ns/ns"
"gopkg.in/nullstone-io/go-api-client.v0"
"gopkg.in/nullstone-io/go-api-client.v0/types"
)
type resourceAutogenSubdomainDelegation struct {
p *provider
}
func newResourceAutogenSubdomainDelegation(p *provider) (*resourceAutogenSubdomainDelegation, error) {
if p == nil {
return nil, fmt.Errorf("a provider is required")
}
return &resourceAutogenSubdomainDelegation{p: p}, nil
}
var (
_ server.Resource = (*resourceAutogenSubdomainDelegation)(nil)
_ server.ResourceUpdater = (*resourceAutogenSubdomainDelegation)(nil)
)
func (r *resourceAutogenSubdomainDelegation) Schema(ctx context.Context) *tfprotov5.Schema {
return &tfprotov5.Schema{
Block: &tfprotov5.SchemaBlock{
Attributes: []*tfprotov5.SchemaAttribute{
deprecatedIDAttribute(),
{
Name: "subdomain_id",
Type: tftypes.Number,
Description: "The autogen subdomain belongs to this subdomain.",
Required: true,
DescriptionKind: tfprotov5.StringKindMarkdown,
},
{
Name: "env_id",
Type: tftypes.Number,
Description: "The autogen subdomain belongs to this env.",
Required: true,
DescriptionKind: tfprotov5.StringKindMarkdown,
},
{
Name: "nameservers",
Required: true,
Type: tftypes.List{ElementType: tftypes.String},
Description: "A list of nameservers that refer to a DNS zone where this subdomain can delegate.",
DescriptionKind: tfprotov5.StringKindMarkdown,
},
},
},
}
}
func (r *resourceAutogenSubdomainDelegation) Validate(ctx context.Context, config map[string]tftypes.Value) ([]*tfprotov5.Diagnostic, error) {
return nil, nil
}
func (r *resourceAutogenSubdomainDelegation) PlanCreate(ctx context.Context, proposed map[string]tftypes.Value, config map[string]tftypes.Value) (map[string]tftypes.Value, []*tfprotov5.Diagnostic, error) {
// There are never any creates, just set the values to proposed
return map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
"subdomain_id": proposed["subdomain_id"],
"env_id": proposed["env_id"],
"nameservers": proposed["nameservers"],
}, nil, nil
}
func (r *resourceAutogenSubdomainDelegation) PlanUpdate(ctx context.Context, proposed map[string]tftypes.Value, config map[string]tftypes.Value, prior map[string]tftypes.Value) (map[string]tftypes.Value, []*tfprotov5.Diagnostic, error) {
return map[string]tftypes.Value{
"id": prior["id"],
"subdomain_id": proposed["subdomain_id"],
"env_id": proposed["env_id"],
"nameservers": proposed["nameservers"],
}, nil, nil
}
func (r *resourceAutogenSubdomainDelegation) Read(ctx context.Context, config map[string]tftypes.Value) (map[string]tftypes.Value, []*tfprotov5.Diagnostic, error) {
state := map[string]tftypes.Value{}
diags := make([]*tfprotov5.Diagnostic, 0)
subdomainId := extractInt64FromConfig(config, "subdomain_id")
envId := extractInt64FromConfig(config, "env_id")
nsClient := &api.Client{Config: r.p.NsConfig}
autogenSubdomain, err := nsClient.AutogenSubdomain().Get(subdomainId, envId)
if err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "error retrieving autogen subdomain delegation",
Detail: err.Error(),
})
} else if autogenSubdomain == nil {
diags = append(diags, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "unable to find autogen subdomain for the given subdomain and environment",
Detail: fmt.Sprintf("subdomain_id=%d env_id=%d", subdomainId, envId),
})
} else {
state["id"] = tftypes.NewValue(tftypes.String, fmt.Sprintf("%d", autogenSubdomain.Id))
state["subdomain_id"] = tftypes.NewValue(tftypes.Number, &subdomainId)
state["env_id"] = tftypes.NewValue(tftypes.Number, &envId)
state["nameservers"] = ns.NameserversToProtov5(autogenSubdomain.Nameservers)
}
return state, diags, nil
}
func (r *resourceAutogenSubdomainDelegation) Create(ctx context.Context, planned map[string]tftypes.Value, config map[string]tftypes.Value, prior map[string]tftypes.Value) (state map[string]tftypes.Value, diags []*tfprotov5.Diagnostic, err error) {
return r.Update(ctx, planned, config, prior)
}
func (r *resourceAutogenSubdomainDelegation) Update(ctx context.Context, planned map[string]tftypes.Value, config map[string]tftypes.Value, prior map[string]tftypes.Value) (map[string]tftypes.Value, []*tfprotov5.Diagnostic, error) {
state := map[string]tftypes.Value{}
diags := make([]*tfprotov5.Diagnostic, 0)
subdomainId := extractInt64FromConfig(config, "subdomain_id")
envId := extractInt64FromConfig(config, "env_id")
nameservers, _ := extractStringSliceFromConfig(planned, "nameservers")
autogenSubdomain := &types.AutogenSubdomain{Nameservers: nameservers}
var id int64
nsClient := &api.Client{Config: r.p.NsConfig}
if result, err := nsClient.AutogenSubdomainDelegation().Update(subdomainId, envId, autogenSubdomain); err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "error updating autogen subdomain delegation",
Detail: err.Error(),
})
} else if result == nil {
diags = append(diags, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: fmt.Sprintf("The autogen_subdomain_delegation for the subdomain %d and env %d is missing.", subdomainId, envId),
})
} else {
id = autogenSubdomain.Id
nameservers = autogenSubdomain.Nameservers
}
state["id"] = tftypes.NewValue(tftypes.String, fmt.Sprintf("%d", id))
state["subdomain_id"] = tftypes.NewValue(tftypes.Number, &subdomainId)
state["env_id"] = tftypes.NewValue(tftypes.Number, &envId)
state["nameservers"] = ns.NameserversToProtov5(nameservers)
return state, diags, nil
}
func (r *resourceAutogenSubdomainDelegation) Destroy(ctx context.Context, prior map[string]tftypes.Value) ([]*tfprotov5.Diagnostic, error) {
diags := make([]*tfprotov5.Diagnostic, 0)
subdomainId := extractInt64FromConfig(prior, "subdomain_id")
envId := extractInt64FromConfig(prior, "env_id")
nsClient := &api.Client{Config: r.p.NsConfig}
if found, err := nsClient.AutogenSubdomainDelegation().Destroy(subdomainId, envId); err != nil {
diags = append(diags, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "error destroying autogen subdomain delegation",
Detail: err.Error(),
})
} else if !found {
diags = append(diags, &tfprotov5.Diagnostic{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: fmt.Sprintf("The autogen_subdomain_delegation for the subdomain %d and env %d is missing.", subdomainId, envId),
})
}
return diags, nil
}