Skip to content

Commit

Permalink
Add command to force delete a Runner resource class
Browse files Browse the repository at this point in the history
  • Loading branch information
christian-stephen committed Jul 14, 2022
1 parent e15f916 commit f848adb
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 10 deletions.
9 changes: 7 additions & 2 deletions api/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,13 @@ func (r *Runner) GetResourceClassesByNamespace(namespace string) ([]ResourceClas
return resp.Items, err
}

func (r *Runner) DeleteResourceClass(id string) error {
req, err := r.rc.NewRequest("DELETE", &url.URL{Path: "runner/resource/" + url.PathEscape(id)}, nil)
func (r *Runner) DeleteResourceClass(id string, force bool) error {
path := "runner/resource/" + url.PathEscape(id)
if force {
path = fmt.Sprintf("runner/resource/%s/force", url.PathEscape(id))
}

req, err := r.rc.NewRequest("DELETE", &url.URL{Path: path}, nil)
if err != nil {
return err
}
Expand Down
23 changes: 21 additions & 2 deletions api/runner/runner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ func TestRunner_DeleteResourceClass(t *testing.T) {
defer cleanup()

t.Run("Check resource-class is deleted", func(t *testing.T) {
err := runner.DeleteResourceClass("51628548-4627-4813-9f9b-8cc9637ac879")
err := runner.DeleteResourceClass("51628548-4627-4813-9f9b-8cc9637ac879", false)
assert.NilError(t, err)
})

Expand All @@ -167,13 +167,32 @@ func TestRunner_DeleteResourceClass(t *testing.T) {
})
}

func TestRunner_DeleteResourceClass_Force(t *testing.T) {
fix := fixture{}
runner, cleanup := fix.Run(http.StatusOK, ``)
defer cleanup()

err := runner.DeleteResourceClass("5a1ef22d-444b-45db-8e98-21d7c42fb80b", true)
assert.NilError(t, err)

assert.Check(t, cmp.Equal(fix.URL(), url.URL{Path: "/api/v2/runner/resource/5a1ef22d-444b-45db-8e98-21d7c42fb80b/force"}))
assert.Check(t, cmp.Equal(fix.method, "DELETE"))
assert.Check(t, cmp.DeepEqual(fix.Header(), http.Header{
"Accept-Encoding": {"gzip"},
"Accept-Type": {"application/json"},
"Circle-Token": {"fake-token"},
"User-Agent": {version.UserAgent()},
}))
assert.Check(t, cmp.Equal(fix.Body(), ``))
}

func TestRunner_DeleteResourceClass_PathEscaping(t *testing.T) {
fix := fixture{}
runner, cleanup := fix.Run(http.StatusOK, ``)
defer cleanup()

t.Run("Check resource-class is deleted", func(t *testing.T) {
err := runner.DeleteResourceClass("escape~,/;?~noescape~$&+:=@")
err := runner.DeleteResourceClass("escape~,/;?~noescape~$&+:=@", false)
assert.NilError(t, err)
})

Expand Down
10 changes: 7 additions & 3 deletions cmd/runner/resource_class.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ func newResourceClassCommand(o *runnerOpts, preRunE validator) *cobra.Command {
"Generate a default token")
cmd.AddCommand(createCmd)

cmd.AddCommand(&cobra.Command{
forceDelete := false
deleteCmd := &cobra.Command{
Use: "delete <resource-class>",
Short: "Delete a resource-class",
Aliases: []string{"rm"},
Expand All @@ -58,9 +59,12 @@ func newResourceClassCommand(o *runnerOpts, preRunE validator) *cobra.Command {
if err != nil {
return err
}
return o.r.DeleteResourceClass(rc.ID)
return o.r.DeleteResourceClass(rc.ID, forceDelete)
},
})
}
deleteCmd.PersistentFlags().BoolVarP(&forceDelete, "force", "f", false,
"Delete resource-class and any associated tokens")
cmd.AddCommand(deleteCmd)

cmd.AddCommand(&cobra.Command{
Use: "list <namespace>",
Expand Down
2 changes: 1 addition & 1 deletion cmd/runner/resource_class_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func (r *runnerMock) GetResourceClassesByNamespace(namespace string) ([]runner.R
return rcs, nil
}

func (r *runnerMock) DeleteResourceClass(id string) error {
func (r *runnerMock) DeleteResourceClass(id string, force bool) error {
for i, rc := range r.resourceClasses {
if rc.ID == id {
r.resourceClasses = append(r.resourceClasses[:i], r.resourceClasses[i+1:]...)
Expand Down
2 changes: 1 addition & 1 deletion cmd/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ type running interface {
GetResourceClassByName(resourceClass string) (rc *runner.ResourceClass, err error)
GetNamespaceByResourceClass(resourceClass string) (ns string, err error)
GetResourceClassesByNamespace(namespace string) ([]runner.ResourceClass, error)
DeleteResourceClass(id string) error
DeleteResourceClass(id string, force bool) error
CreateToken(resourceClass, nickname string) (token *runner.Token, err error)
GetRunnerTokensByResourceClass(resourceClass string) ([]runner.Token, error)
DeleteToken(id string) error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
Usage:
runner resource-class delete <resource-class>
runner resource-class delete <resource-class> [flags]

Aliases:
delete, rm

Flags:
-f, --force Delete resource-class and any associated tokens

0 comments on commit f848adb

Please sign in to comment.