Skip to content

Commit

Permalink
Update RequireResource to handle duplicate short names and proper ful…
Browse files Browse the repository at this point in the history
…ly qualified names (also make life a little better for windows file system contributors) (radius-project#7134)

# Description

Updated clivalidation.RequireResourceType so that it can take either
fully qualified names or short names.. and if a short name is used and
there are duplicate short names display the list of matching short
names.

I also added a .gitattributes to the following folders so that their
test files would maintain lf line ends instead of crlf line ends when
cloned or pulled into a windows filesystem.

* pkg/corperp/renderers/containers/testdata
* pkg/recipes/terraform/config/testdata
* pkg/recipes/terraform/testdata/.terraform/modules


## Type of change

This pull request fixes a bug in Radius and has an approved issue 

Fixes: radius-project#7070

This pull request fixes a bug in Radius but its not an approved issue

Fixes:  radius-project#7121

---------

Signed-off-by: Josh <josh@liveoak.ws>
  • Loading branch information
jhandel authored Feb 8, 2024
1 parent e261d3a commit f26db73
Show file tree
Hide file tree
Showing 8 changed files with 132 additions and 3 deletions.
14 changes: 11 additions & 3 deletions pkg/cli/clivalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ func RequireResource(cmd *cobra.Command, args []string) (resourceType string, re
// are present. If either is missing, an error is returned.
func RequireResourceTypeAndName(args []string) (string, string, error) {
if len(args) < 2 {
return "", "", errors.New("No resource type or name provided")
return "", "", errors.New("no resource type or name provided")
}
resourceType, err := RequireResourceType(args)
if err != nil {
Expand All @@ -270,13 +270,21 @@ func RequireResourceType(args []string) (string, error) {
}
resourceTypeName := args[0]
supportedTypes := []string{}
foundTypes := []string{}
for _, resourceType := range clients.ResourceTypesList {
supportedType := strings.Split(resourceType, "/")[1]
supportedTypes = append(supportedTypes, supportedType)
if strings.EqualFold(supportedType, resourceTypeName) {
return resourceType, nil
//check to see if the resource type is the correct short or long name.
if strings.EqualFold(supportedType, resourceTypeName) || strings.EqualFold(resourceType, resourceTypeName) {
foundTypes = append(foundTypes, resourceType)
}
}
if len(foundTypes) == 1 {
return foundTypes[0], nil
} else if len(foundTypes) > 1 {
return "", fmt.Errorf("multiple resource types match '%s'. Please specify the full resource type and try again:\n\n%s\n",
resourceTypeName, strings.Join(foundTypes, "\n"))
}
return "", fmt.Errorf("'%s' is not a valid resource type. Available Types are: \n\n%s\n",
resourceTypeName, strings.Join(supportedTypes, "\n"))
}
Expand Down
82 changes: 82 additions & 0 deletions pkg/cli/clivalidation_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
Copyright 2024 The Radius Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package cli

import (
"errors"
"fmt"
"strings"
"testing"

"github.com/radius-project/radius/pkg/cli/clients"
"github.com/stretchr/testify/require"
)

func Test_RequireResourceType(t *testing.T) {

supportedTypes := []string{}

for _, resourceType := range clients.ResourceTypesList {
supportedType := strings.Split(resourceType, "/")[1]
supportedTypes = append(supportedTypes, supportedType)
}

resourceTypesErrorString := strings.Join(supportedTypes, "\n")

tests := []struct {
name string
args []string
want string
wantErr error
}{
{
name: "No arguments",
args: []string{},
want: "",
wantErr: errors.New("no resource type provided"),
},
{
name: "Supported resource type",
args: []string{"mongoDatabases"},
want: "Applications.Datastores/mongoDatabases",
wantErr: nil,
},
{
name: "Multiple resource types",
args: []string{"secretStores"},
want: "",
wantErr: fmt.Errorf("multiple resource types match 'secretStores'. Please specify the full resource type and try again:\n\nApplications.Dapr/secretStores\nApplications.Core/secretStores\n"),
},
{
name: "Unsupported resource type",
args: []string{"unsupported"},
want: "",
wantErr: fmt.Errorf("'unsupported' is not a valid resource type. Available Types are: \n\n" + resourceTypesErrorString + "\n"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, err := RequireResourceType(tt.args)
if len(tt.want) > 0 {
require.Equal(t, tt.want, got)
} else {
require.Equal(t, tt.wantErr, err)
}
})
}
}
9 changes: 9 additions & 0 deletions pkg/cli/cmd/resource/delete/delete_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,15 @@ func Test_Validate(t *testing.T) {
Config: configWithWorkspace,
},
},
{
Name: "List Command with ambiguous args",
Input: []string{"secretStores"},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Config: configWithWorkspace,
},
},
}
radcli.SharedValidateValidation(t, NewCommand, testcases)
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/cli/cmd/resource/list/list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,15 @@ func Test_Validate(t *testing.T) {
Config: configWithWorkspace,
},
},
{
Name: "List Command with ambiguous args",
Input: []string{"secretStores"},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Config: configWithWorkspace,
},
},
}
radcli.SharedValidateValidation(t, NewCommand, testcases)
}
Expand Down
9 changes: 9 additions & 0 deletions pkg/cli/cmd/resource/show/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,15 @@ func Test_Validate(t *testing.T) {
Config: configWithWorkspace,
},
},
{
Name: "List Command with ambiguous args",
Input: []string{"secretStores"},
ExpectedValid: false,
ConfigHolder: framework.ConfigHolder{
ConfigFilePath: "",
Config: configWithWorkspace,
},
},
}
radcli.SharedValidateValidation(t, NewCommand, testcases)
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/corerp/renderers/container/testdata/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#test files should use lf line endings
*.tf text eol=lf
*.json text eol=lf
*.yaml text eol=lf
4 changes: 4 additions & 0 deletions pkg/recipes/terraform/config/testdata/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#test files should use lf line endings
*.tf text eol=lf
*.json text eol=lf
*.yaml text eol=lf
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#test files should use lf line endings
*.tf text eol=lf
*.json text eol=lf
*.yaml text eol=lf

0 comments on commit f26db73

Please sign in to comment.