2018-09-03 10:08:03 +00:00
|
|
|
package webhooks
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2023-09-01 22:27:02 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
|
|
|
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/request"
|
|
|
|
"github.com/portainer/portainer/pkg/libhttp/response"
|
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
|
2021-10-11 23:12:08 +00:00
|
|
|
// @description **Access policy**: authenticated
|
2021-11-30 02:31:16 +00:00
|
|
|
// @security ApiKeyAuth
|
2021-02-23 03:21:39 +00:00
|
|
|
// @security jwt
|
|
|
|
// @tags webhooks
|
|
|
|
// @accept json
|
|
|
|
// @produce json
|
2024-02-09 01:44:29 +00:00
|
|
|
// @param filters query string false "Filters (json-string)" example({"EndpointID":1,"ResourceID":"abc12345-abcd-2345-ab12-58005b4a0260"})
|
2021-02-23 03:21:39 +00:00
|
|
|
// @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 {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.BadRequest("Invalid query parameter: filters", err)
|
2018-09-03 10:08:03 +00:00
|
|
|
}
|
|
|
|
|
2022-01-27 00:38:29 +00:00
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve user info from request context", err)
|
2022-01-27 00:38:29 +00:00
|
|
|
}
|
|
|
|
if !securityContext.IsAdmin {
|
|
|
|
return response.JSON(w, []portainer.Webhook{})
|
|
|
|
}
|
|
|
|
|
2023-06-22 21:28:07 +00:00
|
|
|
webhooks, err := handler.DataStore.Webhook().ReadAll()
|
2018-09-03 10:08:03 +00:00
|
|
|
if err != nil {
|
2022-09-14 23:42:39 +00:00
|
|
|
return httperror.InternalServerError("Unable to retrieve webhooks from the database", err)
|
2018-09-03 10:08:03 +00:00
|
|
|
}
|
|
|
|
|
2023-05-23 03:05:55 +00:00
|
|
|
webhooks = filterWebhooks(webhooks, &filters)
|
|
|
|
|
2018-09-03 10:08:03 +00:00
|
|
|
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
|
|
|
|
}
|