From 3c9a99682ebb47542050f09eb85cbf310e7f2c48 Mon Sep 17 00:00:00 2001 From: ston1th Date: Sun, 25 Oct 2020 22:09:19 +0100 Subject: [PATCH] added search for template node --- server.go | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/server.go b/server.go index a82d801..466f1a9 100644 --- a/server.go +++ b/server.go @@ -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 }