added server ref
This commit is contained in:
parent
b4a538849a
commit
78911eca56
4 changed files with 107 additions and 68 deletions
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