This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
forked from yandex-cloud/terraform-provider-yandex
-
Notifications
You must be signed in to change notification settings - Fork 6
/
resource_yandex_compute_instance_migrate.go
78 lines (61 loc) · 1.76 KB
/
resource_yandex_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
package yandex
import (
"fmt"
"log"
"strconv"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/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
}
// when adding case 1, 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)
newResourcesMap := make(map[string]string)
for k, value := range is.Attributes {
if !strings.HasPrefix(k, "resources.") {
continue
}
if k == "resources.#" {
continue
}
// Key is now of the form resources.%d.{core,core_fraction,memory}
keyParts := strings.Split(k, ".")
// Sanity check
badFormat := false
if len(keyParts) != 3 {
badFormat = true
} else if _, err := strconv.Atoi(keyParts[1]); err != nil {
badFormat = true
}
if badFormat {
return is, fmt.Errorf(
"migration error: found resource key in unexpected format: %s", k)
}
// New key formed without Set hashcode, just List 0-indexed
newKey := fmt.Sprintf("%s.0.%s", keyParts[0], keyParts[2])
newResourcesMap[newKey] = value
delete(is.Attributes, k)
}
for newKey, value := range newResourcesMap {
is.Attributes[newKey] = value
}
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}