mirror of
https://github.com/empayre/fleet.git
synced 2024-11-07 01:15:22 +00:00
0122f6cb0a
This PR adds the `host_ids` and `label_ids` field to the packs HTTP API so that one can operate on the hosts/labels which a pack is scheduled to be executed on. This replaces (and deletes) the `/api/v1/kolide/packs/123/labels/456` API in favor of `PATCH /api/v1/packs/123` and specifying the `label_ids` field. This also allows for bulk operations. Consider the following API examples: ## Creating a pack with a known set of hosts and labels The key addition is the `host_ids` and `label_ids` field in both the request and the response. ### Request ``` POST /api/v1/kolide/packs ``` ```json { "name": "My new pack", "description": "The newest of the packs", "host_ids": [1, 2, 3], "label_ids": [1, 3, 5] } ``` ### Response ```json { "pack": { "id": 123, "name": "My new pack", "description": "The newest of the packs", "platform": "", "created_by": 1, "disabled": false, "query_count": 0, "total_hosts_count": 5, "host_ids": [1, 2, 3], "label_ids": [1, 3, 5] } } ``` ## Modifying the hosts and/or labels that a pack is scheduled to execute on ### Request ``` PATCH /api/v1/kolide/packs/123 ``` ```json { "host_ids": [1, 2, 3, 4, 5], "label_ids": [1, 3, 5, 7] } ``` ### Response ```json { "pack": { "id": 123, "name": "My new pack", "description": "The newest of the packs", "platform": "", "created_by": 1, "disabled": false, "query_count": 0, "total_hosts_count": 5, "host_ids": [1, 2, 3, 4, 5], "label_ids": [1, 3, 5, 7] } } ``` close #633
171 lines
3.5 KiB
Go
171 lines
3.5 KiB
Go
package service
|
|
|
|
import (
|
|
"testing"
|
|
|
|
"github.com/kolide/kolide-ose/server/config"
|
|
"github.com/kolide/kolide-ose/server/datastore/inmem"
|
|
"github.com/kolide/kolide-ose/server/kolide"
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
"golang.org/x/net/context"
|
|
)
|
|
|
|
func TestListPacks(t *testing.T) {
|
|
ds, err := inmem.New(config.TestConfig())
|
|
assert.Nil(t, err)
|
|
|
|
svc, err := newTestService(ds, nil)
|
|
assert.Nil(t, err)
|
|
|
|
ctx := context.Background()
|
|
|
|
queries, err := svc.ListPacks(ctx, kolide.ListOptions{})
|
|
assert.Nil(t, err)
|
|
assert.Len(t, queries, 0)
|
|
|
|
_, err = ds.NewPack(&kolide.Pack{
|
|
Name: "foo",
|
|
})
|
|
assert.Nil(t, err)
|
|
|
|
queries, err = svc.ListPacks(ctx, kolide.ListOptions{})
|
|
assert.Nil(t, err)
|
|
assert.Len(t, queries, 1)
|
|
}
|
|
|
|
func TestGetPack(t *testing.T) {
|
|
ds, err := inmem.New(config.TestConfig())
|
|
assert.Nil(t, err)
|
|
|
|
svc, err := newTestService(ds, nil)
|
|
assert.Nil(t, err)
|
|
|
|
ctx := context.Background()
|
|
|
|
pack := &kolide.Pack{
|
|
Name: "foo",
|
|
}
|
|
_, err = ds.NewPack(pack)
|
|
assert.Nil(t, err)
|
|
assert.NotZero(t, pack.ID)
|
|
|
|
packVerify, err := svc.GetPack(ctx, pack.ID)
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, pack.ID, packVerify.ID)
|
|
}
|
|
|
|
func TestNewPack(t *testing.T) {
|
|
ds, err := inmem.New(config.TestConfig())
|
|
assert.Nil(t, err)
|
|
|
|
svc, err := newTestService(ds, nil)
|
|
assert.Nil(t, err)
|
|
|
|
ctx := context.Background()
|
|
|
|
labelName := "label"
|
|
labelQuery := "select 1"
|
|
label, err := svc.NewLabel(ctx, kolide.LabelPayload{
|
|
Name: &labelName,
|
|
Query: &labelQuery,
|
|
})
|
|
|
|
packName := "foo"
|
|
packLabelIDs := []uint{label.ID}
|
|
pack, err := svc.NewPack(ctx, kolide.PackPayload{
|
|
Name: &packName,
|
|
LabelIDs: &packLabelIDs,
|
|
})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
packs, err := ds.ListPacks(kolide.ListOptions{})
|
|
assert.Nil(t, err)
|
|
require.Len(t, packs, 1)
|
|
assert.Equal(t, pack.ID, packs[0].ID)
|
|
|
|
labels, err := ds.ListLabelsForPack(pack.ID)
|
|
assert.Nil(t, err)
|
|
require.Len(t, labels, 1)
|
|
assert.Equal(t, label.ID, labels[0].ID)
|
|
}
|
|
|
|
func TestModifyPack(t *testing.T) {
|
|
ds, err := inmem.New(config.TestConfig())
|
|
assert.Nil(t, err)
|
|
|
|
svc, err := newTestService(ds, nil)
|
|
assert.Nil(t, err)
|
|
|
|
ctx := context.Background()
|
|
|
|
label := &kolide.Label{
|
|
Name: "label",
|
|
Query: "select 1",
|
|
}
|
|
label, err = ds.NewLabel(label)
|
|
assert.Nil(t, err)
|
|
assert.NotZero(t, label.ID)
|
|
|
|
pack := &kolide.Pack{
|
|
Name: "foo",
|
|
}
|
|
pack, err = ds.NewPack(pack)
|
|
assert.Nil(t, err)
|
|
assert.NotZero(t, pack.ID)
|
|
|
|
newName := "bar"
|
|
labelIDs := []uint{label.ID}
|
|
packVerify, err := svc.ModifyPack(ctx, pack.ID, kolide.PackPayload{
|
|
Name: &newName,
|
|
LabelIDs: &labelIDs,
|
|
})
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, pack.ID, packVerify.ID)
|
|
assert.Equal(t, "bar", packVerify.Name)
|
|
|
|
labels, err := ds.ListLabelsForPack(pack.ID)
|
|
assert.Nil(t, err)
|
|
require.Len(t, labels, 1)
|
|
assert.Equal(t, label.ID, labels[0].ID)
|
|
|
|
newLabelIDs := []uint{}
|
|
packVerify2, err := svc.ModifyPack(ctx, pack.ID, kolide.PackPayload{
|
|
LabelIDs: &newLabelIDs,
|
|
})
|
|
assert.Nil(t, err)
|
|
|
|
assert.Equal(t, pack.ID, packVerify2.ID)
|
|
|
|
labels, err = ds.ListLabelsForPack(pack.ID)
|
|
assert.Nil(t, err)
|
|
require.Len(t, labels, 0)
|
|
}
|
|
|
|
func TestDeletePack(t *testing.T) {
|
|
ds, err := inmem.New(config.TestConfig())
|
|
assert.Nil(t, err)
|
|
|
|
svc, err := newTestService(ds, nil)
|
|
assert.Nil(t, err)
|
|
|
|
ctx := context.Background()
|
|
|
|
pack := &kolide.Pack{
|
|
Name: "foo",
|
|
}
|
|
_, err = ds.NewPack(pack)
|
|
assert.Nil(t, err)
|
|
assert.NotZero(t, pack.ID)
|
|
|
|
err = svc.DeletePack(ctx, pack.ID)
|
|
assert.Nil(t, err)
|
|
|
|
queries, err := ds.ListPacks(kolide.ListOptions{})
|
|
assert.Nil(t, err)
|
|
assert.Len(t, queries, 0)
|
|
}
|