2018-09-03 10:08:03 +00:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
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"
|
2018-09-03 10:08:03 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type webhookListOperationFilters struct {
|
|
|
|
ResourceID string `json:"ResourceID"`
|
|
|
|
EndpointID int `json:"EndpointID"`
|
|
|
|
}
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @summary List webhooks
|
|
|
|
// @description
|
|
|
|
// @security jwt
|
|
|
|
// @tags webhooks
|
|
|
|
// @accept json
|
|
|
|
// @produce json
|
|
|
|
// @param body body webhookCreatePayload true "Webhook data"
|
|
|
|
// @param filters query webhookListOperationFilters false "Filters"
|
|
|
|
// @success 200 {array} portainer.Webhook
|
|
|
|
// @failure 400
|
|
|
|
// @failure 500
|
|
|
|
// @router /webhooks [get]
|
2018-09-03 10:08:03 +00:00
|
|
|
func (handler *Handler) webhookList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
var filters webhookListOperationFilters
|
|
|
|
err := request.RetrieveJSONQueryParameter(r, "filters", &filters, true)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: filters", err}
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
webhooks, err := handler.DataStore.Webhook().Webhooks()
|
2018-09-03 10:08:03 +00:00
|
|
|
webhooks = filterWebhooks(webhooks, &filters)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve webhooks from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, webhooks)
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterWebhooks(webhooks []portainer.Webhook, filters *webhookListOperationFilters) []portainer.Webhook {
|
|
|
|
if filters.EndpointID == 0 && filters.ResourceID == "" {
|
|
|
|
return webhooks
|
|
|
|
}
|
|
|
|
|
|
|
|
filteredWebhooks := make([]portainer.Webhook, 0, len(webhooks))
|
|
|
|
for _, webhook := range webhooks {
|
|
|
|
if webhook.EndpointID == portainer.EndpointID(filters.EndpointID) && webhook.ResourceID == string(filters.ResourceID) {
|
|
|
|
filteredWebhooks = append(filteredWebhooks, webhook)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filteredWebhooks
|
|
|
|
}
|