Add DeleteHost func to service pkg (#2312)

This commit is contained in:
Iain Steers 2020-10-01 19:14:18 -04:00 committed by GitHub
parent a2f49dd620
commit da99617882
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -2,6 +2,7 @@ package service
import (
"encoding/json"
"fmt"
"net/http"
"github.com/pkg/errors"
@ -61,3 +62,38 @@ func (c *Client) HostByIdentifier(identifier string) (*HostResponse, error) {
return responseBody.Host, nil
}
// DeleteHost deletes the host with the matching id.
func (c *Client) DeleteHost(id uint) error {
verb := "DELETE"
path := fmt.Sprintf("/api/v1/kolide/hosts/%d", id)
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 host received status %d %s",
response.StatusCode,
extractServerErrorText(response.Body),
)
}
var responseBody deleteHostResponse
err = json.NewDecoder(response.Body).Decode(&responseBody)
if err != nil {
return errors.Wrap(err, "decode delete host response")
}
if responseBody.Err != nil {
return errors.Errorf("delete host: %s", responseBody.Err)
}
return nil
}