Completely delete host (#1063)

This commit is contained in:
John Murphy 2017-01-21 01:22:33 +08:00 committed by Jason Meller
parent d6f7f39312
commit 92b93a3c3b
4 changed files with 28 additions and 4 deletions

View file

@ -136,6 +136,26 @@ func testDeleteHost(t *testing.T, ds kolide.Datastore) {
assert.NotNil(t, err)
}
func testIdempotentDeleteHost(t *testing.T, ds kolide.Datastore) {
host, err := ds.NewHost(&kolide.Host{
DetailUpdateTime: time.Now(),
SeenTime: time.Now(),
NodeKey: "1",
UUID: "1",
HostName: "foo.local",
})
require.Nil(t, err)
require.NotNil(t, host)
id := host.ID
err = ds.DeleteHost(host.ID)
host, err = ds.Host(host.ID)
assert.NotNil(t, err)
err = ds.DeleteHost(id)
assert.Nil(t, err)
}
func testListHost(t *testing.T, ds kolide.Datastore) {
hosts := []*kolide.Host{}
for i := 0; i < 10; i++ {

View file

@ -68,4 +68,5 @@ var testFunctions = [...]func(*testing.T, kolide.Datastore){
testGenerateHostStatusStatistics,
testMarkHostSeen,
testDuplicateNewQuery,
testIdempotentDeleteHost,
}

View file

@ -49,11 +49,10 @@ func (d *Datastore) DeleteHost(hid uint) error {
d.mtx.Lock()
defer d.mtx.Unlock()
if _, ok := d.hosts[hid]; !ok {
return notFound("Host").WithID(hid)
if _, ok := d.hosts[hid]; ok {
delete(d.hosts, hid)
}
delete(d.hosts, hid)
return nil
}

View file

@ -242,7 +242,11 @@ func (d *Datastore) SaveHost(host *kolide.Host) error {
}
func (d *Datastore) DeleteHost(hid uint) error {
return d.deleteEntity("hosts", hid)
_, err := d.db.Exec("DELETE FROM hosts WHERE id = ?", hid)
if err != nil {
return errors.Wrapf(err, "deleting host with id %d", hid)
}
return nil
}
// TODO needs test