-
Notifications
You must be signed in to change notification settings - Fork 62
/
resource_peering.go
441 lines (393 loc) · 17.4 KB
/
resource_peering.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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
// Copyright 2022 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"
net "github.com/confluentinc/ccloud-sdk-go-v2/networking/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"
"regexp"
"strings"
)
const (
paramGcp = "gcp"
paramVpc = "vpc"
paramRoutes = "routes"
paramCustomerRegion = "customer_region"
paramTenant = "tenant"
paramVnet = "vnet"
paramProject = "project"
paramVpcNetwork = "vpc_network"
paramImportCustomRoutes = "import_custom_routes"
awsPeeringKind = "AwsPeering"
azurePeeringKind = "AzurePeering"
gcpPeeringKind = "GcpPeering"
)
var acceptedCloudProvidersForPeering = []string{paramAws, paramAzure, paramGcp}
var paramAwsAccount = fmt.Sprintf("%s.0.%s", paramAws, paramAccount)
var paramAwsVpc = fmt.Sprintf("%s.0.%s", paramAws, paramVpc)
var paramAwsRoutes = fmt.Sprintf("%s.0.%s", paramAws, paramRoutes)
var paramAwsCustomerRegion = fmt.Sprintf("%s.0.%s", paramAws, paramCustomerRegion)
var paramAzureTenant = fmt.Sprintf("%s.0.%s", paramAzure, paramTenant)
var paramAzureVnet = fmt.Sprintf("%s.0.%s", paramAzure, paramVnet)
var paramAzureCustomerRegion = fmt.Sprintf("%s.0.%s", paramAzure, paramCustomerRegion)
var paramGcpProject = fmt.Sprintf("%s.0.%s", paramGcp, paramProject)
var paramGcpVpcNetwork = fmt.Sprintf("%s.0.%s", paramGcp, paramVpcNetwork)
var paramGcpImportCustomRoutes = fmt.Sprintf("%s.0.%s", paramGcp, paramImportCustomRoutes)
func peeringResource() *schema.Resource {
return &schema.Resource{
CreateContext: peeringCreate,
ReadContext: peeringRead,
UpdateContext: peeringUpdate,
DeleteContext: peeringDelete,
Importer: &schema.ResourceImporter{
StateContext: peeringImport,
},
Schema: map[string]*schema.Schema{
paramDisplayName: {
Type: schema.TypeString,
Description: "The name of the Peering.",
Optional: true,
Computed: true,
},
paramAws: awsPeeringSchema(),
paramAzure: azurePeeringSchema(),
paramGcp: gcpPeeringSchema(),
paramNetwork: requiredNetworkSchema(),
paramEnvironment: environmentSchema(),
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(networkingAPICreateTimeout),
Delete: schema.DefaultTimeout(networkingAPIDeleteTimeout),
},
}
}
func peeringCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
c := meta.(*Client)
displayName := d.Get(paramDisplayName).(string)
networkId := extractStringValueFromBlock(d, paramNetwork, paramId)
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
isAwsPeering := len(d.Get(paramAws).([]interface{})) > 0
isAzurePeering := len(d.Get(paramAzure).([]interface{})) > 0
isGcpPeering := len(d.Get(paramGcp).([]interface{})) > 0
spec := net.NewNetworkingV1PeeringSpec()
if displayName != "" {
spec.SetDisplayName(displayName)
}
if isAwsPeering {
account := d.Get(paramAwsAccount).(string)
vpc := d.Get(paramAwsVpc).(string)
routes := convertToStringSlice(d.Get(paramAwsRoutes).([]interface{}))
customerRegion := d.Get(paramAwsCustomerRegion).(string)
spec.SetCloud(net.NetworkingV1PeeringSpecCloudOneOf{NetworkingV1AwsPeering: net.NewNetworkingV1AwsPeering(awsPeeringKind, account, vpc, routes, customerRegion)})
} else if isAzurePeering {
tenant := d.Get(paramAzureTenant).(string)
vnet := d.Get(paramAzureVnet).(string)
customerRegion := d.Get(paramAzureCustomerRegion).(string)
spec.SetCloud(net.NetworkingV1PeeringSpecCloudOneOf{NetworkingV1AzurePeering: net.NewNetworkingV1AzurePeering(azurePeeringKind, tenant, vnet, customerRegion)})
} else if isGcpPeering {
project := d.Get(paramGcpProject).(string)
vpcNetwork := d.Get(paramGcpVpcNetwork).(string)
importCustomerRoutes := d.Get(paramGcpImportCustomRoutes).(bool)
gcpPeering := net.NewNetworkingV1GcpPeering(gcpPeeringKind, project, vpcNetwork)
gcpPeering.SetImportCustomRoutes(importCustomerRoutes)
spec.SetCloud(net.NetworkingV1PeeringSpecCloudOneOf{NetworkingV1GcpPeering: gcpPeering})
} else {
return diag.Errorf("None of %q, %q, %q blocks was provided for confluent_peering resource", paramAws, paramAzure, paramGcp)
}
spec.SetNetwork(net.ObjectReference{Id: networkId})
spec.SetEnvironment(net.ObjectReference{Id: environmentId})
createPeeringRequest := net.NetworkingV1Peering{Spec: spec}
createPeeringRequestJson, err := json.Marshal(createPeeringRequest)
if err != nil {
return diag.Errorf("error creating Peering: error marshaling %#v to json: %s", createPeeringRequest, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Creating new Peering: %s", createPeeringRequestJson))
createdPeering, _, err := executePeeringCreate(c.netApiContext(ctx), c, createPeeringRequest)
if err != nil {
return diag.Errorf("error creating Peering %q: %s", createdPeering.GetId(), createDescriptiveError(err))
}
d.SetId(createdPeering.GetId())
if err := waitForPeeringToProvision(c.netApiContext(ctx), c, environmentId, d.Id()); err != nil {
return diag.Errorf("error waiting for Peering %q to provision: %s", d.Id(), createDescriptiveError(err))
}
createdPeeringJson, err := json.Marshal(createdPeering)
if err != nil {
return diag.Errorf("error creating Peering %q: error marshaling %#v to json: %s", d.Id(), createdPeering, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished creating Peering %q: %s", d.Id(), createdPeeringJson), map[string]interface{}{peeringLoggingKey: d.Id()})
return peeringRead(ctx, d, meta)
}
func executePeeringCreate(ctx context.Context, c *Client, peering net.NetworkingV1Peering) (net.NetworkingV1Peering, *http.Response, error) {
req := c.netClient.PeeringsNetworkingV1Api.CreateNetworkingV1Peering(c.netApiContext(ctx)).NetworkingV1Peering(peering)
return req.Execute()
}
func executePeeringRead(ctx context.Context, c *Client, environmentId string, peeringId string) (net.NetworkingV1Peering, *http.Response, error) {
req := c.netClient.PeeringsNetworkingV1Api.GetNetworkingV1Peering(c.netApiContext(ctx), peeringId).Environment(environmentId)
return req.Execute()
}
func peeringRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, fmt.Sprintf("Reading Peering %q", d.Id()), map[string]interface{}{peeringLoggingKey: d.Id()})
peeringId := d.Id()
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
if _, err := readPeeringAndSetAttributes(ctx, d, meta, environmentId, peeringId); err != nil {
return diag.FromErr(fmt.Errorf("error reading Peering %q: %s", d.Id(), createDescriptiveError(err)))
}
return nil
}
func readPeeringAndSetAttributes(ctx context.Context, d *schema.ResourceData, meta interface{}, environmentId, peeringId string) ([]*schema.ResourceData, error) {
c := meta.(*Client)
peering, resp, err := executePeeringRead(c.netApiContext(ctx), c, environmentId, peeringId)
if err != nil {
tflog.Warn(ctx, fmt.Sprintf("Error reading Peering %q: %s", d.Id(), createDescriptiveError(err)), map[string]interface{}{peeringLoggingKey: d.Id()})
isResourceNotFound := isNonKafkaRestApiResourceNotFound(resp)
if isResourceNotFound && !d.IsNewResource() {
tflog.Warn(ctx, fmt.Sprintf("Removing Peering %q in TF state because Peering could not be found on the server", d.Id()), map[string]interface{}{peeringLoggingKey: d.Id()})
d.SetId("")
return nil, nil
}
return nil, err
}
peeringJson, err := json.Marshal(peering)
if err != nil {
return nil, fmt.Errorf("error reading Peering %q: error marshaling %#v to json: %s", peeringId, peering, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Fetched Peering %q: %s", d.Id(), peeringJson), map[string]interface{}{peeringLoggingKey: d.Id()})
if _, err := setPeeringAttributes(d, peering); err != nil {
return nil, createDescriptiveError(err)
}
tflog.Debug(ctx, fmt.Sprintf("Finished reading Peering %q", d.Id()), map[string]interface{}{peeringLoggingKey: d.Id()})
return []*schema.ResourceData{d}, nil
}
func setPeeringAttributes(d *schema.ResourceData, peering net.NetworkingV1Peering) (*schema.ResourceData, error) {
if err := d.Set(paramDisplayName, peering.Spec.GetDisplayName()); err != nil {
return nil, err
}
if peering.Spec.Cloud.NetworkingV1AwsPeering != nil {
if err := d.Set(paramAws, []interface{}{map[string]interface{}{
paramAccount: peering.Spec.Cloud.NetworkingV1AwsPeering.GetAccount(),
paramVpc: peering.Spec.Cloud.NetworkingV1AwsPeering.GetVpc(),
paramRoutes: peering.Spec.Cloud.NetworkingV1AwsPeering.GetRoutes(),
paramCustomerRegion: peering.Spec.Cloud.NetworkingV1AwsPeering.GetCustomerRegion(),
}}); err != nil {
return nil, err
}
} else if peering.Spec.Cloud.NetworkingV1AzurePeering != nil {
if err := d.Set(paramAzure, []interface{}{map[string]interface{}{
paramTenant: peering.Spec.Cloud.NetworkingV1AzurePeering.GetTenant(),
paramVnet: peering.Spec.Cloud.NetworkingV1AzurePeering.GetVnet(),
paramCustomerRegion: peering.Spec.Cloud.NetworkingV1AzurePeering.GetCustomerRegion(),
}}); err != nil {
return nil, err
}
} else if peering.Spec.Cloud.NetworkingV1GcpPeering != nil {
if err := d.Set(paramGcp, []interface{}{map[string]interface{}{
paramProject: peering.Spec.Cloud.NetworkingV1GcpPeering.GetProject(),
paramVpcNetwork: peering.Spec.Cloud.NetworkingV1GcpPeering.GetVpcNetwork(),
paramImportCustomRoutes: peering.Spec.Cloud.NetworkingV1GcpPeering.GetImportCustomRoutes(),
}}); err != nil {
return nil, err
}
}
if err := setStringAttributeInListBlockOfSizeOne(paramNetwork, paramId, peering.Spec.Network.GetId(), d); err != nil {
return nil, err
}
if err := setStringAttributeInListBlockOfSizeOne(paramEnvironment, paramId, peering.Spec.Environment.GetId(), d); err != nil {
return nil, err
}
d.SetId(peering.GetId())
return d, nil
}
func peeringDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
tflog.Debug(ctx, fmt.Sprintf("Deleting Peering %q", d.Id()), map[string]interface{}{peeringLoggingKey: d.Id()})
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
c := meta.(*Client)
req := c.netClient.PeeringsNetworkingV1Api.DeleteNetworkingV1Peering(c.netApiContext(ctx), d.Id()).Environment(environmentId)
_, err := req.Execute()
if err != nil {
return diag.Errorf("error deleting Peering %q: %s", d.Id(), createDescriptiveError(err))
}
if err := waitForPeeringToBeDeleted(c.netApiContext(ctx), c, environmentId, d.Id()); err != nil {
return diag.Errorf("error waiting for Peering %q to be deleted: %s", d.Id(), createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished deleting Peering %q", d.Id()), map[string]interface{}{peeringLoggingKey: d.Id()})
return nil
}
func peeringUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
if d.HasChangeExcept(paramDisplayName) {
return diag.Errorf("error updating Peering %q: only %q attribute can be updated for Peering", d.Id(), paramDisplayName)
}
c := meta.(*Client)
updatedDisplayName := d.Get(paramDisplayName).(string)
environmentId := extractStringValueFromBlock(d, paramEnvironment, paramId)
updatePeeringRequest := net.NewNetworkingV1PeeringUpdate()
updateSpec := net.NewNetworkingV1PeeringSpecUpdate()
updateSpec.SetDisplayName(updatedDisplayName)
updateSpec.SetEnvironment(net.ObjectReference{Id: environmentId})
updatePeeringRequest.SetSpec(*updateSpec)
updatePeeringRequestJson, err := json.Marshal(updatePeeringRequest)
if err != nil {
return diag.Errorf("error updating Peering %q: error marshaling %#v to json: %s", d.Id(), updatePeeringRequest, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Updating Peering %q: %s", d.Id(), updatePeeringRequestJson), map[string]interface{}{peeringLoggingKey: d.Id()})
req := c.netClient.PeeringsNetworkingV1Api.UpdateNetworkingV1Peering(c.netApiContext(ctx), d.Id()).NetworkingV1PeeringUpdate(*updatePeeringRequest)
updatedPeering, _, err := req.Execute()
if err != nil {
return diag.Errorf("error updating Peering %q: %s", d.Id(), createDescriptiveError(err))
}
updatedPeeringJson, err := json.Marshal(updatedPeering)
if err != nil {
return diag.Errorf("error updating Peering %q: error marshaling %#v to json: %s", d.Id(), updatedPeering, createDescriptiveError(err))
}
tflog.Debug(ctx, fmt.Sprintf("Finished updating Peering %q: %s", d.Id(), updatedPeeringJson), map[string]interface{}{peeringLoggingKey: d.Id()})
return peeringRead(ctx, d, meta)
}
func peeringImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
tflog.Debug(ctx, fmt.Sprintf("Importing Peering %q", d.Id()), map[string]interface{}{peeringLoggingKey: d.Id()})
envIDAndPeeringId := d.Id()
parts := strings.Split(envIDAndPeeringId, "/")
if len(parts) != 2 {
return nil, fmt.Errorf("error importing Peering: invalid format: expected '<env ID>/<peer ID>'")
}
environmentId := parts[0]
peeringId := parts[1]
d.SetId(peeringId)
// Mark resource as new to avoid d.Set("") when getting 404
d.MarkNewResource()
if _, err := readPeeringAndSetAttributes(ctx, d, meta, environmentId, peeringId); err != nil {
return nil, fmt.Errorf("error importing Peering %q: %s", d.Id(), err)
}
tflog.Debug(ctx, fmt.Sprintf("Finished importing Peering %q", d.Id()), map[string]interface{}{peeringLoggingKey: d.Id()})
return []*schema.ResourceData{d}, nil
}
func awsPeeringSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramAccount: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "AWS account for VPC to peer with the network.",
ValidateFunc: validation.StringMatch(regexp.MustCompile(`^\d{12}$`), "AWS Account ID is expected to consist of exactly 12 digits."),
},
paramVpc: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The id of the AWS VPC to peer with.",
ValidateFunc: validation.StringMatch(regexp.MustCompile("^vpc-"), "AWS VPC ID must start with 'vpc-'."),
},
paramRoutes: {
Type: schema.TypeList,
Required: true,
ForceNew: true,
MinItems: 1,
Elem: &schema.Schema{Type: schema.TypeString},
Description: "List of routes for the peering.",
// TODO: ValidateFunc and ValidateDiagFunc are not yet supported on lists or sets.
// TODO: copy validation from confluent_network.cidr attribute
},
paramCustomerRegion: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Region of customer VPC.",
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
ExactlyOneOf: acceptedCloudProvidersForPeering,
}
}
func azurePeeringSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramTenant: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Customer Azure tenant.",
ValidateFunc: validation.StringIsNotEmpty,
},
paramVnet: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Customer VNet to peer with.",
ValidateFunc: validation.StringIsNotEmpty,
},
paramCustomerRegion: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Region of customer VNet.",
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
ExactlyOneOf: acceptedCloudProvidersForPeering,
}
}
func gcpPeeringSchema() *schema.Schema {
return &schema.Schema{
Type: schema.TypeList,
Optional: true,
ForceNew: true,
MinItems: 1,
MaxItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
paramProject: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the GCP project.",
ValidateFunc: validation.StringIsNotEmpty,
},
paramVpcNetwork: {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "The name of the GCP VPC network to peer with.",
ValidateFunc: validation.StringIsNotEmpty,
},
paramImportCustomRoutes: {
Type: schema.TypeBool,
Optional: true,
ForceNew: true,
Default: false,
Description: "Enable customer route import.",
},
},
},
ExactlyOneOf: acceptedCloudProvidersForPeering,
}
}