2018-09-03 10:08:03 +00:00
package webhooks
import (
2020-07-07 21:57:52 +00:00
"errors"
2018-09-03 10:08:03 +00:00
"net/http"
"github.com/asaskevich/govalidator"
2019-12-05 04:02:27 +00:00
"github.com/gofrs/uuid"
2018-09-10 10:01:38 +00:00
httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response"
2021-02-23 03:21:39 +00:00
portainer "github.com/portainer/portainer/api"
2020-07-07 21:57:52 +00:00
bolterrors "github.com/portainer/portainer/api/bolt/errors"
2018-09-03 10:08:03 +00:00
)
type webhookCreatePayload struct {
ResourceID string
EndpointID int
WebhookType int
}
func ( payload * webhookCreatePayload ) Validate ( r * http . Request ) error {
if govalidator . IsNull ( payload . ResourceID ) {
2020-07-07 21:57:52 +00:00
return errors . New ( "Invalid ResourceID" )
2018-09-03 10:08:03 +00:00
}
if payload . EndpointID == 0 {
2020-07-07 21:57:52 +00:00
return errors . New ( "Invalid EndpointID" )
2018-09-03 10:08:03 +00:00
}
if payload . WebhookType != 1 {
2020-07-07 21:57:52 +00:00
return errors . New ( "Invalid WebhookType" )
2018-09-03 10:08:03 +00:00
}
return nil
}
2021-02-23 03:21:39 +00:00
// @summary Create a webhook
2021-10-11 23:12:08 +00:00
// @description **Access policy**: authenticated
2021-02-23 03:21:39 +00:00
// @security jwt
// @tags webhooks
// @accept json
// @produce json
// @param body body webhookCreatePayload true "Webhook data"
// @success 200 {object} portainer.Webhook
// @failure 400
// @failure 409
// @failure 500
// @router /webhooks [post]
2018-09-03 10:08:03 +00:00
func ( handler * Handler ) webhookCreate ( w http . ResponseWriter , r * http . Request ) * httperror . HandlerError {
var payload webhookCreatePayload
err := request . DecodeAndValidateJSONPayload ( r , & payload )
if err != nil {
return & httperror . HandlerError { http . StatusBadRequest , "Invalid request payload" , err }
}
2020-05-20 05:23:15 +00:00
webhook , err := handler . DataStore . Webhook ( ) . WebhookByResourceID ( payload . ResourceID )
2020-07-07 21:57:52 +00:00
if err != nil && err != bolterrors . ErrObjectNotFound {
2018-09-03 10:08:03 +00:00
return & httperror . HandlerError { http . StatusInternalServerError , "An error occurred retrieving webhooks from the database" , err }
}
if webhook != nil {
2020-07-07 21:57:52 +00:00
return & httperror . HandlerError { http . StatusConflict , "A webhook for this resource already exists" , errors . New ( "A webhook for this resource already exists" ) }
2018-09-03 10:08:03 +00:00
}
token , err := uuid . NewV4 ( )
if err != nil {
return & httperror . HandlerError { http . StatusInternalServerError , "Error creating unique token" , err }
}
webhook = & portainer . Webhook {
Token : token . String ( ) ,
ResourceID : payload . ResourceID ,
EndpointID : portainer . EndpointID ( payload . EndpointID ) ,
WebhookType : portainer . WebhookType ( payload . WebhookType ) ,
}
2020-05-20 05:23:15 +00:00
err = handler . DataStore . Webhook ( ) . CreateWebhook ( webhook )
2018-09-03 10:08:03 +00:00
if err != nil {
return & httperror . HandlerError { http . StatusInternalServerError , "Unable to persist the webhook inside the database" , err }
}
return response . JSON ( w , webhook )
}