2016-09-26 18:48:55 +00:00
|
|
|
package service
|
2016-09-04 05:13:42 +00:00
|
|
|
|
|
|
|
import (
|
2017-03-15 15:55:30 +00:00
|
|
|
"context"
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
"github.com/fleetdm/fleet/server/fleet"
|
2021-01-26 00:26:14 +00:00
|
|
|
"github.com/go-kit/kit/endpoint"
|
2016-09-04 05:13:42 +00:00
|
|
|
)
|
|
|
|
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
type packResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
fleet.Pack
|
2017-01-20 13:57:02 +00:00
|
|
|
QueryCount uint `json:"query_count"`
|
|
|
|
|
|
|
|
// All current hosts in the pack. Hosts which are selected explicty and
|
|
|
|
// hosts which are part of a label.
|
|
|
|
TotalHostsCount uint `json:"total_hosts_count"`
|
|
|
|
|
|
|
|
// IDs of hosts which were explicitly selected.
|
|
|
|
HostIDs []uint `json:"host_ids"`
|
|
|
|
LabelIDs []uint `json:"label_ids"`
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
}
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func packResponseForPack(ctx context.Context, svc fleet.Service, pack fleet.Pack) (*packResponse, error) {
|
|
|
|
opts := fleet.ListOptions{}
|
2017-01-20 13:57:02 +00:00
|
|
|
queries, err := svc.GetScheduledQueriesInPack(ctx, pack.ID, opts)
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-01-20 13:57:02 +00:00
|
|
|
|
|
|
|
hosts, err := svc.ListExplicitHostsInPack(ctx, pack.ID, opts)
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-30 15:31:28 +00:00
|
|
|
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
labels, err := svc.ListLabelsForPack(ctx, pack.ID)
|
2017-12-04 14:43:43 +00:00
|
|
|
labelIDs := make([]uint, len(labels))
|
2017-01-10 04:30:51 +00:00
|
|
|
for i, label := range labels {
|
|
|
|
labelIDs[i] = label.ID
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2017-03-30 15:31:28 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
hostMetrics, err := svc.CountHostsInTargets(ctx, nil, fleet.HostTargets{HostIDs: hosts, LabelIDs: labelIDs})
|
2017-03-30 15:31:28 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
return &packResponse{
|
|
|
|
Pack: pack,
|
|
|
|
QueryCount: uint(len(queries)),
|
2017-03-30 15:31:28 +00:00
|
|
|
TotalHostsCount: hostMetrics.TotalHosts,
|
2017-03-01 19:56:13 +00:00
|
|
|
HostIDs: hosts,
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
LabelIDs: labelIDs,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Pack
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getPackRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type getPackResponse struct {
|
2016-11-22 21:56:05 +00:00
|
|
|
Pack packResponse `json:"pack,omitempty"`
|
2016-10-11 16:22:11 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getPackResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetPackEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-09-04 05:13:42 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getPackRequest)
|
2016-11-22 21:56:05 +00:00
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
pack, err := svc.GetPack(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return getPackResponse{Err: err}, nil
|
|
|
|
}
|
2016-11-22 21:56:05 +00:00
|
|
|
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
resp, err := packResponseForPack(ctx, svc, *pack)
|
2016-11-22 21:56:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return getPackResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return getPackResponse{
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
Pack: *resp,
|
2016-11-22 21:56:05 +00:00
|
|
|
}, nil
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
2016-10-07 17:26:48 +00:00
|
|
|
// List Packs
|
2016-09-04 05:13:42 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
2016-10-13 18:21:47 +00:00
|
|
|
type listPacksRequest struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
ListOptions fleet.ListOptions
|
2016-10-13 18:21:47 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
type listPacksResponse struct {
|
2016-11-22 21:56:05 +00:00
|
|
|
Packs []packResponse `json:"packs"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
2016-10-07 17:26:48 +00:00
|
|
|
func (r listPacksResponse) error() error { return r.Err }
|
2016-09-04 05:13:42 +00:00
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeListPacksEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-09-04 05:13:42 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
2016-10-13 18:21:47 +00:00
|
|
|
req := request.(listPacksRequest)
|
|
|
|
packs, err := svc.ListPacks(ctx, req.ListOptions)
|
2016-09-04 05:13:42 +00:00
|
|
|
if err != nil {
|
2016-09-06 15:02:11 +00:00
|
|
|
return getPackResponse{Err: err}, nil
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
2016-10-07 17:26:48 +00:00
|
|
|
|
2017-12-04 14:43:43 +00:00
|
|
|
resp := listPacksResponse{Packs: make([]packResponse, len(packs))}
|
2017-01-18 19:13:41 +00:00
|
|
|
for i, pack := range packs {
|
Add host_ids and label_ids fields to the packs API (#737)
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
2017-01-03 17:32:06 +00:00
|
|
|
packResp, err := packResponseForPack(ctx, svc, *pack)
|
2016-11-22 21:56:05 +00:00
|
|
|
if err != nil {
|
|
|
|
return getPackResponse{Err: err}, nil
|
|
|
|
}
|
2017-01-18 19:13:41 +00:00
|
|
|
resp.Packs[i] = *packResp
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
return resp, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-15 14:13:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Create Pack
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type createPackRequest struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
payload fleet.PackPayload
|
2018-06-15 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type createPackResponse struct {
|
|
|
|
Pack packResponse `json:"pack,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r createPackResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeCreatePackEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-06-15 14:13:11 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(createPackRequest)
|
|
|
|
pack, err := svc.NewPack(ctx, req.payload)
|
|
|
|
if err != nil {
|
|
|
|
return createPackResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := packResponseForPack(ctx, svc, *pack)
|
|
|
|
if err != nil {
|
|
|
|
return createPackResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return createPackResponse{
|
|
|
|
Pack: *resp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Modify Pack
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type modifyPackRequest struct {
|
|
|
|
ID uint
|
2021-06-06 22:07:29 +00:00
|
|
|
payload fleet.PackPayload
|
2018-06-15 14:13:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type modifyPackResponse struct {
|
|
|
|
Pack packResponse `json:"pack,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r modifyPackResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeModifyPackEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-06-15 14:13:11 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(modifyPackRequest)
|
|
|
|
pack, err := svc.ModifyPack(ctx, req.ID, req.payload)
|
|
|
|
if err != nil {
|
|
|
|
return modifyPackResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resp, err := packResponseForPack(ctx, svc, *pack)
|
|
|
|
if err != nil {
|
|
|
|
return modifyPackResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return modifyPackResponse{
|
|
|
|
Pack: *resp,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-09-04 05:13:42 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Pack
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deletePackRequest struct {
|
2018-05-04 18:05:55 +00:00
|
|
|
Name string
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type deletePackResponse struct {
|
2016-09-06 15:02:11 +00:00
|
|
|
Err error `json:"error,omitempty"`
|
2016-09-04 05:13:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r deletePackResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeDeletePackEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2016-09-04 05:13:42 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deletePackRequest)
|
2018-05-04 18:05:55 +00:00
|
|
|
err := svc.DeletePack(ctx, req.Name)
|
2016-09-04 05:13:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return deletePackResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deletePackResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-04 01:01:57 +00:00
|
|
|
|
2018-06-15 14:13:11 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Delete Pack By ID
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type deletePackByIDRequest struct {
|
|
|
|
ID uint
|
|
|
|
}
|
|
|
|
|
|
|
|
type deletePackByIDResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r deletePackByIDResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeDeletePackByIDEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-06-15 14:13:11 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(deletePackByIDRequest)
|
|
|
|
err := svc.DeletePackByID(ctx, req.ID)
|
|
|
|
if err != nil {
|
|
|
|
return deletePackByIDResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return deletePackByIDResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-05-04 01:01:57 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Apply Pack Specs
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type applyPackSpecsRequest struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Specs []*fleet.PackSpec `json:"specs"`
|
2018-05-04 01:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type applyPackSpecsResponse struct {
|
|
|
|
Err error `json:"error,omitempty"`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r applyPackSpecsResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeApplyPackSpecsEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-05-04 01:01:57 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(applyPackSpecsRequest)
|
|
|
|
err := svc.ApplyPackSpecs(ctx, req.Specs)
|
|
|
|
if err != nil {
|
|
|
|
return applyPackSpecsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return applyPackSpecsResponse{}, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Pack Specs
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getPackSpecsResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Specs []*fleet.PackSpec `json:"specs"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2018-05-04 01:01:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getPackSpecsResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetPackSpecsEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-05-04 01:01:57 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
specs, err := svc.GetPackSpecs(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return getPackSpecsResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return getPackSpecsResponse{Specs: specs}, nil
|
|
|
|
}
|
|
|
|
}
|
2018-05-08 01:54:29 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
// Get Pack Spec
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
type getPackSpecResponse struct {
|
2021-06-06 22:07:29 +00:00
|
|
|
Spec *fleet.PackSpec `json:"specs,omitempty"`
|
|
|
|
Err error `json:"error,omitempty"`
|
2018-05-08 01:54:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (r getPackSpecResponse) error() error { return r.Err }
|
|
|
|
|
2021-06-06 22:07:29 +00:00
|
|
|
func makeGetPackSpecEndpoint(svc fleet.Service) endpoint.Endpoint {
|
2018-05-08 01:54:29 +00:00
|
|
|
return func(ctx context.Context, request interface{}) (interface{}, error) {
|
|
|
|
req := request.(getGenericSpecRequest)
|
|
|
|
spec, err := svc.GetPackSpec(ctx, req.Name)
|
|
|
|
if err != nil {
|
|
|
|
return getPackSpecResponse{Err: err}, nil
|
|
|
|
}
|
|
|
|
return getPackSpecResponse{Spec: spec}, nil
|
|
|
|
}
|
|
|
|
}
|