fix(api): update to template file format for Edge templates

2.0
Anthony Lapenna 2020-05-18 12:20:27 +12:00
parent 4fdb0934cb
commit d9665bc939
1 changed files with 11 additions and 8 deletions

View File

@ -2,7 +2,6 @@ package edgetemplates
import ( import (
"encoding/json" "encoding/json"
"log"
"net/http" "net/http"
httperror "github.com/portainer/libhttp/error" httperror "github.com/portainer/libhttp/error"
@ -11,6 +10,11 @@ import (
"github.com/portainer/portainer/api/http/client" "github.com/portainer/portainer/api/http/client"
) )
type templateFileFormat struct {
Version string `json:"version"`
Templates []portainer.Template `json:"templates"`
}
// GET request on /api/edgetemplates // GET request on /api/edgetemplates
func (handler *Handler) edgeTemplateList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError { func (handler *Handler) edgeTemplateList(w http.ResponseWriter, r *http.Request) *httperror.HandlerError {
settings, err := handler.SettingsService.Settings() settings, err := handler.SettingsService.Settings()
@ -24,22 +28,21 @@ func (handler *Handler) edgeTemplateList(w http.ResponseWriter, r *http.Request)
} }
var templateData []byte var templateData []byte
templateData, err = client.Get(url, 0) templateData, err = client.Get(url, 10)
if err != nil { if err != nil {
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve external templates", err} return &httperror.HandlerError{http.StatusInternalServerError, "Unable to retrieve external templates", err}
} }
var templates []portainer.Template var templateFile templateFileFormat
err = json.Unmarshal(templateData, &templates) err = json.Unmarshal(templateData, &templateFile)
if err != nil { if err != nil {
log.Printf("[DEBUG] [http,edge,templates] [failed parsing edge templates] [body: %s]", templateData) return &httperror.HandlerError{http.StatusInternalServerError, "Unable to parse template file", err}
return &httperror.HandlerError{http.StatusInternalServerError, "Unable to parse external templates", err}
} }
filteredTemplates := []portainer.Template{} filteredTemplates := make([]portainer.Template, 0)
for _, template := range templates { for _, template := range templateFile.Templates {
if template.Type == portainer.EdgeStackTemplate { if template.Type == portainer.EdgeStackTemplate {
filteredTemplates = append(filteredTemplates, template) filteredTemplates = append(filteredTemplates, template)
} }