mirror of
https://github.com/kubevela/kubevela.git
synced 2026-08-23 22:46:53 +00:00
Fix: support more Terraform variable types (#2194)
Including null, list(...), map(...) Terraform types
This commit is contained in:
@@ -55,6 +55,12 @@ const (
|
||||
TerraformVariableTuple string = "tuple"
|
||||
TerraformVariableMap string = "map"
|
||||
TerraformVariableObject string = "object"
|
||||
TerraformVariableNull string = ""
|
||||
|
||||
TerraformListTypePrefix string = "list("
|
||||
TerraformTupleTypePrefix string = "tuple("
|
||||
TerraformMapTypePrefix string = "map("
|
||||
TerraformObjectTypePrefix string = "object("
|
||||
)
|
||||
|
||||
// ErrNoSectionParameterInCue means there is not parameter section in Cue template of a workload
|
||||
@@ -142,6 +148,18 @@ func GetOpenAPISchemaFromTerraformComponentDefinition(configuration string) ([]b
|
||||
schema = openapi3.NewArraySchema()
|
||||
case TerraformVariableMap, TerraformVariableObject:
|
||||
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)
|
||||
}
|
||||
|
||||
// To identify unusual list type
|
||||
if schema == nil {
|
||||
switch {
|
||||
case strings.HasPrefix(v.Type, TerraformListTypePrefix) || strings.HasPrefix(v.Type, TerraformTupleTypePrefix):
|
||||
schema = openapi3.NewArraySchema()
|
||||
case strings.HasPrefix(v.Type, TerraformMapTypePrefix) || strings.HasPrefix(v.Type, TerraformObjectTypePrefix):
|
||||
schema = openapi3.NewObjectSchema()
|
||||
}
|
||||
}
|
||||
schema.Title = k
|
||||
required = append(required, k)
|
||||
|
||||
@@ -18,6 +18,7 @@
|
||||
package utils
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"io/ioutil"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
@@ -240,7 +241,16 @@ func TestNewCapabilityComponentDef(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestGetOpenAPISchemaFromTerraformComponentDefinition(t *testing.T) {
|
||||
configuration := `
|
||||
type want struct {
|
||||
subStr string
|
||||
err error
|
||||
}
|
||||
cases := map[string]struct {
|
||||
configuration string
|
||||
want want
|
||||
}{
|
||||
"valid": {
|
||||
configuration: `
|
||||
module "rds" {
|
||||
source = "terraform-alicloud-modules/rds/alicloud"
|
||||
engine = "MySQL"
|
||||
@@ -300,11 +310,64 @@ variable "listVar" {
|
||||
|
||||
variable "mapVar" {
|
||||
type = "map"
|
||||
}`
|
||||
}`,
|
||||
want: want{
|
||||
subStr: "account_name",
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
"null type variable": {
|
||||
configuration: `
|
||||
variable "name" {
|
||||
default = "abc"
|
||||
}`,
|
||||
want: want{
|
||||
subStr: "",
|
||||
err: errors.New("null type variable is NOT supported, please specify a type for the variable: name"),
|
||||
},
|
||||
},
|
||||
"complicated list variable": {
|
||||
configuration: `
|
||||
variable "aaa" {
|
||||
type = list(object({
|
||||
type = string
|
||||
sourceArn = string
|
||||
config = string
|
||||
}))
|
||||
default = []
|
||||
}`,
|
||||
want: want{
|
||||
subStr: "aaa",
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
"complicated map variable": {
|
||||
configuration: `
|
||||
variable "bbb" {
|
||||
type = map({
|
||||
type = string
|
||||
sourceArn = string
|
||||
config = string
|
||||
})
|
||||
default = []
|
||||
}`,
|
||||
want: want{
|
||||
subStr: "bbb",
|
||||
err: nil,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
schema, err := GetOpenAPISchemaFromTerraformComponentDefinition(configuration)
|
||||
assert.NilError(t, err)
|
||||
data := string(schema)
|
||||
assert.Equal(t, strings.Contains(data, "account_name"), true)
|
||||
assert.Equal(t, strings.Contains(data, "intVar"), true)
|
||||
for name, tc := range cases {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
schema, err := GetOpenAPISchemaFromTerraformComponentDefinition(tc.configuration)
|
||||
if diff := cmp.Diff(tc.want.err, err, test.EquateErrors()); diff != "" {
|
||||
t.Errorf("\n%s\nGetOpenAPISchemaFromTerraformComponentDefinition(...): -want error, +got error:\n%s", name, diff)
|
||||
}
|
||||
if tc.want.err == nil {
|
||||
data := string(schema)
|
||||
assert.Equal(t, strings.Contains(data, tc.want.subStr), true)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user