crowdsec/pkg/cwapi/enroll.go

28 lines
768 B
Go
Raw Normal View History

2020-05-15 09:39:16 +00:00
package cwapi
import (
"fmt"
log "github.com/sirupsen/logrus"
)
func (ctx *ApiCtx) Enroll(userID string) error {
toPush := map[string]string{"user_id": userID}
jsonResp := &ApiResp{}
errResp := &ApiResp{}
2020-05-15 09:39:16 +00:00
resp, err := ctx.Http.New().Post(ctx.EnrollPath).BodyJSON(&toPush).Receive(jsonResp, errResp)
2020-05-15 09:39:16 +00:00
if err != nil {
return fmt.Errorf("api enroll: HTTP request creation failed: %s", err)
}
if resp.StatusCode != 200 {
return fmt.Errorf("api enroll: user '%s' return bad HTTP code (%d): %s", userID, resp.StatusCode, errResp.Message)
2020-05-15 09:39:16 +00:00
}
if jsonResp.Message == "" || jsonResp.Message != "OK" || jsonResp.StatusCode != 200 {
2020-07-27 16:35:31 +00:00
return fmt.Errorf("api user enroll failed")
}
2020-05-15 09:39:16 +00:00
log.Printf("user '%s' is enrolled successfully", string(userID))
return nil
}