Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update RequireResource to handle duplicate short names and proper fully qualified names (also make life a little better for windows file system contributors) #7134

Merged
merged 4 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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) {
jhandel marked this conversation as resolved.
Show resolved Hide resolved
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
Loading