/
resource_private_link_attachment_connection.go
378 lines (321 loc) · 17.3 KB
/
resource_private_link_attachment_connection.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
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright 2023 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"
netpl "github.com/confluentinc/ccloud-sdk-go-v2/networking-privatelink/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"
"net/http"
"strings"
)
const (
paramVpcEndpointId = "vpc_endpoint_id"
paramPrivateEndpointResourceId = "private_endpoint_resource_id"
paramPrivateServiceConnectConnectionId = "private_service_connect_connection_id"
paramPrivateLinkAttachment = "private_link_attachment"
paramAwsPrivateLinkAttachmentConnection = "AwsPrivateLinkAttachmentConnection"
paramAzurePrivateLinkAttachmentConnection = "AzurePrivateLinkAttachmentConnection"
paramGcpPrivateLinkAttachmentConnection = "GcpPrivateLinkAttachmentConnection"
)
var acceptedPrivateLinkAttachmentConnectionKinds = []string{paramAws, paramAzure, paramGcp}
func privateLinkAttachmentConnectionResource() *schema.Resource {
return &schema.Resource{
ReadContext: privateLinkAttachmentConnectionRead,
CreateContext: privateLinkAttachmentConnectionCreate,
DeleteContext: privateLinkAttachmentConnectionDelete,
UpdateContext: privateLinkAttachmentConnectionUpdate,
Importer: &schema.ResourceImporter{
StateContext: privateLinkAttachmentConnectionImport,
},
Schema: map[string]*schema.Schema{
paramDisplayName: {
Type: schema.TypeString,
Description: "The name of the Private Link Attachment Connection.",
Optional: true,
Computed: true,
},
paramResourceName: {
Type: schema.TypeString,
Computed: true,
},
paramEnvironment: environmentSchema(),
paramPrivateLinkAttachment: privateLinkAttachmentSchema(),
paramAws: awsPlattcSchema(),
paramAzure: azurePlattcSchema(),
paramGcp: gcpPlattcSchema(),
},
}
}
func awsPlattcSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramVpcEndpointId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Id of a VPC Endpoint that is connected to the VPC Endpoint service.",
},
},
},
ExactlyOneOf: acceptedPrivateLinkAttachmentConnectionKinds,
}
}
func azurePlattcSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramPrivateEndpointResourceId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Resource Id of the PrivateEndpoint that is connected to the PrivateLink service.",
},
},
},
ExactlyOneOf: acceptedPrivateLinkAttachmentConnectionKinds,
}
}
func gcpPlattcSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramPrivateServiceConnectConnectionId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Id of the Private Service connection.",
},
},
},
ExactlyOneOf: acceptedPrivateLinkAttachmentConnectionKinds,
}
}
func privateLinkAttachmentSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramId: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The unique identifier for the private link attachment.",
},
},
},
Required: true,
MinItems: 1,
MaxItems: 1,
ForceNew: true,
Description: "The private_link_attachment to which this belongs.",
}
}
func privateLinkAttachmentConnectionRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
plattcId := d.Id()
if plattcId == "" {
return diag.Errorf("error reading Private Link Attachment Connection: Private Link Attachment Connection id is missing")
}
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
if _, err := readPrivateLinkAttachmentConnectionAndSetAttributes(ctx, d, meta, plattcId, environmentId); err != nil {
return diag.FromErr(fmt.Errorf("error reading Private Link Attachment Connection %q: %s", plattcId, createDescriptiveError(err)))
}
return nil
}
func readPrivateLinkAttachmentConnectionAndSetAttributes(ctx context.Context, d *schema.ResourceData, meta interface{}, plattcId string, environmentId string) ([]*schema.ResourceData, error) {
tflog.Debug(ctx, fmt.Sprintf("Reading Private Link Attachment Connection %q=%q", paramId, plattcId), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: plattcId})
c := meta.(*Client)
plattc, resp, err := executePlattcRead(c.netPLApiContext(ctx), c, plattcId, environmentId)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("Error reading Private Link Attachment Connection %q: %s", plattcId, createDescriptiveError(err)), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: plattcId})
isResourceNotFound := isNonKafkaRestApiResourceNotFound(resp)
if isResourceNotFound && !d.IsNewResource() {
tflog.Warn(ctx, fmt.Sprintf("Removing Private Link Attachment Connection %q in TF state because Private Link Attachment Connection could not be found on the server", plattcId), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: plattcId})
d.SetId("")
return nil, nil
}
return nil, err
}
plattcJson, err := json.Marshal(plattc)
if err != nil {
return nil, fmt.Errorf("error reading Private Link Attachment Connection %q: error marshaling %#v to json: %s", plattcId, plattc, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Fetched Private Link Attachment Connection %q: %s", plattcId, plattcJson), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: plattcId})
if _, err := setPrivateLinkAttachmentConnectionAttributes(d, plattc); err != nil {
return nil, createDescriptiveError(err)
}
tflog.Debug(ctx, fmt.Sprintf("Finished reading Private Link Attachment Connection %q", plattcId), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: plattcId})
return []*schema.ResourceData{d}, nil
}
func executePlattcRead(ctx context.Context, c *Client, plattcId string, environmentId string) (netpl.NetworkingV1PrivateLinkAttachmentConnection, *http.Response, error) {
request := c.netPLClient.PrivateLinkAttachmentConnectionsNetworkingV1Api.GetNetworkingV1PrivateLinkAttachmentConnection(c.netPLApiContext(ctx), plattcId).Environment(environmentId)
return c.netPLClient.PrivateLinkAttachmentConnectionsNetworkingV1Api.GetNetworkingV1PrivateLinkAttachmentConnectionExecute(request)
}
func privateLinkAttachmentConnectionCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
spec := netpl.NewNetworkingV1PrivateLinkAttachmentConnectionSpec()
displayName := d.Get(paramDisplayName).(string)
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
plattId := extractStringValueFromBlock(d, paramPrivateLinkAttachment, paramId)
spec.SetDisplayName(displayName)
spec.SetEnvironment(netpl.ObjectReference{Id: environmentId})
spec.SetPrivateLinkAttachment(netpl.ObjectReference{Id: plattId})
vpcEndpointId := extractStringValueFromBlock(d, paramAws, paramVpcEndpointId)
privateEndpointResourceId := extractStringValueFromBlock(d, paramAzure, paramPrivateEndpointResourceId)
privateServiceConnectConnectionId := extractStringValueFromBlock(d, paramGcp, paramPrivateServiceConnectConnectionId)
if vpcEndpointId != "" {
spec.SetCloud(netpl.NetworkingV1PrivateLinkAttachmentConnectionSpecCloudOneOf{NetworkingV1AwsPrivateLinkAttachmentConnection: netpl.NewNetworkingV1AwsPrivateLinkAttachmentConnection(paramAwsPrivateLinkAttachmentConnection, vpcEndpointId)})
} else if privateEndpointResourceId != "" {
spec.SetCloud(netpl.NetworkingV1PrivateLinkAttachmentConnectionSpecCloudOneOf{NetworkingV1AzurePrivateLinkAttachmentConnection: netpl.NewNetworkingV1AzurePrivateLinkAttachmentConnection(paramAzurePrivateLinkAttachmentConnection, privateEndpointResourceId)})
} else if privateServiceConnectConnectionId != "" {
spec.SetCloud(netpl.NetworkingV1PrivateLinkAttachmentConnectionSpecCloudOneOf{NetworkingV1GcpPrivateLinkAttachmentConnection: netpl.NewNetworkingV1GcpPrivateLinkAttachmentConnection(paramGcpPrivateLinkAttachmentConnection, privateServiceConnectConnectionId)})
}
plattc := netpl.NewNetworkingV1PrivateLinkAttachmentConnection()
plattc.SetSpec(*spec)
c := meta.(*Client)
request := c.netPLClient.PrivateLinkAttachmentConnectionsNetworkingV1Api.CreateNetworkingV1PrivateLinkAttachmentConnection(c.netPLApiContext(ctx))
request = request.NetworkingV1PrivateLinkAttachmentConnection(*plattc)
createPrivateLinkAttachmentConnectionRequestJson, err := json.Marshal(request)
if err != nil {
return diag.Errorf("error creating Private Link Attachment Connection: error marshaling %#v to json: %s", request, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Creating new Private Link Attachment Connection: %s", createPrivateLinkAttachmentConnectionRequestJson))
createdPlattc, _, err := c.netPLClient.PrivateLinkAttachmentConnectionsNetworkingV1Api.CreateNetworkingV1PrivateLinkAttachmentConnectionExecute(request)
if err != nil {
return diag.Errorf("error creating Private Link Attachment Connection %s", createDescriptiveError(err))
}
plattcId := createdPlattc.GetId()
d.SetId(plattcId)
if err := waitForPrivateLinkAttachmentConnectionToProvision(c.netPLApiContext(ctx), c, environmentId, d.Id()); err != nil {
return diag.Errorf("error waiting for Private Link Attachment Connection %q to provision: %s", plattcId, createDescriptiveError(err))
}
createdPrivateLinkAttachmentConnectionJson, err := json.Marshal(createdPlattc)
if err != nil {
return diag.Errorf("error creating Private Link Attachment Connection %q: error marshaling %#v to json: %s", plattcId, createdPlattc, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished creating Private Link Attachment Connection %q: %s", plattcId, createdPrivateLinkAttachmentConnectionJson), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: plattcId})
return privateLinkAttachmentConnectionRead(ctx, d, meta)
}
func privateLinkAttachmentConnectionDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
plattcId := d.Id()
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
tflog.Debug(ctx, fmt.Sprintf("deleting Private Link Attachment Connection %q=%q", paramId, plattcId), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: plattcId})
c := meta.(*Client)
request := c.netPLClient.PrivateLinkAttachmentConnectionsNetworkingV1Api.DeleteNetworkingV1PrivateLinkAttachmentConnection(c.netPLApiContext(ctx), plattcId).Environment(environmentId)
_, err := c.netPLClient.PrivateLinkAttachmentConnectionsNetworkingV1Api.DeleteNetworkingV1PrivateLinkAttachmentConnectionExecute(request)
if err != nil {
return diag.Errorf("error deleting Private Link Attachment Connection %q: %s", plattcId, createDescriptiveError(err))
}
if err := waitForPrivateLinkAttachmentConnectionToBeDeleted(c.netApiContext(ctx), c, environmentId, d.Id()); err != nil {
return diag.Errorf("error waiting for Private Link Attachment Connection %q to be deleted: %s", d.Id(), createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished deleting Private Link Attachment Connection %q", plattcId), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: plattcId})
return nil
}
func privateLinkAttachmentConnectionUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.HasChangeExcept(paramDisplayName) {
return diag.Errorf("error updating Private Link Attachment Connection %q: only %q attribute can be updated for Private Link Attachment Connection", d.Id(), paramDisplayName)
}
spec := netpl.NewNetworkingV1PrivateLinkAttachmentConnectionSpecUpdate()
plattcId := d.Id()
displayName := d.Get(paramDisplayName).(string)
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
spec.SetDisplayName(displayName)
spec.SetEnvironment(netpl.ObjectReference{Id: environmentId})
plattc := netpl.NewNetworkingV1PrivateLinkAttachmentConnectionUpdate()
plattc.SetSpec(*spec)
c := meta.(*Client)
request := c.netPLClient.PrivateLinkAttachmentConnectionsNetworkingV1Api.UpdateNetworkingV1PrivateLinkAttachmentConnection(c.netPLApiContext(ctx), plattcId)
request = request.NetworkingV1PrivateLinkAttachmentConnectionUpdate(*plattc)
updatePrivateLinkAttachmentConnectionRequestJson, err := json.Marshal(request)
if err != nil {
return diag.Errorf("error updating Private Link Attachment Connection: error marshaling %#v to json: %s", request, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Updating new Private Link Attachment Connection: %s", updatePrivateLinkAttachmentConnectionRequestJson))
updatedPlattc, _, err := c.netPLClient.PrivateLinkAttachmentConnectionsNetworkingV1Api.UpdateNetworkingV1PrivateLinkAttachmentConnectionExecute(request)
if err != nil {
return diag.Errorf("error updating Private Link Attachment Connection, %s", createDescriptiveError(err))
}
updatedPrivateLinkAttachmentConnectionJson, err := json.Marshal(updatedPlattc)
if err != nil {
return diag.Errorf("error updating Private Link Attachment Connection %q: error marshaling %#v to json: %s", plattcId, updatedPlattc, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished updating Private Link Attachment Connection %q: %s", plattcId, updatedPrivateLinkAttachmentConnectionJson), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: plattcId})
return privateLinkAttachmentConnectionRead(ctx, d, meta)
}
func privateLinkAttachmentConnectionImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
tflog.Debug(ctx, fmt.Sprintf("Importing Private Link Attachment Connection %q", d.Id()), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: d.Id()})
envIDAndPlattcId := d.Id()
parts := strings.Split(envIDAndPlattcId, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("error importing Private Link Attachment Connection: invalid format: expected '<env ID>/<Private Link Attachment Connection ID>'")
}
environmentId := parts[0]
plattcId := parts[1]
d.SetId(plattcId)
// Mark resource as new to avoid d.Set("") when getting 404
d.MarkNewResource()
if _, err := readPrivateLinkAttachmentConnectionAndSetAttributes(ctx, d, meta, plattcId, environmentId); err != nil {
return nil, fmt.Errorf("error importing Private Link Attachment Connection %q: %s", d.Id(), err)
}
tflog.Debug(ctx, fmt.Sprintf("Finished importing Private Link Attachment Connection %q", d.Id()), map[string]interface{}{privateLinkAttachmentConnectionLoggingKey: d.Id()})
return []*schema.ResourceData{d}, nil
}
func setPrivateLinkAttachmentConnectionAttributes(d *schema.ResourceData, plattc netpl.NetworkingV1PrivateLinkAttachmentConnection) (*schema.ResourceData, error) {
if err := d.Set(paramDisplayName, plattc.Spec.GetDisplayName()); err != nil {
return nil, err
}
if err := setStringAttributeInListBlockOfSizeOne(paramEnvironment, paramId, plattc.Spec.Environment.GetId(), d); err != nil {
return nil, err
}
if err := setStringAttributeInListBlockOfSizeOne(paramPrivateLinkAttachment, paramId, plattc.Spec.PrivateLinkAttachment.GetId(), d); err != nil {
return nil, err
}
if err := d.Set(paramResourceName, plattc.Metadata.GetResourceName()); err != nil {
return nil, err
}
if plattc.Spec.Cloud.NetworkingV1AwsPrivateLinkAttachmentConnection != nil {
if err := setStringAttributeInListBlockOfSizeOne(paramAws, paramVpcEndpointId, plattc.Spec.Cloud.NetworkingV1AwsPrivateLinkAttachmentConnection.GetVpcEndpointId(), d); err != nil {
return nil, err
}
} else if plattc.Spec.Cloud.NetworkingV1AzurePrivateLinkAttachmentConnection != nil {
if err := setStringAttributeInListBlockOfSizeOne(paramAzure, paramPrivateEndpointResourceId, plattc.Spec.Cloud.NetworkingV1AzurePrivateLinkAttachmentConnection.GetPrivateEndpointResourceId(), d); err != nil {
return nil, err
}
} else if plattc.Spec.Cloud.NetworkingV1GcpPrivateLinkAttachmentConnection != nil {
if err := setStringAttributeInListBlockOfSizeOne(paramGcp, paramPrivateServiceConnectConnectionId, plattc.Spec.Cloud.NetworkingV1GcpPrivateLinkAttachmentConnection.GetPrivateServiceConnectConnectionId(), d); err != nil {
return nil, err
}
}
d.SetId(plattc.GetId())
return d, nil
}