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 (
|
|
|
|
"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"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
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 {
|
|
|
|
return portainer.Error("Invalid type value. Value must be one of: 1 (Storidge)")
|
|
|
|
}
|
2018-06-18 12:30:44 +00:00
|
|
|
if payload.Type == 1 && govalidator.IsNull(payload.URL) {
|
|
|
|
return portainer.Error("Invalid extension URL")
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// POST request on /api/endpoints/:id/extensions
|
|
|
|
func (handler *Handler) endpointExtensionAdd(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
endpointID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err}
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
endpoint, err := handler.DataStore.Endpoint().Endpoint(portainer.EndpointID(endpointID))
|
2018-06-19 11:15:10 +00:00
|
|
|
if err == portainer.ErrObjectNotFound {
|
2018-06-11 13:13:19 +00:00
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint with the specified identifier inside the database", err}
|
|
|
|
} else if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an endpoint with the specified identifier inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint changes inside the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
return response.JSON(w, extension)
|
|
|
|
}
|