mirror of
https://github.com/fleetdm/fleet
synced 2026-05-23 17:08:53 +00:00
Add DeleteHost func to service pkg (#2312)
This commit is contained in:
parent
a2f49dd620
commit
da99617882
1 changed files with 36 additions and 0 deletions
|
|
@ -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
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue