/
resource_custom_connector_plugin.go
314 lines (272 loc) · 15 KB
/
resource_custom_connector_plugin.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
// Copyright 2021 Confluent Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package provider
import (
"context"
"encoding/json"
"fmt"
ccp "github.com/confluentinc/ccloud-sdk-go-v2/connect-custom-plugin/v1"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"net/http"
"path/filepath"
"strings"
)
const (
paramDocumentationLink = "documentation_link"
paramConnectorClass = "connector_class"
paramConnectorType = "connector_type"
paramSensitiveConfigProperties = "sensitive_config_properties"
paramFilename = "filename"
presignedUrlLocation = "PRESIGNED_URL_LOCATION"
)
func customConnectorPluginResource() *schema.Resource {
return &schema.Resource{
CreateContext: customConnectorPluginCreate,
ReadContext: customConnectorPluginRead,
UpdateContext: customConnectorPluginUpdate,
DeleteContext: customConnectorPluginDelete,
Importer: &schema.ResourceImporter{
StateContext: customConnectorPluginImport,
},
Schema: map[string]*schema.Schema{
paramDisplayName: {
Type: schema.TypeString,
Required: true,
Description: "A human-readable name for the Custom Connector Plugin.",
ValidateFunc: validation.StringIsNotEmpty,
},
paramDescription: {
Type: schema.TypeString,
Optional: true,
Description: "A free-form description of the Custom Connector Plugin.",
},
paramDocumentationLink: {
Type: schema.TypeString,
Optional: true,
Description: "A documentation link of the Custom Connector Plugin.",
},
paramConnectorClass: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Java class or alias for connector. You can get connector class from connector documentation provided by developer.",
},
paramConnectorType: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Custom Connector type.",
ValidateFunc: validation.StringInSlice([]string{"SOURCE", "SINK"}, false),
},
paramSensitiveConfigProperties: {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
Description: "A list of sensitive properties where a sensitive property is a connector configuration property that must be hidden after a user enters property value when setting up connector.",
},
paramFilename: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The path to the file that will be created.",
},
},
}
}
func customConnectorPluginUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.HasChangesExcept(paramDisplayName, paramDescription, paramDocumentationLink) {
return diag.Errorf("error updating Custom Connector Plugin %q: only %q, %q, %q attributes can be updated for Custom Connector Plugin", d.Id(), paramDisplayName, paramDescription, paramDocumentationLink)
}
updateCustomConnectorPluginRequest := ccp.NewConnectV1CustomConnectorPluginUpdate()
if d.HasChange(paramDisplayName) {
updatedDisplayName := d.Get(paramDisplayName).(string)
updateCustomConnectorPluginRequest.SetDisplayName(updatedDisplayName)
}
if d.HasChange(paramDescription) {
updatedDescription := d.Get(paramDescription).(string)
updateCustomConnectorPluginRequest.SetDescription(updatedDescription)
}
if d.HasChange(paramDocumentationLink) {
updatedDocumentationLink := d.Get(paramDocumentationLink).(string)
updateCustomConnectorPluginRequest.SetDescription(updatedDocumentationLink)
}
updateCustomConnectorPluginRequestJson, err := json.Marshal(updateCustomConnectorPluginRequest)
if err != nil {
return diag.Errorf("error updating Custom Connector Plugin %q: error marshaling %#v to json: %s", d.Id(), updateCustomConnectorPluginRequest, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Updating Custom Connector Plugin %q: %s", d.Id(), updateCustomConnectorPluginRequestJson), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
c := meta.(*Client)
updatedCustomConnectorPlugin, _, err := c.ccpClient.CustomConnectorPluginsConnectV1Api.UpdateConnectV1CustomConnectorPlugin(c.ccpApiContext(ctx), d.Id()).ConnectV1CustomConnectorPluginUpdate(*updateCustomConnectorPluginRequest).Execute()
if err != nil {
return diag.Errorf("error updating Custom Connector Plugin %q: %s", d.Id(), createDescriptiveError(err))
}
updatedCustomConnectorPluginJson, err := json.Marshal(updatedCustomConnectorPlugin)
if err != nil {
return diag.Errorf("error updating Custom Connector Plugin %q: error marshaling %#v to json: %s", d.Id(), updatedCustomConnectorPlugin, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished updating Custom Connector Plugin %q: %s", d.Id(), updatedCustomConnectorPluginJson), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
return customConnectorPluginRead(ctx, d, meta)
}
func customConnectorPluginCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
displayName := d.Get(paramDisplayName).(string)
description := d.Get(paramDescription).(string)
documentationLink := d.Get(paramDocumentationLink).(string)
connectorClass := d.Get(paramConnectorClass).(string)
connectorType := d.Get(paramConnectorType).(string)
sensitiveConfigProperties := convertToStringSlice(d.Get(paramSensitiveConfigProperties).(*schema.Set).List())
filename := d.Get(paramFilename).(string)
// Part 1: Get Upload ID
uploadID, err := uploadCustomConnectorPlugin(c.ccpApiContext(ctx), c, filename)
if err != nil {
return diag.Errorf("error creating Custom Connector Plugin: %s", createDescriptiveError(err))
}
// Part 2: Creating Custom Connector Plugin
createCustomConnectorPluginRequest := ccp.NewConnectV1CustomConnectorPlugin()
createCustomConnectorPluginRequest.SetDisplayName(displayName)
createCustomConnectorPluginRequest.SetDescription(description)
createCustomConnectorPluginRequest.SetDocumentationLink(documentationLink)
createCustomConnectorPluginRequest.SetConnectorClass(connectorClass)
createCustomConnectorPluginRequest.SetConnectorType(connectorType)
createCustomConnectorPluginRequest.SetSensitiveConfigProperties(sensitiveConfigProperties)
createCustomConnectorPluginRequest.SetUploadSource(ccp.ConnectV1CustomConnectorPluginUploadSourceOneOf{
ConnectV1UploadSourcePresignedUrl: ccp.NewConnectV1UploadSourcePresignedUrl(presignedUrlLocation, uploadID),
})
createCustomConnectorPluginRequestJson, err := json.Marshal(createCustomConnectorPluginRequest)
if err != nil {
return diag.Errorf("error creating Custom Connector Plugin: error marshaling %#v to json: %s", createCustomConnectorPluginRequest, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Creating new Custom Connector Plugin: %s", createCustomConnectorPluginRequestJson))
createdCustomConnectorPlugin, _, err := executeCustomConnectorPluginCreate(c.ccpApiContext(ctx), c, createCustomConnectorPluginRequest)
if err != nil {
return diag.Errorf("error creating Custom Connector Plugin %q: %s", displayName, createDescriptiveError(err))
}
d.SetId(createdCustomConnectorPlugin.GetId())
createdCustomConnectorPluginJson, err := json.Marshal(createdCustomConnectorPlugin)
if err != nil {
return diag.Errorf("error creating Custom Connector Plugin %q: error marshaling %#v to json: %s", d.Id(), createdCustomConnectorPlugin, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished creating Custom Connector Plugin %q: %s", d.Id(), createdCustomConnectorPluginJson), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
return customConnectorPluginRead(ctx, d, meta)
}
// https://github.com/confluentinc/cli/blob/main/internal/connect/command_custom_plugin_create.go#L78
func uploadCustomConnectorPlugin(ctx context.Context, c *Client, filename string) (string, error) {
extension := strings.ToLower(strings.TrimPrefix(filepath.Ext(filename), "."))
if extension != "zip" && extension != "jar" {
return "", fmt.Errorf(`error uploading Custom Connector Plugin: only file extensions ".jar" and ".zip" are allowed`)
}
createPresignedUrlRequest := *ccp.NewConnectV1PresignedUrlRequest()
createPresignedUrlRequest.SetContentFormat(extension)
createdPresignedUrl, _, err := c.ccpClient.PresignedUrlsConnectV1Api.PresignedUploadUrlConnectV1PresignedUrl(c.ccpApiContext(ctx)).ConnectV1PresignedUrlRequest(createPresignedUrlRequest).Execute()
if err != nil {
return "", fmt.Errorf(`error uploading Custom Connector Plugin: error fetching presigned upload URL: %s`, err)
}
if err := uploadFile(createdPresignedUrl.GetUploadUrl(), filename, createdPresignedUrl.GetUploadFormData()); err != nil {
return "", fmt.Errorf(`error uploading Custom Connector Plugin: error uploading a file: %s`, err)
}
return createdPresignedUrl.GetUploadId(), nil
}
func executeCustomConnectorPluginCreate(ctx context.Context, c *Client, customConnectorPlugin *ccp.ConnectV1CustomConnectorPlugin) (ccp.ConnectV1CustomConnectorPlugin, *http.Response, error) {
req := c.ccpClient.CustomConnectorPluginsConnectV1Api.CreateConnectV1CustomConnectorPlugin(c.ccpApiContext(ctx)).ConnectV1CustomConnectorPlugin(*customConnectorPlugin)
return req.Execute()
}
func customConnectorPluginDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, fmt.Sprintf("Deleting Custom Connector Plugin %q", d.Id()), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
c := meta.(*Client)
req := c.ccpClient.CustomConnectorPluginsConnectV1Api.DeleteConnectV1CustomConnectorPlugin(c.ccpApiContext(ctx), d.Id())
_, err := req.Execute()
if err != nil {
return diag.Errorf("error deleting Custom Connector Plugin %q: %s", d.Id(), createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished deleting Custom Connector Plugin %q", d.Id()), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
return nil
}
func executeCustomConnectorPluginRead(ctx context.Context, c *Client, customConnectorPluginId string) (ccp.ConnectV1CustomConnectorPlugin, *http.Response, error) {
req := c.ccpClient.CustomConnectorPluginsConnectV1Api.GetConnectV1CustomConnectorPlugin(c.ccpApiContext(ctx), customConnectorPluginId)
return req.Execute()
}
func customConnectorPluginRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, fmt.Sprintf("Reading Custom Connector Plugin %q", d.Id()), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
c := meta.(*Client)
filename := d.Get(paramFilename).(string)
_, err := readCustomConnectorPluginAndSetAttributes(ctx, d, c, filename)
if err != nil {
return diag.Errorf("error reading Custom Connector Plugin %q: %s", d.Id(), createDescriptiveError(err))
}
return nil
}
func readCustomConnectorPluginAndSetAttributes(ctx context.Context, d *schema.ResourceData, c *Client, filename string) ([]*schema.ResourceData, error) {
customConnectorPlugin, resp, err := executeCustomConnectorPluginRead(c.ccpApiContext(ctx), c, d.Id())
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("Error reading Custom Connector Plugin %q: %s", d.Id(), createDescriptiveError(err)), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
isResourceNotFound := isNonKafkaRestApiResourceNotFound(resp)
if isResourceNotFound && !d.IsNewResource() {
tflog.Warn(ctx, fmt.Sprintf("Removing Custom Connector Plugin %q in TF state because Custom Connector Plugin could not be found on the server", d.Id()), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
d.SetId("")
return nil, nil
}
return nil, createDescriptiveError(err)
}
customConnectorPluginJson, err := json.Marshal(customConnectorPlugin)
if err != nil {
return nil, fmt.Errorf("error reading Custom Connector Plugin %q: error marshaling %#v to json: %s", d.Id(), customConnectorPlugin, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Fetched Custom Connector Plugin %q: %s", d.Id(), customConnectorPluginJson), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
if _, err := setCustomConnectorPluginAttributes(d, customConnectorPlugin, filename); err != nil {
return nil, createDescriptiveError(err)
}
tflog.Debug(ctx, fmt.Sprintf("Finished reading Custom Connector Plugin %q", d.Id()), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
return []*schema.ResourceData{d}, nil
}
func setCustomConnectorPluginAttributes(d *schema.ResourceData, customConnectorPlugin ccp.ConnectV1CustomConnectorPlugin, filename string) (*schema.ResourceData, error) {
if err := d.Set(paramDisplayName, customConnectorPlugin.GetDisplayName()); err != nil {
return nil, createDescriptiveError(err)
}
if err := d.Set(paramDescription, customConnectorPlugin.GetDescription()); err != nil {
return nil, createDescriptiveError(err)
}
if err := d.Set(paramDocumentationLink, customConnectorPlugin.GetDocumentationLink()); err != nil {
return nil, createDescriptiveError(err)
}
if err := d.Set(paramConnectorClass, customConnectorPlugin.GetConnectorClass()); err != nil {
return nil, createDescriptiveError(err)
}
if err := d.Set(paramConnectorType, customConnectorPlugin.GetConnectorType()); err != nil {
return nil, createDescriptiveError(err)
}
if err := d.Set(paramSensitiveConfigProperties, customConnectorPlugin.GetSensitiveConfigProperties()); err != nil {
return nil, createDescriptiveError(err)
}
if err := d.Set(paramFilename, filename); err != nil {
return nil, createDescriptiveError(err)
}
d.SetId(customConnectorPlugin.GetId())
return d, nil
}
func customConnectorPluginImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
tflog.Debug(ctx, fmt.Sprintf("Importing Custom Connector Plugin %q", d.Id()), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
filename := getEnv("IMPORT_CUSTOM_CONNECTOR_PLUGIN_FILENAME", "")
// Mark resource as new to avoid d.Set("") when getting 404
d.MarkNewResource()
if _, err := readCustomConnectorPluginAndSetAttributes(ctx, d, meta.(*Client), filename); err != nil {
return nil, fmt.Errorf("error importing Custom Connector Plugin %q: %s", d.Id(), createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished importing Custom Connector Plugin %q", d.Id()), map[string]interface{}{customConnectorPluginLoggingKey: d.Id()})
return []*schema.ResourceData{d}, nil
}