2018-06-11 13:13:19 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
2018-08-07 15:43:36 +00:00
|
|
|
"encoding/json"
|
2018-06-11 13:13:19 +00:00
|
|
|
"net/http"
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
|
|
|
"github.com/portainer/libhttp/response"
|
2019-03-21 01:20:14 +00:00
|
|
|
"github.com/portainer/portainer/api"
|
|
|
|
"github.com/portainer/portainer/api/http/client"
|
|
|
|
"github.com/portainer/portainer/api/http/security"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
2018-07-03 18:31:02 +00:00
|
|
|
// GET request on /api/templates
|
2018-06-11 13:13:19 +00:00
|
|
|
func (handler *Handler) templateList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
2018-08-07 15:43:36 +00:00
|
|
|
settings, err := handler.SettingsService.Settings()
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2018-08-07 15:43:36 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
var templates []portainer.Template
|
|
|
|
if settings.TemplatesURL == "" {
|
|
|
|
templates, err = handler.TemplateService.Templates()
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve templates from the database", err}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
var templateData []byte
|
2018-09-24 00:09:12 +00:00
|
|
|
templateData, err = client.Get(settings.TemplatesURL, 0)
|
2018-08-07 15:43:36 +00:00
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve external templates", err}
|
|
|
|
}
|
|
|
|
|
|
|
|
err = json.Unmarshal(templateData, &templates)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to parse external templates", err}
|
|
|
|
}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 18:31:02 +00:00
|
|
|
securityContext, err := security.RetrieveRestrictedRequestContext(r)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2018-07-03 18:31:02 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve info from request context", err}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2018-07-03 18:31:02 +00:00
|
|
|
filteredTemplates := security.FilterTemplates(templates, securityContext)
|
|
|
|
return response.JSON(w, filteredTemplates)
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|