added search for template node

This commit is contained in:
ston1th 2020-10-25 22:09:19 +01:00
commit 3c9a99682e

View file

@ -290,15 +290,16 @@ func (c *ServerClient) GetByURL(ctx context.Context, url string) (s *Server, err
}
type ServerTemplateOpts struct {
Name string
TemplateID string
Name string
TemplateID string
// TemplateNode is optional
TemplateNode string
Pool string
TargetStorage string
TargetNode string
}
func (o ServerTemplateOpts) Validate() error {
func (o ServerTemplateOpts) Validate(ctx context.Context, c *Client) error {
if o.Name == "" {
return errors.New("missing name")
}
@ -306,7 +307,14 @@ func (o ServerTemplateOpts) Validate() error {
return errors.New("missing template id")
}
if o.TemplateNode == "" {
return errors.New("missing template node")
ref, err := c.Node.FindServerByID(ctx, o.TemplateID)
if err != nil {
return fmt.Errorf("template node not found err: %w", err)
}
if ref.Node == "" {
return errors.New("missing template node")
}
o.TemplateNode = ref.Node
}
return nil
}
@ -324,7 +332,7 @@ func (o ServerTemplateOpts) body() httpbody {
}
func (c *ServerClient) CreateFromTemplate(ctx context.Context, opts ServerTemplateOpts) (t *Task, url string, err error) {
err = opts.Validate()
err = opts.Validate(ctx, c.client)
if err != nil {
return
}