From 24f147a72cd3e55975e662ae6104091dc601cbe3 Mon Sep 17 00:00:00 2001 From: Zheng Xi Zhou Date: Tue, 8 Feb 2022 18:48:38 +0800 Subject: [PATCH] Fix: support more Terraform variable types (#3204) Support Any, set and some complicated variable types Signed-off-by: Zheng Xi Zhou --- pkg/controller/utils/capability.go | 27 +++++++++- pkg/controller/utils/capability_test.go | 67 ++++++++++++++++++++++++- 2 files changed, 90 insertions(+), 4 deletions(-) diff --git a/pkg/controller/utils/capability.go b/pkg/controller/utils/capability.go index cbfe33526..c8d53c37b 100644 --- a/pkg/controller/utils/capability.go +++ b/pkg/controller/utils/capability.go @@ -61,11 +61,13 @@ const ( TerraformVariableMap string = "map" TerraformVariableObject string = "object" TerraformVariableNull string = "" + TerraformVariableAny string = "any" TerraformListTypePrefix string = "list(" TerraformTupleTypePrefix string = "tuple(" TerraformMapTypePrefix string = "map(" TerraformObjectTypePrefix string = "object(" + TerraformSetTypePrefix string = "set(" typeTraitDefinition = "trait" typeComponentDefinition = "component" @@ -157,17 +159,38 @@ func GetOpenAPISchemaFromTerraformComponentDefinition(configuration string) ([]b schema = openapi3.NewArraySchema() case TerraformVariableMap, TerraformVariableObject: schema = openapi3.NewObjectSchema() + case TerraformVariableAny: + switch v.Default.(type) { + case []interface{}: + schema = openapi3.NewArraySchema() + case map[string]interface{}: + schema = openapi3.NewObjectSchema() + } case TerraformVariableNull: - return nil, fmt.Errorf("null type variable is NOT supported, please specify a type for the variable: %s", v.Name) + switch v.Default.(type) { + case nil, string: + schema = openapi3.NewStringSchema() + case []interface{}: + schema = openapi3.NewArraySchema() + case map[string]interface{}: + schema = openapi3.NewObjectSchema() + case int, float64: + schema = openapi3.NewFloat64Schema() + default: + return nil, fmt.Errorf("null type variable is NOT supported, please specify a type for the variable: %s", v.Name) + } } // To identify unusual list type if schema == nil { switch { - case strings.HasPrefix(v.Type, TerraformListTypePrefix) || strings.HasPrefix(v.Type, TerraformTupleTypePrefix): + case strings.HasPrefix(v.Type, TerraformListTypePrefix) || strings.HasPrefix(v.Type, TerraformTupleTypePrefix) || + strings.HasPrefix(v.Type, TerraformSetTypePrefix): schema = openapi3.NewArraySchema() case strings.HasPrefix(v.Type, TerraformMapTypePrefix) || strings.HasPrefix(v.Type, TerraformObjectTypePrefix): schema = openapi3.NewObjectSchema() + default: + return nil, fmt.Errorf("the type `%s` of variable %s is NOT supported", v.Type, v.Name) } } schema.Title = k diff --git a/pkg/controller/utils/capability_test.go b/pkg/controller/utils/capability_test.go index 25276deb2..1dfed4254 100644 --- a/pkg/controller/utils/capability_test.go +++ b/pkg/controller/utils/capability_test.go @@ -20,6 +20,7 @@ package utils import ( "context" "errors" + "fmt" "io/ioutil" "os" "path/filepath" @@ -326,8 +327,38 @@ variable "name" { default = "abc" }`, want: want{ - subStr: "", - err: errors.New("null type variable is NOT supported, please specify a type for the variable: name"), + subStr: "abc", + err: nil, + }, + }, + "null type variable, while default value is a slice": { + configuration: ` +variable "name" { + default = [123] +}`, + want: want{ + subStr: "123", + err: nil, + }, + }, + "null type variable, while default value is a map": { + configuration: ` +variable "name" { + default = {a = 1} +}`, + want: want{ + subStr: "a", + err: nil, + }, + }, + "null type variable, while default value is number": { + configuration: ` +variable "name" { + default = 123 +}`, + want: want{ + subStr: "123", + err: nil, }, }, "complicated list variable": { @@ -354,6 +385,38 @@ variable "bbb" { config = string }) default = [] +}`, + want: want{ + subStr: "bbb", + err: nil, + }, + }, + "not supported complicated variable": { + configuration: ` +variable "bbb" { + type = xxxxx(string) +}`, + want: want{ + subStr: "", + err: fmt.Errorf("the type `%s` of variable %s is NOT supported", "xxxxx(string)", "bbb"), + }, + }, + "any type, slice default": { + configuration: ` +variable "bbb" { + type = any + default = [] +}`, + want: want{ + subStr: "bbb", + err: nil, + }, + }, + "any type, map default": { + configuration: ` +variable "bbb" { + type = any + default = {} }`, want: want{ subStr: "bbb",