mirror of
https://github.com/empayre/fleet.git
synced 2024-11-06 17:05:18 +00:00
051d3c8b07
We need to properly escape and unescape the name parameter. Fixes #1948
139 lines
3.6 KiB
Go
139 lines
3.6 KiB
Go
package service
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"net/url"
|
|
|
|
"github.com/kolide/fleet/server/kolide"
|
|
"github.com/pkg/errors"
|
|
)
|
|
|
|
// ApplyLabels sends the list of Labels to be applied (upserted) to the
|
|
// Fleet instance.
|
|
func (c *Client) ApplyLabels(specs []*kolide.LabelSpec) error {
|
|
req := applyLabelSpecsRequest{Specs: specs}
|
|
response, err := c.AuthenticatedDo("POST", "/api/v1/kolide/spec/labels", req)
|
|
if err != nil {
|
|
return errors.Wrap(err, "POST /api/v1/kolide/spec/labels")
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return errors.Errorf(
|
|
"apply labels received status %d %s",
|
|
response.StatusCode,
|
|
extractServerErrorText(response.Body),
|
|
)
|
|
}
|
|
|
|
var responseBody applyLabelSpecsResponse
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
if err != nil {
|
|
return errors.Wrap(err, "decode apply label spec response")
|
|
}
|
|
|
|
if responseBody.Err != nil {
|
|
return errors.Errorf("apply label spec: %s", responseBody.Err)
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
// GetLabel retrieves information about a label by name
|
|
func (c *Client) GetLabel(name string) (*kolide.LabelSpec, error) {
|
|
verb, path := "GET", "/api/v1/kolide/spec/labels/"+url.PathEscape(name)
|
|
response, err := c.AuthenticatedDo(verb, path, nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "GET /api/v1/kolide/spec/labels")
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
switch response.StatusCode {
|
|
case http.StatusNotFound:
|
|
return nil, notFoundErr{}
|
|
}
|
|
if response.StatusCode != http.StatusOK {
|
|
return nil, errors.Errorf(
|
|
"get label received status %d %s",
|
|
response.StatusCode,
|
|
extractServerErrorText(response.Body),
|
|
)
|
|
}
|
|
|
|
var responseBody getLabelSpecResponse
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "decode get label spec response")
|
|
}
|
|
|
|
if responseBody.Err != nil {
|
|
return nil, errors.Errorf("get label spec: %s", responseBody.Err)
|
|
}
|
|
|
|
return responseBody.Spec, nil
|
|
}
|
|
|
|
// GetLabels retrieves the list of all Labels.
|
|
func (c *Client) GetLabels() ([]*kolide.LabelSpec, error) {
|
|
response, err := c.AuthenticatedDo("GET", "/api/v1/kolide/spec/labels", nil)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "GET /api/v1/kolide/spec/labels")
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
return nil, errors.Errorf(
|
|
"get labels received status %d %s",
|
|
response.StatusCode,
|
|
extractServerErrorText(response.Body),
|
|
)
|
|
}
|
|
|
|
var responseBody getLabelSpecsResponse
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
if err != nil {
|
|
return nil, errors.Wrap(err, "decode get label spec response")
|
|
}
|
|
|
|
if responseBody.Err != nil {
|
|
return nil, errors.Errorf("get label spec: %s", responseBody.Err)
|
|
}
|
|
|
|
return responseBody.Specs, nil
|
|
}
|
|
|
|
// DeleteLabel deletes the label with the matching name.
|
|
func (c *Client) DeleteLabel(name string) error {
|
|
verb, path := "DELETE", "/api/v1/kolide/labels/"+url.PathEscape(name)
|
|
response, err := c.AuthenticatedDo(verb, path, nil)
|
|
if err != nil {
|
|
return errors.Wrapf(err, "%s %s", verb, path)
|
|
}
|
|
defer response.Body.Close()
|
|
|
|
switch response.StatusCode {
|
|
case http.StatusNotFound:
|
|
return notFoundErr{}
|
|
}
|
|
if response.StatusCode != http.StatusOK {
|
|
return errors.Errorf(
|
|
"delete label received status %d %s",
|
|
response.StatusCode,
|
|
extractServerErrorText(response.Body),
|
|
)
|
|
}
|
|
|
|
var responseBody deleteLabelResponse
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
if err != nil {
|
|
return errors.Wrap(err, "decode get label spec response")
|
|
}
|
|
|
|
if responseBody.Err != nil {
|
|
return errors.Errorf("get label spec: %s", responseBody.Err)
|
|
}
|
|
|
|
return nil
|
|
}
|