Skip to content

Commit

Permalink
refactor(kumactl) generalized the delete command in order to use for …
Browse files Browse the repository at this point in the history
…all resource types

Right now the design is to handle the resource types via subcommands. Refactored so that the delete command accepts a resourceType arg.
Based on the passed resource type arg the corresponding resource will be deleted.

* returns an error in case of no mesh with the given name.

Fix #279
  • Loading branch information
pradeepmurugesan committed Oct 17, 2019
1 parent ae78bee commit f505a47
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 54 deletions.
60 changes: 53 additions & 7 deletions app/kumactl/cmd/delete/delete.go
Original file line number Diff line number Diff line change
@@ -1,23 +1,69 @@
package delete

import (
"context"
kumactl_cmd "github.com/Kong/kuma/app/kumactl/pkg/cmd"
"github.com/Kong/kuma/pkg/core/resources/apis/mesh"
"github.com/Kong/kuma/pkg/core/resources/model"
"github.com/Kong/kuma/pkg/core/resources/registry"
"github.com/Kong/kuma/pkg/core/resources/store"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)

type deleteContext struct {
*kumactl_cmd.RootContext
}

func NewDeleteCmd(pctx *kumactl_cmd.RootContext) *cobra.Command {
ctx := &deleteContext{RootContext: pctx}
cmd := &cobra.Command{
Use: "delete",
Short: "Delete Kuma resources",
Long: `Delete Kuma resources.`,
Args: cobra.ExactArgs(2),
RunE: func(cmd *cobra.Command, args []string) error {
rs, err := pctx.CurrentResourceStore()
if err != nil {
return err
}
resourceTypeArg := args[0]
name := args[1]

var resource model.Resource
var resourceType model.ResourceType
switch resourceTypeArg {
case "mesh":
resourceType = model.ResourceType(mesh.MeshType)

default:
return errors.Errorf("unknown resource type: %s. Allowed types: mesh, dataplane, proxytemplate, traffic-log, traffic-permission", resourceTypeArg)
}

if resource, err = registry.Global().NewObject(resourceType); err != nil {
return err
}

if err := deleteResource(name, resource, resourceType, rs); err != nil {
return err
}

cmd.Printf("deleted %s %q\n", resourceType, name)
return nil
},
}

// sub-commands
cmd.AddCommand(newDeleteMeshCmd(ctx))
return cmd
}

func deleteResource(name string, resource model.Resource, resourceType model.ResourceType, rs store.ResourceStore) error {
getOptions := store.GetByKey(model.DefaultNamespace, name, name)
if err := rs.Get(context.Background(), resource, getOptions); err != nil {
if store.IsResourceNotFound(err) {
return errors.Errorf("there is no %s with name %q", resourceType, name)
}
return errors.Wrapf(err, "failed to get %s with the name %q", resourceType, name)
}

deleteOptions := store.DeleteByKey(model.DefaultNamespace, name, name)
if err := rs.Delete(context.Background(), resource, deleteOptions); err != nil {
return errors.Wrapf(err, "failed to delete %s with the name %q", resourceType, name)
}

return nil
}
44 changes: 0 additions & 44 deletions app/kumactl/cmd/delete/delete_meshes.go

This file was deleted.

6 changes: 3 additions & 3 deletions app/kumactl/cmd/delete/delete_meshes_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var _ = Describe("kumactl delete mesh", func() {
},
}

Describe("DeleteMeshCommand", func() {
Describe("Delete Mesh", func() {

var rootCtx *kumactl_cmd.RootContext
var rootCmd *cobra.Command
Expand Down Expand Up @@ -111,9 +111,9 @@ var _ = Describe("kumactl delete mesh", func() {
// then
Expect(err).To(HaveOccurred())
// and
Expect(err.Error()).To(Equal("accepts 1 arg(s), received 0"))
Expect(err.Error()).To(Equal("accepts 2 arg(s), received 1"))
// and
Expect(outbuf.String()).To(MatchRegexp(`Error: accepts 1 arg\(s\), received 0`))
Expect(outbuf.String()).To(MatchRegexp(`Error: accepts 2 arg\(s\), received 1`))
// and
Expect(errbuf.Bytes()).To(BeEmpty())
})
Expand Down
78 changes: 78 additions & 0 deletions app/kumactl/cmd/delete/delete_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package delete_test

import (
"bytes"
"github.com/Kong/kuma/app/kumactl/cmd"
kumactl_cmd "github.com/Kong/kuma/app/kumactl/pkg/cmd"
config_proto "github.com/Kong/kuma/pkg/config/app/kumactl/v1alpha1"
core_store "github.com/Kong/kuma/pkg/core/resources/store"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
"github.com/spf13/cobra"
"path/filepath"
"time"
)

var _ = Describe("kumactl delete ", func() {
Describe("Delete Command", func() {
var rootCtx *kumactl_cmd.RootContext
var rootCmd *cobra.Command
var outbuf, errbuf *bytes.Buffer
var store core_store.ResourceStore

BeforeEach(func() {
// setup
rootCtx = &kumactl_cmd.RootContext{
Runtime: kumactl_cmd.RootRuntime{
Now: func() time.Time { return time.Now() },
NewResourceStore: func(*config_proto.ControlPlaneCoordinates_ApiServer) (core_store.ResourceStore, error) {
return store, nil
},
},
}
rootCmd = cmd.NewRootCmd(rootCtx)
outbuf = &bytes.Buffer{}
errbuf = &bytes.Buffer{}
rootCmd.SetOut(outbuf)
rootCmd.SetErr(errbuf)
})

It("should throw an error in case of no args", func() {
// given
rootCmd.SetArgs([]string{
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"),
"delete"})

// when
err := rootCmd.Execute()

// then
Expect(err).To(HaveOccurred())
// and
Expect(err.Error()).To(Equal("accepts 2 arg(s), received 0"))
// and
Expect(outbuf.String()).To(MatchRegexp(`Error: accepts 2 arg\(s\), received 0`))
// and
Expect(errbuf.Bytes()).To(BeEmpty())
})

It("should throw an error in case of unsupported resource type", func() {
// given
rootCmd.SetArgs([]string{
"--config-file", filepath.Join("..", "testdata", "sample-kumactl.config.yaml"),
"delete", "some-type", "some-name"})

// when
err := rootCmd.Execute()

// then
Expect(err).To(HaveOccurred())
// and
Expect(err.Error()).To(Equal("unknown resource type: some-type. Allowed types: mesh, dataplane, proxytemplate, traffic-log, traffic-permission"))
// and
Expect(outbuf.String()).To(MatchRegexp(`unknown resource type: some-type. Allowed types: mesh, dataplane, proxytemplate, traffic-log, traffic-permission`))
// and
Expect(errbuf.Bytes()).To(BeEmpty())
})
})
})

0 comments on commit f505a47

Please sign in to comment.