Skip to content

Commit 4702c30

Browse files
authored
Merge pull request #107 from yannickstruyf3/fix-97
added nil checking for deviceProperties
2 parents 8989265 + e53bfa1 commit 4702c30

12 files changed

Lines changed: 4813 additions & 154 deletions
Lines changed: 22 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,44 @@
11
package nutanix
22

33
import (
4-
"fmt"
54
"log"
65
"sort"
7-
"strings"
8-
9-
"github.com/hashicorp/terraform/terraform"
106
)
117

12-
func resourceNutanixCategoriesMigrateState(
13-
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
14-
switch v {
15-
case 0:
16-
log.Println("[INFO] Found Nutanix State v0; migrating to v1")
17-
return migrateNutanixCategoriesV0toV1(is)
18-
default:
19-
return is, fmt.Errorf("Unexpected schema version: %d", v)
20-
}
21-
}
22-
23-
func migrateNutanixCategoriesV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
24-
if is.Empty() || is.Attributes == nil {
8+
func resourceNutanixCategoriesMigrateState(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
9+
if len(rawState) == 0 || rawState == nil {
2510
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
26-
return is, nil
11+
return rawState, nil
2712
}
2813

29-
keys := make([]string, 0, len(is.Attributes))
30-
for k := range is.Attributes {
14+
keys := make([]string, 0, len(rawState))
15+
for k := range rawState {
3116
keys = append(keys, k)
3217
}
3318

3419
sort.Strings(keys)
3520

36-
log.Printf("[DEBUG] Attributes before migration: %#v", is.Attributes)
21+
log.Printf("[DEBUG] meta: %#v", meta)
22+
log.Printf("[DEBUG] Attributes before migration: %#v", rawState)
3723

38-
if l, ok := is.Attributes["categories.%"]; ok && l != "" {
39-
var tempCat []map[string]string
40-
41-
for _, k := range keys {
42-
v := is.Attributes[k]
43-
44-
if k == "categories.%" {
45-
is.Attributes["categories.#"] = v
46-
delete(is.Attributes, "categories.%")
47-
continue
24+
if l, ok := rawState["categories"]; ok {
25+
if assertedL, ok := l.(map[string]interface{}); ok {
26+
c := make([]interface{}, 0)
27+
keys := make([]string, 0, len(assertedL))
28+
for k := range assertedL {
29+
keys = append(keys, k)
4830
}
49-
50-
if strings.HasPrefix(k, "categories.") {
51-
path := strings.Split(k, ".")
52-
if len(path) != 2 {
53-
return is, fmt.Errorf("found unexpected categories field: %#v", k)
54-
}
55-
56-
if path[1] == "#" {
57-
continue
58-
}
59-
60-
log.Printf("[DEBUG] key=%s", k)
61-
62-
tempCat = append(tempCat, map[string]string{
63-
"name": path[1],
64-
"value": v,
31+
sort.Strings(keys)
32+
for _, name := range keys {
33+
value := assertedL[name]
34+
c = append(c, map[string]interface{}{
35+
"name": name,
36+
"value": value.(string),
6537
})
66-
67-
delete(is.Attributes, k)
6838
}
69-
}
70-
flattenTempCategories(tempCat, is)
71-
}
72-
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
73-
return is, nil
74-
}
75-
76-
func flattenTempCategories(categories []map[string]string, is *terraform.InstanceState) {
77-
for index, category := range categories {
78-
for key, value := range category {
79-
is.Attributes[fmt.Sprintf("categories.%d.%s", index, key)] = value
39+
rawState["categories"] = c
8040
}
8141
}
42+
log.Printf("[DEBUG] Attributes after migration: %#v", rawState)
43+
return rawState, nil
8244
}

nutanix/categories_schema_migrate_test.go

Lines changed: 78 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,127 @@
11
package nutanix
22

33
import (
4+
"reflect"
45
"testing"
5-
6-
"github.com/hashicorp/terraform/terraform"
76
)
87

98
func TestResourceNutanixCategoriesteUpgradeV0(t *testing.T) {
109
cases := map[string]struct {
11-
StateVersion int
12-
Attributes map[string]string
13-
Expected map[string]string
14-
Meta interface{}
10+
Attributes map[string]interface{}
11+
Expected map[string]interface{}
12+
Meta interface{}
1513
}{
1614
"v0_without_values": {
17-
StateVersion: 0,
18-
Attributes: map[string]string{
19-
"categories.%": "0",
15+
Attributes: map[string]interface{}{
16+
"categories": make(map[string]interface{}),
2017
},
21-
Expected: map[string]string{
22-
"categories.#": "0",
18+
Expected: map[string]interface{}{
19+
"categories": make([]interface{}, 0),
2320
},
2421
},
2522
"v0_with_values": {
26-
StateVersion: 0,
27-
Attributes: map[string]string{
28-
"categories.%": "2",
29-
"categories.os_type": "ubuntu",
30-
"categories.os_version": "18.04",
23+
Attributes: map[string]interface{}{
24+
"categories": map[string]interface{}{
25+
"os_type": "ubuntu",
26+
"os_version": "18.04",
27+
},
3128
},
32-
Expected: map[string]string{
33-
"categories.#": "2",
34-
"categories.0.name": "os_type",
35-
"categories.0.value": "ubuntu",
36-
"categories.1.name": "os_version",
37-
"categories.1.value": "18.04",
29+
Expected: map[string]interface{}{
30+
31+
"categories": []interface{}{
32+
map[string]interface{}{"name": "os_type", "value": "ubuntu"},
33+
map[string]interface{}{"name": "os_version", "value": "18.04"},
34+
},
3835
},
3936
},
4037
"v0_categories_not_set": {
41-
StateVersion: 0,
42-
Attributes: map[string]string{
38+
Attributes: map[string]interface{}{
4339
"name": "test-name",
4440
},
45-
Expected: map[string]string{
41+
Expected: map[string]interface{}{
4642
"name": "test-name",
4743
},
4844
},
4945
"v0_multiple_categories": {
50-
StateVersion: 0,
51-
Attributes: map[string]string{
52-
"categories.%": "3",
53-
"categories.os_type": "ubuntu",
54-
"categories.os_version": "18.04",
55-
"categories.tier": "application",
56-
"categories.test": "test-value",
46+
Attributes: map[string]interface{}{
47+
"categories": map[string]interface{}{
48+
"os_type": "ubuntu",
49+
"os_version": "18.04",
50+
"tier": "application",
51+
"test": "test-value",
52+
},
5753
},
58-
Expected: map[string]string{
59-
"categories.#": "3",
60-
"categories.0.name": "os_type",
61-
"categories.0.value": "ubuntu",
62-
"categories.1.name": "os_version",
63-
"categories.1.value": "18.04",
64-
"categories.2.name": "test",
65-
"categories.2.value": "test-value",
66-
"categories.3.name": "tier",
67-
"categories.3.value": "application",
54+
Expected: map[string]interface{}{
55+
"categories": []interface{}{
56+
map[string]interface{}{"name": "os_type",
57+
"value": "ubuntu"},
58+
map[string]interface{}{"name": "os_version",
59+
"value": "18.04"},
60+
map[string]interface{}{"name": "test",
61+
"value": "test-value"},
62+
map[string]interface{}{"name": "tier",
63+
"value": "application"},
64+
},
6865
},
6966
},
7067
"v0_already_migrated": {
71-
StateVersion: 0,
72-
Attributes: map[string]string{
73-
"categories.#": "3",
74-
"categories.0.name": "os_type",
75-
"categories.0.value": "ubuntu",
76-
"categories.1.name": "os_version",
77-
"categories.1.value": "18.04",
78-
"categories.2.name": "tier",
79-
"categories.2.value": "application",
68+
Attributes: map[string]interface{}{
69+
"categories": []interface{}{
70+
map[string]interface{}{"name": "os_type",
71+
"value": "ubuntu"},
72+
map[string]interface{}{"name": "os_version",
73+
"value": "18.04"},
74+
map[string]interface{}{"name": "tier",
75+
"value": "application"},
76+
},
8077
},
81-
Expected: map[string]string{
82-
"categories.#": "3",
83-
"categories.0.name": "os_type",
84-
"categories.0.value": "ubuntu",
85-
"categories.1.name": "os_version",
86-
"categories.1.value": "18.04",
87-
"categories.2.name": "tier",
88-
"categories.2.value": "application",
78+
Expected: map[string]interface{}{
79+
"categories": []interface{}{
80+
map[string]interface{}{"name": "os_type",
81+
"value": "ubuntu"},
82+
map[string]interface{}{"name": "os_version",
83+
"value": "18.04"},
84+
map[string]interface{}{"name": "tier",
85+
"value": "application"},
86+
},
8987
},
9088
},
9189
"v0_empty_value": {
92-
StateVersion: 0,
93-
Attributes: map[string]string{
94-
"categories.%": "3",
95-
"categories.os_type": "",
96-
"categories.os_version": "",
97-
"categories.tier": "",
90+
Attributes: map[string]interface{}{
91+
"categories": map[string]interface{}{
92+
"os_type": "",
93+
"os_version": "",
94+
"tier": "",
95+
"test": "",
96+
},
9897
},
99-
Expected: map[string]string{
100-
"categories.#": "3",
101-
"categories.0.name": "os_type",
102-
"categories.0.value": "",
103-
"categories.1.name": "os_version",
104-
"categories.1.value": "",
105-
"categories.2.name": "tier",
106-
"categories.2.value": "",
98+
Expected: map[string]interface{}{
99+
"categories": []interface{}{
100+
map[string]interface{}{"name": "os_type",
101+
"value": ""},
102+
map[string]interface{}{"name": "os_version",
103+
"value": ""},
104+
map[string]interface{}{"name": "test",
105+
"value": ""},
106+
map[string]interface{}{"name": "tier",
107+
"value": ""},
108+
},
107109
},
108110
},
109111
}
110112

111113
for tn, tc := range cases {
112-
is := &terraform.InstanceState{
113-
ID: "i-abc123",
114-
Attributes: tc.Attributes,
115-
}
116-
is, err := resourceNutanixCategoriesMigrateState(
117-
tc.StateVersion, is, tc.Meta)
114+
is, err := resourceNutanixCategoriesMigrateState(tc.Attributes, tc.Meta)
118115

119116
if err != nil {
120117
t.Fatalf("bad: %s, err: %#v", tn, err)
121118
}
122119

123120
for k, v := range tc.Expected {
124-
if is.Attributes[k] != v {
121+
if !reflect.DeepEqual(is[k], v) {
125122
t.Fatalf(
126123
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
127-
tn, k, v, k, is.Attributes[k], is.Attributes)
124+
tn, k, v, k, is[k], is)
128125
}
129126
}
130127
}

0 commit comments

Comments
 (0)