added server ref
This commit is contained in:
parent
b4a538849a
commit
78911eca56
4 changed files with 107 additions and 68 deletions
28
node.go
28
node.go
|
|
@ -25,15 +25,15 @@ const (
|
|||
)
|
||||
|
||||
type Node struct {
|
||||
Name string `json:"node"`
|
||||
Status NodeStatus `json:"status"`
|
||||
CPU float64 `json:"cpu"`
|
||||
MaxCPU float64 `json:"maxcpu"`
|
||||
Mem uint64 `json:"mem"`
|
||||
MaxMem uint64 `json:"maxmem"`
|
||||
Disk uint64 `json:"disk"`
|
||||
MaxDisk uint64 `json:"maxdisk"`
|
||||
serverList ServerList `json:"-"`
|
||||
Name string `json:"node"`
|
||||
Status NodeStatus `json:"status"`
|
||||
CPU float64 `json:"cpu"`
|
||||
MaxCPU float64 `json:"maxcpu"`
|
||||
Mem uint64 `json:"mem"`
|
||||
MaxMem uint64 `json:"maxmem"`
|
||||
Disk uint64 `json:"disk"`
|
||||
MaxDisk uint64 `json:"maxdisk"`
|
||||
serverList ServerRefList `json:"-"`
|
||||
}
|
||||
|
||||
func (n *Node) memFreePercent() float64 {
|
||||
|
|
@ -191,7 +191,7 @@ func (c *NodeClient) List(ctx context.Context) (nl NodeList, err error) {
|
|||
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)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -205,7 +205,7 @@ func (c *NodeClient) ListServers(ctx context.Context, n *Node) (sl ServerList, e
|
|||
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)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
@ -240,11 +240,11 @@ func (c *NodeClient) FindServer(ctx context.Context, name, id string) (s *Server
|
|||
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, "")
|
||||
}
|
||||
|
||||
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 == "" {
|
||||
err = ErrEmptyID
|
||||
return
|
||||
|
|
@ -252,7 +252,7 @@ func (c *NodeClient) FindServerByID(ctx context.Context, id string) (s *Server,
|
|||
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)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
|
|||
14
pool.go
14
pool.go
|
|
@ -103,9 +103,9 @@ type poolMember struct {
|
|||
|
||||
type poolMembers []poolMember
|
||||
|
||||
func (pms poolMembers) ServerList() (sl ServerList) {
|
||||
func (pms poolMembers) ServerList() (sl ServerRefList) {
|
||||
for _, pm := range pms {
|
||||
sl = append(sl, &Server{
|
||||
sl = append(sl, &ServerRef{
|
||||
ID: strconv.Itoa(pm.ID),
|
||||
Name: pm.Name,
|
||||
Node: pm.Node,
|
||||
|
|
@ -118,7 +118,7 @@ type pool struct {
|
|||
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 == "" {
|
||||
err = ErrEmptyPoolName
|
||||
return
|
||||
|
|
@ -133,7 +133,7 @@ func (c *PoolClient) ListMembers(ctx context.Context, name string) (sl ServerLis
|
|||
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
|
||||
if poolname != "" {
|
||||
pools = append(pools, poolname)
|
||||
|
|
@ -162,15 +162,15 @@ func (c *PoolClient) FindServer(ctx context.Context, poolname, servername, id st
|
|||
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, "")
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
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)
|
||||
if err != nil {
|
||||
return
|
||||
|
|
|
|||
13
scheme.go
13
scheme.go
|
|
@ -37,6 +37,19 @@ func ParseURL(s string) (pool, node, id string, err error) {
|
|||
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 {
|
||||
return fmt.Sprintf("%s://%s/%s/%s", PVEScheme, pool, node, id)
|
||||
}
|
||||
|
|
|
|||
122
server.go
122
server.go
|
|
@ -12,8 +12,6 @@ import (
|
|||
|
||||
var ErrServerNotFound = errors.New("server not found")
|
||||
|
||||
type ServerList []*Server
|
||||
|
||||
type Server struct {
|
||||
ID string `json:"vmid"`
|
||||
Name string `json:"name"`
|
||||
|
|
@ -75,6 +73,32 @@ func (s *Server) K8sID() string {
|
|||
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 {
|
||||
Cores Cores `json:"cores"`
|
||||
Memory Memory `json:"memory"`
|
||||
|
|
@ -115,11 +139,11 @@ func (n Nameserver) String() string {
|
|||
|
||||
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 {
|
||||
return
|
||||
}
|
||||
s = &Server{ID: id, Node: node}
|
||||
s = &Server{ID: ref.ID, Node: ref.Node}
|
||||
|
||||
for k := range sc {
|
||||
switch k {
|
||||
|
|
@ -211,9 +235,9 @@ func (c *ServerClient) NextID(ctx context.Context) (id string, err error) {
|
|||
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)
|
||||
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 {
|
||||
return
|
||||
}
|
||||
|
|
@ -221,35 +245,17 @@ func (c *ServerClient) getConfig(ctx context.Context, node, id string) (cfg serv
|
|||
return
|
||||
}
|
||||
|
||||
func (c *ServerClient) getServer(ctx context.Context, node, id string) (s *Server, cfg serverConfig, err error) {
|
||||
cfg, err = c.getConfig(ctx, node, id)
|
||||
func (c *ServerClient) getServer(ctx context.Context, ref *ServerRef) (s *Server, cfg serverConfig, err error) {
|
||||
cfg, err = c.getConfig(ctx, ref)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
s, err = cfg.Server(node, id)
|
||||
s, err = cfg.Server(ref)
|
||||
return
|
||||
}
|
||||
|
||||
func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err error) {
|
||||
pool, node, id, err := ParseURL(url)
|
||||
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)
|
||||
func (c *ServerClient) GetByRef(ctx context.Context, ref *ServerRef) (s *Server, err error) {
|
||||
s, cfg, err := c.getServer(ctx, ref)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -263,6 +269,26 @@ func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err
|
|||
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 {
|
||||
Name string
|
||||
TemplateID string
|
||||
|
|
@ -324,36 +350,36 @@ func (c *ServerClient) CreateFromTemplate(ctx context.Context, opts ServerTempla
|
|||
return
|
||||
}
|
||||
|
||||
func (c *ServerClient) Start(ctx context.Context, s *Server) (t *Task, err error) {
|
||||
return c.setStatus(ctx, s, "start")
|
||||
func (c *ServerClient) Start(ctx context.Context, ref *ServerRef) (t *Task, err error) {
|
||||
return c.setStatus(ctx, ref, "start")
|
||||
}
|
||||
|
||||
func (c *ServerClient) Reboot(ctx context.Context, s *Server) (t *Task, err error) {
|
||||
return c.setStatus(ctx, s, "reboot")
|
||||
func (c *ServerClient) Reboot(ctx context.Context, ref *ServerRef) (t *Task, err error) {
|
||||
return c.setStatus(ctx, ref, "reboot")
|
||||
}
|
||||
|
||||
func (c *ServerClient) Shutdown(ctx context.Context, s *Server) (t *Task, err error) {
|
||||
return c.setStatus(ctx, s, "shutdown")
|
||||
func (c *ServerClient) Shutdown(ctx context.Context, ref *ServerRef) (t *Task, err error) {
|
||||
return c.setStatus(ctx, ref, "shutdown")
|
||||
}
|
||||
|
||||
func (c *ServerClient) Reset(ctx context.Context, s *Server) (t *Task, err error) {
|
||||
return c.setStatus(ctx, s, "reset")
|
||||
func (c *ServerClient) Reset(ctx context.Context, ref *ServerRef) (t *Task, err error) {
|
||||
return c.setStatus(ctx, ref, "reset")
|
||||
}
|
||||
|
||||
func (c *ServerClient) Suspend(ctx context.Context, s *Server) (t *Task, err error) {
|
||||
return c.setStatus(ctx, s, "suspent")
|
||||
func (c *ServerClient) Suspend(ctx context.Context, ref *ServerRef) (t *Task, err error) {
|
||||
return c.setStatus(ctx, ref, "suspent")
|
||||
}
|
||||
|
||||
func (c *ServerClient) Resume(ctx context.Context, s *Server) (t *Task, err error) {
|
||||
return c.setStatus(ctx, s, "resume")
|
||||
func (c *ServerClient) Resume(ctx context.Context, ref *ServerRef) (t *Task, err error) {
|
||||
return c.setStatus(ctx, ref, "resume")
|
||||
}
|
||||
|
||||
func (c *ServerClient) Stop(ctx context.Context, s *Server) (t *Task, err error) {
|
||||
return c.setStatus(ctx, s, "stop")
|
||||
func (c *ServerClient) Stop(ctx context.Context, ref *ServerRef) (t *Task, err error) {
|
||||
return c.setStatus(ctx, ref, "stop")
|
||||
}
|
||||
|
||||
func (c *ServerClient) setStatus(ctx context.Context, s *Server, 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)
|
||||
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", ref.Node, ref.ID, status), nil)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
|
@ -370,7 +396,7 @@ type statusObj struct {
|
|||
func (c *ServerClient) GetStatus(ctx context.Context, s *Server, lock *string) (err error) {
|
||||
var lockval string
|
||||
if lock == nil {
|
||||
cfg, err := c.getConfig(ctx, s.Node, s.ID)
|
||||
cfg, err := c.getConfig(ctx, s.Ref())
|
||||
if err != nil {
|
||||
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) {
|
||||
cur, _, err := c.getServer(ctx, s.Node, s.ID)
|
||||
cur, _, err := c.getServer(ctx, s.Ref())
|
||||
if err != nil {
|
||||
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) {
|
||||
if s.Status == ServerStatusRunning || s.Status == ServerStatusUnknown {
|
||||
task, err := c.Stop(ctx, s)
|
||||
task, err := c.Stop(ctx, s.Ref())
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue