1
0
Fork 0
mirror of https://github.com/portainer/portainer.git synced 2025-07-24 15:59:41 +02:00

feat(templates): introduce templates management (#2017)

This commit is contained in:
Anthony Lapenna 2018-07-03 20:31:02 +02:00 committed by GitHub
parent e7939a5384
commit 61c285bd2e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
63 changed files with 3489 additions and 637 deletions

View file

@ -1,50 +1,26 @@
package templates
import (
"io/ioutil"
"net/http"
httperror "github.com/portainer/portainer/http/error"
"github.com/portainer/portainer/http/request"
"github.com/portainer/portainer/http/response"
"github.com/portainer/portainer/http/security"
)
// GET request on /api/templates?key=<key>
// GET request on /api/templates
func (handler *Handler) templateList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
key, err := request.RetrieveQueryParameter(r, "key", false)
templates, err := handler.TemplateService.Templates()
if err != nil {
return &httperror.HandlerError{http.StatusBadRequest, "Invalid query parameter: key", err}
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve templates from the database", err}
}
templatesURL, templateErr := handler.retrieveTemplateURLFromKey(key)
if templateErr != nil {
return templateErr
}
resp, err := http.Get(templatesURL)
securityContext, err := security.RetrieveRestrictedRequestContext(r)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve templates via the network", err}
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to read template response", err}
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve info from request context", err}
}
return response.Bytes(w, body, "application/json")
}
func (handler *Handler) retrieveTemplateURLFromKey(key string) (string, *httperror.HandlerError) {
switch key {
case "containers":
settings, err := handler.SettingsService.Settings()
if err != nil {
return "", &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve settings from the database", err}
}
return settings.TemplatesURL, nil
case "linuxserver.io":
return containerTemplatesURLLinuxServerIo, nil
}
return "", &httperror.HandlerError{http.StatusBadRequest, "Invalid value for query parameter: key. Value must be one of: containers or linuxserver.io", request.ErrInvalidQueryParameter}
filteredTemplates := security.FilterTemplates(templates, securityContext)
return response.JSON(w, filteredTemplates)
}