Skip to content
This repository has been archived by the owner on Dec 15, 2020. It is now read-only.

Commit

Permalink
Fix deletion of labels in UI (#1848)
Browse files Browse the repository at this point in the history
- Add endpoint for deletion of label by ID
- Use ID endpoint from frontend JS

Fixes #1847
  • Loading branch information
zwass authored Jun 25, 2018
1 parent 8b7edf7 commit 0683269
Show file tree
Hide file tree
Showing 8 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion frontend/kolide/entities/labels.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export default (client) => {
},
destroy: (label) => {
const { LABELS } = endpoints;
const endpoint = client._endpoint(`${LABELS}/${label.id}`);
const endpoint = client._endpoint(`${LABELS}/id/${label.id}`);

return client.authenticatedDelete(endpoint);
},
Expand Down
1 change: 0 additions & 1 deletion frontend/kolide/entities/queries.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ export default (client) => {
.then(response => response.query);
},
destroy: ({ id }) => {
console.log('foobar');
const { QUERIES } = endpoints;
const endpoint = `${client._endpoint(QUERIES)}/id/${id}`;

Expand Down
2 changes: 1 addition & 1 deletion frontend/test/mocks/label_mocks.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export default {
valid: (bearerToken, label) => {
return createRequestMock({
bearerToken,
endpoint: `/api/v1/kolide/labels/${label.id}`,
endpoint: `/api/v1/kolide/labels/id/${label.id}`,
method: 'delete',
response: {},
});
Expand Down
2 changes: 2 additions & 0 deletions server/kolide/labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ type LabelService interface {
GetLabel(ctx context.Context, id uint) (label *Label, err error)

DeleteLabel(ctx context.Context, name string) (err error)
// DeleteLabelByID is for backwards compatibility with the UI
DeleteLabelByID(ctx context.Context, id uint) (err error)

// HostIDsForLabel returns ids of hosts that belong to the label identified
// by lid
Expand Down
25 changes: 25 additions & 0 deletions server/service/endpoint_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,31 @@ func makeDeleteLabelEndpoint(svc kolide.Service) endpoint.Endpoint {
}
}

////////////////////////////////////////////////////////////////////////////////
// Delete Label By ID
////////////////////////////////////////////////////////////////////////////////

type deleteLabelByIDRequest struct {
ID uint
}

type deleteLabelByIDResponse struct {
Err error `json:"error,omitempty"`
}

func (r deleteLabelByIDResponse) error() error { return r.Err }

func makeDeleteLabelByIDEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(deleteLabelByIDRequest)
err := svc.DeleteLabelByID(ctx, req.ID)
if err != nil {
return deleteLabelByIDResponse{Err: err}, nil
}
return deleteLabelByIDResponse{}, nil
}
}

////////////////////////////////////////////////////////////////////////////////
// Apply Label Specs
////////////////////////////////////////////////////////////////////////////////
Expand Down
5 changes: 5 additions & 0 deletions server/service/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ type KolideEndpoints struct {
GetLabel endpoint.Endpoint
ListLabels endpoint.Endpoint
DeleteLabel endpoint.Endpoint
DeleteLabelByID endpoint.Endpoint
ApplyLabelSpecs endpoint.Endpoint
GetLabelSpecs endpoint.Endpoint
GetLabelSpec endpoint.Endpoint
Expand Down Expand Up @@ -172,6 +173,7 @@ func MakeKolideServerEndpoints(svc kolide.Service, jwtKey string) KolideEndpoint
GetLabel: authenticatedUser(jwtKey, svc, makeGetLabelEndpoint(svc)),
ListLabels: authenticatedUser(jwtKey, svc, makeListLabelsEndpoint(svc)),
DeleteLabel: authenticatedUser(jwtKey, svc, makeDeleteLabelEndpoint(svc)),
DeleteLabelByID: authenticatedUser(jwtKey, svc, makeDeleteLabelByIDEndpoint(svc)),
ApplyLabelSpecs: authenticatedUser(jwtKey, svc, makeApplyLabelSpecsEndpoint(svc)),
GetLabelSpecs: authenticatedUser(jwtKey, svc, makeGetLabelSpecsEndpoint(svc)),
GetLabelSpec: authenticatedUser(jwtKey, svc, makeGetLabelSpecEndpoint(svc)),
Expand Down Expand Up @@ -256,6 +258,7 @@ type kolideHandlers struct {
GetLabel http.Handler
ListLabels http.Handler
DeleteLabel http.Handler
DeleteLabelByID http.Handler
ApplyLabelSpecs http.Handler
GetLabelSpecs http.Handler
GetLabelSpec http.Handler
Expand Down Expand Up @@ -343,6 +346,7 @@ func makeKolideKitHandlers(e KolideEndpoints, opts []kithttp.ServerOption) *koli
GetLabel: newServer(e.GetLabel, decodeGetLabelRequest),
ListLabels: newServer(e.ListLabels, decodeListLabelsRequest),
DeleteLabel: newServer(e.DeleteLabel, decodeDeleteLabelRequest),
DeleteLabelByID: newServer(e.DeleteLabelByID, decodeDeleteLabelByIDRequest),
ApplyLabelSpecs: newServer(e.ApplyLabelSpecs, decodeApplyLabelSpecsRequest),
GetLabelSpecs: newServer(e.GetLabelSpecs, decodeNoParamsRequest),
GetLabelSpec: newServer(e.GetLabelSpec, decodeGetGenericSpecRequest),
Expand Down Expand Up @@ -471,6 +475,7 @@ func attachKolideAPIRoutes(r *mux.Router, h *kolideHandlers) {
r.Handle("/api/v1/kolide/labels/{id}", h.GetLabel).Methods("GET").Name("get_label")
r.Handle("/api/v1/kolide/labels", h.ListLabels).Methods("GET").Name("list_labels")
r.Handle("/api/v1/kolide/labels/{name}", h.DeleteLabel).Methods("DELETE").Name("delete_label")
r.Handle("/api/v1/kolide/labels/id/{id}", h.DeleteLabelByID).Methods("DELETE").Name("delete_label_by_id")
r.Handle("/api/v1/kolide/spec/labels", h.ApplyLabelSpecs).Methods("POST").Name("apply_label_specs")
r.Handle("/api/v1/kolide/spec/labels", h.GetLabelSpecs).Methods("GET").Name("get_label_specs")
r.Handle("/api/v1/kolide/spec/labels/{name}", h.GetLabelSpec).Methods("GET").Name("get_label_spec")
Expand Down
8 changes: 8 additions & 0 deletions server/service/service_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ func (svc service) DeleteLabel(ctx context.Context, name string) error {
return svc.ds.DeleteLabel(name)
}

func (svc service) DeleteLabelByID(ctx context.Context, id uint) error {
label, err := svc.ds.Label(id)
if err != nil {
return err
}
return svc.ds.DeleteLabel(label.Name)
}

func (svc service) HostIDsForLabel(lid uint) ([]uint, error) {
hosts, err := svc.ds.ListHostsInLabel(lid)
if err != nil {
Expand Down
10 changes: 10 additions & 0 deletions server/service/transport_labels.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,16 @@ func decodeDeleteLabelRequest(ctx context.Context, r *http.Request) (interface{}
return req, nil
}

func decodeDeleteLabelByIDRequest(ctx context.Context, r *http.Request) (interface{}, error) {
id, err := idFromRequest(r, "id")
if err != nil {
return nil, err
}
var req deleteLabelByIDRequest
req.ID = id
return req, nil
}

func decodeGetLabelRequest(ctx context.Context, r *http.Request) (interface{}, error) {
id, err := idFromRequest(r, "id")
if err != nil {
Expand Down

0 comments on commit 0683269

Please sign in to comment.