added server ref

This commit is contained in:
ston1th 2020-10-25 21:29:19 +01:00
commit 78911eca56
4 changed files with 107 additions and 68 deletions

28
node.go
View file

@ -25,15 +25,15 @@ const (
) )
type Node struct { type Node struct {
Name string `json:"node"` Name string `json:"node"`
Status NodeStatus `json:"status"` Status NodeStatus `json:"status"`
CPU float64 `json:"cpu"` CPU float64 `json:"cpu"`
MaxCPU float64 `json:"maxcpu"` MaxCPU float64 `json:"maxcpu"`
Mem uint64 `json:"mem"` Mem uint64 `json:"mem"`
MaxMem uint64 `json:"maxmem"` MaxMem uint64 `json:"maxmem"`
Disk uint64 `json:"disk"` Disk uint64 `json:"disk"`
MaxDisk uint64 `json:"maxdisk"` MaxDisk uint64 `json:"maxdisk"`
serverList ServerList `json:"-"` serverList ServerRefList `json:"-"`
} }
func (n *Node) memFreePercent() float64 { func (n *Node) memFreePercent() float64 {
@ -191,7 +191,7 @@ func (c *NodeClient) List(ctx context.Context) (nl NodeList, err error) {
return return
} }
func (c *NodeClient) ListServers(ctx context.Context, n *Node) (sl ServerList, err error) { func (c *NodeClient) ListServers(ctx context.Context, n *Node) (sl ServerRefList, err error) {
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/nodes/%s/qemu", n.Name), nil) req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/nodes/%s/qemu", n.Name), nil)
if err != nil { if err != nil {
return return
@ -205,7 +205,7 @@ func (c *NodeClient) ListServers(ctx context.Context, n *Node) (sl ServerList, e
return return
} }
func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server, err error) { func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *ServerRef, err error) {
nl, err := c.List(ctx) nl, err := c.List(ctx)
if err != nil { if err != nil {
return return
@ -240,11 +240,11 @@ func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server
return nil, ErrServerNotFound return nil, ErrServerNotFound
} }
func (c *NodeClient) FindServerByName(ctx context.Context, name string) (s *Server, err error) { func (c *NodeClient) FindServerByName(ctx context.Context, name string) (s *ServerRef, err error) {
return c.FindServer(ctx, name, "") return c.FindServer(ctx, name, "")
} }
func (c *NodeClient) FindServerByID(ctx context.Context, id string) (s *Server, err error) { func (c *NodeClient) FindServerByID(ctx context.Context, id string) (s *ServerRef, err error) {
if id == "" { if id == "" {
err = ErrEmptyID err = ErrEmptyID
return return
@ -252,7 +252,7 @@ func (c *NodeClient) FindServerByID(ctx context.Context, id string) (s *Server,
return c.FindServer(ctx, "", id) return c.FindServer(ctx, "", id)
} }
func (c *NodeClient) FindServerByURL(ctx context.Context, url string) (s *Server, err error) { func (c *NodeClient) FindServerByURL(ctx context.Context, url string) (s *ServerRef, err error) {
_, _, id, err := ParseURL(url) _, _, id, err := ParseURL(url)
if err != nil { if err != nil {
return return

14
pool.go
View file

@ -103,9 +103,9 @@ type poolMember struct {
type poolMembers []poolMember type poolMembers []poolMember
func (pms poolMembers) ServerList() (sl ServerList) { func (pms poolMembers) ServerList() (sl ServerRefList) {
for _, pm := range pms { for _, pm := range pms {
sl = append(sl, &Server{ sl = append(sl, &ServerRef{
ID: strconv.Itoa(pm.ID), ID: strconv.Itoa(pm.ID),
Name: pm.Name, Name: pm.Name,
Node: pm.Node, Node: pm.Node,
@ -118,7 +118,7 @@ type pool struct {
Members poolMembers `json:"members"` Members poolMembers `json:"members"`
} }
func (c *PoolClient) ListMembers(ctx context.Context, name string) (sl ServerList, err error) { func (c *PoolClient) ListMembers(ctx context.Context, name string) (sl ServerRefList, err error) {
if name == "" { if name == "" {
err = ErrEmptyPoolName err = ErrEmptyPoolName
return return
@ -133,7 +133,7 @@ func (c *PoolClient) ListMembers(ctx context.Context, name string) (sl ServerLis
return return
} }
func (c *PoolClient) FindServer(ctx context.Context, poolname, servername, id string) (s *Server, err error) { func (c *PoolClient) FindServer(ctx context.Context, poolname, servername, id string) (s *ServerRef, err error) {
var pools []string var pools []string
if poolname != "" { if poolname != "" {
pools = append(pools, poolname) pools = append(pools, poolname)
@ -162,15 +162,15 @@ func (c *PoolClient) FindServer(ctx context.Context, poolname, servername, id st
return nil, ErrServerNotFound return nil, ErrServerNotFound
} }
func (c *PoolClient) FindServerByName(ctx context.Context, poolname, servername string) (s *Server, err error) { func (c *PoolClient) FindServerByName(ctx context.Context, poolname, servername string) (s *ServerRef, err error) {
return c.FindServer(ctx, poolname, servername, "") return c.FindServer(ctx, poolname, servername, "")
} }
func (c *PoolClient) FindServerByID(ctx context.Context, poolname, id string) (s *Server, err error) { func (c *PoolClient) FindServerByID(ctx context.Context, poolname, id string) (s *ServerRef, err error) {
return c.FindServer(ctx, poolname, "", id) return c.FindServer(ctx, poolname, "", id)
} }
func (c *PoolClient) FindServerByURL(ctx context.Context, url string) (s *Server, err error) { func (c *PoolClient) FindServerByURL(ctx context.Context, url string) (s *ServerRef, err error) {
pool, _, id, err := ParseURL(url) pool, _, id, err := ParseURL(url)
if err != nil { if err != nil {
return return

View file

@ -37,6 +37,19 @@ func ParseURL(s string) (pool, node, id string, err error) {
return u.Host, a[1], a[2], nil return u.Host, a[1], a[2], nil
} }
func ServerRefFromURL(s string) (ref *ServerRef, err error) {
pool, node, id, err := ParseURL(s)
if err != nil {
return
}
ref = &ServerRef{
ID: id,
Node: node,
Pool: pool,
}
return
}
func NewURL(pool, node, id string) string { func NewURL(pool, node, id string) string {
return fmt.Sprintf("%s://%s/%s/%s", PVEScheme, pool, node, id) return fmt.Sprintf("%s://%s/%s/%s", PVEScheme, pool, node, id)
} }

122
server.go
View file

@ -12,8 +12,6 @@ import (
var ErrServerNotFound = errors.New("server not found") var ErrServerNotFound = errors.New("server not found")
type ServerList []*Server
type Server struct { type Server struct {
ID string `json:"vmid"` ID string `json:"vmid"`
Name string `json:"name"` Name string `json:"name"`
@ -75,6 +73,32 @@ func (s *Server) K8sID() string {
return NewURL(s.Pool, "", s.ID) return NewURL(s.Pool, "", s.ID)
} }
func (s *Server) Ref() *ServerRef {
return &ServerRef{
ID: s.ID,
Node: s.Node,
Name: s.Name,
Pool: s.Pool,
}
}
type ServerRefList []*ServerRef
type ServerRef struct {
ID string `json:"vmid"`
Name string `json:"name"`
Node string
Pool string
}
func (ref *ServerRef) InstanceID() string {
return NewURL("", ref.Node, ref.ID)
}
func (ref *ServerRef) K8sID() string {
return NewURL(ref.Pool, "", ref.ID)
}
type Resources struct { type Resources struct {
Cores Cores `json:"cores"` Cores Cores `json:"cores"`
Memory Memory `json:"memory"` Memory Memory `json:"memory"`
@ -115,11 +139,11 @@ func (n Nameserver) String() string {
type serverConfig map[string]interface{} type serverConfig map[string]interface{}
func (sc serverConfig) Server(node, id string) (s *Server, err error) { func (sc serverConfig) Server(ref *ServerRef) (s *Server, err error) {
if len(sc) == 0 { if len(sc) == 0 {
return return
} }
s = &Server{ID: id, Node: node} s = &Server{ID: ref.ID, Node: ref.Node}
for k := range sc { for k := range sc {
switch k { switch k {
@ -211,9 +235,9 @@ func (c *ServerClient) NextID(ctx context.Context) (id string, err error) {
return return
} }
func (c *ServerClient) getConfig(ctx context.Context, node, id string) (cfg serverConfig, err error) { func (c *ServerClient) getConfig(ctx context.Context, ref *ServerRef) (cfg serverConfig, err error) {
cfg = make(serverConfig) cfg = make(serverConfig)
req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/nodes/%s/qemu/%s/config?current=1", node, id), nil) req, err := c.client.NewRequest(ctx, "GET", fmt.Sprintf("/nodes/%s/qemu/%s/config?current=1", ref.Node, ref.ID), nil)
if err != nil { if err != nil {
return return
} }
@ -221,35 +245,17 @@ func (c *ServerClient) getConfig(ctx context.Context, node, id string) (cfg serv
return return
} }
func (c *ServerClient) getServer(ctx context.Context, node, id string) (s *Server, cfg serverConfig, err error) { func (c *ServerClient) getServer(ctx context.Context, ref *ServerRef) (s *Server, cfg serverConfig, err error) {
cfg, err = c.getConfig(ctx, node, id) cfg, err = c.getConfig(ctx, ref)
if err != nil { if err != nil {
return return
} }
s, err = cfg.Server(node, id) s, err = cfg.Server(ref)
return return
} }
func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err error) { func (c *ServerClient) GetByRef(ctx context.Context, ref *ServerRef) (s *Server, err error) {
pool, node, id, err := ParseURL(url) s, cfg, err := c.getServer(ctx, ref)
if err != nil {
return
}
var srv *Server
if node == "" && pool != "" {
srv, err = c.client.Pool.FindServerByID(ctx, pool, id)
if err != nil {
return
}
node = srv.Node
} else if node == "" && pool == "" {
srv, err = c.client.Node.FindServerByID(ctx, id)
if err != nil {
return
}
node = srv.Node
}
s, cfg, err := c.getServer(ctx, node, id)
if err != nil { if err != nil {
return return
} }
@ -263,6 +269,26 @@ func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err
return return
} }
func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err error) {
ref, err := ServerRefFromURL(url)
if err != nil {
return
}
if ref.Node == "" && ref.Pool != "" {
ref, err = c.client.Pool.FindServerByID(ctx, ref.Pool, ref.ID)
if err != nil {
return
}
} else if ref.Node == "" && ref.Pool == "" {
ref, err = c.client.Node.FindServerByID(ctx, ref.ID)
if err != nil {
return
}
}
s, err = c.GetByRef(ctx, ref)
return
}
type ServerTemplateOpts struct { type ServerTemplateOpts struct {
Name string Name string
TemplateID string TemplateID string
@ -324,36 +350,36 @@ func (c *ServerClient) CreateFromTemplate(ctx context.Context, opts ServerTempla
return return
} }
func (c *ServerClient) Start(ctx context.Context, s *Server) (t *Task, err error) { func (c *ServerClient) Start(ctx context.Context, ref *ServerRef) (t *Task, err error) {
return c.setStatus(ctx, s, "start") return c.setStatus(ctx, ref, "start")
} }
func (c *ServerClient) Reboot(ctx context.Context, s *Server) (t *Task, err error) { func (c *ServerClient) Reboot(ctx context.Context, ref *ServerRef) (t *Task, err error) {
return c.setStatus(ctx, s, "reboot") return c.setStatus(ctx, ref, "reboot")
} }
func (c *ServerClient) Shutdown(ctx context.Context, s *Server) (t *Task, err error) { func (c *ServerClient) Shutdown(ctx context.Context, ref *ServerRef) (t *Task, err error) {
return c.setStatus(ctx, s, "shutdown") return c.setStatus(ctx, ref, "shutdown")
} }
func (c *ServerClient) Reset(ctx context.Context, s *Server) (t *Task, err error) { func (c *ServerClient) Reset(ctx context.Context, ref *ServerRef) (t *Task, err error) {
return c.setStatus(ctx, s, "reset") return c.setStatus(ctx, ref, "reset")
} }
func (c *ServerClient) Suspend(ctx context.Context, s *Server) (t *Task, err error) { func (c *ServerClient) Suspend(ctx context.Context, ref *ServerRef) (t *Task, err error) {
return c.setStatus(ctx, s, "suspent") return c.setStatus(ctx, ref, "suspent")
} }
func (c *ServerClient) Resume(ctx context.Context, s *Server) (t *Task, err error) { func (c *ServerClient) Resume(ctx context.Context, ref *ServerRef) (t *Task, err error) {
return c.setStatus(ctx, s, "resume") return c.setStatus(ctx, ref, "resume")
} }
func (c *ServerClient) Stop(ctx context.Context, s *Server) (t *Task, err error) { func (c *ServerClient) Stop(ctx context.Context, ref *ServerRef) (t *Task, err error) {
return c.setStatus(ctx, s, "stop") return c.setStatus(ctx, ref, "stop")
} }
func (c *ServerClient) setStatus(ctx context.Context, s *Server, status string) (t *Task, err error) { func (c *ServerClient) setStatus(ctx context.Context, ref *ServerRef, status string) (t *Task, err error) {
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/nodes/%s/qemu/%s/status/%s", s.Node, s.ID, status), nil) req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/nodes/%s/qemu/%s/status/%s", ref.Node, ref.ID, status), nil)
if err != nil { if err != nil {
return return
} }
@ -370,7 +396,7 @@ type statusObj struct {
func (c *ServerClient) GetStatus(ctx context.Context, s *Server, lock *string) (err error) { func (c *ServerClient) GetStatus(ctx context.Context, s *Server, lock *string) (err error) {
var lockval string var lockval string
if lock == nil { if lock == nil {
cfg, err := c.getConfig(ctx, s.Node, s.ID) cfg, err := c.getConfig(ctx, s.Ref())
if err != nil { if err != nil {
return err return err
} }
@ -429,7 +455,7 @@ func (c *ServerClient) resizeDisk(ctx context.Context, s *Server) (err error) {
} }
func (c *ServerClient) Update(ctx context.Context, s *Server) (t *Task, err error) { func (c *ServerClient) Update(ctx context.Context, s *Server) (t *Task, err error) {
cur, _, err := c.getServer(ctx, s.Node, s.ID) cur, _, err := c.getServer(ctx, s.Ref())
if err != nil { if err != nil {
return return
} }
@ -468,7 +494,7 @@ func (c *ServerClient) Update(ctx context.Context, s *Server) (t *Task, err erro
func (c *ServerClient) Delete(ctx context.Context, s *Server, f OnTaskChange) (t *Task, err error) { func (c *ServerClient) Delete(ctx context.Context, s *Server, f OnTaskChange) (t *Task, err error) {
if s.Status == ServerStatusRunning || s.Status == ServerStatusUnknown { if s.Status == ServerStatusRunning || s.Status == ServerStatusUnknown {
task, err := c.Stop(ctx, s) task, err := c.Stop(ctx, s.Ref())
if err != nil { if err != nil {
return nil, err return nil, err
} }