Skip to content

Commit

Permalink
Merge pull request nutanix#111 from yannickstruyf3/bugfix/cloudinit-f…
Browse files Browse the repository at this point in the history
…inal

Bugfix/cloudinit final
  • Loading branch information
marinsalinas authored Apr 9, 2020
2 parents f7504f9 + 6768673 commit b460a80
Show file tree
Hide file tree
Showing 15 changed files with 4,935 additions and 45 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
module github.com/terraform-providers/terraform-provider-nutanix

require (
github.com/gogo/protobuf v1.2.0
github.com/golang/protobuf v1.3.1 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/hashicorp/terraform v0.12.3
Expand Down
44 changes: 44 additions & 0 deletions nutanix/categories_schema_migrate.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package nutanix

import (
"log"
"sort"
)

func resourceNutanixCategoriesMigrateState(rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
if len(rawState) == 0 || rawState == nil {
log.Println("[DEBUG] Empty InstanceState; nothing to migrate.")
return rawState, nil
}

keys := make([]string, 0, len(rawState))
for k := range rawState {
keys = append(keys, k)
}

sort.Strings(keys)

log.Printf("[DEBUG] meta: %#v", meta)
log.Printf("[DEBUG] Attributes before migration: %#v", rawState)

if l, ok := rawState["categories"]; ok {
if assertedL, ok := l.(map[string]interface{}); ok {
c := make([]interface{}, 0)
keys := make([]string, 0, len(assertedL))
for k := range assertedL {
keys = append(keys, k)
}
sort.Strings(keys)
for _, name := range keys {
value := assertedL[name]
c = append(c, map[string]interface{}{
"name": name,
"value": value.(string),
})
}
rawState["categories"] = c
}
}
log.Printf("[DEBUG] Attributes after migration: %#v", rawState)
return rawState, nil
}
128 changes: 128 additions & 0 deletions nutanix/categories_schema_migrate_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package nutanix

import (
"reflect"
"testing"
)

func TestResourceNutanixCategoriesteUpgradeV0(t *testing.T) {
cases := map[string]struct {
Attributes map[string]interface{}
Expected map[string]interface{}
Meta interface{}
}{
"v0_without_values": {
Attributes: map[string]interface{}{
"categories": make(map[string]interface{}),
},
Expected: map[string]interface{}{
"categories": make([]interface{}, 0),
},
},
"v0_with_values": {
Attributes: map[string]interface{}{
"categories": map[string]interface{}{
"os_type": "ubuntu",
"os_version": "18.04",
},
},
Expected: map[string]interface{}{

"categories": []interface{}{
map[string]interface{}{"name": "os_type", "value": "ubuntu"},
map[string]interface{}{"name": "os_version", "value": "18.04"},
},
},
},
"v0_categories_not_set": {
Attributes: map[string]interface{}{
"name": "test-name",
},
Expected: map[string]interface{}{
"name": "test-name",
},
},
"v0_multiple_categories": {
Attributes: map[string]interface{}{
"categories": map[string]interface{}{
"os_type": "ubuntu",
"os_version": "18.04",
"tier": "application",
"test": "test-value",
},
},
Expected: map[string]interface{}{
"categories": []interface{}{
map[string]interface{}{"name": "os_type",
"value": "ubuntu"},
map[string]interface{}{"name": "os_version",
"value": "18.04"},
map[string]interface{}{"name": "test",
"value": "test-value"},
map[string]interface{}{"name": "tier",
"value": "application"},
},
},
},
"v0_already_migrated": {
Attributes: map[string]interface{}{
"categories": []interface{}{
map[string]interface{}{"name": "os_type",
"value": "ubuntu"},
map[string]interface{}{"name": "os_version",
"value": "18.04"},
map[string]interface{}{"name": "tier",
"value": "application"},
},
},
Expected: map[string]interface{}{
"categories": []interface{}{
map[string]interface{}{"name": "os_type",
"value": "ubuntu"},
map[string]interface{}{"name": "os_version",
"value": "18.04"},
map[string]interface{}{"name": "tier",
"value": "application"},
},
},
},
"v0_empty_value": {
Attributes: map[string]interface{}{
"categories": map[string]interface{}{
"os_type": "",
"os_version": "",
"tier": "",
"test": "",
},
},
Expected: map[string]interface{}{
"categories": []interface{}{
map[string]interface{}{"name": "os_type",
"value": ""},
map[string]interface{}{"name": "os_version",
"value": ""},
map[string]interface{}{"name": "test",
"value": ""},
map[string]interface{}{"name": "tier",
"value": ""},
},
},
},
}

for tn, tc := range cases {
is, err := resourceNutanixCategoriesMigrateState(tc.Attributes, tc.Meta)

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

for k, v := range tc.Expected {
if !reflect.DeepEqual(is[k], v) {
t.Fatalf(
"bad: %s\n\n expected: %#v -> %#v\n got: %#v -> %#v\n in: %#v",
tn, k, v, k, is[k], is)
}
}
}
}
Loading

0 comments on commit b460a80

Please sign in to comment.