better pool and changed schema

This commit is contained in:
ston1th 2020-06-03 23:10:22 +02:00
commit 249d7c6844
4 changed files with 82 additions and 21 deletions

View file

@ -10,6 +10,10 @@ import (
"strings"
)
var ErrServerNotFound = errors.New("server not found")
type ServerList []*Server
type Server struct {
ID string `json:"vmid"`
Name string `json:"name"`
@ -180,11 +184,26 @@ func (c *ServerClient) getServer(ctx context.Context, node, id string) (s *Serve
}
func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err error) {
node, id, err := ParseURL(url)
pool, id, err := ParseURL(url)
if err != nil {
return
}
s, cfg, err := c.getServer(ctx, node, id)
var srv *Server
if pool != "" {
srv, err = c.client.Pool.FindServerByID(ctx, pool, id)
if err == ErrServerNotFound {
return
}
}
srv, err = c.client.Node.FindServerByID(ctx, id)
if err != nil {
return
}
if srv == nil {
err = ErrServerNotFound
return
}
s, cfg, err := c.getServer(ctx, srv.Node, srv.ID)
if err != nil {
return
}
@ -200,7 +219,8 @@ func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err
type ServerTemplateOpts struct {
Name string
TemplateURL string
TemplateID string
TemplateNode string
Pool string
TargetStorage string
TargetNode string
@ -210,8 +230,11 @@ func (o ServerTemplateOpts) Validate() error {
if o.Name == "" {
return errors.New("missing name")
}
if o.TemplateURL == "" {
return errors.New("missing template url")
if o.TemplateID == "" {
return errors.New("missing template id")
}
if o.TemplateNode == "" {
return errors.New("missing template node")
}
return nil
}
@ -233,23 +256,17 @@ func (c *ServerClient) CreateFromTemplate(ctx context.Context, opts ServerTempla
if err != nil {
return
}
tempnode, id, err := ParseURL(opts.TemplateURL)
if err != nil {
return
}
nextid, err := c.NextID(ctx)
if err != nil {
return
}
body := opts.body()
body["newid"] = nextid
if opts.TargetNode != "" && tempnode != opts.TargetNode {
if opts.TargetNode != "" && opts.TemplateNode != opts.TargetNode {
body["target"] = opts.TargetNode
url = NewURL(opts.TargetNode, nextid)
} else {
url = NewURL(tempnode, nextid)
}
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/nodes/%s/qemu/%s/clone", tempnode, id), body.Reader())
url = NewURL(opts.Pool, nextid)
req, err := c.client.NewRequest(ctx, "POST", fmt.Sprintf("/nodes/%s/qemu/%s/clone", opts.TemplateNode, opts.TemplateID), body.Reader())
if err != nil {
return
}