forked from hashicorp/terraform-provider-google
-
Notifications
You must be signed in to change notification settings - Fork 0
/
resource_compute_instance_migrate.go
515 lines (444 loc) · 16.5 KB
/
resource_compute_instance_migrate.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
package google
import (
"fmt"
"log"
"strconv"
"strings"
compute "google.golang.org/api/compute/v1"
"github.com/hashicorp/terraform/helper/hashcode"
"github.com/hashicorp/terraform/terraform"
)
func resourceComputeInstanceMigrateState(
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
if is.Empty() {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
return is, nil
}
var err error
switch v {
case 0:
log.Println("[INFO] Found Compute Instance State v0; migrating to v1")
is, err = migrateStateV0toV1(is)
if err != nil {
return is, err
}
fallthrough
case 1:
log.Println("[INFO] Found Compute Instance State v1; migrating to v2")
is, err = migrateStateV1toV2(is)
if err != nil {
return is, err
}
fallthrough
case 2:
log.Println("[INFO] Found Compute Instance State v2; migrating to v3")
is, err = migrateStateV2toV3(is)
if err != nil {
return is, err
}
fallthrough
case 3:
log.Println("[INFO] Found Compute Instance State v3; migrating to v4")
is, err = migrateStateV3toV4(is, meta)
if err != nil {
return is, err
}
fallthrough
case 4:
log.Println("[INFO] Found Compute Instance State v4; migrating to v5")
is, err = migrateStateV4toV5(is, meta)
if err != nil {
return is, err
}
fallthrough
case 5:
log.Println("[INFO] Found Compute Instance State v5; migrating to v6")
is, err = migrateStateV5toV6(is)
if err != nil {
return is, err
}
// when adding case 6, make sure to turn this into a fallthrough
return is, err
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}
func migrateStateV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
// Delete old count
delete(is.Attributes, "metadata.#")
newMetadata := make(map[string]string)
for k, v := range is.Attributes {
if !strings.HasPrefix(k, "metadata.") {
continue
}
// We have a key that looks like "metadata.*" and we know it's not
// metadata.# because we deleted it above, so it must be metadata.<N>.<key>
// from the List of Maps. Just need to convert it to a single Map by
// ditching the '<N>' field.
kParts := strings.SplitN(k, ".", 3)
// Sanity check: all three parts should be there and <N> should be a number
badFormat := false
if len(kParts) != 3 {
badFormat = true
} else if _, err := strconv.Atoi(kParts[1]); err != nil {
badFormat = true
}
if badFormat {
return is, fmt.Errorf(
"migration error: found metadata key in unexpected format: %s", k)
}
// Rejoin as "metadata.<key>"
newK := strings.Join([]string{kParts[0], kParts[2]}, ".")
newMetadata[newK] = v
delete(is.Attributes, k)
}
for k, v := range newMetadata {
is.Attributes[k] = v
}
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
func migrateStateV1toV2(is *terraform.InstanceState) (*terraform.InstanceState, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
// Maps service account index to list of scopes for that sccount
newScopesMap := make(map[string][]string)
for k, v := range is.Attributes {
if !strings.HasPrefix(k, "service_account.") {
continue
}
if k == "service_account.#" {
continue
}
if strings.HasSuffix(k, ".scopes.#") {
continue
}
if strings.HasSuffix(k, ".email") {
continue
}
// Key is now of the form service_account.%d.scopes.%d
kParts := strings.Split(k, ".")
// Sanity check: all three parts should be there and <N> should be a number
badFormat := false
if len(kParts) != 4 {
badFormat = true
} else if _, err := strconv.Atoi(kParts[1]); err != nil {
badFormat = true
}
if badFormat {
return is, fmt.Errorf(
"migration error: found scope key in unexpected format: %s", k)
}
newScopesMap[kParts[1]] = append(newScopesMap[kParts[1]], v)
delete(is.Attributes, k)
}
for service_acct_index, newScopes := range newScopesMap {
for _, newScope := range newScopes {
hash := hashcode.String(canonicalizeServiceScope(newScope))
newKey := fmt.Sprintf("service_account.%s.scopes.%d", service_acct_index, hash)
is.Attributes[newKey] = newScope
}
}
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
func migrateStateV2toV3(is *terraform.InstanceState) (*terraform.InstanceState, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
is.Attributes["create_timeout"] = "4"
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
func migrateStateV3toV4(is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
// Read instance from GCP. Since disks are not necessarily returned from the API in the order they were set,
// we have no other way to know which source belongs to which attached disk.
// Also note that the following code modifies the returned instance- if you need immutability, please change
// this to make a copy of the needed data.
config := meta.(*Config)
instance, err := getInstanceFromInstanceState(config, is)
if err != nil {
return is, fmt.Errorf("migration error: %s", err)
}
diskList, err := getAllDisksFromInstanceState(config, is)
if err != nil {
return is, fmt.Errorf("migration error: %s", err)
}
allDisks := make(map[string]*compute.Disk)
for _, disk := range diskList {
allDisks[disk.Name] = disk
}
hasBootDisk := is.Attributes["boot_disk.#"] == "1"
scratchDisks := 0
if v := is.Attributes["scratch_disk.#"]; v != "" {
scratchDisks, err = strconv.Atoi(v)
if err != nil {
return is, fmt.Errorf("migration error: found scratch_disk.# value in unexpected format: %s", err)
}
}
attachedDisks := 0
if v := is.Attributes["attached_disk.#"]; v != "" {
attachedDisks, err = strconv.Atoi(v)
if err != nil {
return is, fmt.Errorf("migration error: found attached_disk.# value in unexpected format: %s", err)
}
}
disks, err := strconv.Atoi(is.Attributes["disk.#"])
if err != nil {
return is, fmt.Errorf("migration error: found disk.# value in unexpected format: %s", err)
}
for i := 0; i < disks; i++ {
if !hasBootDisk && i == 0 {
is.Attributes["boot_disk.#"] = "1"
// Note: the GCP API does not allow for scratch disks to be boot disks, so this situation
// should never occur.
if is.Attributes["disk.0.scratch_disk"] == "true" {
return is, fmt.Errorf("migration error: found scratch disk at index 0")
}
for _, disk := range instance.Disks {
if disk.Boot {
sourceUrl := strings.Split(disk.Source, "/")
is.Attributes["boot_disk.0.source"] = sourceUrl[len(sourceUrl)-1]
is.Attributes["boot_disk.0.device_name"] = disk.DeviceName
break
}
}
is.Attributes["boot_disk.0.auto_delete"] = is.Attributes["disk.0.auto_delete"]
is.Attributes["boot_disk.0.disk_encryption_key_raw"] = is.Attributes["disk.0.disk_encryption_key_raw"]
is.Attributes["boot_disk.0.disk_encryption_key_sha256"] = is.Attributes["disk.0.disk_encryption_key_sha256"]
if is.Attributes["disk.0.size"] != "" && is.Attributes["disk.0.size"] != "0" {
is.Attributes["boot_disk.0.initialize_params.#"] = "1"
is.Attributes["boot_disk.0.initialize_params.0.size"] = is.Attributes["disk.0.size"]
}
if is.Attributes["disk.0.type"] != "" {
is.Attributes["boot_disk.0.initialize_params.#"] = "1"
is.Attributes["boot_disk.0.initialize_params.0.type"] = is.Attributes["disk.0.type"]
}
if is.Attributes["disk.0.image"] != "" {
is.Attributes["boot_disk.0.initialize_params.#"] = "1"
is.Attributes["boot_disk.0.initialize_params.0.image"] = is.Attributes["disk.0.image"]
}
} else if is.Attributes[fmt.Sprintf("disk.%d.scratch", i)] == "true" {
// Note: the GCP API does not allow for scratch disks without auto_delete, so this situation
// should never occur.
if is.Attributes[fmt.Sprintf("disk.%d.auto_delete", i)] != "true" {
return is, fmt.Errorf("migration error: attempted to migrate scratch disk where auto_delete is not true")
}
is.Attributes[fmt.Sprintf("scratch_disk.%d.interface", scratchDisks)] = "SCSI"
scratchDisks++
} else {
// If disk is neither boot nor scratch, then it is attached.
disk, err := getDiskFromAttributes(config, instance, allDisks, is.Attributes, i)
if err != nil {
return is, fmt.Errorf("migration error: %s", err)
}
is.Attributes[fmt.Sprintf("attached_disk.%d.source", attachedDisks)] = disk.Source
is.Attributes[fmt.Sprintf("attached_disk.%d.device_name", attachedDisks)] = disk.DeviceName
is.Attributes[fmt.Sprintf("attached_disk.%d.disk_encryption_key_raw", attachedDisks)] = is.Attributes[fmt.Sprintf("disk.%d.disk_encryption_key_raw", i)]
is.Attributes[fmt.Sprintf("attached_disk.%d.disk_encryption_key_sha256", attachedDisks)] = is.Attributes[fmt.Sprintf("disk.%d.disk_encryption_key_sha256", i)]
attachedDisks++
}
}
for k, _ := range is.Attributes {
if !strings.HasPrefix(k, "disk.") {
continue
}
delete(is.Attributes, k)
}
if scratchDisks > 0 {
is.Attributes["scratch_disk.#"] = strconv.Itoa(scratchDisks)
}
if attachedDisks > 0 {
is.Attributes["attached_disk.#"] = strconv.Itoa(attachedDisks)
}
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}
func migrateStateV4toV5(is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
if v := is.Attributes["disk.#"]; v != "" {
return migrateStateV3toV4(is, meta)
}
return is, nil
}
func getInstanceFromInstanceState(config *Config, is *terraform.InstanceState) (*compute.Instance, error) {
project, ok := is.Attributes["project"]
if !ok {
if config.Project == "" {
return nil, fmt.Errorf("could not determine 'project'")
} else {
project = config.Project
}
}
zone, ok := is.Attributes["zone"]
if !ok {
return nil, fmt.Errorf("could not determine 'zone'")
}
instance, err := config.clientCompute.Instances.Get(
project, zone, is.ID).Do()
if err != nil {
return nil, fmt.Errorf("error reading instance: %s", err)
}
return instance, nil
}
func getAllDisksFromInstanceState(config *Config, is *terraform.InstanceState) ([]*compute.Disk, error) {
project, ok := is.Attributes["project"]
if !ok {
if config.Project == "" {
return nil, fmt.Errorf("could not determine 'project'")
} else {
project = config.Project
}
}
zone, ok := is.Attributes["zone"]
if !ok {
return nil, fmt.Errorf("could not determine 'zone'")
}
diskList := []*compute.Disk{}
token := ""
for {
disks, err := config.clientCompute.Disks.List(project, zone).PageToken(token).Do()
if err != nil {
return nil, fmt.Errorf("error reading disks: %s", err)
}
diskList = append(diskList, disks.Items...)
token = disks.NextPageToken
if token == "" {
break
}
}
return diskList, nil
}
func getDiskFromAttributes(config *Config, instance *compute.Instance, allDisks map[string]*compute.Disk, attributes map[string]string, i int) (*compute.AttachedDisk, error) {
if diskSource := attributes[fmt.Sprintf("disk.%d.disk", i)]; diskSource != "" {
return getDiskFromSource(instance, diskSource)
}
if deviceName := attributes[fmt.Sprintf("disk.%d.device_name", i)]; deviceName != "" {
return getDiskFromDeviceName(instance, deviceName)
}
if encryptionKey := attributes[fmt.Sprintf("disk.%d.disk_encryption_key_raw", i)]; encryptionKey != "" {
return getDiskFromEncryptionKey(instance, encryptionKey)
}
autoDelete, err := strconv.ParseBool(attributes[fmt.Sprintf("disk.%d.auto_delete", i)])
if err != nil {
return nil, fmt.Errorf("error parsing auto_delete attribute of disk %d", i)
}
image := attributes[fmt.Sprintf("disk.%d.image", i)]
// We know project and zone are set because we used them to read the instance
project, ok := attributes["project"]
if !ok {
project = config.Project
}
zone := attributes["zone"]
return getDiskFromAutoDeleteAndImage(config, instance, allDisks, autoDelete, image, project, zone)
}
func getDiskFromSource(instance *compute.Instance, source string) (*compute.AttachedDisk, error) {
for _, disk := range instance.Disks {
if disk.Boot == true || disk.Type == "SCRATCH" {
// Ignore boot/scratch disks since this is just for finding attached disks
continue
}
// we can just compare suffixes because terraform only allows setting "disk" by name and uses
// the zone of the instance so we know there can be no duplicate names.
if strings.HasSuffix(disk.Source, "/"+source) {
return disk, nil
}
}
return nil, fmt.Errorf("could not find attached disk with source %q", source)
}
func getDiskFromDeviceName(instance *compute.Instance, deviceName string) (*compute.AttachedDisk, error) {
for _, disk := range instance.Disks {
if disk.Boot == true || disk.Type == "SCRATCH" {
// Ignore boot/scratch disks since this is just for finding attached disks
continue
}
if disk.DeviceName == deviceName {
return disk, nil
}
}
return nil, fmt.Errorf("could not find attached disk with deviceName %q", deviceName)
}
func getDiskFromEncryptionKey(instance *compute.Instance, encryptionKey string) (*compute.AttachedDisk, error) {
encryptionSha, err := hash256(encryptionKey)
if err != nil {
return nil, err
}
for _, disk := range instance.Disks {
if disk.Boot == true || disk.Type == "SCRATCH" {
// Ignore boot/scratch disks since this is just for finding attached disks
continue
}
if disk.DiskEncryptionKey.Sha256 == encryptionSha {
return disk, nil
}
}
return nil, fmt.Errorf("could not find attached disk with encryption hash %q", encryptionSha)
}
func getDiskFromAutoDeleteAndImage(config *Config, instance *compute.Instance, allDisks map[string]*compute.Disk, autoDelete bool, image, project, zone string) (*compute.AttachedDisk, error) {
img, err := resolveImage(config, project, image)
if err != nil {
return nil, err
}
imgParts := strings.Split(img, "/projects/")
canonicalImage := imgParts[len(imgParts)-1]
for i, disk := range instance.Disks {
if disk.Boot == true || disk.Type == "SCRATCH" {
// Ignore boot/scratch disks since this is just for finding attached disks
continue
}
if disk.AutoDelete == autoDelete {
// Read the disk to check if its image matches
sourceUrl := strings.Split(disk.Source, "/")
fullDisk := allDisks[sourceUrl[len(sourceUrl)-1]]
sourceImage, err := getRelativePath(fullDisk.SourceImage)
if err != nil {
return nil, err
}
if canonicalImage == sourceImage {
// Delete this disk because there might be multiple that match
instance.Disks = append(instance.Disks[:i], instance.Disks[i+1:]...)
return disk, nil
}
}
}
// We're not done! It's possible the disk was created with an image family rather than the image itself.
// Now, do the exact same iteration but do some prefix matching to check if the families match.
// This assumes that all disks with a given family have a sourceImage whose name starts with the name of
// the image family.
canonicalImage = strings.Replace(canonicalImage, "/family/", "/", -1)
for i, disk := range instance.Disks {
if disk.Boot == true || disk.Type == "SCRATCH" {
// Ignore boot/scratch disks since this is just for finding attached disks
continue
}
if disk.AutoDelete == autoDelete {
// Read the disk to check if its image matches
sourceUrl := strings.Split(disk.Source, "/")
fullDisk := allDisks[sourceUrl[len(sourceUrl)-1]]
sourceImage, err := getRelativePath(fullDisk.SourceImage)
if err != nil {
return nil, err
}
if strings.Contains(sourceImage, "/"+canonicalImage+"-") {
// Delete this disk because there might be multiple that match
instance.Disks = append(instance.Disks[:i], instance.Disks[i+1:]...)
return disk, nil
}
}
}
return nil, fmt.Errorf("could not find attached disk with image %q", image)
}
func migrateStateV5toV6(is *terraform.InstanceState) (*terraform.InstanceState, error) {
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
if is.Attributes["boot_disk.0.initialize_params.#"] == "1" {
if (is.Attributes["boot_disk.0.initialize_params.0.size"] == "0" ||
is.Attributes["boot_disk.0.initialize_params.0.size"] == "") &&
is.Attributes["boot_disk.0.initialize_params.0.type"] == "" &&
is.Attributes["boot_disk.0.initialize_params.0.image"] == "" {
is.Attributes["boot_disk.0.initialize_params.#"] = "0"
delete(is.Attributes, "boot_disk.0.initialize_params.0.size")
delete(is.Attributes, "boot_disk.0.initialize_params.0.type")
delete(is.Attributes, "boot_disk.0.initialize_params.0.image")
}
}
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}