mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 17:08:53 +00:00
Fix deletion of labels in UI (#1848)
- Add endpoint for deletion of label by ID - Use ID endpoint from frontend JS Fixes #1847
This commit is contained in:
parent
8b7edf759d
commit
06832697d0
8 changed files with 52 additions and 3 deletions
|
|
@ -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);
|
||||
},
|
||||
|
|
|
|||
|
|
@ -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}`;
|
||||
|
||||
|
|
|
|||
|
|
@ -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: {},
|
||||
});
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
////////////////////////////////////////////////////////////////////////////////
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
@ -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)),
|
||||
|
|
@ -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
|
||||
|
|
@ -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),
|
||||
|
|
@ -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")
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
|
|
@ -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 {
|
||||
|
|
|
|||
Loading…
Reference in a new issue