Skip to content

Commit

Permalink
wire in new fields to cli
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Singer committed Dec 5, 2023
1 parent 9f7eab7 commit dae7778
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 28 deletions.
24 changes: 11 additions & 13 deletions pkg/engine2/cli.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import (
"fmt"
"os"
"path/filepath"
"reflect"
"runtime/pprof"
"strings"

"github.com/iancoleman/strcase"
"github.com/klothoplatform/klotho/pkg/analytics"
"github.com/klothoplatform/klotho/pkg/closenicely"
construct "github.com/klothoplatform/klotho/pkg/construct2"
Expand Down Expand Up @@ -150,6 +152,14 @@ func addSubProperties(properties map[string]any, subProperties map[string]knowle
"configurationDisabled": details.ConfigurationDisabled,
"required": details.Required,
}
validationFields := []string{"MinLength", "MaxLength", "MinValue", "MaxValue", "AllowedValues"}
for _, validationField := range validationFields {
valField := reflect.ValueOf(subProperty).Elem().FieldByName(validationField)
if valField.IsValid() && !valField.IsZero() {
val := valField.Interface()
properties[details.Name].(map[string]any)[strcase.ToLowerCamel(validationField)] = val
}
}
if subProperty.SubProperties() != nil {
properties[details.Name].(map[string]any)["properties"] = map[string]any{}
addSubProperties(properties[details.Name].(map[string]any)["properties"].(map[string]any), subProperty.SubProperties())
Expand All @@ -168,19 +178,7 @@ func (em *EngineMain) ListResourceTypes(cmd *cobra.Command, args []string) error

for _, resourceType := range resourceTypes {
properties := map[string]any{}
for _, property := range resourceType.Properties {
details := property.Details()
properties[details.Name] = map[string]any{
"type": property.Type(),
"deployTime": details.DeployTime,
"configurationDisabled": details.ConfigurationDisabled,
"required": details.Required,
}
if property.SubProperties() != nil {
properties[details.Name].(map[string]any)["properties"] = map[string]any{}
addSubProperties(properties[details.Name].(map[string]any)["properties"].(map[string]any), property.SubProperties())
}
}
addSubProperties(properties, resourceType.Properties)
typeAndClassifications[resourceType.QualifiedTypeName] = resourceInfo{
Classifications: resourceType.Classification.Is,
Properties: properties,
Expand Down
3 changes: 3 additions & 0 deletions pkg/knowledge_base2/reader/embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,9 @@ func ModelsFromFS(dir fs.FS) (map[string]*Model, error) {
func TemplatesFromFs(dir fs.FS, models map[string]*Model) (map[construct.ResourceId]*knowledgebase.ResourceTemplate, error) {
templates := map[construct.ResourceId]*knowledgebase.ResourceTemplate{}
err := fs.WalkDir(dir, ".", func(path string, d fs.DirEntry, nerr error) error {
if d.Name() == "lambda_function.yaml" {
zap.S().Debug("Skipping lambda function: ", path)
}
zap.S().Debug("Loading resource template: ", path)
if d.IsDir() {
return nil
Expand Down
42 changes: 29 additions & 13 deletions pkg/knowledge_base2/reader/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,8 @@ type (
MinLength *int `yaml:"min_length"`
MaxLength *int `yaml:"max_length"`

MinValue *float64 `yaml:"lower_bound"`
MaxValue *float64 `yaml:"upper_bound"`
MinValue *float64 `yaml:"min_value"`
MaxValue *float64 `yaml:"max_value"`

AllowedTypes construct.ResourceList `yaml:"allowed_types"`

Expand Down Expand Up @@ -85,7 +85,7 @@ func (p *Properties) Convert() (knowledgebase.Properties, error) {
}
props[name] = propertyType
}
return props, nil
return props, errs
}

func (p *Property) Convert() (knowledgebase.Property, error) {
Expand Down Expand Up @@ -130,18 +130,34 @@ func (p *Property) Convert() (knowledgebase.Property, error) {
continue
}

if dstField.Type() == srcField.Type() {
if dstField.Type().AssignableTo(srcField.Type()) {
dstField.Set(srcField)
} else {
if conversion, found := fieldConversion[fieldName]; found {
err := conversion(srcField, p, propertyType)
if err != nil {
return nil, err
}
} else {
return nil, fmt.Errorf("invalid property type %s", fieldName)
continue
}

if dstField.Kind() == reflect.Ptr && srcField.Kind() == reflect.Ptr {
if srcField.Type().Elem().AssignableTo(dstField.Type().Elem()) {
dstField.Set(srcField)
continue
} else if srcField.Type().Elem().ConvertibleTo(dstField.Type().Elem()) {
val := srcField.Elem().Convert(dstField.Type().Elem())
// set dest field to a pointer of val
dstField.Set(reflect.New(dstField.Type().Elem()))
dstField.Elem().Set(val)
continue
}
}

if conversion, found := fieldConversion[fieldName]; found {
err := conversion(srcField, p, propertyType)
if err != nil {
return nil, err
}
continue
}

return nil, fmt.Errorf("invalid field name %s, for property %s of type %s", fieldName, p.Name, p.Type)

}

details := propertyType.Details()
Expand Down Expand Up @@ -235,7 +251,7 @@ func init() {
}
args := strings.Split(val, ",")
if len(args) != 2 {
return nil, fmt.Errorf("invalid number of arguments for map property type")
return nil, fmt.Errorf("invalid number of arguments for map property type: %s", val)
}
keyVal, err := InitializeProperty(args[0])
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion pkg/templates/aws/resources/eks_fargate_profile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ properties:
Namespace:
type: string
Labels:
type: map(string)
type: map(string,string)

classifications:
is:
Expand Down
4 changes: 4 additions & 0 deletions pkg/templates/aws/resources/lambda_function.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ properties:
Timeout:
type: int
default_value: 180
min_value: 1
max_value: 900
MemorySize:
type: int
default_value: 512
min_value: 128
max_value: 10240
EfsAccessPoint:
type: resource(aws:efs_access_point)
LogGroup:
Expand Down
3 changes: 3 additions & 0 deletions pkg/templates/aws/resources/load_balancer.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,9 @@ properties:
Type:
type: string
default_value: network
allowed_values:
- network
- application
NlbUri:
type: string
configuration_disabled: true
Expand Down
2 changes: 1 addition & 1 deletion pkg/templates/kubernetes/resources/storage_class.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ properties:
mountOptions:
type: list(string)
allowVolumeExpansion:
type: boolean
type: bool
reclaimPolicy:
type: string
volumeBindingMode:
Expand Down

0 comments on commit dae7778

Please sign in to comment.