2020-07-06 23:18:39 +00:00
|
|
|
package customtemplates
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2021-09-02 05:28:51 +00:00
|
|
|
"strconv"
|
2020-07-06 23:18:39 +00:00
|
|
|
|
2021-09-02 05:28:51 +00:00
|
|
|
"github.com/pkg/errors"
|
2020-07-06 23:18:39 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/response"
|
2021-02-22 23:52:18 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2020-07-06 23:18:39 +00:00
|
|
|
"github.com/portainer/portainer/api/http/security"
|
|
|
|
"github.com/portainer/portainer/api/internal/authorization"
|
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// @id CustomTemplateList
|
|
|
|
// @summary List available custom templates
|
|
|
|
// @description List available custom templates.
|
|
|
|
// @description **Access policy**: authenticated
|
|
|
|
// @tags custom_templates
|
2021-11-30 02:31:16 +00:00
|
|
|
// @security ApiKeyAuth
|
2021-02-23 03:21:39 +00:00
|
|
|
// @security jwt
|
|
|
|
// @produce json
|
2021-09-02 05:28:51 +00:00
|
|
|
// @param type query []int true "Template types" Enums(1,2,3)
|
2021-02-23 03:21:39 +00:00
|
|
|
// @success 200 {array} portainer.CustomTemplate "Success"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /custom_templates [get]
|
2020-07-06 23:18:39 +00:00
|
|
|
func (handler *Handler) customTemplateList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
2021-09-02 05:28:51 +00:00
|
|
|
templateTypes, err := parseTemplateTypes(r)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusBadRequest, "Invalid Custom template type", err}
|
|
|
|
}
|
|
|
|
|
2020-07-06 23:18:39 +00:00
|
|
|
customTemplates, err := handler.DataStore.CustomTemplate().CustomTemplates()
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve custom templates from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceControls, err := handler.DataStore.ResourceControl().ResourceControls()
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve resource controls from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
customTemplates = authorization.DecorateCustomTemplates(customTemplates, resourceControls)
|
|
|
|
|
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve info from request context", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !securityContext.IsAdmin {
|
|
|
|
user, err := handler.DataStore.User().User(securityContext.UserID)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve user information from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
userTeamIDs := make([]portainer.TeamID, 0)
|
|
|
|
for _, membership := range securityContext.UserMemberships {
|
|
|
|
userTeamIDs = append(userTeamIDs, membership.TeamID)
|
|
|
|
}
|
|
|
|
|
|
|
|
customTemplates = authorization.FilterAuthorizedCustomTemplates(customTemplates, user, userTeamIDs)
|
|
|
|
}
|
|
|
|
|
2021-09-02 05:28:51 +00:00
|
|
|
customTemplates = filterByType(customTemplates, templateTypes)
|
|
|
|
|
2020-07-06 23:18:39 +00:00
|
|
|
return response.JSON(w, customTemplates)
|
|
|
|
}
|
2021-09-02 05:28:51 +00:00
|
|
|
|
|
|
|
func parseTemplateTypes(r *http.Request) ([]portainer.StackType, error) {
|
|
|
|
err := r.ParseForm()
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed to parse request params")
|
|
|
|
}
|
|
|
|
|
|
|
|
types, exist := r.Form["type"]
|
|
|
|
if !exist {
|
|
|
|
return []portainer.StackType{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
res := []portainer.StackType{}
|
|
|
|
for _, templateTypeStr := range types {
|
|
|
|
templateType, err := strconv.Atoi(templateTypeStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.WithMessage(err, "failed parsing template type")
|
|
|
|
}
|
|
|
|
|
|
|
|
res = append(res, portainer.StackType(templateType))
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func filterByType(customTemplates []portainer.CustomTemplate, templateTypes []portainer.StackType) []portainer.CustomTemplate {
|
|
|
|
if len(templateTypes) == 0 {
|
|
|
|
return customTemplates
|
|
|
|
}
|
|
|
|
|
|
|
|
typeSet := map[portainer.StackType]bool{}
|
|
|
|
for _, templateType := range templateTypes {
|
|
|
|
typeSet[templateType] = true
|
|
|
|
}
|
|
|
|
|
|
|
|
filtered := []portainer.CustomTemplate{}
|
|
|
|
|
|
|
|
for _, template := range customTemplates {
|
|
|
|
if typeSet[template.Type] {
|
|
|
|
filtered = append(filtered, template)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return filtered
|
|
|
|
}
|