From 7500c1b953d3a44f9280fe8f18c8b626ea43b2f8 Mon Sep 17 00:00:00 2001 From: Yash Mehrotra Date: Wed, 25 Oct 2023 14:57:59 +0530 Subject: [PATCH] fix: handle direct values in topology property lookups --- fixtures/topology/component-with-properties.yml | 8 ++++++++ pkg/topology/run.go | 7 +++++++ pkg/topology/run_test.go | 6 +++--- 3 files changed, 18 insertions(+), 3 deletions(-) diff --git a/fixtures/topology/component-with-properties.yml b/fixtures/topology/component-with-properties.yml index 14ee91f57..3db5df218 100644 --- a/fixtures/topology/component-with-properties.yml +++ b/fixtures/topology/component-with-properties.yml @@ -88,3 +88,11 @@ spec: {'name': 'company', 'text': 'Acme'}, {'name': 'location', 'text': 'Mars'}, ].toJSON() + # Test property as direct value + - name: key + lookup: + http: + - endpoint: https://httpbin.demo.aws.flanksource.com/status/200 + name: value_lookup + display: + expr: '"value"' diff --git a/pkg/topology/run.go b/pkg/topology/run.go index 7395dafa5..8ab9c5288 100644 --- a/pkg/topology/run.go +++ b/pkg/topology/run.go @@ -305,6 +305,13 @@ func lookupProperty(ctx *ComponentContext, property *v1.Property) ([]byte, error return nil, fmt.Errorf("unknown property type %T", results) } data := []byte(dataStr) + // When the lookup returns just a value + // set the current property's text as that value + if !isComponentList(data) && !isPropertyList(data) { + prop := pkg.NewProperty(*property) + prop.Text = dataStr + return json.Marshal(pkg.Properties{prop}) + } return data, nil } diff --git a/pkg/topology/run_test.go b/pkg/topology/run_test.go index 88e56fe77..210673cf9 100644 --- a/pkg/topology/run_test.go +++ b/pkg/topology/run_test.go @@ -49,9 +49,9 @@ var _ = ginkgo.Describe("Test topology run", ginkgo.Ordered, func() { componentB := rootComponent[0].Components[1] componentC := rootComponent[0].Components[2] - Expect(string(componentA.Properties.AsJSON())).To(MatchJSON(`[{"name":"error_percentage","value":1,"min":0,"max":100},{"name":"owner","text":"team-a"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"}]`)) - Expect(string(componentB.Properties.AsJSON())).To(MatchJSON(`[{"name":"error_percentage","value":10,"min":0,"max":100},{"name":"owner","text":"team-b"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"}]`)) - Expect(string(componentC.Properties.AsJSON())).To(MatchJSON(`[{"name":"error_percentage","value":50,"min":0,"max":100},{"name":"owner","text":"team-b"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"}]`)) + Expect(string(componentA.Properties.AsJSON())).To(MatchJSON(`[{"name":"error_percentage","value":1,"min":0,"max":100},{"name":"owner","text":"team-a"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"},{"name":"key","text":"value"}]`)) + Expect(string(componentB.Properties.AsJSON())).To(MatchJSON(`[{"name":"error_percentage","value":10,"min":0,"max":100},{"name":"owner","text":"team-b"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"},{"name":"key","text":"value"}]`)) + Expect(string(componentC.Properties.AsJSON())).To(MatchJSON(`[{"name":"error_percentage","value":50,"min":0,"max":100},{"name":"owner","text":"team-b"},{"name":"company","text":"Acme"},{"name":"location","text":"Mars"},{"name":"key","text":"value"}]`)) }) ginkgo.It("should create component with forEach functionality", func() {