-
Notifications
You must be signed in to change notification settings - Fork 0
/
destination.go
239 lines (200 loc) · 7.51 KB
/
destination.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
package resource
import (
"context"
"encoding/json"
"fmt"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework-jsontypes/jsontypes"
"github.com/hashicorp/terraform-plugin-framework/path"
res "github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/jinzhu/copier"
"github.com/streamkap-com/terraform-provider-streamkap/internal/api"
)
// Ensure provider defined types fully satisfy framework interfaces.
var (
_ res.Resource = &Destination{}
_ res.ResourceWithImportState = &Destination{}
)
func NewDestinationResource() res.Resource {
return &Destination{}
}
// Destination defines the resource implementation.
type Destination struct {
client api.StreamkapAPI
}
// DestinationModel describes the resource data model.
type DestinationModel struct {
ID basetypes.StringValue `json:"id" tfsdk:"id"`
Name *string `json:"name" tfsdk:"name"`
Connector *string `json:"connector" tfsdk:"connector"`
Config jsontypes.Exact `json:"config" tfsdk:"config"`
}
func (r *Destination) Metadata(ctx context.Context, req res.MetadataRequest, resp *res.MetadataResponse) {
resp.TypeName = req.ProviderTypeName + "_destination"
}
func (r *Destination) Schema(ctx context.Context, req res.SchemaRequest, resp *res.SchemaResponse) {
resp.Schema = schema.Schema{
// This description is used by the documentation generator and the language server.
MarkdownDescription: "Destination resource",
Attributes: map[string]schema.Attribute{
"id": schema.StringAttribute{
Computed: true,
MarkdownDescription: "Destination identifier",
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
"name": schema.StringAttribute{
Required: true,
MarkdownDescription: "Destination name",
},
"connector": schema.StringAttribute{
Required: true,
MarkdownDescription: "Destination connector",
},
"config": schema.StringAttribute{
Required: true,
MarkdownDescription: "Destination config",
CustomType: jsontypes.ExactType{},
},
},
}
}
func (r *Destination) Configure(ctx context.Context, req res.ConfigureRequest, resp *res.ConfigureResponse) {
// Prevent panic if the provider has not been configured.
if req.ProviderData == nil {
return
}
client, ok := req.ProviderData.(api.StreamkapAPI)
if !ok {
resp.Diagnostics.AddError(
"Unexpected Destination Configure Type",
fmt.Sprintf("Expected api.StreamkapAPI, got: %T. Please report this issue to the provider developers.", req.ProviderData),
)
return
}
r.client = client
}
func (r *Destination) Create(ctx context.Context, req res.CreateRequest, resp *res.CreateResponse) {
var data DestinationModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
var config map[string]interface{}
data.Config.Unmarshal(&config)
destination, err := r.client.CreateDestination(ctx, api.CreateDestinationRequest{
Name: data.Name,
Connector: data.Connector,
Config: config,
})
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to create destination, got error: %s", err))
return
}
data.ID = types.StringValue(destination.ID)
data.Name = &destination.Name
data.Connector = &destination.Connector
sourceString, err := json.Marshal(config)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to parse destination, got error: %s", err))
return
}
data.Config = jsontypes.NewExactValue(string(sourceString))
tflog.Trace(ctx, "created a resource")
// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *Destination) Read(ctx context.Context, req res.ReadRequest, resp *res.ReadResponse) {
var data SourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
destination, err := r.client.GetDestination(ctx, data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to read destination, got error: %s", err))
return
}
if destination != nil {
sourceString, err := json.Marshal(destination[0])
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to parse destination, got error: %s", err))
return
}
copier.CopyWithOption(&data, &destination[0], copier.Option{DeepCopy: true})
data.Config = jsontypes.NewExactValue(string(sourceString))
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *Destination) Update(ctx context.Context, req res.UpdateRequest, resp *res.UpdateResponse) {
var data SourceModel
// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
destination, err := r.client.GetDestination(ctx, data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get destination, got error: %s", err))
return
}
if len(destination) == 0 {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to get destination, got error: %s", err))
return
}
var currentInstance api.Source
copier.CopyWithOption(¤tInstance, &destination[0], copier.Option{DeepCopy: true})
json.Unmarshal([]byte(data.Config.String()), ¤tInstance.Config)
diff := cmp.Diff(destination[0], currentInstance, cmp.AllowUnexported())
if diff != "" {
var config map[string]interface{}
data.Config.Unmarshal(&config)
fmt.Println("config", config)
updatedSource, err := r.client.UpdateDestination(ctx, api.CreateDestinationRequest{
Name: data.Name,
Connector: data.Connector,
Config: config,
ID: data.ID.ValueString(),
})
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to update destination, got error: %s", err))
return
}
sourceString, err := json.Marshal(updatedSource.Config)
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to parse source, got error: %s", err))
return
}
copier.CopyWithOption(&data, &updatedSource, copier.Option{DeepCopy: true})
data.Config = jsontypes.NewExactValue(string(sourceString))
}
// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
}
func (r *Destination) Delete(ctx context.Context, req res.DeleteRequest, resp *res.DeleteResponse) {
var data SourceModel
// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &data)...)
if resp.Diagnostics.HasError() {
return
}
err := r.client.DeleteDestination(ctx, data.ID.ValueString())
if err != nil {
resp.Diagnostics.AddError("Client Error", fmt.Sprintf("Unable to delete destination, got error: %s", err))
return
}
tflog.Trace(ctx, "deleted a resource")
}
func (r *Destination) ImportState(ctx context.Context, req res.ImportStateRequest, resp *res.ImportStateResponse) {
res.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}