/
connector_certificate.go
216 lines (173 loc) · 7.1 KB
/
connector_certificate.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
package resources
import (
"context"
"fmt"
"github.com/fivetran/terraform-provider-fivetran/fivetran/framework/core"
"github.com/fivetran/terraform-provider-fivetran/fivetran/framework/core/model"
fivetranSchema "github.com/fivetran/terraform-provider-fivetran/fivetran/framework/core/schema"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
)
func ConnectorCertificate() resource.Resource {
return &connectorCertificate{}
}
type connectorCertificate struct {
core.ProviderResource
}
// Ensure the implementation satisfies the desired interfaces.
var _ resource.ResourceWithConfigure = &connectorCertificate{}
var _ resource.ResourceWithImportState = &connectorCertificate{}
func (r *connectorCertificate) Metadata(ctx context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_connector_certificates"
}
func (r *connectorCertificate) Schema(ctx context.Context, req resource.SchemaRequest, resp *resource.SchemaResponse) {
resp.Schema = fivetranSchema.CertificateConnectorResource()
}
func (r *connectorCertificate) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}
func (r *connectorCertificate) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
if r.GetClient() == nil {
resp.Diagnostics.AddError(
"Unconfigured Fivetran Client",
"Please report this issue to the provider developers.",
)
return
}
var data model.CertificateConnector
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
for _, item := range data.Certificate.Elements() {
if element, ok := item.(basetypes.ObjectValue); ok {
svc := r.GetClient().NewCertificateConnectorCertificateApprove()
svc.ConnectorID(data.ConnectorId.ValueString())
svc.Hash(element.Attributes()["hash"].(basetypes.StringValue).ValueString())
svc.EncodedCert(element.Attributes()["encoded_cert"].(basetypes.StringValue).ValueString())
response, err := svc.Do(ctx)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Connector Certificate Resource.",
fmt.Sprintf("%v; code: %v; message: %v", err, response.Code, response.Message),
)
return
}
}
}
listResponse, err := core.ReadCertificatesFromUpstream(ctx, r.GetClient(), data.ConnectorId.ValueString(), "connector")
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Connector Certificate Resource.",
fmt.Sprintf("%v; code: %v", err, listResponse.Code),
)
return
}
data.ReadFromResponse(ctx, listResponse)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *connectorCertificate) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
if r.GetClient() == nil {
resp.Diagnostics.AddError(
"Unconfigured Fivetran Client",
"Please report this issue to the provider developers.",
)
return
}
var data model.CertificateConnector
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
stateMap := make(map[string]string)
for _, item := range data.Certificate.Elements() {
if element, ok := item.(basetypes.ObjectValue); ok {
stateMap[element.Attributes()["hash"].(basetypes.StringValue).ValueString()] = element.Attributes()["encoded_cert"].(basetypes.StringValue).ValueString()
}
}
listResponse, err := core.ReadCertificatesFromUpstream(ctx, r.GetClient(), data.ConnectorId.ValueString(), "connector")
if err != nil {
resp.Diagnostics.AddError(
"Unable to Read Connector Certificate Resource.",
fmt.Sprintf("%v; code: %v", err, listResponse.Code),
)
return
}
data.ReadFromResponse(ctx, listResponse)
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *connectorCertificate) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
if r.GetClient() == nil {
resp.Diagnostics.AddError(
"Unconfigured Fivetran Client",
"Please report this issue to the provider developers.",
)
return
}
var plan, state model.CertificateConnector
resp.Diagnostics.Append(req.Plan.Get(ctx, &plan)...)
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
planMap := make(map[string]string)
for _, item := range plan.Certificate.Elements() {
if element, ok := item.(basetypes.ObjectValue); ok {
planMap[element.Attributes()["hash"].(basetypes.StringValue).ValueString()] = element.Attributes()["encoded_cert"].(basetypes.StringValue).ValueString()
}
}
for _, connector := range state.Certificate.Elements() {
if element, ok := connector.(basetypes.ObjectValue); ok {
hash := element.Attributes()["hash"].(basetypes.StringValue).ValueString()
if _, ok := planMap[hash]; !ok {
// no such hash in plan - revoke
if updateResponse, err := r.GetClient().NewConnectorCertificateRevoke().ConnectorID(plan.ConnectorId.ValueString()).Hash(hash).Do(ctx); err != nil {
resp.Diagnostics.AddError(
"Unable to Update Connector Certificate Resource. Failed to revoke certificate with hash "+hash,
fmt.Sprintf("%v; code: %v; message: %v", err, updateResponse.Code, updateResponse.Message),
)
return
}
} else {
// plan and state has this item - we could just remove it from plan map
delete(planMap, hash)
}
}
}
// in plan map left only new items we have to approve
for h, c := range planMap {
if updateResponse, err := r.GetClient().NewCertificateConnectorCertificateApprove().ConnectorID(plan.ConnectorId.ValueString()).Hash(h).EncodedCert(c).Do(ctx); err != nil {
resp.Diagnostics.AddError(
"Unable to Update Connector Certificate Resource. Unable to approve certificate with hash "+h,
fmt.Sprintf("%v; code: %v; message: %v", err, updateResponse.Code, updateResponse.Message),
)
return
}
}
listResponse, err := core.ReadCertificatesFromUpstream(ctx, r.GetClient(), plan.ConnectorId.ValueString(), "connector")
if err != nil {
resp.Diagnostics.AddError(
"Unable to Create Connector Certificate Resource.",
fmt.Sprintf("%v; code: %v", err, listResponse.Code),
)
return
}
plan.ReadFromResponse(ctx, listResponse)
resp.Diagnostics.Append(resp.State.Set(ctx, &plan)...)
}
func (r *connectorCertificate) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) {
if r.GetClient() == nil {
resp.Diagnostics.AddError(
"Unconfigured Fivetran Client",
"Please report this issue to the provider developers.",
)
return
}
var data model.CertificateConnector
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
hashes := []string{}
for _, items := range data.Certificate.Elements() {
if element, ok := items.(basetypes.ObjectValue); ok {
hashes = append(hashes, element.Attributes()["hash"].(basetypes.StringValue).ValueString())
}
}
response, err := core.RevokeCertificates(ctx, r.GetClient(), data.Id.ValueString(), "connector", hashes)
if err != nil {
resp.Diagnostics.AddError(
"Unable to Delete Connector Certificate Resource.",
fmt.Sprintf("%v; code: %v", err, response.Code),
)
}
}