2018-06-11 13:13:19 +00:00
|
|
|
package templates
|
|
|
|
|
|
|
|
import (
|
2020-04-15 05:49:34 +00:00
|
|
|
"io"
|
2018-06-11 13:13:19 +00:00
|
|
|
"net/http"
|
|
|
|
|
2018-09-10 10:01:38 +00:00
|
|
|
httperror "github.com/portainer/libhttp/error"
|
2021-02-23 03:21:39 +00:00
|
|
|
portainer "github.com/portainer/portainer/api"
|
2018-06-11 13:13:19 +00:00
|
|
|
)
|
|
|
|
|
2021-02-23 03:21:39 +00:00
|
|
|
// introduced for swagger
|
|
|
|
type listResponse struct {
|
|
|
|
Version string
|
|
|
|
Templates []portainer.Template
|
|
|
|
}
|
|
|
|
|
|
|
|
// @id TemplateList
|
|
|
|
// @summary List available templates
|
|
|
|
// @description List available templates.
|
|
|
|
// @description **Access policy**: restricted
|
|
|
|
// @tags templates
|
|
|
|
// @security jwt
|
|
|
|
// @produce json
|
|
|
|
// @success 200 {object} listResponse "Success"
|
|
|
|
// @failure 500 "Server error"
|
|
|
|
// @router /templates [get]
|
2018-06-11 13:13:19 +00:00
|
|
|
func (handler *Handler) templateList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
|
2020-05-20 05:23:15 +00:00
|
|
|
settings, err := handler.DataStore.Settings().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}
|
|
|
|
}
|
|
|
|
|
2020-04-15 05:49:34 +00:00
|
|
|
resp, err := http.Get(settings.TemplatesURL)
|
|
|
|
if err != nil {
|
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve templates via the network", err}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
2020-04-15 05:49:34 +00:00
|
|
|
defer resp.Body.Close()
|
2018-06-11 13:13:19 +00:00
|
|
|
|
2020-04-15 05:49:34 +00:00
|
|
|
w.Header().Set("Content-Type", "application/json")
|
|
|
|
_, err = io.Copy(w, resp.Body)
|
2018-06-11 13:13:19 +00:00
|
|
|
if err != nil {
|
2020-04-15 05:49:34 +00:00
|
|
|
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to write templates from templates URL", err}
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|
|
|
|
|
2020-04-15 05:49:34 +00:00
|
|
|
return nil
|
2018-06-11 13:13:19 +00:00
|
|
|
}
|