Skip to content

Commit

Permalink
fix: Handle empty custom fields correctly
Browse files Browse the repository at this point in the history
Empty custom fields should be passed as nil to Netbox and nil values
from Netbox should be passed to Terraform as an empty string.

Netbox distingushes between an Empty string and null so this behaviour
mirrors the web UI.
  • Loading branch information
bmhughes committed Nov 9, 2021
1 parent 7015047 commit b3d342e
Showing 1 changed file with 26 additions and 20 deletions.
46 changes: 26 additions & 20 deletions netbox/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,6 @@ func convertArrayInterfaceString(arrayInterface []interface{}) string {

// Pick the custom fields in the state file and update values with data from API
func updateCustomFieldsFromAPI(stateCustomFields, customFields interface{}) []map[string]string {

var tfCms []map[string]string

switch t := customFields.(type) {
Expand All @@ -196,11 +195,12 @@ func updateCustomFieldsFromAPI(stateCustomFields, customFields interface{}) []ma
for key, value := range t {
if stateCustomField.(map[string]interface{})["name"].(string) == key {
var strValue string
if value != nil {
cm := map[string]string{}
cm["name"] = key
cm["type"] = stateCustomField.(map[string]interface{})["type"].(string)

cm := map[string]string{}
cm["name"] = key
cm["type"] = stateCustomField.(map[string]interface{})["type"].(string)

if value != nil {
switch v := value.(type) {
case []interface{}:
strValue = convertArrayInterfaceString(v)
Expand All @@ -215,8 +215,11 @@ func updateCustomFieldsFromAPI(stateCustomFields, customFields interface{}) []ma
}

cm["value"] = strValue
tfCms = append(tfCms, cm)
} else {
cm["value"] = ""
}

tfCms = append(tfCms, cm)
}
}
}
Expand All @@ -241,23 +244,26 @@ func convertCustomFieldsFromTerraformToAPI(stateCustomFields []interface{}, cust
cfType := customField["type"].(string)
cfValue := customField["value"].(string)

if cfType == "integer" {
cfValueInt, _ := strconv.Atoi(cfValue)
toReturn[cfName] = cfValueInt
} else if cfType == CustomFieldBoolean {
if cfValue == "true" {
toReturn[cfName] = true
} else if cfValue == "false" {
toReturn[cfName] = false
if len(cfValue) > 0 {
if cfType == "integer" {
cfValueInt, _ := strconv.Atoi(cfValue)
toReturn[cfName] = cfValueInt
} else if cfType == CustomFieldBoolean {
if cfValue == "true" {
toReturn[cfName] = true
} else if cfValue == "false" {
toReturn[cfName] = false
}
} else if cfType == "multiple" {
cfValueArray := strings.Split(cfValue, ",")
sort.Strings(cfValueArray)
toReturn[cfName] = cfValueArray
} else {
toReturn[cfName] = cfValue
}
} else if cfType == "multiple" {
cfValueArray := strings.Split(cfValue, ",")
sort.Strings(cfValueArray)
toReturn[cfName] = cfValueArray
} else {
toReturn[cfName] = cfValue
toReturn[cfName] = nil
}

}

return toReturn
Expand Down

0 comments on commit b3d342e

Please sign in to comment.