Skip to content

Commit

Permalink
Add rad resource delete command
Browse files Browse the repository at this point in the history
- issue: #2682
  • Loading branch information
Bharath Joginapally authored and Bharath Joginapally committed Jun 24, 2022
1 parent 1c248a7 commit db50968
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 0 deletions.
57 changes: 57 additions & 0 deletions cmd/rad/cmd/resourceDelete.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// ------------------------------------------------------------
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
// ------------------------------------------------------------

package cmd

import (
"errors"

"github.com/project-radius/radius/pkg/cli"
"github.com/project-radius/radius/pkg/cli/environments"
"github.com/spf13/cobra"
)

// resourceDeleteCmd command to show details of a resource
var resourceDeleteCmd = &cobra.Command{
Use: "delete",
Short: "Delete a RAD resource",
Long: "Deletes a RAD resource with the given name",
RunE: deleteResource,
}

func init() {
resourceCmd.AddCommand(resourceDeleteCmd)
}

func deleteResource(cmd *cobra.Command, args []string) error {
config := ConfigFromContext(cmd.Context())
env, err := cli.RequireEnvironment(cmd, config)
if err != nil {
return err
}

isUCPEnabled := false
if env.GetKind() == environments.KindKubernetes {
isUCPEnabled = env.(*environments.KubernetesEnvironment).GetEnableUCP()
}

if !isUCPEnabled {
return errors.New("Delete is not enabled")
}

client, err := environments.CreateApplicationsManagementClient(cmd.Context(), env)
if err != nil {
return err
}

resourceType, resourceName, err := cli.RequireResourceTypeAndName(args)
if err != nil {
return err
}

deleteResponse, err := client.DeleteResource(cmd.Context(), resourceType, resourceName)

return printOutput(cmd, deleteResponse, false)
}
1 change: 1 addition & 0 deletions pkg/cli/clients/clients.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ type LegacyManagementClient interface {
type ApplicationsManagementClient interface {
ListAllResourcesByApplication(ctx context.Context, applicationName string) ([]generated.GenericResource, error)
ShowResourceByApplication(ctx context.Context, applicationName string, resourceType string) ([]generated.GenericResource, error)
DeleteResource(ctx context.Context, resourceType string, resourceName string) (generated.GenericResourcesDeleteResponse, error)
}

func ShallowCopy(params DeploymentParameters) DeploymentParameters {
Expand Down
12 changes: 12 additions & 0 deletions pkg/cli/clivalidation.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,18 @@ func RequireResource(cmd *cobra.Command, args []string) (resourceType string, re
return results[0], results[1], nil
}

func RequireResourceTypeAndName(args []string) (string, string, error) {
if len(args) < 2 {
return "", "", errors.New("No resource Type or Name provided")
}
resourceType, err := RequireResourceType(args)
if err != nil {
return "", "", err
}
resourceName := args[1]
return resourceType, resourceName, nil
}

//example of resource Type: Applications.Core/httpRoutes, Applications.Connector/redisCaches
func RequireResourceType(args []string) (string, error) {
if len(args) < 1 {
Expand Down
6 changes: 6 additions & 0 deletions pkg/cli/ucp/ucp_management.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,14 @@ func (amc *ARMApplicationsManagementClient) ShowResourceByApplication(ctx contex
return results, nil
}

func (um *ARMApplicationsManagementClient) DeleteResource(ctx context.Context, resourceType string, resourceName string) (generated.GenericResourcesDeleteResponse, error) {
client := generated.NewGenericResourcesClient(um.Connection, um.RootScope, resourceType)
return client.Delete(ctx, resourceName, nil)
}

func isResourceWithApplication(ctx context.Context, resource generated.GenericResource, applicationName string) (bool, error) {
log := logr.FromContextOrDiscard(ctx)

obj, found := resource.Properties["application"]
// A resource may not have an application associated with it.
if !found {
Expand Down

0 comments on commit db50968

Please sign in to comment.