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-08-07 15:43:36 +00:00
|
|
|
"github.com/portainer/portainer"
|
|
|
|
"github.com/portainer/portainer/http/client"
|
2018-06-11 13:13:19 +00:00
|
|
|
httperror "github.com/portainer/portainer/http/error"
|
|
|
|
"github.com/portainer/portainer/http/response"
|
2018-07-03 18:31:02 +00:00
|
|
|
"github.com/portainer/portainer/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
|
|
|
|
templateData, err = client.Get(settings.TemplatesURL)
|
|
|
|
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
|
|
|
}
|