-
Notifications
You must be signed in to change notification settings - Fork 13
/
resource_server_migrate.go
203 lines (171 loc) · 5.89 KB
/
resource_server_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
package ddcloud
import (
"fmt"
"log"
"sort"
"strings"
"strconv"
"github.com/hashicorp/terraform/terraform"
)
// Migrate state for ddcloud_server.
func resourceServerMigrateState(schemaVersion int, instanceState *terraform.InstanceState, provider interface{}) (migratedState *terraform.InstanceState, err error) {
if instanceState.Empty() {
log.Println("Empty Server state; nothing to migrate.")
migratedState = instanceState
return
}
const currentSchemaVersion = 3
for schemaVersion < currentSchemaVersion {
switch schemaVersion {
case 0:
log.Println("Found Server state v0; migrating to v1")
migratedState, err = migrateServerStateV0toV1(instanceState)
case 1:
log.Println("Found Server state v1; migrating to v2")
migratedState, err = migrateServerStateV1toV2(instanceState)
case 2:
log.Println("Found Server state v2; migrating to v3")
migratedState, err = migrateServerStateV2toV3(instanceState)
default:
err = fmt.Errorf("Unexpected schema version: %d", schemaVersion)
}
if err != nil {
return
}
schemaVersion++
}
return
}
// Migrate state for ddcloud_server (v0 to v1).
//
// disk.HASH.xxx -> disk.INDEX.xxx (where INDEX is the 0-based index of the disk in the set).
// Note that we should really be sorting disks by SCSI unit Id, but that's a little complicated for now.
//
// Also, os_image_id / os_image_name / customer_image_id / customer_image_name -> image, image_type
func migrateServerStateV0toV1(instanceState *terraform.InstanceState) (migratedState *terraform.InstanceState, err error) {
migratedState = instanceState
// Convert disks from Set ("disk.HASH.property") to List ("disk.INDEX.property")
//
// Where INDEX is the 0-based index of the disk in the set.
var keys []string
for key := range migratedState.Attributes {
keys = append(keys, key)
}
sort.Strings(keys)
nextIndex := 0
diskIndexesByHash := make(map[string]int)
for _, key := range keys {
if !strings.HasPrefix(key, "disk.") {
continue
}
// Should be "disk.HASH.property".
keyParts := strings.Split(key, ".")
if len(keyParts) != 3 {
continue
}
hash := keyParts[1]
diskIndex, ok := diskIndexesByHash[hash]
if !ok {
nextIndex++
diskIndex = nextIndex
diskIndexesByHash[hash] = diskIndex
}
value := migratedState.Attributes[key]
delete(migratedState.Attributes, key)
// Convert to "disk.N.property"
keyParts[1] = strconv.Itoa(diskIndex)
key = strings.Join(keyParts, ".")
migratedState.Attributes[key] = value
}
log.Printf("Server attributes after migration from v0 to v1: %#v",
migratedState.Attributes,
)
return
}
// Migrate state for ddcloud_server (v1 to v2).
//
// os_image_id = "xxx" -> image { id = "xxx", type = "os" }
// os_image_name = "xxx" -> image { name = "xxx", type = "os" }
// customer_image_id = "xxx" -> image { id = "xxx", type = "customer" }
// customer_image_name = "xxx" -> image { name = "xxx", type = "customer" }
func migrateServerStateV1toV2(instanceState *terraform.InstanceState) (migratedState *terraform.InstanceState, err error) {
migratedState = instanceState
var (
osImageID string
osImageName string
customerImageID string
customerImageName string
)
osImageID, _ = migratedState.Attributes[resourceKeyServerOSImageID]
delete(migratedState.Attributes, resourceKeyServerOSImageID)
osImageName, _ = migratedState.Attributes[resourceKeyServerOSImageName]
delete(migratedState.Attributes, resourceKeyServerOSImageName)
customerImageID, _ = migratedState.Attributes[resourceKeyServerCustomerImageID]
delete(migratedState.Attributes, resourceKeyServerCustomerImageID)
customerImageName, _ = migratedState.Attributes[resourceKeyServerCustomerImageName]
delete(migratedState.Attributes, resourceKeyServerCustomerImageName)
// Single-item list.
migratedState.Attributes[resourceKeyServerImage+".#"] = "1"
setImageProperty := func(propertyName string, propertyValue string) {
key := fmt.Sprintf("%s.0.%s",
resourceKeyServerImage,
propertyName,
)
migratedState.Attributes[key] = propertyValue
}
if osImageID != "" {
setImageProperty("id", osImageID)
setImageProperty(resourceKeyServerImageType, "os")
} else if osImageName != "" {
setImageProperty("name", osImageName)
setImageProperty(resourceKeyServerImageType, "os")
} else if customerImageID != "" {
setImageProperty("id", customerImageID)
setImageProperty(resourceKeyServerImageType, "customer")
} else if customerImageName != "" {
setImageProperty("name", customerImageName)
setImageProperty(resourceKeyServerImageType, "customer")
}
log.Printf("Server attributes after migration from v1 to v2: %#v",
migratedState.Attributes,
)
return
}
// Migrate state for ddcloud_server (v2 to v3).
//
// image { id = "xxx", type = "os" } -> image = "xxx"
// image { name = "xxx", type = "os" } -> image = "xxx"
// image { id = "xxx", type = "customer" } -> image = "xxx", image_type = "customer"
// image { name = "xxx", type = "customer" } -> image = "xxx", image_type = "customer"
func migrateServerStateV2toV3(instanceState *terraform.InstanceState) (migratedState *terraform.InstanceState, err error) {
migratedState = instanceState
const keyPrefix = "image."
var image, imageType string
for key := range migratedState.Attributes {
if !strings.HasPrefix(key, keyPrefix) {
continue
}
value := migratedState.Attributes[key]
delete(migratedState.Attributes, key)
if key == keyPrefix+"#" { // "image.#"
continue // Count - nothing else to do.
}
switch key[len(keyPrefix+"0")+1:] { // "image.0."
case "id":
case "name":
image = value
case resourceKeyServerImageType:
imageType = value
}
}
if image != "" {
migratedState.Attributes[resourceKeyServerImage] = image
if imageType == "customer" {
migratedState.Attributes[resourceKeyServerImageType] = "customer"
}
}
log.Printf("Server attributes after migration from v1 to v2: %#v",
migratedState.Attributes,
)
return
}