Skip to content

Commit

Permalink
recompute non configurable defaults
Browse files Browse the repository at this point in the history
  • Loading branch information
Jordan Singer committed Nov 21, 2023
1 parent 8e8ad14 commit 2502e3c
Show file tree
Hide file tree
Showing 8 changed files with 58 additions and 37 deletions.
16 changes: 15 additions & 1 deletion pkg/engine2/operational_eval/vertex_property.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/klothoplatform/klotho/pkg/engine2/operational_rule"
"github.com/klothoplatform/klotho/pkg/engine2/solution_context"
knowledgebase "github.com/klothoplatform/klotho/pkg/knowledge_base2"
"go.uber.org/zap"
)

type (
Expand Down Expand Up @@ -110,6 +111,9 @@ func (prop *propertyVertex) UpdateFrom(otherV Vertex) {
}

func (v *propertyVertex) Evaluate(eval *Evaluator) error {
if v.Ref.Property == "Triggers" {
zap.L().Debug("evaluating triggers")
}
sol := eval.Solution.With("resource", v.Ref.Resource).With("property", v.Ref.Property)
res, err := sol.RawView().Vertex(v.Ref.Resource)
if err != nil {
Expand Down Expand Up @@ -161,7 +165,17 @@ func (v *propertyVertex) evaluateConstraints(sol solution_context.SolutionContex
return fmt.Errorf("could not get current value for %s: %w", v.Ref, err)
}

if currentValue == nil && setConstraint.Operator == "" && v.Template != nil && v.Template.DefaultValue != nil {
recompute := false
if v.Template != nil && v.Template.ConfigurationDisabled && v.Template.DefaultValue != nil && len(v.EdgeRules) == 0 {
// If the property is configured via edge rules, we don't want to recompute it
// as it will be set by the edge rules
recompute = true
}
setDefault := false
if currentValue == nil && v.Template != nil && v.Template.DefaultValue != nil {
setDefault = true
}
if setConstraint.Operator == "" && (setDefault || recompute) {
err = solution_context.ConfigureResource(
sol,
res,
Expand Down
12 changes: 6 additions & 6 deletions pkg/graph_addons/walk.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,19 +38,19 @@ func walk[K comparable, T any](
f WalkGraphFunc[K],
deps map[K]map[K]graph.Edge[K],
) error {
visited := make(set.Set[K])
queued := make(set.Set[K])
var queue []K

queued.Add(start)
for d := range deps[start] {
queue = append(queue, d)
queued.Add(d)
}
visited.Add(start)

var err error
var current K
for len(queue) > 0 {
current, queue = queue[0], queue[1:]
visited.Add(current)

nerr := f(current, err)
if errors.Is(nerr, StopWalk) {
Expand All @@ -62,10 +62,10 @@ func walk[K comparable, T any](
err = nerr

for d := range deps[current] {
if visited.Contains(d) {
continue
if !queued.Contains(d) {
queue = append(queue, d)
queued.Add(d)
}
queue = append(queue, d)
}
}
return err
Expand Down
32 changes: 18 additions & 14 deletions pkg/knowledge_base2/property_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -290,11 +290,11 @@ func (list *ListPropertyType) Parse(value any, ctx DynamicContext, data DynamicV
if !ok {
// before we fail, check to see if the entire value is a template
if strVal, ok := value.(string); ok {
var result []any
err := ctx.ExecuteDecode(strVal, data, &result)
return result, err
err := ctx.ExecuteDecode(strVal, data, &val)
if err != nil {
return nil, fmt.Errorf("invalid list value %v", value)
}
}
return nil, fmt.Errorf("invalid list value %v", value)
}

for _, v := range val {
Expand Down Expand Up @@ -331,11 +331,12 @@ func (s *SetPropertyType) Parse(value any, ctx DynamicContext, data DynamicValue
if !ok {
// before we fail, check to see if the entire value is a template
if strVal, ok := value.(string); ok {
var result []any
err := ctx.ExecuteDecode(strVal, data, &result)
return result, err
err := ctx.ExecuteDecode(strVal, data, &val)
if err != nil {
return nil, fmt.Errorf("invalid list value %v", value)
}
}
return nil, fmt.Errorf("invalid list value %v", value)

}

for _, v := range val {
Expand Down Expand Up @@ -370,12 +371,15 @@ func (m *MapPropertyType) Parse(value any, ctx DynamicContext, data DynamicValue
if !ok {
// before we fail, check to see if the entire value is a template
if strVal, ok := value.(string); ok {
err := ctx.ExecuteDecode(strVal, data, &result)
return result, err
}
mapVal, ok = value.(construct.Properties)
if !ok {
return nil, fmt.Errorf("invalid map value %v", value)
err := ctx.ExecuteDecode(strVal, data, &mapVal)
if err != nil {
return result, fmt.Errorf("invalid map value %v, decoding string err: %s", value, err)
}
} else {
mapVal, ok = value.(construct.Properties)
if !ok {
return nil, fmt.Errorf("invalid map value %v", value)
}
}
}
// If we are an object with sub properties then we know that we need to get the type of our sub properties to determine how we are parsed into a value
Expand Down
9 changes: 1 addition & 8 deletions pkg/templates/aws/edges/api_deployment-api_integration.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
source: aws:api_deployment
target: aws:api_integration
operational_rules:
- configuration_rules:
- resource: '{{ .Source }}'
configuration:
field: Triggers
value: |
{"{{ .Target.Name }}": "{{ .Target.Name }}"}
target: aws:api_integration
9 changes: 1 addition & 8 deletions pkg/templates/aws/edges/api_deployment-api_method.yaml
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
source: aws:api_deployment
target: aws:api_method
operational_rules:
- configuration_rules:
- resource: '{{ .Source }}'
configuration:
field: Triggers
value: |
{"{{ .Target.Name }}": "{{ .Target.Name }}"}
target: aws:api_method
13 changes: 13 additions & 0 deletions pkg/templates/aws/resources/api_deployment.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ properties:
- aws:rest_api
Triggers:
type: map(string,string)
configuration_disabled: true
default_value: |
{{ $api := fieldValue "RestApi" .Self }}
{{ $integrations := allDownstream "aws:api_integration" $api}}
{{ $methods := allDownstream "aws:api_method" $api }}
{
{{- range $index, $integration := $integrations }}
"integ/{{ $integration.Name }}": "{{ $integration.Name }}"{{ if or (ne $index (sub (len $integrations) 1)) (ne (len $methods) 0) }},{{ end}}
{{- end }}
{{- range $index, $method := $methods }}
"method/{{ $method.Name }}": "{{ $method.Name }}"{{ if ne $index (sub (len $integrations) 1) }},{{ end}}
{{- end }}
}
classification:

Expand Down
2 changes: 2 additions & 0 deletions pkg/templates/aws/resources/api_integration.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ properties:
Resource:
type: resource(aws:api_resource)
operational_rule:
if: |
{{ ne (fieldValue "Route" .Self) "/" }}
steps:
- direction: upstream
resources:
Expand Down
2 changes: 2 additions & 0 deletions pkg/templates/aws/resources/api_method.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ properties:
Resource:
type: resource(aws:api_resource)
operational_rule:
if: |
{{ ne (fieldValue "Route" (downstream "aws:api_integration" .Self)) "/" }}
steps:
- direction: upstream
resources:
Expand Down

0 comments on commit 2502e3c

Please sign in to comment.