2018-06-11 13:13:19 +00:00
|
|
|
package endpointgroups
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2019-10-07 02:42:01 +00:00
|
|
|
"reflect"
|
2018-06-11 13:13:19 +00:00
|
|
|
|
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 endpointGroupUpdatePayload struct {
|
2019-07-20 23:28:11 +00:00
|
|
|
Name string
|
|
|
|
Description string
|
|
|
|
Tags []string
|
|
|
|
UserAccessPolicies portainer.UserAccessPolicies
|
|
|
|
TeamAccessPolicies portainer.TeamAccessPolicies
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (payload *endpointGroupUpdatePayload) Validate(r *http.Request) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// PUT request on /api/endpoint_groups/:id
|
|
|
|
func (handler *Handler) endpointGroupUpdate(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}
|
|
|
|
}
|
|
|
|
|
|
|
|
var payload endpointGroupUpdatePayload
|
|
|
|
err = request.DecodeAndValidateJSONPayload(r, &payload)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid request payload", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
endpointGroup, err := handler.EndpointGroupService.EndpointGroup(portainer.EndpointGroupID(endpointGroupID))
|
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 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}
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Name != "" {
|
|
|
|
endpointGroup.Name = payload.Name
|
|
|
|
}
|
|
|
|
|
|
|
|
if payload.Description != "" {
|
|
|
|
endpointGroup.Description = payload.Description
|
|
|
|
}
|
|
|
|
|
2018-06-15 07:18:25 +00:00
|
|
|
if payload.Tags != nil {
|
|
|
|
endpointGroup.Tags = payload.Tags
|
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
|
2019-09-09 22:58:26 +00:00
|
|
|
updateAuthorizations := false
|
2019-10-07 02:42:01 +00:00
|
|
|
if payload.UserAccessPolicies != nil && !reflect.DeepEqual(payload.UserAccessPolicies, endpointGroup.UserAccessPolicies) {
|
2019-05-24 06:04:58 +00:00
|
|
|
endpointGroup.UserAccessPolicies = payload.UserAccessPolicies
|
2019-09-09 22:58:26 +00:00
|
|
|
updateAuthorizations = true
|
2019-05-24 06:04:58 +00:00
|
|
|
}
|
|
|
|
|
2019-10-07 02:42:01 +00:00
|
|
|
if payload.TeamAccessPolicies != nil && !reflect.DeepEqual(payload.TeamAccessPolicies, endpointGroup.TeamAccessPolicies) {
|
2019-05-24 06:04:58 +00:00
|
|
|
endpointGroup.TeamAccessPolicies = payload.TeamAccessPolicies
|
2019-09-09 22:58:26 +00:00
|
|
|
updateAuthorizations = true
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-05-24 06:04:58 +00:00
|
|
|
err = handler.EndpointGroupService.UpdateEndpointGroup(endpointGroup.ID, endpointGroup)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2019-05-24 06:04:58 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to persist endpoint group changes inside the database", err}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2019-09-09 22:58:26 +00:00
|
|
|
if updateAuthorizations {
|
2019-10-07 02:42:01 +00:00
|
|
|
err = handler.AuthorizationService.UpdateUsersAuthorizations()
|
2019-09-09 22:58:26 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to update user authorizations", err}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-06-11 13:13:19 +00:00
|
|
|
return response.JSON(w, endpointGroup)
|
|
|
|
}
|