forked from hashicorp/terraform-provider-google-beta
/
resource_bigtable_table.go
403 lines (341 loc) · 13.1 KB
/
resource_bigtable_table.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
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package bigtable
import (
"context"
"fmt"
"log"
"time"
"cloud.google.com/go/bigtable"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"github.com/lorioux/google-beta/google-beta/tpgresource"
transport_tpg "github.com/lorioux/google-beta/google-beta/transport"
"github.com/lorioux/google-beta/google-beta/verify"
)
func ResourceBigtableTable() *schema.Resource {
return &schema.Resource{
Create: resourceBigtableTableCreate,
Read: resourceBigtableTableRead,
Update: resourceBigtableTableUpdate,
Delete: resourceBigtableTableDestroy,
Importer: &schema.ResourceImporter{
State: resourceBigtableTableImport,
},
// Set a longer timeout for table creation as adding column families can be slow.
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(45 * time.Minute),
Update: schema.DefaultTimeout(20 * time.Minute),
},
// ----------------------------------------------------------------------
// IMPORTANT: Do not add any additional ForceNew fields to this resource.
// Destroying/recreating tables can lead to data loss for users.
// ----------------------------------------------------------------------
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: `The name of the table. Must be 1-50 characters and must only contain hyphens, underscores, periods, letters and numbers.`,
},
"column_family": {
Type: schema.TypeSet,
Optional: true,
Description: `A group of columns within a table which share a common configuration. This can be specified multiple times.`,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"family": {
Type: schema.TypeString,
Required: true,
Description: `The name of the column family.`,
},
},
},
},
"instance_name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
DiffSuppressFunc: tpgresource.CompareResourceNames,
Description: `The name of the Bigtable instance.`,
},
"split_keys": {
Type: schema.TypeList,
Optional: true,
ForceNew: true,
Elem: &schema.Schema{Type: schema.TypeString},
Description: `A list of predefined keys to split the table on. !> Warning: Modifying the split_keys of an existing table will cause Terraform to delete/recreate the entire google_bigtable_table 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.`,
},
"deletion_protection": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{"PROTECTED", "UNPROTECTED"}, false),
Elem: &schema.Schema{Type: schema.TypeString},
Description: `A field to make the table protected against data loss i.e. when set to PROTECTED, deleting the table, the column families in the table, and the instance containing the table would be prohibited. If not provided, currently deletion protection will be set to UNPROTECTED as it is the API default value.`,
},
"change_stream_retention": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: verify.ValidateDuration(),
Description: `Duration to retain change stream data for the table. Set to 0 to disable. Must be between 1 and 7 days.`,
},
},
UseJSONNumber: true,
}
}
func resourceBigtableTableCreate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
ctx := context.Background()
project, err := tpgresource.GetProject(d, config)
if err != nil {
return err
}
instanceName := tpgresource.GetResourceNameFromSelfLink(d.Get("instance_name").(string))
c, err := config.BigTableClientFactory(userAgent).NewAdminClient(project, instanceName)
if err != nil {
return fmt.Errorf("Error starting admin client. %s", err)
}
if err := d.Set("instance_name", instanceName); err != nil {
return fmt.Errorf("Error setting instance_name: %s", err)
}
defer c.Close()
tableId := d.Get("name").(string)
tblConf := bigtable.TableConf{TableID: tableId}
// Check if deletion protection is given
// If not given, currently tblConf.DeletionProtection will be set to false in the API
deletionProtection := d.Get("deletion_protection")
if deletionProtection == "PROTECTED" {
tblConf.DeletionProtection = bigtable.Protected
} else if deletionProtection == "UNPROTECTED" {
tblConf.DeletionProtection = bigtable.Unprotected
}
if changeStreamRetention, ok := d.GetOk("change_stream_retention"); ok {
tblConf.ChangeStreamRetention, err = time.ParseDuration(changeStreamRetention.(string))
if err != nil {
return fmt.Errorf("Error parsing change stream retention: %s", err)
}
}
// Set the split keys if given.
if v, ok := d.GetOk("split_keys"); ok {
tblConf.SplitKeys = tpgresource.ConvertStringArr(v.([]interface{}))
}
// Set the column families if given.
columnFamilies := make(map[string]bigtable.GCPolicy)
if d.Get("column_family.#").(int) > 0 {
columns := d.Get("column_family").(*schema.Set).List()
for _, co := range columns {
column := co.(map[string]interface{})
if v, ok := column["family"]; ok {
// By default, there is no GC rules.
columnFamilies[v.(string)] = bigtable.NoGcPolicy()
}
}
}
tblConf.Families = columnFamilies
// This method may return before the table's creation is complete - we may need to wait until
// it exists in the future.
// Set a longer timeout as creating table and adding column families can be pretty slow.
ctxWithTimeout, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutCreate))
defer cancel() // Always call cancel.
err = c.CreateTableFromConf(ctxWithTimeout, &tblConf)
if err != nil {
return fmt.Errorf("Error creating table. %s", err)
}
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/instances/{{instance_name}}/tables/{{name}}")
if err != nil {
return fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return resourceBigtableTableRead(d, meta)
}
func resourceBigtableTableRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
ctx := context.Background()
project, err := tpgresource.GetProject(d, config)
if err != nil {
return err
}
instanceName := tpgresource.GetResourceNameFromSelfLink(d.Get("instance_name").(string))
c, err := config.BigTableClientFactory(userAgent).NewAdminClient(project, instanceName)
if err != nil {
return fmt.Errorf("Error starting admin client. %s", err)
}
defer c.Close()
name := d.Get("name").(string)
table, err := c.TableInfo(ctx, name)
if err != nil {
if tpgresource.IsNotFoundGrpcError(err) {
log.Printf("[WARN] Removing %s because it's gone", name)
d.SetId("")
return nil
}
return err
}
if err := d.Set("project", project); err != nil {
return fmt.Errorf("Error setting project: %s", err)
}
if err := d.Set("column_family", FlattenColumnFamily(table.Families)); err != nil {
return fmt.Errorf("Error setting column_family: %s", err)
}
deletionProtection := table.DeletionProtection
if deletionProtection == bigtable.Protected {
if err := d.Set("deletion_protection", "PROTECTED"); err != nil {
return fmt.Errorf("Error setting deletion_protection: %s", err)
}
} else if deletionProtection == bigtable.Unprotected {
if err := d.Set("deletion_protection", "UNPROTECTED"); err != nil {
return fmt.Errorf("Error setting deletion_protection: %s", err)
}
} else {
return fmt.Errorf("Error setting deletion_protection, it should be either PROTECTED or UNPROTECTED")
}
changeStreamRetention := table.ChangeStreamRetention
if changeStreamRetention != nil {
if err := d.Set("change_stream_retention", changeStreamRetention.(time.Duration).String()); err != nil {
return fmt.Errorf("Error setting change_stream_retention: %s", err)
}
}
return nil
}
func resourceBigtableTableUpdate(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
ctx := context.Background()
project, err := tpgresource.GetProject(d, config)
if err != nil {
return err
}
instanceName := tpgresource.GetResourceNameFromSelfLink(d.Get("instance_name").(string))
c, err := config.BigTableClientFactory(userAgent).NewAdminClient(project, instanceName)
if err != nil {
return fmt.Errorf("Error starting admin client. %s", err)
}
defer c.Close()
o, n := d.GetChange("column_family")
oSet := o.(*schema.Set)
nSet := n.(*schema.Set)
name := d.Get("name").(string)
// Add column families that are in new but not in old
for _, new := range nSet.Difference(oSet).List() {
column := new.(map[string]interface{})
if v, ok := column["family"]; ok {
log.Printf("[DEBUG] adding column family %q", v)
if err := c.CreateColumnFamily(ctx, name, v.(string)); err != nil {
return fmt.Errorf("Error creating column family %q: %s", v, err)
}
}
}
// Remove column families that are in old but not in new
for _, old := range oSet.Difference(nSet).List() {
column := old.(map[string]interface{})
if v, ok := column["family"]; ok {
log.Printf("[DEBUG] removing column family %q", v)
if err := c.DeleteColumnFamily(ctx, name, v.(string)); err != nil {
return fmt.Errorf("Error deleting column family %q: %s", v, err)
}
}
}
ctxWithTimeout, cancel := context.WithTimeout(ctx, d.Timeout(schema.TimeoutCreate))
defer cancel()
if d.HasChange("deletion_protection") {
deletionProtection := d.Get("deletion_protection")
if deletionProtection == "PROTECTED" {
if err := c.UpdateTableWithDeletionProtection(ctxWithTimeout, name, bigtable.Protected); err != nil {
return fmt.Errorf("Error updating deletion protection in table %v: %s", name, err)
}
} else if deletionProtection == "UNPROTECTED" {
if err := c.UpdateTableWithDeletionProtection(ctxWithTimeout, name, bigtable.Unprotected); err != nil {
return fmt.Errorf("Error updating deletion protection in table %v: %s", name, err)
}
}
}
if d.HasChange("change_stream_retention") {
changeStreamRetention := d.Get("change_stream_retention")
changeStream, err := time.ParseDuration(changeStreamRetention.(string))
if err != nil {
return fmt.Errorf("Error parsing change stream retention: %s", err)
}
if changeStream == 0 {
if err := c.UpdateTableDisableChangeStream(ctxWithTimeout, name); err != nil {
return fmt.Errorf("Error disabling change stream retention in table %v: %s", name, err)
}
} else {
if err := c.UpdateTableWithChangeStream(ctxWithTimeout, name, changeStream); err != nil {
return fmt.Errorf("Error updating change stream retention in table %v: %s", name, err)
}
}
}
return resourceBigtableTableRead(d, meta)
}
func resourceBigtableTableDestroy(d *schema.ResourceData, meta interface{}) error {
config := meta.(*transport_tpg.Config)
userAgent, err := tpgresource.GenerateUserAgentString(d, config.UserAgent)
if err != nil {
return err
}
ctx := context.Background()
project, err := tpgresource.GetProject(d, config)
if err != nil {
return err
}
instanceName := tpgresource.GetResourceNameFromSelfLink(d.Get("instance_name").(string))
c, err := config.BigTableClientFactory(userAgent).NewAdminClient(project, instanceName)
if err != nil {
return fmt.Errorf("Error starting admin client. %s", err)
}
defer c.Close()
name := d.Get("name").(string)
err = c.DeleteTable(ctx, name)
if err != nil {
return fmt.Errorf("Error deleting table. %s", err)
}
d.SetId("")
return nil
}
func FlattenColumnFamily(families []string) []map[string]interface{} {
result := make([]map[string]interface{}, 0, len(families))
for _, f := range families {
data := make(map[string]interface{})
data["family"] = f
result = append(result, data)
}
return result
}
// TODO(rileykarson): Fix the stored import format after rebasing 3.0.0
func resourceBigtableTableImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
config := meta.(*transport_tpg.Config)
if err := tpgresource.ParseImportId([]string{
"projects/(?P<project>[^/]+)/instances/(?P<instance_name>[^/]+)/tables/(?P<name>[^/]+)",
"(?P<project>[^/]+)/(?P<instance_name>[^/]+)/(?P<name>[^/]+)",
"(?P<instance_name>[^/]+)/(?P<name>[^/]+)",
}, d, config); err != nil {
return nil, err
}
// Replace import id for the resource id
id, err := tpgresource.ReplaceVars(d, config, "projects/{{project}}/instances/{{instance_name}}/tables/{{name}}")
if err != nil {
return nil, fmt.Errorf("Error constructing id: %s", err)
}
d.SetId(id)
return []*schema.ResourceData{d}, nil
}