2018-05-07 19:44:40 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2021-11-22 14:13:26 +00:00
|
|
|
"fmt"
|
2018-05-07 19:44:40 +00:00
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2018-05-07 19:44:40 +00:00
|
|
|
)
|
|
|
|
|
2018-05-09 00:10:01 +00:00
|
|
|
// ApplyLabels sends the list of Labels to be applied (upserted) to the
|
2018-05-07 19:44:40 +00:00
|
|
|
// Fleet instance.
|
2021-06-06 22:07:29 +00:00
|
|
|
func (c *Client) ApplyLabels(specs []*fleet.LabelSpec) error {
|
2018-05-07 19:44:40 +00:00
|
|
|
req := applyLabelSpecsRequest{Specs: specs}
|
2021-02-10 20:13:11 +00:00
|
|
|
response, err := c.AuthenticatedDo("POST", "/api/v1/fleet/spec/labels", "", req)
|
2018-05-07 19:44:40 +00:00
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("POST /api/v1/fleet/spec/labels: %w", err)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf(
|
2018-05-09 23:54:23 +00:00
|
|
|
"apply labels received status %d %s",
|
|
|
|
response.StatusCode,
|
|
|
|
extractServerErrorText(response.Body),
|
|
|
|
)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody applyLabelSpecsResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("decode apply label spec response: %w", err)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("apply label spec: %s", responseBody.Err)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2018-05-09 00:10:01 +00:00
|
|
|
// GetLabel retrieves information about a label by name
|
2021-06-06 22:07:29 +00:00
|
|
|
func (c *Client) GetLabel(name string) (*fleet.LabelSpec, error) {
|
2021-02-10 20:13:11 +00:00
|
|
|
verb, path := "GET", "/api/v1/fleet/spec/labels/"+url.PathEscape(name)
|
2020-11-05 04:45:16 +00:00
|
|
|
response, err := c.AuthenticatedDo(verb, path, "", nil)
|
2018-05-09 00:10:01 +00:00
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, fmt.Errorf("GET /api/v1/fleet/spec/labels: %w", err)
|
2018-05-09 00:10:01 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return nil, notFoundErr{}
|
|
|
|
}
|
|
|
|
if response.StatusCode != http.StatusOK {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, fmt.Errorf(
|
2018-05-09 23:54:23 +00:00
|
|
|
"get label received status %d %s",
|
|
|
|
response.StatusCode,
|
|
|
|
extractServerErrorText(response.Body),
|
|
|
|
)
|
2018-05-09 00:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody getLabelSpecResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, fmt.Errorf("decode get label spec response: %w", err)
|
2018-05-09 00:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, fmt.Errorf("get label spec: %s", responseBody.Err)
|
2018-05-09 00:10:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return responseBody.Spec, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetLabels retrieves the list of all Labels.
|
2021-06-06 22:07:29 +00:00
|
|
|
func (c *Client) GetLabels() ([]*fleet.LabelSpec, error) {
|
2021-02-10 20:13:11 +00:00
|
|
|
response, err := c.AuthenticatedDo("GET", "/api/v1/fleet/spec/labels", "", nil)
|
2018-05-07 19:44:40 +00:00
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, fmt.Errorf("GET /api/v1/fleet/spec/labels: %w", err)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, fmt.Errorf(
|
2018-05-09 23:54:23 +00:00
|
|
|
"get labels received status %d %s",
|
|
|
|
response.StatusCode,
|
|
|
|
extractServerErrorText(response.Body),
|
|
|
|
)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody getLabelSpecsResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, fmt.Errorf("decode get label spec response: %w", err)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return nil, fmt.Errorf("get label spec: %s", responseBody.Err)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return responseBody.Specs, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteLabel deletes the label with the matching name.
|
|
|
|
func (c *Client) DeleteLabel(name string) error {
|
2021-02-10 20:13:11 +00:00
|
|
|
verb, path := "DELETE", "/api/v1/fleet/labels/"+url.PathEscape(name)
|
2020-11-05 04:45:16 +00:00
|
|
|
response, err := c.AuthenticatedDo(verb, path, "", nil)
|
2018-05-07 19:44:40 +00:00
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("%s %s: %w", verb, path, err)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
2018-05-08 02:07:00 +00:00
|
|
|
switch response.StatusCode {
|
|
|
|
case http.StatusNotFound:
|
|
|
|
return notFoundErr{}
|
|
|
|
}
|
2018-05-07 19:44:40 +00:00
|
|
|
if response.StatusCode != http.StatusOK {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf(
|
2018-05-09 23:54:23 +00:00
|
|
|
"delete label received status %d %s",
|
|
|
|
response.StatusCode,
|
|
|
|
extractServerErrorText(response.Body),
|
|
|
|
)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody deleteLabelResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("decode get label spec response: %w", err)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
2021-11-22 14:13:26 +00:00
|
|
|
return fmt.Errorf("get label spec: %s", responseBody.Err)
|
2018-05-07 19:44:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|