2020-11-05 01:06:55 +00:00
|
|
|
package service
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
|
|
|
"net/http"
|
|
|
|
|
2021-06-26 04:46:51 +00:00
|
|
|
"github.com/fleetdm/fleet/v4/server/fleet"
|
2020-11-05 01:06:55 +00:00
|
|
|
"github.com/pkg/errors"
|
|
|
|
)
|
|
|
|
|
|
|
|
// CreateUser creates a new user, skipping the invitation process.
|
2021-06-06 22:07:29 +00:00
|
|
|
func (c *Client) CreateUser(p fleet.UserPayload) error {
|
2021-02-10 20:13:11 +00:00
|
|
|
verb, path := "POST", "/api/v1/fleet/users/admin"
|
2020-11-05 04:45:16 +00:00
|
|
|
response, err := c.AuthenticatedDo(verb, path, "", p)
|
2020-11-05 01:06:55 +00:00
|
|
|
if err != nil {
|
|
|
|
return errors.Wrapf(err, "%s %s", verb, path)
|
|
|
|
}
|
|
|
|
defer response.Body.Close()
|
|
|
|
|
|
|
|
if response.StatusCode != http.StatusOK {
|
|
|
|
return errors.Errorf(
|
|
|
|
"create user received status %d %s",
|
|
|
|
response.StatusCode,
|
|
|
|
extractServerErrorText(response.Body),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
var responseBody createUserResponse
|
|
|
|
err = json.NewDecoder(response.Body).Decode(&responseBody)
|
|
|
|
if err != nil {
|
|
|
|
return errors.Wrap(err, "decode create user response")
|
|
|
|
}
|
|
|
|
|
|
|
|
if responseBody.Err != nil {
|
|
|
|
return errors.Errorf("create user: %s", responseBody.Err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|