mirror of
https://github.com/anand34577/ferrum.git
synced 2026-09-16 15:45:09 +00:00
61 lines
1.6 KiB
Go
61 lines
1.6 KiB
Go
package api
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/go-chi/chi/v5"
|
|
)
|
|
|
|
type templateItem struct {
|
|
VolID string `json:"volid"`
|
|
Node string `json:"node"`
|
|
Storage string `json:"storage"`
|
|
Content string `json:"content"` // "iso" | "vztmpl"
|
|
Size int64 `json:"size,omitempty"`
|
|
}
|
|
|
|
// listTemplates aggregates ISO images and LXC templates across every node
|
|
// and storage on a connection, so the create-guest wizard can offer a real
|
|
// picker instead of asking the user to type a volume ID from memory.
|
|
func (s *Server) listTemplates(w http.ResponseWriter, r *http.Request) {
|
|
connID := chi.URLParam(r, "id")
|
|
client, err := s.clientFor(r.Context(), connID)
|
|
if err != nil {
|
|
s.writeError(w, http.StatusBadGateway, err)
|
|
return
|
|
}
|
|
|
|
nodes, err := client.Nodes(r.Context())
|
|
if err != nil {
|
|
s.writeError(w, http.StatusBadGateway, err)
|
|
return
|
|
}
|
|
|
|
out := []templateItem{}
|
|
for _, node := range nodes {
|
|
storages, err := client.NodeStorage(r.Context(), node.Node)
|
|
if err != nil {
|
|
continue // one unreachable/misconfigured node shouldn't blank the whole catalog
|
|
}
|
|
for _, storage := range storages {
|
|
if storage.Active == 0 {
|
|
continue
|
|
}
|
|
items, err := client.StorageContent(r.Context(), node.Node, storage.Storage)
|
|
if err != nil {
|
|
continue
|
|
}
|
|
for _, item := range items {
|
|
if item.Content != "iso" && item.Content != "vztmpl" {
|
|
continue
|
|
}
|
|
out = append(out, templateItem{
|
|
VolID: item.VolID, Node: node.Node, Storage: storage.Storage,
|
|
Content: item.Content, Size: item.Size,
|
|
})
|
|
}
|
|
}
|
|
}
|
|
writeJSON(w, http.StatusOK, out)
|
|
}
|