Skip to content

Commit

Permalink
refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
nithyatsu committed Jan 15, 2025
1 parent 7643379 commit ab8ce55
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 41 deletions.
27 changes: 26 additions & 1 deletion pkg/cli/cmd/resourcetype/common/resourcetype.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ limitations under the License.
package common

import (
"context"
"slices"

"github.com/radius-project/radius/pkg/cli/clients"
"github.com/radius-project/radius/pkg/cli/clierrors"
"github.com/radius-project/radius/pkg/cli/output"
"github.com/radius-project/radius/pkg/ucp/api/v20231001preview"
)
Expand Down Expand Up @@ -62,9 +67,29 @@ func GetResourceTypeTableFormat() output.FormatterOptions {
JSONPath: "{ .ResourceProviderNamespace }",
},
{
Heading: "DEFAULT APIVERSION",
Heading: "APIVERSION",
JSONPath: "{ .APIVersions }",
},
},
}
}

func GetResourceTypeDetails(ctx context.Context, resourceProviderName string, resourceTypeName string, client clients.ApplicationsManagementClient) (ResourceType, error) {
resourceProvider, err := client.GetResourceProviderSummary(ctx, "local", resourceProviderName)
if clients.Is404Error(err) {
return ResourceType{}, clierrors.Message("The resource provider %q was not found or has been deleted.", resourceProviderName)
} else if err != nil {
return ResourceType{}, err
}

resourceTypes := ResourceTypesForProvider(&resourceProvider)
idx := slices.IndexFunc(resourceTypes, func(rt ResourceType) bool {
return rt.Name == resourceProviderName+"/"+resourceTypeName
})

if idx < 0 {
return ResourceType{}, clierrors.Message("Resource type %q not found in resource provider %q.", resourceTypeName, resourceProvider)
}

return resourceTypes[idx], nil
}
21 changes: 2 additions & 19 deletions pkg/cli/cmd/resourcetype/create/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ package create

import (
"context"
"slices"
"strings"

"github.com/radius-project/radius/pkg/cli"
Expand Down Expand Up @@ -175,24 +174,8 @@ func (r *Runner) Run(ctx context.Context) error {
if err != nil {
return err
}

resourceProvider, err := client.GetResourceProviderSummary(ctx, "local", r.ResourceProvider.Name)
if clients.Is404Error(err) {
return clierrors.Message("The resource provider %q was not found or has been deleted.", r.ResourceProvider.Name)
} else if err != nil {
return err
}

resourceTypes := common.ResourceTypesForProvider(&resourceProvider)
idx := slices.IndexFunc(resourceTypes, func(rt common.ResourceType) bool {
return rt.Name == r.ResourceProvider.Name+"/"+r.ResourceTypeName
})

if idx < 0 {
return clierrors.Message("Resource type %q not found in resource provider %q.", r.ResourceTypeName, r.ResourceProvider)
}

err = r.Output.WriteFormatted(r.Format, resourceTypes[idx], common.GetResourceTypeTableFormat())
resourceTypeDetails, err := common.GetResourceTypeDetails(ctx, r.ResourceProvider.Name, r.ResourceTypeName, client)
err = r.Output.WriteFormatted(r.Format, resourceTypeDetails, common.GetResourceTypeTableFormat())
if err != nil {
return err
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/cli/cmd/resourcetype/create/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ func Test_Validate(t *testing.T) {
testcases := []radcli.ValidateInput{
{
Name: "Valid",
Input: []string{"coolResources", "--from-file", "testdata/valid.yaml"},
Input: []string{"testResources", "--from-file", "testdata/valid.yaml"},
ExpectedValid: true,
ConfigHolder: framework.ConfigHolder{Config: config},
},
Expand Down Expand Up @@ -136,7 +136,7 @@ func Test_Run(t *testing.T) {
JSONPath: "{ .ResourceProviderNamespace }",
},
{
Heading: "DEFAULT APIVERSION",
Heading: "APIVERSION",
JSONPath: "{ .APIVersions }",
},
},
Expand Down
22 changes: 3 additions & 19 deletions pkg/cli/cmd/resourcetype/show/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,9 @@ package show

import (
"context"
"slices"
"strings"

"github.com/radius-project/radius/pkg/cli"
"github.com/radius-project/radius/pkg/cli/clients"
"github.com/radius-project/radius/pkg/cli/clierrors"
"github.com/radius-project/radius/pkg/cli/cmd/commonflags"
"github.com/radius-project/radius/pkg/cli/cmd/resourcetype/common"
Expand Down Expand Up @@ -110,27 +108,13 @@ func (r *Runner) Run(ctx context.Context) error {
if err != nil {
return err
}

resourceProvider, err := client.GetResourceProviderSummary(ctx, "local", r.ResourceProviderNamespace)
if clients.Is404Error(err) {
return clierrors.Message("The resource provider %q was not found or has been deleted.", r.ResourceProviderNamespace)
} else if err != nil {
resourceTypeDetails, err := common.GetResourceTypeDetails(ctx, r.ResourceProviderNamespace, r.ResourceTypeSuffix, client)
if err != nil {
return err
}

resourceTypes := common.ResourceTypesForProvider(&resourceProvider)
idx := slices.IndexFunc(resourceTypes, func(rt common.ResourceType) bool {
return rt.Name == r.ResourceTypeName
})

if idx < 0 {
return clierrors.Message("Resource type %q not found in resource provider %q.", r.ResourceTypeSuffix, r.ResourceProviderNamespace)
}

err = r.Output.WriteFormatted(r.Format, resourceTypes[idx], common.GetResourceTypeTableFormat())
err = r.Output.WriteFormatted(r.Format, resourceTypeDetails, common.GetResourceTypeTableFormat())
if err != nil {
return err
}

return nil
}

0 comments on commit ab8ce55

Please sign in to comment.