197 lines
4.0 KiB
Go
197 lines
4.0 KiB
Go
|
package ui
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"net/http"
|
||
|
|
||
|
"esxlib/api/cloud"
|
||
|
|
||
|
"git.twelvetwelve.org/library/snap"
|
||
|
)
|
||
|
|
||
|
var (
|
||
|
ErrNoIdProvided = fmt.Errorf("invalid request: no {id} provided")
|
||
|
)
|
||
|
|
||
|
func (s *Server) setupHandlers() {
|
||
|
uiHandlers := []handlerMap{
|
||
|
{
|
||
|
path: "/",
|
||
|
auth: false,
|
||
|
methods: []string{http.MethodGet},
|
||
|
handler: s.uiHandlerWithUserAndError(s.uiDashboard, nil),
|
||
|
},
|
||
|
{
|
||
|
path: "/ui/",
|
||
|
auth: false,
|
||
|
methods: []string{http.MethodGet},
|
||
|
handler: s.uiHandlerWithUserAndError(s.uiDashboard, nil),
|
||
|
},
|
||
|
{
|
||
|
path: "/ui/machines/",
|
||
|
auth: false,
|
||
|
methods: []string{http.MethodGet},
|
||
|
handler: s.uiHandlerWithUserAndError(s.uiMachines, nil),
|
||
|
},
|
||
|
{
|
||
|
path: "/ui/zones/",
|
||
|
auth: false,
|
||
|
methods: []string{http.MethodGet},
|
||
|
handler: s.uiHandlerWithUserAndError(s.uiZones, nil),
|
||
|
},
|
||
|
{
|
||
|
path: "/ui/zone/{id}/",
|
||
|
auth: false,
|
||
|
methods: []string{http.MethodGet},
|
||
|
handler: s.uiHandlerWithUserAndError(s.uiZoneShow, nil),
|
||
|
},
|
||
|
}
|
||
|
|
||
|
for k := range uiHandlers {
|
||
|
v := uiHandlers[k]
|
||
|
|
||
|
if v.auth {
|
||
|
panic(false)
|
||
|
/*
|
||
|
redirect := AuthLoginPath
|
||
|
if v.redirect != "" {
|
||
|
redirect = v.redirect
|
||
|
}
|
||
|
|
||
|
if v.handler != nil {
|
||
|
s.snap.HandleFuncAuthenticated(v.path, redirect, v.handler).Methods(v.methods...)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
s.snap.HandleFuncAuthenticated(v.path, redirect, func(ctx *snap.Context) {
|
||
|
ctx.RenderWithMeta(v.template, nil, nil)
|
||
|
}).Methods(v.methods...)
|
||
|
*/
|
||
|
} else {
|
||
|
if v.handler != nil {
|
||
|
s.snap.HandleFunc(v.path, v.handler).Methods(v.methods...)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
s.snap.HandleFunc(v.path, func(ctx *snap.Context) {
|
||
|
ctx.RenderWithMeta(v.template, nil, nil)
|
||
|
}).Methods(v.methods...)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
func (s *Server) uiDashboard(uid string, ctx *snap.Context) error {
|
||
|
var err error
|
||
|
content := struct {
|
||
|
Resp *cloud.VMListResponse
|
||
|
}{}
|
||
|
|
||
|
content.Resp, err = s.api.VMList(context.Background(), nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return s.RenderWithMeta(ctx, "dashboard.html", nil, &content)
|
||
|
}
|
||
|
|
||
|
func (s *Server) uiMachines(uid string, ctx *snap.Context) error {
|
||
|
content := struct {
|
||
|
Machines []*cloud.VMInfo
|
||
|
}{}
|
||
|
|
||
|
resp, err := s.api.VMList(context.Background(), nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
content.Machines = resp.Vms
|
||
|
|
||
|
return s.RenderWithMeta(ctx, "machines.html", nil, &content)
|
||
|
}
|
||
|
|
||
|
func (s *Server) uiMachineShow(uid string, ctx *snap.Context) error {
|
||
|
content := struct {
|
||
|
Machine *cloud.VMInfo
|
||
|
}{}
|
||
|
|
||
|
id, _ := ctx.ParseVars().GetVar("id")
|
||
|
if id == "" {
|
||
|
return ErrNoIdProvided
|
||
|
}
|
||
|
|
||
|
resp, err := s.api.VMList(context.Background(), nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
content.Machines = resp.Vms
|
||
|
|
||
|
return s.RenderWithMeta(ctx, "machines.html", nil, &content)
|
||
|
}
|
||
|
|
||
|
func (s *Server) uiZones(uid string, ctx *snap.Context) error {
|
||
|
var err error
|
||
|
content := struct {
|
||
|
Resp *cloud.ZonesListResponse
|
||
|
}{}
|
||
|
|
||
|
content.Resp, err = s.api.ZonesList(context.Background(), nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return s.RenderWithMeta(ctx, "zones.html", nil, &content)
|
||
|
}
|
||
|
|
||
|
func (s *Server) uiZoneShow(uid string, ctx *snap.Context) error {
|
||
|
content := struct {
|
||
|
Zone *cloud.Zone
|
||
|
Slugs []*cloud.Slug
|
||
|
Networks []*cloud.Network
|
||
|
Machines []*cloud.VMInfo
|
||
|
}{}
|
||
|
|
||
|
id, _ := ctx.ParseVars().GetVar("id")
|
||
|
if id == "" {
|
||
|
return ErrNoIdProvided
|
||
|
}
|
||
|
|
||
|
resp, err := s.api.ZonesList(context.Background(), nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for idx := range resp.Zones {
|
||
|
if resp.Zones[idx].Id == id {
|
||
|
content.Zone = resp.Zones[idx]
|
||
|
break
|
||
|
}
|
||
|
}
|
||
|
|
||
|
vms, err := s.api.VMList(context.Background(), nil)
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
for idx := range vms.Vms {
|
||
|
if vms.Vms[idx].Zone == id {
|
||
|
content.Machines = append(content.Machines, vms.Vms[idx])
|
||
|
}
|
||
|
}
|
||
|
|
||
|
nets, err := s.api.NetworksList(context.Background(), &cloud.NetworksListRequest{ZoneId: id})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
content.Networks = nets.Networks
|
||
|
|
||
|
slugs, err := s.api.VMListSlugs(context.Background(), &cloud.VMListSlugsRequest{ZoneId: id})
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
content.Slugs = slugs.Slugs
|
||
|
|
||
|
return s.RenderWithMeta(ctx, "zone_show.html", nil, &content)
|
||
|
}
|