Support user config list containing an object
Elasticsearch index patterns are defined as such. In configuration
they look like this:
```
elasticsearch_user_config {
elasticsearch_version = "6"
index_patterns {
pattern = "foo"
max_index_count = 3
}
index_patterns {
pattern = "bar"
max_index_count = 5
}
}
```
@@ -124,7 +124,8 @@ func generateTerraformUserConfigSchema(key string, definition map[string]interfa
}
case "array":
var itemType schema.ValueType
- typeString := definition["items"].(map[string]interface{})["type"].(string)
+ itemDefinition := definition["items"].(map[string]interface{})
+ typeString := itemDefinition["type"].(string)
switch typeString {
case "number":
itemType = schema.TypeFloat
@@ -134,6 +135,8 @@ func generateTerraformUserConfigSchema(key string, definition map[string]interfa
itemType = schema.TypeBool
case "string":
itemType = schema.TypeString
+ case "object":
+ itemType = schema.TypeList
default:
panic(fmt.Sprintf("Unexpected user config schema array item type: %T / %v", typeString, typeString))
}
@@ -147,13 +150,19 @@ func generateTerraformUserConfigSchema(key string, definition map[string]interfa
diffFunction = ipFilterArrayDiffSuppressFunc
valueDiffFunction = ipFilterValueDiffSuppressFunc
}
+ var elem interface{}
+ if itemType == schema.TypeList {
+ elem = &schema.Resource{Schema: GenerateTerraformUserConfigSchema(itemDefinition)}
+ } else {
+ elem = &schema.Schema{
+ DiffSuppressFunc: valueDiffFunction,
+ Type: itemType,
+ }
+ }
return &schema.Schema{
Description: title,
DiffSuppressFunc: diffFunction,
- Elem: &schema.Schema{
- DiffSuppressFunc: valueDiffFunction,
- Type: itemType,
- },
+ Elem: elem,
MaxItems: maxItems,
Optional: true,
Sensitive: sensitive,
@@ -385,12 +394,7 @@ func convertTerraformUserConfigValueToAPICompatibleFormat(
if value == nil {
omit = true
} else {
- switch value.(type) {
- case []interface{}:
- default:
- panic(fmt.Sprintf("Invalid %v user config key type %T for %v, expected map", serviceType, value, key))
- }
- asList := value.([]interface{})
+ if asList, isList := value.([]interface{}); isList {
if len(asList) == 0 || (len(asList) == 1 && asList[0] == nil) {
omit = true
} else {
@@ -403,6 +407,13 @@ func convertTerraformUserConfigValueToAPICompatibleFormat(
)
}
}
+ } else if asMap, isMap := value.(map[string]interface{}); isMap {
+ convertedValue = convertTerraformUserConfigToAPICompatibleFormat(
+ serviceType, newResource, asMap, definition["properties"].(map[string]interface{}),
+ )
+ } else {
+ panic(fmt.Sprintf("Invalid %v user config key type %T for %v, expected map", serviceType, value, key))
+ }
}
case "array":
if value == nil {
