From 034feac18f1ffb641682e7108eee40773f7b74a9 Mon Sep 17 00:00:00 2001 From: Young Bu Park Date: Thu, 25 Jan 2024 10:39:39 -0800 Subject: [PATCH] fix CR Signed-off-by: Young Bu Park --- .../controller/applications/graph_util.go | 18 +++++++++++++----- .../controller/applications/graph_util_test.go | 14 +++++++------- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/pkg/corerp/frontend/controller/applications/graph_util.go b/pkg/corerp/frontend/controller/applications/graph_util.go index 773a725d6d..6c5d1bae83 100644 --- a/pkg/corerp/frontend/controller/applications/graph_util.go +++ b/pkg/corerp/frontend/controller/applications/graph_util.go @@ -19,6 +19,7 @@ package applications import ( "context" "encoding/json" + "errors" "net/url" "sort" "strings" @@ -34,6 +35,11 @@ import ( "github.com/radius-project/radius/pkg/ucp/resources" ) +var ( + // ErrInvalidSource reprents the error when the source is not a valid resource ID or URL. + ErrInvalidSource = errors.New("source is not a valid resource ID or URL") +) + const ( connectionsPath = "/properties/connections" portsPath = "/properties/container/ports" @@ -511,12 +517,12 @@ func connectionsFromAPIData(resource generated.GenericResource, allResources []g // findSourceResource looks up resource id by using source string by the following steps: // 1. Immediately return the resource ID if the source is a valid resource ID. // 2. Parse the hostname from source and look up the hostname in the resource list if the source is a valid URL. -// 3. Otherwise, return the original source string with false boolean value. -func findSourceResource(source string, allResources []generated.GenericResource) (string, bool) { +// 3. Otherwise, return the original source string with error. +func findSourceResource(source string, allResources []generated.GenericResource) (string, error) { // 1. Return the resource id if the source is a valid resource ID id, err := resources.Parse(source) if err == nil && id.IsResource() { - return id.String(), true + return id.String(), nil } // 2. Parse hostname from source and look up hostname in resource list. @@ -529,15 +535,17 @@ func findSourceResource(source string, allResources []generated.GenericResource) sourceURL, err := url.Parse(source) if err == nil { + // Linear search resource name in resource list. for _, resource := range allResources { if to.String(resource.Name) == sourceURL.Hostname() { - return to.String(resource.ID), true + return to.String(resource.ID), nil } } + // Fall back to original source string if not found. } // 3. Return the original source string with false boolean value. - return orig, false + return orig, ErrInvalidSource } // providesFromAPIData is specifically to support HTTPRoute. diff --git a/pkg/corerp/frontend/controller/applications/graph_util_test.go b/pkg/corerp/frontend/controller/applications/graph_util_test.go index ec98d4f368..de94c21dab 100644 --- a/pkg/corerp/frontend/controller/applications/graph_util_test.go +++ b/pkg/corerp/frontend/controller/applications/graph_util_test.go @@ -178,35 +178,35 @@ func TestFindSourceResource(t *testing.T) { resourceDataFile string parsedSource string - ok bool + wantErr error }{ { name: "valid source ID", source: "/planes/radius/local/resourcegroups/default/providers/Applications.Datastores/sqlDatabases/sql-db", resourceDataFile: "graph-app-directroute-in.json", parsedSource: "/planes/radius/local/resourcegroups/default/providers/Applications.Datastores/sqlDatabases/sql-db", - ok: true, + wantErr: nil, }, { name: "invalid source", source: "invalid", resourceDataFile: "graph-app-directroute-in.json", parsedSource: "invalid", - ok: false, + wantErr: ErrInvalidSource, }, { name: "direct route without scheme", source: "backendapp:8080", resourceDataFile: "graph-app-directroute-in.json", parsedSource: "/planes/radius/local/resourcegroups/default/providers/Applications.Core/containers/backendapp", - ok: true, + wantErr: nil, }, { name: "direct route with scheme", source: "http://backendapp:8080", resourceDataFile: "graph-app-directroute-in.json", parsedSource: "/planes/radius/local/resourcegroups/default/providers/Applications.Core/containers/backendapp", - ok: true, + wantErr: nil, }, } @@ -214,9 +214,9 @@ func TestFindSourceResource(t *testing.T) { t.Run(tc.name, func(t *testing.T) { resources := []generated.GenericResource{} testutil.MustUnmarshalFromFile(tc.resourceDataFile, &resources) - parsedSource, ok := findSourceResource(tc.source, resources) + parsedSource, err := findSourceResource(tc.source, resources) require.Equal(t, tc.parsedSource, parsedSource) - require.Equal(t, tc.ok, ok) + require.ErrorIs(t, err, tc.wantErr) }) } }