0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 02:31:10 +00:00

fix(vm): do not delete a VM during retry on create (#1711)

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
Pavel Boldyrev 2025-01-13 20:40:16 -05:00 committed by GitHub
parent 085caaf927
commit c57dc78119
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 13 deletions

View File

@ -6,12 +6,6 @@
//go:build acceptance || all
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package test
import (
@ -161,7 +155,7 @@ func TestAccResourceVM(t *testing.T) {
architecture = "x86_64"
}
}`, WithAPIToken()),
ExpectError: regexp.MustCompile(`the CPU architecture can only be set by the root account`),
ExpectError: regexp.MustCompile(`can only be set by the root account`),
},
{
Config: te.RenderConfig(`

View File

@ -79,10 +79,19 @@ func (c *Client) CreateVM(ctx context.Context, d *CreateRequestBody) error {
// CreateVMAsync creates a virtual machine asynchronously. Returns ID of the started task.
func (c *Client) CreateVMAsync(ctx context.Context, d *CreateRequestBody) (*string, error) {
resBody := &CreateResponseBody{}
retrying := false
// retry the request if we get an error that the VM already exists
// but only if we're retrying. If this error is returned by the first
// request, we'll just return the error (i.e. can't "override" the VM).
err := retry.Do(
func() error {
return c.DoRequest(ctx, http.MethodPost, c.basePath(), d, resBody)
err := c.DoRequest(ctx, http.MethodPost, c.basePath(), d, resBody)
if err != nil && retrying && strings.Contains(err.Error(), "already exists") {
return nil
}
return err
},
retry.Context(ctx),
retry.Attempts(3),
@ -94,11 +103,10 @@ func (c *Client) CreateVMAsync(ctx context.Context, d *CreateRequestBody) (*stri
"error": err.Error(),
})
if e := c.DeleteVM(ctx); e != nil {
tflog.Warn(ctx, "deleting VM after failed creation", map[string]interface{}{
"error": e,
})
}
retrying = true
}),
retry.RetryIf(func(err error) bool {
return strings.Contains(err.Error(), "got no worker upid")
}),
)
if err != nil {