Fix: support more Terraform variable types (#3204)

Support Any, set and some complicated variable types

Signed-off-by: Zheng Xi Zhou <zzxwill@gmail.com>
This commit is contained in:
Zheng Xi Zhou
2022-02-08 18:48:38 +08:00
committed by GitHub
parent 7395994bdd
commit 24f147a72c
2 changed files with 90 additions and 4 deletions
+25 -2
View File
@@ -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
+65 -2
View File
@@ -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",