2018-06-11 13:13:19 +00:00
|
|
|
package endpoints
|
|
|
|
|
2018-12-09 03:49:27 +00:00
|
|
|
// TODO: legacy extension management
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
import (
|
2020-07-07 21:57:52 +00:00
|
|
|
"errors"
|
2018-06-11 13:13:19 +00:00
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/asaskevich/govalidator"
|
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-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type endpointExtensionAddPayload struct {
|
|
|
|
Type int
|
|
|
|
URL string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *endpointExtensionAddPayload) Validate(r *http.Request) error {
|
|
|
|
if payload.Type != 1 {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid type value. Value must be one of: 1 (Storidge)")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2018-06-18 12:30:44 +00:00
|
|
|
if payload.Type == 1 && govalidator.IsNull(payload.URL) {
|
2020-07-07 21:57:52 +00:00
|
|
|
return errors.New("Invalid extension URL")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-10-11 23:12:08 +00:00
|
|
|
// @id endpointExtensionAdd
|
|
|
|
// @tags endpoints
|
|
|
|
// @deprecated
|
|
|
|
// @param id path int true "Environment(Endpoint) identifier"
|
|
|
|
// @success 204 "Success"
|
|
|
|
// @router /endpoints/{id}/extensions [post]
|
2018-06-11 13:13:19 +00:00
|
|
|
func (handler *Handler) endpointExtensionAdd(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
endpointID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid environment identifier route variable", err}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID))
|
2020-07-07 21:57:52 +00:00
|
|
|
if err == bolterrors.ErrObjectNotFound {
|
2021-09-08 08:42:17 +00:00
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find an environment with the specified identifier inside the database", err}
|
2018-06-11 13:13:19 +00:00
|
|
|
} else if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an environment with the specified identifier inside the database", err}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var payload endpointExtensionAddPayload
|
|
|
|
err = request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
extensionType := portainer.EndpointExtensionType(payload.Type)
|
|
|
|
|
|
|
|
var extension *portainer.EndpointExtension
|
2019-05-24 21:53:10 +00:00
|
|
|
for idx := range endpoint.Extensions {
|
|
|
|
if endpoint.Extensions[idx].Type == extensionType {
|
|
|
|
extension = &endpoint.Extensions[idx]
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if extension != nil {
|
|
|
|
extension.URL = payload.URL
|
|
|
|
} else {
|
|
|
|
extension = &portainer.EndpointExtension{
|
|
|
|
Type: extensionType,
|
|
|
|
URL: payload.URL,
|
|
|
|
}
|
|
|
|
endpoint.Extensions = append(endpoint.Extensions, *extension)
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
err = handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2021-09-08 08:42:17 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist environment changes inside the database", err}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, extension)
|
|
|
|
}
|