mirror of
https://github.com/portainer/portainer.git
synced 2025-07-24 15:59:41 +02:00
feat(custom-templates): filter templates by edge [EE-6565] (#10979)
This commit is contained in:
parent
441a8bbbbf
commit
2826a4ce39
10 changed files with 229 additions and 21 deletions
|
@ -8,8 +8,11 @@ import (
|
|||
portainer "github.com/portainer/portainer/api"
|
||||
"github.com/portainer/portainer/api/http/security"
|
||||
"github.com/portainer/portainer/api/internal/authorization"
|
||||
"github.com/portainer/portainer/api/internal/slices"
|
||||
httperror "github.com/portainer/portainer/pkg/libhttp/error"
|
||||
"github.com/portainer/portainer/pkg/libhttp/request"
|
||||
"github.com/portainer/portainer/pkg/libhttp/response"
|
||||
"github.com/rs/zerolog/log"
|
||||
)
|
||||
|
||||
// @id CustomTemplateList
|
||||
|
@ -21,6 +24,7 @@ import (
|
|||
// @security jwt
|
||||
// @produce json
|
||||
// @param type query []int true "Template types" Enums(1,2,3)
|
||||
// @param edge query boolean false "Filter by edge templates"
|
||||
// @success 200 {array} portainer.CustomTemplate "Success"
|
||||
// @failure 500 "Server error"
|
||||
// @router /custom_templates [get]
|
||||
|
@ -30,6 +34,8 @@ func (handler *Handler) customTemplateList(w http.ResponseWriter, r *http.Reques
|
|||
return httperror.BadRequest("Invalid Custom template type", err)
|
||||
}
|
||||
|
||||
edge := retrieveEdgeParam(r)
|
||||
|
||||
customTemplates, err := handler.DataStore.CustomTemplate().ReadAll()
|
||||
if err != nil {
|
||||
return httperror.InternalServerError("Unable to retrieve custom templates from the database", err)
|
||||
|
@ -63,9 +69,37 @@ func (handler *Handler) customTemplateList(w http.ResponseWriter, r *http.Reques
|
|||
|
||||
customTemplates = filterByType(customTemplates, templateTypes)
|
||||
|
||||
if edge != nil {
|
||||
customTemplates = slices.Filter(customTemplates, func(customTemplate portainer.CustomTemplate) bool {
|
||||
return customTemplate.EdgeTemplate == *edge
|
||||
})
|
||||
}
|
||||
|
||||
for i := range customTemplates {
|
||||
customTemplate := &customTemplates[i]
|
||||
if customTemplate.GitConfig != nil && customTemplate.GitConfig.Authentication != nil {
|
||||
customTemplate.GitConfig.Authentication.Password = ""
|
||||
}
|
||||
}
|
||||
|
||||
return response.JSON(w, customTemplates)
|
||||
}
|
||||
|
||||
func retrieveEdgeParam(r *http.Request) *bool {
|
||||
var edge *bool
|
||||
edgeParam, _ := request.RetrieveQueryParameter(r, "edge", true)
|
||||
if edgeParam != "" {
|
||||
edgeVal, err := strconv.ParseBool(edgeParam)
|
||||
if err != nil {
|
||||
log.Warn().Err(err).Msg("failed parsing edge param")
|
||||
return nil
|
||||
}
|
||||
|
||||
edge = &edgeVal
|
||||
}
|
||||
return edge
|
||||
}
|
||||
|
||||
func parseTemplateTypes(r *http.Request) ([]portainer.StackType, error) {
|
||||
err := r.ParseForm()
|
||||
if err != nil {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue