Allow deleted label names to be replaced (#1309)

Fixes #1310
This commit is contained in:
Zachary Wasserman 2017-02-24 17:47:30 -08:00 committed by GitHub
parent d18c0aa146
commit 879363311c
4 changed files with 69 additions and 3 deletions

View File

@ -386,3 +386,42 @@ func testSaveLabel(t *testing.T, db kolide.Datastore) {
assert.Equal(t, label.Name, saved.Name)
assert.Equal(t, label.Description, saved.Description)
}
func testReplaceDeletedLabel(t *testing.T, db kolide.Datastore) {
if db.Name() == "inmem" {
t.Skip("inmem is being deprecated, test skipped")
}
label := &kolide.Label{
Name: "my label",
Query: "select 1 from processes;",
}
saved, err := db.NewLabel(label)
require.Nil(t, err)
saved, err = db.Label(saved.ID)
require.Nil(t, err)
assert.Equal(t, label.Name, saved.Name)
assert.Equal(t, label.Description, saved.Description)
newLabel := &kolide.Label{
Name: "my label",
Query: " select * from time",
}
// Replace should fail when label already exists and isn't soft deleted
_, err = db.NewLabel(newLabel)
require.NotNil(t, err)
// Now delete label and replace should succeed
err = db.DeleteLabel(label.ID)
require.Nil(t, err)
saved, err = db.NewLabel(newLabel)
require.Nil(t, err)
saved, err = db.Label(saved.ID)
require.Nil(t, err)
assert.Equal(t, newLabel.Name, saved.Name)
assert.Equal(t, newLabel.Description, saved.Description)
}

View File

@ -73,4 +73,5 @@ var testFunctions = [...]func(*testing.T, kolide.Datastore){
testLicense,
testSaveLabel,
testFlappingNetworkInterfaces,
testReplaceDeletedLabel,
}

View File

@ -11,8 +11,25 @@ import (
// NewLabel creates a new kolide.Label
func (d *Datastore) NewLabel(label *kolide.Label) (*kolide.Label, error) {
sql := `
var (
deletedLabel kolide.Label
query string
)
err := d.db.Get(&deletedLabel,
"SELECT * FROM labels WHERE name = ? AND deleted", label.Name)
switch err {
case nil:
query = `
REPLACE INTO labels (
name,
description,
query,
platform,
label_type
) VALUES ( ?, ?, ?, ?, ?)
`
case sql.ErrNoRows:
query = `
INSERT INTO labels (
name,
description,
@ -21,7 +38,10 @@ func (d *Datastore) NewLabel(label *kolide.Label) (*kolide.Label, error) {
label_type
) VALUES ( ?, ?, ?, ?, ?)
`
result, err := d.db.Exec(sql, label.Name, label.Description, label.Query, label.Platform, label.LabelType)
default:
return nil, errors.Wrap(err, "check for existing label")
}
result, err := d.db.Exec(query, label.Name, label.Description, label.Query, label.Platform, label.LabelType)
if err != nil {
return nil, errors.Wrap(err, "inserting label")
}

View File

@ -119,11 +119,17 @@ func (r createLabelResponse) error() error { return r.Err }
func makeCreateLabelEndpoint(svc kolide.Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
req := request.(createLabelRequest)
label, err := svc.NewLabel(ctx, req.payload)
if err != nil {
return createLabelResponse{Err: err}, nil
}
labelResp, err := labelResponseForLabel(ctx, svc, label)
if err != nil {
return createLabelResponse{Err: err}, nil
}
return createLabelResponse{Label: *labelResp}, nil
}
}