Skip to content

Commit

Permalink
fix CR
Browse files Browse the repository at this point in the history
Signed-off-by: Young Bu Park <youngp@microsoft.com>
  • Loading branch information
youngbupark committed Jan 25, 2024
1 parent bb5fede commit 034feac
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 12 deletions.
18 changes: 13 additions & 5 deletions pkg/corerp/frontend/controller/applications/graph_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package applications
import (
"context"
"encoding/json"
"errors"
"net/url"
"sort"
"strings"
Expand All @@ -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"
Expand Down Expand Up @@ -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.
Expand All @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions pkg/corerp/frontend/controller/applications/graph_util_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,45 +178,45 @@ 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,
},
}

for _, tc := range tests {
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)
})
}
}

0 comments on commit 034feac

Please sign in to comment.