-
Notifications
You must be signed in to change notification settings - Fork 114
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #107 from yannickstruyf3/fix-97
added nil checking for deviceProperties
- Loading branch information
Showing
12 changed files
with
4,813 additions
and
154 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.