Skip to content

Commit

Permalink
feat(cdsctl): cdsctl admin service delete --name ... (#3644)
Browse files Browse the repository at this point in the history
Signed-off-by: Yvonnick Esnault <[email protected]>
  • Loading branch information
yesnault authored Nov 30, 2018
1 parent f292d60 commit eaa06fa
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 2 deletions.
22 changes: 22 additions & 0 deletions cli/cdsctl/admin_services.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"fmt"
"reflect"
"strings"

"github.com/spf13/cobra"

Expand All @@ -20,6 +21,7 @@ func adminServices() *cobra.Command {
cli.NewListCommand(adminServiceListCmd, adminServiceListRun, nil),
cli.NewListCommand(adminServiceStatusCmd, adminServiceStatusRun, nil),
cli.NewCommand(adminServiceGetCmd, adminServiceGetRun, nil),
cli.NewDeleteCommand(adminServiceDeleteCmd, adminServiceDeleteRun, nil, withAllCommandModifiers()...),
})
}

Expand Down Expand Up @@ -131,3 +133,23 @@ func adminServiceGetRun(v cli.Values) error {
fmt.Println(string(btes))
return nil
}

var adminServiceDeleteCmd = cli.Command{
Name: "delete",
Short: "Delete a CDS service from registered service",
VariadicArgs: cli.Arg{
Name: "name",
},
}

func adminServiceDeleteRun(v cli.Values) error {
if v.GetString("name") == "" {
return fmt.Errorf("name for service is mandatory")
}
for _, n := range strings.Split(v.GetString("name"), ",") {
if err := client.ServiceDelete(n); err != nil {
return err
}
}
return nil
}
15 changes: 15 additions & 0 deletions engine/api/admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,21 @@ func (api *API) getAdminServicesHandler() service.Handler {
}
}

func (api *API) deleteAdminServiceHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
name := vars["name"]
srv, err := services.FindByName(api.mustDB(), name)
if err != nil {
return err
}
if err := services.Delete(api.mustDB(), srv); err != nil {
return err
}
return service.WriteJSON(w, srv, http.StatusOK)
}
}

func (api *API) getAdminServiceHandler() service.Handler {
return func(ctx context.Context, w http.ResponseWriter, r *http.Request) error {
vars := mux.Vars(r)
Expand Down
2 changes: 1 addition & 1 deletion engine/api/api_routes.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func (api *API) InitRouter() {
r.Handle("/admin/plugin/{name}/binary/{os}/{arch}/infos", r.GET(api.getGRPCluginBinaryInfosHandler))

// Admin service
r.Handle("/admin/service/{name}", r.GET(api.getAdminServiceHandler, NeedAdmin(true)))
r.Handle("/admin/service/{name}", r.GET(api.getAdminServiceHandler, NeedAdmin(true)), r.DELETE(api.deleteAdminServiceHandler, NeedAdmin(true)))
r.Handle("/admin/services", r.GET(api.getAdminServicesHandler, NeedAdmin(true)))
r.Handle("/admin/services/call", r.GET(api.getAdminServiceCallHandler, NeedAdmin(true)), r.POST(api.postAdminServiceCallHandler, NeedAdmin(true)), r.PUT(api.putAdminServiceCallHandler, NeedAdmin(true)), r.DELETE(api.deleteAdminServiceCallHandler, NeedAdmin(true)))

Expand Down
5 changes: 5 additions & 0 deletions sdk/cdsclient/client_admin.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,11 @@ func (c *client) ServiceNameCallGET(name string, query string) ([]byte, error) {
return btes, err
}

func (c *client) ServiceDelete(name string) error {
_, err := c.DeleteJSON(context.Background(), "/admin/service/"+name, nil)
return err
}

func (c *client) ServiceCallGET(stype string, query string) ([]byte, error) {
btes, _, _, err := c.Request(context.Background(), "GET", "/admin/services/call?type="+stype+"&query="+url.QueryEscape(query), nil)
return btes, err
Expand Down
1 change: 1 addition & 0 deletions sdk/cdsclient/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ type Filter struct {
type AdminService interface {
Services() ([]sdk.Service, error)
ServicesByName(name string) (*sdk.Service, error)
ServiceDelete(name string) error
ServicesByType(stype string) ([]sdk.Service, error)
ServiceNameCallGET(name string, url string) ([]byte, error)
ServiceCallGET(stype string, url string) ([]byte, error)
Expand Down
4 changes: 3 additions & 1 deletion tests/clictl_admin_services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,6 @@ testcases:
- script: {{.cds.build.cdsctl}} admin services request --name `{{.cds.build.cdsctl}} admin services list -q|grep hatchery|head -n1` --query /debug/pprof/goroutine\?debug\=2
assertions:
- result.code ShouldEqual 0
- result.systemout ShouldContainSubstring transport.go
- result.systemout ShouldContainSubstring transport.go
- script: {{.cds.build.cdsctl}} admin services delete --force `{{.cds.build.cdsctl}} admin services list -q|grep hatchery|head -n1`

0 comments on commit eaa06fa

Please sign in to comment.