2019-07-20 23:28:11 +00:00
|
|
|
package endpointgroups
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
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
|
|
|
"github.com/portainer/portainer/api/bolt/errors"
|
2019-07-20 23:28:11 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id EndpointGroupAddEndpoint
|
|
|
|
// @summary Add an endpoint to an endpoint group
|
|
|
|
// @description Add an endpoint to an endpoint group
|
|
|
|
// @description **Access policy**: administrator
|
|
|
|
// @tags endpoint_groups
|
|
|
|
// @security jwt
|
|
|
|
// @param id path int true "EndpointGroup identifier"
|
|
|
|
// @param endpointId path int true "Endpoint identifier"
|
|
|
|
// @success 204 "Success"
|
|
|
|
// @failure 400 "Invalid request"
|
|
|
|
// @failure 404 "EndpointGroup not found"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /endpoint_groups/{id}/endpoints/{endpointId} [put]
|
2019-07-20 23:28:11 +00:00
|
|
|
func (handler *Handler) endpointGroupAddEndpoint(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
|
|
|
endpointGroupID, err := request.RetrieveNumericRouteVariableValue(r, "id")
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint group identifier route variable", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpointID, err := request.RetrieveNumericRouteVariableValue(r, "endpointId")
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid endpoint identifier route variable", err}
|
|
|
|
}
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
endpointGroup, err := handler.DataStore.EndpointGroup().EndpointGroup(portainer.EndpointGroupID(endpointGroupID))
|
2020-07-07 21:57:52 +00:00
|
|
|
if err == errors.ErrObjectNotFound {
|
2019-07-20 23:28:11 +00:00
|
|
|
return &httperror.HandlerError{http.StatusNotFound, "Unable to find an endpoint group with the specified identifier inside the database", err}
|
|
|
|
} else if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to find an endpoint group with the specified identifier inside the database", err}
|
|
|
|
}
|
|
|
|
|
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 == errors.ErrObjectNotFound {
|
2019-07-20 23:28:11 +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}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpoint.GroupID = endpointGroup.ID
|
|
|
|
|
2020-05-20 05:23:15 +00:00
|
|
|
err = handler.DataStore.Endpoint().UpdateEndpoint(endpoint.ID, endpoint)
|
2019-07-20 23:28:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint changes inside the database", err}
|
|
|
|
}
|
|
|
|
|
2020-05-14 02:14:28 +00:00
|
|
|
err = handler.updateEndpointRelations(endpoint, endpointGroup)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint relations changes inside the database", err}
|
|
|
|
}
|
|
|
|
|
2019-07-20 23:28:11 +00:00
|
|
|
return response.Empty(w)
|
|
|
|
}
|