mirror of
https://github.com/bpg/terraform-provider-proxmox.git
synced 2025-07-01 19:12:59 +00:00
fix(vm): allow clone
to pass with warnings (#1317)
Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
parent
3f97a7b5c3
commit
fb1105d93b
@ -11,6 +11,7 @@ import (
|
|||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/avast/retry-go/v4"
|
"github.com/avast/retry-go/v4"
|
||||||
@ -86,10 +87,36 @@ func (c *Client) DeleteTask(ctx context.Context, upid string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type taskWaitOptions struct {
|
||||||
|
ignoreWarnings bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// TaskWaitOption is an option for waiting for a task to complete.
|
||||||
|
type TaskWaitOption interface {
|
||||||
|
apply(opts *taskWaitOptions)
|
||||||
|
}
|
||||||
|
|
||||||
|
type withIgnoreWarnings struct{}
|
||||||
|
|
||||||
|
// WithIgnoreWarnings is an option to ignore warnings when waiting for a task to complete.
|
||||||
|
func WithIgnoreWarnings() TaskWaitOption {
|
||||||
|
return withIgnoreWarnings{}
|
||||||
|
}
|
||||||
|
|
||||||
|
func (w withIgnoreWarnings) apply(opts *taskWaitOptions) {
|
||||||
|
opts.ignoreWarnings = true
|
||||||
|
}
|
||||||
|
|
||||||
// WaitForTask waits for a specific task to complete.
|
// WaitForTask waits for a specific task to complete.
|
||||||
func (c *Client) WaitForTask(ctx context.Context, upid string) error {
|
func (c *Client) WaitForTask(ctx context.Context, upid string, opts ...TaskWaitOption) error {
|
||||||
errStillRunning := errors.New("still running")
|
errStillRunning := errors.New("still running")
|
||||||
|
|
||||||
|
options := &taskWaitOptions{}
|
||||||
|
|
||||||
|
for _, opt := range opts {
|
||||||
|
opt.apply(options)
|
||||||
|
}
|
||||||
|
|
||||||
status, err := retry.DoWithData(
|
status, err := retry.DoWithData(
|
||||||
func() (*GetTaskStatusResponseData, error) {
|
func() (*GetTaskStatusResponseData, error) {
|
||||||
status, err := c.GetTaskStatus(ctx, upid)
|
status, err := c.GetTaskStatus(ctx, upid)
|
||||||
@ -132,6 +159,11 @@ func (c *Client) WaitForTask(ctx context.Context, upid string) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if status.ExitCode != "OK" {
|
if status.ExitCode != "OK" {
|
||||||
|
if options.ignoreWarnings &&
|
||||||
|
strings.HasPrefix(status.Status, "WARNINGS: ") && !strings.Contains(status.Status, "ERROR") {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
return fmt.Errorf("task %q failed to complete with exit code: %s", upid, status.ExitCode)
|
return fmt.Errorf("task %q failed to complete with exit code: %s", upid, status.ExitCode)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -21,6 +21,7 @@ import (
|
|||||||
"github.com/hashicorp/terraform-plugin-log/tflog"
|
"github.com/hashicorp/terraform-plugin-log/tflog"
|
||||||
|
|
||||||
"github.com/bpg/terraform-provider-proxmox/proxmox/api"
|
"github.com/bpg/terraform-provider-proxmox/proxmox/api"
|
||||||
|
"github.com/bpg/terraform-provider-proxmox/proxmox/nodes/tasks"
|
||||||
)
|
)
|
||||||
|
|
||||||
// CloneVM clones a virtual machine.
|
// CloneVM clones a virtual machine.
|
||||||
@ -44,7 +45,8 @@ func (c *Client) CloneVM(ctx context.Context, retries int, d *CloneRequestBody)
|
|||||||
return api.ErrNoDataObjectInResponse
|
return api.ErrNoDataObjectInResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
return c.Tasks().WaitForTask(ctx, *resBody.Data)
|
// ignoring warnings as per https://www.mail-archive.com/pve-devel@lists.proxmox.com/msg17724.html
|
||||||
|
return c.Tasks().WaitForTask(ctx, *resBody.Data, tasks.WithIgnoreWarnings())
|
||||||
}, retry.Attempts(uint(retries)), retry.Delay(10*time.Second))
|
}, retry.Attempts(uint(retries)), retry.Delay(10*time.Second))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("error waiting for VM clone: %w", err)
|
return fmt.Errorf("error waiting for VM clone: %w", err)
|
||||||
|
Loading…
Reference in New Issue
Block a user