Skip to content

Commit

Permalink
Merge pull request #107 from yannickstruyf3/fix-97
Browse files Browse the repository at this point in the history
added nil checking for deviceProperties
  • Loading branch information
marinsalinas authored Apr 2, 2020
2 parents 8989265 + e53bfa1 commit 4702c30
Show file tree
Hide file tree
Showing 12 changed files with 4,813 additions and 154 deletions.
82 changes: 22 additions & 60 deletions nutanix/categories_schema_migrate.go
Original file line number Diff line number Diff line change
@@ -1,82 +1,44 @@
package nutanix

import (
"fmt"
"log"
"sort"
"strings"

"github.com/hashicorp/terraform/terraform"
)

func resourceNutanixCategoriesMigrateState(
v int, is *terraform.InstanceState, meta interface{}) (*terraform.InstanceState, error) {
switch v {
case 0:
log.Println("[INFO] Found Nutanix State v0; migrating to v1")
return migrateNutanixCategoriesV0toV1(is)
default:
return is, fmt.Errorf("Unexpected schema version: %d", v)
}
}

func migrateNutanixCategoriesV0toV1(is *terraform.InstanceState) (*terraform.InstanceState, error) {
if is.Empty() || is.Attributes == nil {
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 is, nil
return rawState, nil
}

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

sort.Strings(keys)

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

if l, ok := is.Attributes["categories.%"]; ok && l != "" {
var tempCat []map[string]string

for _, k := range keys {
v := is.Attributes[k]

if k == "categories.%" {
is.Attributes["categories.#"] = v
delete(is.Attributes, "categories.%")
continue
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)
}

if strings.HasPrefix(k, "categories.") {
path := strings.Split(k, ".")
if len(path) != 2 {
return is, fmt.Errorf("found unexpected categories field: %#v", k)
}

if path[1] == "#" {
continue
}

log.Printf("[DEBUG] key=%s", k)

tempCat = append(tempCat, map[string]string{
"name": path[1],
"value": v,
sort.Strings(keys)
for _, name := range keys {
value := assertedL[name]
c = append(c, map[string]interface{}{
"name": name,
"value": value.(string),
})

delete(is.Attributes, k)
}
}
flattenTempCategories(tempCat, is)
}
log.Printf("[DEBUG] Attributes after migration: %#v", is.Attributes)
return is, nil
}

func flattenTempCategories(categories []map[string]string, is *terraform.InstanceState) {
for index, category := range categories {
for key, value := range category {
is.Attributes[fmt.Sprintf("categories.%d.%s", index, key)] = value
rawState["categories"] = c
}
}
log.Printf("[DEBUG] Attributes after migration: %#v", rawState)
return rawState, nil
}
159 changes: 78 additions & 81 deletions nutanix/categories_schema_migrate_test.go
Original file line number Diff line number Diff line change
@@ -1,130 +1,127 @@
package nutanix

import (
"reflect"
"testing"

"github.com/hashicorp/terraform/terraform"
)

func TestResourceNutanixCategoriesteUpgradeV0(t *testing.T) {
cases := map[string]struct {
StateVersion int
Attributes map[string]string
Expected map[string]string
Meta interface{}
Attributes map[string]interface{}
Expected map[string]interface{}
Meta interface{}
}{
"v0_without_values": {
StateVersion: 0,
Attributes: map[string]string{
"categories.%": "0",
Attributes: map[string]interface{}{
"categories": make(map[string]interface{}),
},
Expected: map[string]string{
"categories.#": "0",
Expected: map[string]interface{}{
"categories": make([]interface{}, 0),
},
},
"v0_with_values": {
StateVersion: 0,
Attributes: map[string]string{
"categories.%": "2",
"categories.os_type": "ubuntu",
"categories.os_version": "18.04",
Attributes: map[string]interface{}{
"categories": map[string]interface{}{
"os_type": "ubuntu",
"os_version": "18.04",
},
},
Expected: map[string]string{
"categories.#": "2",
"categories.0.name": "os_type",
"categories.0.value": "ubuntu",
"categories.1.name": "os_version",
"categories.1.value": "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": {
StateVersion: 0,
Attributes: map[string]string{
Attributes: map[string]interface{}{
"name": "test-name",
},
Expected: map[string]string{
Expected: map[string]interface{}{
"name": "test-name",
},
},
"v0_multiple_categories": {
StateVersion: 0,
Attributes: map[string]string{
"categories.%": "3",
"categories.os_type": "ubuntu",
"categories.os_version": "18.04",
"categories.tier": "application",
"categories.test": "test-value",
Attributes: map[string]interface{}{
"categories": map[string]interface{}{
"os_type": "ubuntu",
"os_version": "18.04",
"tier": "application",
"test": "test-value",
},
},
Expected: map[string]string{
"categories.#": "3",
"categories.0.name": "os_type",
"categories.0.value": "ubuntu",
"categories.1.name": "os_version",
"categories.1.value": "18.04",
"categories.2.name": "test",
"categories.2.value": "test-value",
"categories.3.name": "tier",
"categories.3.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": "test",
"value": "test-value"},
map[string]interface{}{"name": "tier",
"value": "application"},
},
},
},
"v0_already_migrated": {
StateVersion: 0,
Attributes: map[string]string{
"categories.#": "3",
"categories.0.name": "os_type",
"categories.0.value": "ubuntu",
"categories.1.name": "os_version",
"categories.1.value": "18.04",
"categories.2.name": "tier",
"categories.2.value": "application",
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]string{
"categories.#": "3",
"categories.0.name": "os_type",
"categories.0.value": "ubuntu",
"categories.1.name": "os_version",
"categories.1.value": "18.04",
"categories.2.name": "tier",
"categories.2.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": {
StateVersion: 0,
Attributes: map[string]string{
"categories.%": "3",
"categories.os_type": "",
"categories.os_version": "",
"categories.tier": "",
Attributes: map[string]interface{}{
"categories": map[string]interface{}{
"os_type": "",
"os_version": "",
"tier": "",
"test": "",
},
},
Expected: map[string]string{
"categories.#": "3",
"categories.0.name": "os_type",
"categories.0.value": "",
"categories.1.name": "os_version",
"categories.1.value": "",
"categories.2.name": "tier",
"categories.2.value": "",
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 := &terraform.InstanceState{
ID: "i-abc123",
Attributes: tc.Attributes,
}
is, err := resourceNutanixCategoriesMigrateState(
tc.StateVersion, is, tc.Meta)
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 is.Attributes[k] != v {
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.Attributes[k], is.Attributes)
tn, k, v, k, is[k], is)
}
}
}
Expand Down
Loading

0 comments on commit 4702c30

Please sign in to comment.