forked from GoogleCloudPlatform/magic-modules
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathresource_bigtable_instance.go
584 lines (506 loc) · 18.9 KB
/
resource_bigtable_instance.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
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
package google
import (
"context"
"fmt"
"log"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"cloud.google.com/go/bigtable"
)
func resourceBigtableInstance() *schema.Resource {
return &schema.Resource{
Create: resourceBigtableInstanceCreate,
Read: resourceBigtableInstanceRead,
Update: resourceBigtableInstanceUpdate,
Delete: resourceBigtableInstanceDestroy,
Importer: &schema.ResourceImporter{
State: resourceBigtableInstanceImport,
},
CustomizeDiff: customdiff.All(
resourceBigtableInstanceClusterReorderTypeList,
),
SchemaVersion: 1,
StateUpgraders: []schema.StateUpgrader{
{
Type: resourceBigtableInstanceResourceV0().CoreConfigSchema().ImpliedType(),
Upgrade: resourceBigtableInstanceUpgradeV0,
Version: 0,
},
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The name (also called Instance Id in the Cloud Console) of the Cloud Bigtable instance.`,
},
"cluster": {
Type: schema.TypeList,
Optional: true,
Computed: true,
Description: `A block of cluster configuration options. This can be specified at least once.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"cluster_id": {
Type: schema.TypeString,
Required: true,
Description: `The ID of the Cloud Bigtable cluster.`,
},
"zone": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: `The zone to create the Cloud Bigtable cluster in. Each cluster must have a different zone in the same region. Zones that support Bigtable instances are noted on the Cloud Bigtable locations page.`,
},
"num_nodes": {
Type: schema.TypeInt,
Optional: true,
// DEVELOPMENT instances could get returned with either zero or one node,
// so mark as computed.
Computed: true,
ValidateFunc: validation.IntAtLeast(1),
Description: `The number of nodes in your Cloud Bigtable cluster. Required, with a minimum of 1 for a PRODUCTION instance. Must be left unset for a DEVELOPMENT instance.`,
},
"storage_type": {
Type: schema.TypeString,
Optional: true,
Default: "SSD",
ValidateFunc: validation.StringInSlice([]string{"SSD", "HDD"}, false),
Description: `The storage type to use. One of "SSD" or "HDD". Defaults to "SSD".`,
},
"kms_key_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: `Describes the Cloud KMS encryption key that will be used to protect the destination Bigtable cluster. The requirements for this key are: 1) The Cloud Bigtable service account associated with the project that contains this cluster must be granted the cloudkms.cryptoKeyEncrypterDecrypter role on the CMEK key. 2) Only regional keys can be used and the region of the CMEK key must match the region of the cluster. 3) All clusters within an instance must use the same CMEK key. Values are of the form projects/{project}/locations/{location}/keyRings/{keyring}/cryptoKeys/{key}`,
},
"autoscaling_config": {
Type: schema.TypeList,
Optional: true,
MaxItems: 1,
Description: "A list of Autoscaling configurations. Only one element is used and allowed.",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"min_nodes": {
Type: schema.TypeInt,
Required: true,
Description: `The minimum number of nodes for autoscaling.`,
},
"max_nodes": {
Type: schema.TypeInt,
Required: true,
Description: `The maximum number of nodes for autoscaling.`,
},
"cpu_target": {
Type: schema.TypeInt,
Required: true,
Description: `The target CPU utilization for autoscaling. Value must be between 10 and 80.`,
},
"storage_target": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: `The target storage utilization for autoscaling, in GB, for each node in a cluster. This number is limited between 2560 (2.5TiB) and 5120 (5TiB) for a SSD cluster and between 8192 (8TiB) and 16384 (16 TiB) for an HDD cluster. If not set, whatever is already set for the cluster will not change, or if the cluster is just being created, it will use the default value of 2560 for SSD clusters and 8192 for HDD clusters.`,
},
},
},
},
},
},
},
"display_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: `The human-readable display name of the Bigtable instance. Defaults to the instance name.`,
},
"instance_type": {
Type: schema.TypeString,
Optional: true,
Default: "PRODUCTION",
ValidateFunc: validation.StringInSlice([]string{"DEVELOPMENT", "PRODUCTION"}, false),
Description: `The instance type to create. One of "DEVELOPMENT" or "PRODUCTION". Defaults to "PRODUCTION".`,
Deprecated: `It is recommended to leave this field unspecified since the distinction between "DEVELOPMENT" and "PRODUCTION" instances is going away, and all instances will become "PRODUCTION" instances. This means that new and existing "DEVELOPMENT" instances will be converted to "PRODUCTION" instances. It is recommended for users to use "PRODUCTION" instances in any case, since a 1-node "PRODUCTION" instance is functionally identical to a "DEVELOPMENT" instance, but without the accompanying restrictions.`,
},
"deletion_protection": {
Type: schema.TypeBool,
Optional: true,
Default: true,
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 that would delete the instance will fail.`,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `A mapping of labels to assign to the resource.`,
},
"project": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
Description: `The ID of the project in which the resource belongs. If it is not provided, the provider project is used.`,
},
},
UseJSONNumber: true,
}
}
func resourceBigtableInstanceCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
ctx := context.Background()
project, err := getProject(d, config)
if err != nil {
return err
}
conf := &bigtable.InstanceWithClustersConfig{
InstanceID: d.Get("name").(string),
}
displayName, ok := d.GetOk("display_name")
if !ok {
displayName = conf.InstanceID
}
conf.DisplayName = displayName.(string)
if _, ok := d.GetOk("labels"); ok {
conf.Labels = expandLabels(d)
}
switch d.Get("instance_type").(string) {
case "DEVELOPMENT":
conf.InstanceType = bigtable.DEVELOPMENT
case "PRODUCTION":
conf.InstanceType = bigtable.PRODUCTION
}
conf.Clusters, err = expandBigtableClusters(d.Get("cluster").([]interface{}), conf.InstanceID, config)
if err != nil {
return err
}
c, err := config.BigTableClientFactory(userAgent).NewInstanceAdminClient(project)
if err != nil {
return fmt.Errorf("Error starting instance admin client. %s", err)
}
defer c.Close()
err = c.CreateInstanceWithClusters(ctx, conf)
if err != nil {
return fmt.Errorf("Error creating instance. %s", err)
}
id, err := replaceVars(d, config, "projects/{{project}}/instances/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return resourceBigtableInstanceRead(d, meta)
}
func resourceBigtableInstanceRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
ctx := context.Background()
project, err := getProject(d, config)
if err != nil {
return err
}
c, err := config.BigTableClientFactory(userAgent).NewInstanceAdminClient(project)
if err != nil {
return fmt.Errorf("Error starting instance admin client. %s", err)
}
defer c.Close()
instanceName := d.Get("name").(string)
instance, err := c.InstanceInfo(ctx, instanceName)
if err != nil {
log.Printf("[WARN] Removing %s because it's gone", instanceName)
d.SetId("")
return nil
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
clusters, err := c.Clusters(ctx, instance.Name)
if err != nil {
return fmt.Errorf("Error retrieving instance clusters. %s", err)
}
clustersNewState := []map[string]interface{}{}
for _, cluster := range clusters {
clustersNewState = append(clustersNewState, flattenBigtableCluster(cluster))
}
log.Printf("[DEBUG] Setting clusters in state: %#v", clustersNewState)
err = d.Set("cluster", clustersNewState)
if err != nil {
return fmt.Errorf("Error setting clusters in state: %s", err.Error())
}
if err := d.Set("name", instance.Name); err != nil {
return fmt.Errorf("Error setting name: %s", err)
}
if err := d.Set("display_name", instance.DisplayName); err != nil {
return fmt.Errorf("Error setting display_name: %s", err)
}
if err := d.Set("labels", instance.Labels); err != nil {
return fmt.Errorf("Error setting labels: %s", err)
}
// Don't set instance_type: we don't want to detect drift on it because it can
// change under-the-hood.
return nil
}
func resourceBigtableInstanceUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
ctx := context.Background()
project, err := getProject(d, config)
if err != nil {
return err
}
c, err := config.BigTableClientFactory(userAgent).NewInstanceAdminClient(project)
if err != nil {
return fmt.Errorf("Error starting instance admin client. %s", err)
}
defer c.Close()
conf := &bigtable.InstanceWithClustersConfig{
InstanceID: d.Get("name").(string),
}
displayName, ok := d.GetOk("display_name")
if !ok {
displayName = conf.InstanceID
}
conf.DisplayName = displayName.(string)
if d.HasChange("labels") {
conf.Labels = expandLabels(d)
}
switch d.Get("instance_type").(string) {
case "DEVELOPMENT":
conf.InstanceType = bigtable.DEVELOPMENT
case "PRODUCTION":
conf.InstanceType = bigtable.PRODUCTION
}
conf.Clusters, err = expandBigtableClusters(d.Get("cluster").([]interface{}), conf.InstanceID, config)
if err != nil {
return err
}
_, err = bigtable.UpdateInstanceAndSyncClusters(ctx, c, conf)
if err != nil {
return fmt.Errorf("Error updating instance. %s", err)
}
return resourceBigtableInstanceRead(d, meta)
}
func resourceBigtableInstanceDestroy(d *schema.ResourceData, meta interface{}) error {
if d.Get("deletion_protection").(bool) {
return fmt.Errorf("cannot destroy instance without setting deletion_protection=false and running `terraform apply`")
}
config := meta.(*Config)
userAgent, err := generateUserAgentString(d, config.userAgent)
if err != nil {
return err
}
ctx := context.Background()
project, err := getProject(d, config)
if err != nil {
return err
}
c, err := config.BigTableClientFactory(userAgent).NewInstanceAdminClient(project)
if err != nil {
return fmt.Errorf("Error starting instance admin client. %s", err)
}
defer c.Close()
name := d.Get("name").(string)
err = c.DeleteInstance(ctx, name)
if err != nil {
return fmt.Errorf("Error deleting instance. %s", err)
}
d.SetId("")
return nil
}
func flattenBigtableCluster(c *bigtable.ClusterInfo) map[string]interface{} {
var storageType string
switch c.StorageType {
case bigtable.SSD:
storageType = "SSD"
case bigtable.HDD:
storageType = "HDD"
}
cluster := map[string]interface{}{
"zone": c.Zone,
"num_nodes": c.ServeNodes,
"cluster_id": c.Name,
"storage_type": storageType,
"kms_key_name": c.KMSKeyName,
}
if c.AutoscalingConfig != nil {
cluster["autoscaling_config"] = make([]map[string]interface{}, 1)
autoscaling_config := cluster["autoscaling_config"].([]map[string]interface{})
autoscaling_config[0] = make(map[string]interface{})
autoscaling_config[0]["min_nodes"] = c.AutoscalingConfig.MinNodes
autoscaling_config[0]["max_nodes"] = c.AutoscalingConfig.MaxNodes
autoscaling_config[0]["cpu_target"] = c.AutoscalingConfig.CPUTargetPercent
autoscaling_config[0]["storage_target"] = c.AutoscalingConfig.StorageUtilizationPerNode
}
return cluster
}
func expandBigtableClusters(clusters []interface{}, instanceID string, config *Config) ([]bigtable.ClusterConfig, error) {
results := make([]bigtable.ClusterConfig, 0, len(clusters))
for _, c := range clusters {
cluster := c.(map[string]interface{})
zone, err := getBigtableZone(cluster["zone"].(string), config)
if err != nil {
return nil, err
}
var storageType bigtable.StorageType
switch cluster["storage_type"].(string) {
case "SSD":
storageType = bigtable.SSD
case "HDD":
storageType = bigtable.HDD
}
cluster_config := bigtable.ClusterConfig{
InstanceID: instanceID,
Zone: zone,
ClusterID: cluster["cluster_id"].(string),
NumNodes: int32(cluster["num_nodes"].(int)),
StorageType: storageType,
KMSKeyName: cluster["kms_key_name"].(string),
}
autoscaling_configs := cluster["autoscaling_config"].([]interface{})
if len(autoscaling_configs) > 0 {
autoscaling_config := autoscaling_configs[0].(map[string]interface{})
cluster_config.AutoscalingConfig = &bigtable.AutoscalingConfig{
MinNodes: autoscaling_config["min_nodes"].(int),
MaxNodes: autoscaling_config["max_nodes"].(int),
CPUTargetPercent: autoscaling_config["cpu_target"].(int),
StorageUtilizationPerNode: autoscaling_config["storage_target"].(int),
}
}
results = append(results, cluster_config)
}
return results, nil
}
// getBigtableZone reads the "zone" value from the given resource data and falls back
// to provider's value if not given. If neither is provided, returns an error.
func getBigtableZone(z string, config *Config) (string, error) {
if z == "" {
if config.Zone != "" {
return config.Zone, nil
}
return "", fmt.Errorf("cannot determine zone: set in cluster.0.zone, or set provider-level zone")
}
return GetResourceNameFromSelfLink(z), nil
}
// resourceBigtableInstanceClusterReorderTypeList causes the cluster block to
// act like a TypeSet while it's a TypeList underneath. It preserves state
// ordering on updates, and causes the resource to get recreated if it would
// attempt to perform an impossible change.
// This doesn't use the standard unordered list utility (https://github.com/GoogleCloudPlatform/magic-modules/blob/main/templates/terraform/unordered_list_customize_diff.erb)
// because some fields can't be modified using the API and we recreate the instance
// when they're changed.
func resourceBigtableInstanceClusterReorderTypeList(_ context.Context, diff *schema.ResourceDiff, meta interface{}) error {
oldCount, newCount := diff.GetChange("cluster.#")
// simulate Required:true, MinItems:1 for "cluster"
if newCount.(int) < 1 {
return fmt.Errorf("config is invalid: Too few cluster blocks: Should have at least 1 \"cluster\" block")
}
// exit early if we're in create (name's old value is nil)
n, _ := diff.GetChange("name")
if n == nil || n == "" {
return nil
}
oldIds := []string{}
clusters := make(map[string]interface{}, newCount.(int))
for i := 0; i < oldCount.(int); i++ {
oldId, _ := diff.GetChange(fmt.Sprintf("cluster.%d.cluster_id", i))
if oldId != nil && oldId != "" {
oldIds = append(oldIds, oldId.(string))
}
}
log.Printf("[DEBUG] Saw old ids: %#v", oldIds)
for i := 0; i < newCount.(int); i++ {
_, newId := diff.GetChange(fmt.Sprintf("cluster.%d.cluster_id", i))
_, c := diff.GetChange(fmt.Sprintf("cluster.%d", i))
clusters[newId.(string)] = c
}
// create a list of clusters using the old order when possible to minimise
// diffs
// initially, add matching clusters to their index by id (nil otherwise)
// then, fill in nils with new clusters.
// [a, b, c, e] -> [c, a, d] becomes [a, nil, c] followed by [a, d, c]
var orderedClusters []interface{}
for i := 0; i < newCount.(int); i++ {
// when i is out of range of old, all values are nil
if i >= len(oldIds) {
orderedClusters = append(orderedClusters, nil)
continue
}
oldId := oldIds[i]
if c, ok := clusters[oldId]; ok {
log.Printf("[DEBUG] Matched: %#v", oldId)
orderedClusters = append(orderedClusters, c)
delete(clusters, oldId)
} else {
orderedClusters = append(orderedClusters, nil)
}
}
log.Printf("[DEBUG] Remaining clusters: %#v", clusters)
for _, elem := range clusters {
for i, e := range orderedClusters {
if e == nil {
orderedClusters[i] = elem
break
}
}
}
err := diff.SetNew("cluster", orderedClusters)
if err != nil {
return fmt.Errorf("Error setting cluster diff: %s", err)
}
// Clusters can't have their zone / storage_type updated, ForceNew if it's
// changed. This will show a diff with the old state on the left side and
// the unmodified new state on the right and the ForceNew attributed to the
// _old state index_ even if the diff appears to have moved.
// This depends on the clusters having been reordered already by the prior
// SetNew call.
// We've implemented it here because it doesn't return an error in the
// client and silently fails.
for i := 0; i < newCount.(int); i++ {
oldId, newId := diff.GetChange(fmt.Sprintf("cluster.%d.cluster_id", i))
if oldId != newId {
continue
}
oZone, nZone := diff.GetChange(fmt.Sprintf("cluster.%d.zone", i))
if oZone != nZone {
err := diff.ForceNew(fmt.Sprintf("cluster.%d.zone", i))
if err != nil {
return fmt.Errorf("Error setting cluster diff: %s", err)
}
}
oST, nST := diff.GetChange(fmt.Sprintf("cluster.%d.storage_type", i))
if oST != nST {
err := diff.ForceNew(fmt.Sprintf("cluster.%d.storage_type", i))
if err != nil {
return fmt.Errorf("Error setting cluster diff: %s", err)
}
}
}
return nil
}
func resourceBigtableInstanceImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*Config)
if err := parseImportId([]string{
"projects/(?P<project>[^/]+)/instances/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<name>[^/]+)",
"(?P<name>[^/]+)",
}, d, config); err != nil {
return nil, err
}
// Replace import id for the resource id
id, err := replaceVars(d, config, "projects/{{project}}/instances/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return []*schema.ResourceData{d}, nil
}