-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathresource_database.go
More file actions
604 lines (542 loc) · 20.7 KB
/
Copy pathresource_database.go
File metadata and controls
604 lines (542 loc) · 20.7 KB
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
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
package provider
import (
"context"
"fmt"
"net/http"
"regexp"
"strings"
"time"
"github.com/datastax/astra-client-go/v2/astra"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
var availableCloudProviders = []string{
"aws",
"gcp",
"azure",
}
var databaseCreateTimeout = time.Minute * 20
var databaseReadTimeout = time.Minute * 5
var databaseDeleteTimeout = time.Minute * 20
var databaseUpdateTimeout = time.Minute * 20
func resourceDatabase() *schema.Resource {
return &schema.Resource{
Description: "`astra_database` provides an Astra Serverless Database resource. You can create and delete databases. Note: Classic Tier databases are not supported by the Terraform provider. (see https://docs.datastax.com/en/astra/docs/index.html for more about Astra DB)",
CreateContext: resourceDatabaseCreate,
ReadContext: resourceDatabaseRead,
DeleteContext: resourceDatabaseDelete,
UpdateContext: resourceDatabaseUpdate,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Timeouts: &schema.ResourceTimeout{
Create: &databaseCreateTimeout,
Read: &databaseReadTimeout,
Delete: &databaseDeleteTimeout,
Update: &databaseUpdateTimeout,
},
Schema: map[string]*schema.Schema{
// Required
"name": {
Description: "Astra database name.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringMatch(regexp.MustCompile("^.{2,}"), "name must be at least 2 characters"),
},
"keyspace": {
Description: "Initial keyspace name. For additional keyspaces, use the astra_keyspace resource.",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateDiagFunc: validateKeyspace,
},
"cloud_provider": {
Description: "The cloud provider to launch the database. (Currently supported: aws, azure, gcp)",
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validation.StringInSlice(availableCloudProviders, true),
DiffSuppressFunc: ignoreCase,
},
"region": {
Description: "Primary Cloud region to launch the database. (see https://docs.datastax.com/en/astra/docs/database-regions.html for supported regions)",
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"additional_regions": {
Description: "Additional Cloud regions for multi-region Database deployment. (see https://docs.datastax.com/en/astra/docs/database-regions.html for supported regions)",
Type: schema.TypeSet,
Optional: true,
ForceNew: false,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
// Optional
"deletion_protection": {
Description: "Whether or not to allow Terraform to destroy the instance. Unless this field is set to false in Terraform state, a `terraform destroy` or `terraform apply` command that deletes the instance will fail. Defaults to `true`.",
Type: schema.TypeBool,
Optional: true,
Default: true,
},
// Computed
"owner_id": {
Description: "The owner id.",
Type: schema.TypeString,
Computed: true,
},
"organization_id": {
Description: "The org id.",
Type: schema.TypeString,
Computed: true,
},
"status": {
Description: "The status",
Type: schema.TypeString,
Computed: true,
},
"cqlsh_url": {
Description: "The cqlsh_url",
Type: schema.TypeString,
Computed: true,
},
"grafana_url": {
Description: "The grafana_url",
Type: schema.TypeString,
Computed: true,
},
"data_endpoint_url": {
Description: "The data_endpoint_url",
Type: schema.TypeString,
Computed: true,
},
"graphql_url": {
Description: "The graphql_url",
Type: schema.TypeString,
Computed: true,
},
"node_count": {
Description: "The node_count",
Type: schema.TypeInt,
Computed: true,
},
"replication_factor": {
Description: "The replication_factor",
Type: schema.TypeInt,
Computed: true,
},
"total_storage": {
Description: "The total_storage",
Type: schema.TypeInt,
Computed: true,
},
"additional_keyspaces": {
Description: "Additional keyspaces",
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"datacenters": {
Description: "Map of Datacenter IDs. The map key is \"cloud_provider.region\". Example: \"GCP.us-east4\".",
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func resourceDatabaseCreate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
name := resourceData.Get("name").(string)
keyspace := resourceData.Get("keyspace").(string)
cloudProvider := resourceData.Get("cloud_provider").(string)
region := resourceData.Get("region").(string)
additionalRegions := (resourceData.Get("additional_regions").(*schema.Set)).List()
// Make sure all regions are valid
if err := ensureValidRegions(ctx, client, resourceData); err != nil {
return err
}
resp, err := client.CreateDatabaseWithResponse(ctx, astra.CreateDatabaseJSONRequestBody{
Name: name,
Keyspace: keyspace,
CloudProvider: astra.CloudProvider(cloudProvider),
CapacityUnits: 1,
Region: region,
Tier: astra.Tier("serverless"),
})
if err != nil {
return diag.FromErr(err)
}
if resp.StatusCode() != http.StatusCreated {
return diag.Errorf("unexpected create database response: %s", string(resp.Body))
}
databaseID := resp.HTTPResponse.Header.Get("location")
// Wait for the database to be ACTIVE then set resource data
if err := waitForDatabaseAndUpdateResource(ctx, resourceData, client, databaseID); err != nil {
return err
}
// Add any additional regions/datacenters
if len(additionalRegions) > 0 {
if err:= addRegionsToDatabase(ctx, resourceData, client, additionalRegions, databaseID, cloudProvider); err != nil {
return err
}
}
return nil
}
func resourceDatabaseRead(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
databaseID := resourceData.Id()
if err := resource.RetryContext(ctx, resourceData.Timeout(schema.TimeoutRead), func() *resource.RetryError {
resp, err := client.GetDatabaseWithResponse(ctx, astra.DatabaseIdParam(databaseID))
if err != nil {
return resource.RetryableError(fmt.Errorf("unable to fetch database (%s): %v", databaseID, err))
}
// Remove from state when database not found
if resp.JSON404 != nil || resp.StatusCode() == http.StatusNotFound {
resourceData.SetId("")
return nil
}
// Retry on 5XX errors
if resp.StatusCode() >= http.StatusInternalServerError {
return resource.RetryableError(fmt.Errorf("error fetching database (%s): %v", databaseID, err))
}
// Don't retry for non 200 status code
db := resp.JSON200
if db == nil {
return resource.NonRetryableError(fmt.Errorf("unexpected response fetching database (%s): %s", databaseID, string(resp.Body)))
}
// If the database is TERMINATING or TERMINATED then remove it from the state
if db.Status == astra.TERMINATING || db.Status == astra.TERMINATED {
resourceData.SetId("")
return nil
}
// Add the database to state
if err := setDatabaseResourceData(resourceData, db); err != nil {
return resource.NonRetryableError(err)
}
return nil
}); err != nil {
return diag.FromErr(err)
}
return nil
}
func resourceDatabaseDelete(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
if protectedFromDelete(resourceData) {
return diag.Errorf("\"deletion_protection\" must be explicitly set to \"false\" in order to destroy astra_database")
}
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
databaseID := resourceData.Id()
alreadyDeleted := false
// get the list of regions and delete any extra regions/datacenters first
regionsToDelete := (resourceData.Get("additional_regions").(*schema.Set)).List()
if len(regionsToDelete) > 1 {
tflog.Debug(ctx, fmt.Sprintf("Multiple regions found. Must delete all additional regions first: %v", regionsToDelete))
cloudProvider := resourceData.Get("cloud_provider").(string)
if err := deleteRegionsFromDatabase(ctx, resourceData, client, regionsToDelete, databaseID, cloudProvider); err != nil {
return err
}
} else {
tflog.Debug(ctx, fmt.Sprintf("Single region found %v", resourceData.Get("region")))
}
if err := resource.RetryContext(ctx, resourceData.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
resp, err := client.TerminateDatabaseWithResponse(ctx, astra.DatabaseIdParam(databaseID), &astra.TerminateDatabaseParams{})
if err != nil {
return resource.RetryableError(err)
}
// Status code 5XX are considered transient
if resp.StatusCode() >= http.StatusInternalServerError {
return resource.RetryableError(fmt.Errorf("error terminating database: %s", string(resp.Body)))
}
// If the database cannot be found then it has been deleted
if resp.StatusCode() == http.StatusNotFound {
alreadyDeleted = true
return nil
}
// All other 4XX status codes are NOT retried
if resp.StatusCode() >= http.StatusBadRequest {
return resource.NonRetryableError(fmt.Errorf("unexpected response attempting to terminate database. Status code: %d, message = %s", resp.StatusCode(), string(resp.Body)))
}
return nil
}); err != nil {
return diag.FromErr(err)
}
// Return early since it has been determined that the database no longer exists
if alreadyDeleted {
resourceData.SetId("")
return nil
}
// Wait for the database to be TERMINATED or not found
if err := resource.RetryContext(ctx, resourceData.Timeout(schema.TimeoutDelete), func() *resource.RetryError {
res, err := client.GetDatabaseWithResponse(ctx, astra.DatabaseIdParam(databaseID))
// Errors sending request should be retried and are assumed to be transient
if err != nil {
return resource.RetryableError(err)
}
// Status code >=5xx are assumed to be transient
if res.StatusCode() >= http.StatusInternalServerError {
return resource.RetryableError(fmt.Errorf("error while fetching database: %s", string(res.Body)))
}
// If the database cannot be found. It has been deleted.
if res.StatusCode() == http.StatusNotFound {
return nil
}
// All other status codes > 200 NOT retried
if res.StatusCode() > http.StatusOK || res.JSON200 == nil {
return resource.NonRetryableError(fmt.Errorf("unexpected response fetching database: %s", string(res.Body)))
}
// Return when the database is in a TERMINATED state
db := res.JSON200
if db.Status == astra.TERMINATED {
return nil
}
// Continue until one of the expected conditions above are met
return resource.RetryableError(fmt.Errorf("expected database to be terminated but is %s", db.Status))
}); err != nil {
return diag.FromErr(err)
}
resourceData.SetId("")
return nil
}
func resourceDatabaseUpdate(ctx context.Context, resourceData *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(astraClients).astraClient.(*astra.ClientWithResponses)
databaseID := resourceData.Id()
cloudProvider := resourceData.Get("cloud_provider").(string)
if resourceData.HasChange("additional_regions") {
// get regions to add and delete
regionsToAdd, regionsToDelete := getRegionUpdates(resourceData.GetChange("additional_regions"))
if len(regionsToAdd) > 0 {
// add any regions to add first
if err := addRegionsToDatabase(ctx, resourceData, client, regionsToAdd, databaseID, cloudProvider); err != nil {
return err
}
}
if len(regionsToDelete) > 0 {
// delete any regions that should be removed
if err := deleteRegionsFromDatabase(ctx, resourceData, client, regionsToDelete, databaseID, cloudProvider); err != nil {
return err
}
}
}
return nil
}
func getRegionUpdates(oldRegions interface{}, newRegions interface{}) ([]interface{}, []interface{}){
mOld := map[string]bool{}
mNew := map[string]bool{}
var regionsToAdd []interface{}
var regionsToDelete []interface{}
oldRegionsList := (oldRegions.(*schema.Set)).List()
newRegionsList := (newRegions.(*schema.Set)).List()
// find any regions to add
for _, v := range oldRegionsList {
mOld[v.(string)] = true
}
for _, v := range newRegionsList {
mNew[v.(string)] = true
}
for _, v := range oldRegionsList {
if !mNew[v.(string)] {
regionsToDelete = append(regionsToDelete, v)
}
}
for _, v := range newRegionsList {
if !mOld[v.(string)] {
regionsToAdd = append(regionsToAdd, v)
}
}
return regionsToAdd, regionsToDelete
}
func addRegionsToDatabase(ctx context.Context, resourceData *schema.ResourceData, client *astra.ClientWithResponses, regions []interface{}, databaseID, cloudProvider string) diag.Diagnostics {
// make sure the regions are valid
if err := ensureValidRegions(ctx, client, resourceData); err != nil {
return err
}
// Currently, DevOps API only allows for adding 1 region at a time
for _, region := range regions {
datacenters := make([]astra.Datacenter, 1)
datacenters[0] = astra.Datacenter {
CloudProvider: astra.CloudProvider(cloudProvider),
Region: region.(string),
Tier: "serverless",
}
resp, err := client.AddDatacentersWithResponse(ctx, astra.DatabaseIdParam(databaseID), datacenters)
if err != nil {
return diag.FromErr(err)
}
if resp.StatusCode() != http.StatusCreated {
return diag.FromErr(fmt.Errorf("Unexpected response adding Regions: %s", string(resp.Body)))
}
// Wait for the database to be ACTIVE then set resource data
if err := waitForDatabaseAndUpdateResource(ctx, resourceData, client, databaseID); err != nil {
return err
}
}
return nil
}
func deleteRegionsFromDatabase(ctx context.Context, resourceData *schema.ResourceData, client *astra.ClientWithResponses, regions []interface{}, databaseID, cloudProvider string) diag.Diagnostics {
// get all the datacenters for the Database ID
dcListResp, err := client.ListDatacentersWithResponse(ctx, astra.DatabaseIdParam(databaseID), &astra.ListDatacentersParams{})
if err != nil {
return diag.FromErr(err)
}
if dcListResp.StatusCode() != http.StatusOK || dcListResp.JSON200 == nil {
return diag.FromErr(fmt.Errorf("Unexpected response fetching Datacenters: %s", dcListResp.Body))
}
dcs := *dcListResp.JSON200
// map regions to DCs
regionDcMap := map[string]astra.Datacenter{}
for _, v := range dcs {
regionDcMap[v.Region] = v
}
// delete each region that exists
for _, v := range regions {
if dc := regionDcMap[v.(string)]; dc.Id != nil {
termResp, err := client.TerminateDatacenterWithResponse(ctx, astra.DatabaseIdParam(databaseID), astra.DatacenterIdParam(*dc.Id))
if err != nil {
return diag.FromErr(err)
}
if termResp.StatusCode() == http.StatusUnauthorized {
return diag.Errorf("Error terminating datacenter for region \"%s\": Insufficient permissions.", v)
}
if termResp.StatusCode() != http.StatusAccepted {
return diag.Errorf("Error terminating datacenter for region \"%s\": Response %d, message = %s", v, termResp.StatusCode(), string(termResp.Body))
}
// Wait for the database to be ACTIVE then set resource data
if err := waitForDatabaseAndUpdateResource(ctx, resourceData, client, databaseID); err != nil {
return err
}
}
}
return nil
}
func waitForDatabaseAndUpdateResource(ctx context.Context, resourceData *schema.ResourceData, client *astra.ClientWithResponses, databaseID string) diag.Diagnostics {
if err := resource.RetryContext(ctx, resourceData.Timeout(schema.TimeoutCreate), func() *resource.RetryError {
res, err := client.GetDatabaseWithResponse(ctx, astra.DatabaseIdParam(databaseID))
// Errors sending request should be retried and are assumed to be transient
if err != nil {
return resource.RetryableError(err)
}
// Status code >=5xx are assumed to be transient
if res.StatusCode() >= http.StatusInternalServerError {
return resource.RetryableError(fmt.Errorf("error while fetching database: %s", string(res.Body)))
}
// Status code > 200 NOT retried
if res.StatusCode() > http.StatusOK || res.JSON200 == nil {
return resource.NonRetryableError(fmt.Errorf("unexpected response fetching database: %s", string(res.Body)))
}
// Success fetching database
db := res.JSON200
switch db.Status {
case astra.ERROR, astra.TERMINATED, astra.TERMINATING:
// If the database reached a terminal state it will never become active
return resource.NonRetryableError(fmt.Errorf("database failed to reach active status: status=%s", db.Status))
case astra.ACTIVE:
if err := setDatabaseResourceData(resourceData, db); err != nil {
return resource.NonRetryableError(err)
}
return nil
default:
return resource.RetryableError(fmt.Errorf("expected database to be active but is %s", db.Status))
}
}); err != nil {
return diag.FromErr(err)
}
return nil
}
func setDatabaseResourceData(resourceData *schema.ResourceData, db *astra.Database) error {
resourceData.SetId(db.Id)
flatDb := flattenDatabase(db)
for k, v := range flatDb {
if k == "id" {
continue
}
if err := resourceData.Set(k, v); err != nil {
return err
}
}
return nil
}
func flattenDatabase(db *astra.Database) map[string]interface{} {
flatDB := map[string]interface{}{
"id": db.Id,
"name": astra.StringValue(db.Info.Name),
"organization_id": db.OrgId,
"owner_id": db.OwnerId,
"status": string(db.Status),
"grafana_url": astra.StringValue(db.GrafanaUrl),
"graphql_url": astra.StringValue(db.GraphqlUrl),
"data_endpoint_url": astra.StringValue(db.DataEndpointUrl),
"cqlsh_url": astra.StringValue(db.CqlshUrl),
"cloud_provider": "",
"region": astra.StringValue(db.Info.Region),
"additional_regions": []string{},
"keyspace": astra.StringValue(db.Info.Keyspace),
"additional_keyspaces": astra.StringSlice(db.Info.AdditionalKeyspaces),
"node_count": db.Storage.NodeCount,
"replication_factor": db.Storage.ReplicationFactor,
"total_storage": db.Storage.TotalStorage,
"datacenters": map[string]interface{}{},
}
if db.Info.CloudProvider != nil {
cloudProvider := *db.Info.CloudProvider
flatDB["cloud_provider"] = string(cloudProvider)
}
if db.Info.Datacenters != nil && len(*db.Info.Datacenters) > 1 {
regions := make([]string, len(*db.Info.Datacenters) - 1)
datacenters := make(map[string]interface{}, len(*db.Info.Datacenters))
regionIndex := 0
for _, dc := range *db.Info.Datacenters {
if dc.Region != flatDB["region"].(string) {
regions[regionIndex] = dc.Region
regionIndex++
}
// make a datacenter key of cloud_provider.region
dcKey := flatDB["cloud_provider"].(string) + "." + dc.Region
datacenters[dcKey] = *dc.Id
}
flatDB["additional_regions"] = regions
flatDB["datacenters"] = datacenters
}
return flatDB
}
func ensureValidRegions(ctx context.Context, client *astra.ClientWithResponses, resourceData *schema.ResourceData) diag.Diagnostics {
// get the list of serverless regions
regionsResp, err := client.ListServerlessRegionsWithResponse(ctx)
if err != nil {
return diag.FromErr(err)
} else if regionsResp.StatusCode() != http.StatusOK {
return diag.Errorf("unexpected list available regions response: %s", string(regionsResp.Body))
}
// make sure all of the regions are valid
cloudProvider := resourceData.Get("cloud_provider").(string)
primaryRegion := resourceData.Get("region").(string)
if findMatchingRegion(cloudProvider, primaryRegion, "serverless", *regionsResp.JSON200) == nil {
return diag.Errorf("cloud provider and Primary region combination not available: %s/%s", cloudProvider, primaryRegion)
}
regions := (resourceData.Get("additional_regions").(*schema.Set)).List()
for _, r := range regions {
region := r.(string)
dbRegion := findMatchingRegion(cloudProvider, region, "serverless", *regionsResp.JSON200)
if dbRegion == nil {
return diag.Errorf("cloud provider and region combination not available: %s/%s", cloudProvider, region)
}
}
return nil
}
func findMatchingRegion(provider, region, tier string, availableRegions []astra.ServerlessRegion) *astra.ServerlessRegion {
for _, ar := range availableRegions {
if strings.EqualFold(string(ar.CloudProvider), provider) &&
strings.EqualFold(ar.Name, region) {
return &ar
}
}
return nil
}