0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-30 18:42:58 +00:00

feat(vm): Wait for the VM creation task to complete (#305)

feat(vm): Add sync wait in VM create operation
This commit is contained in:
Pavel Boldyrev 2023-04-17 21:30:08 -04:00 committed by GitHub
parent d24f60aaa2
commit 8addb1d1d5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 45 additions and 10 deletions

View File

@ -77,8 +77,45 @@ func (c *VirtualEnvironmentClient) CreateVM(
ctx context.Context,
nodeName string,
d *VirtualEnvironmentVMCreateRequestBody,
timeout int,
) error {
return c.DoRequest(ctx, http.MethodPost, fmt.Sprintf("nodes/%s/qemu", url.PathEscape(nodeName)), d, nil)
taskID, err := c.CreateVMAsync(ctx, nodeName, d)
if err != nil {
return err
}
err = c.WaitForNodeTask(ctx, nodeName, *taskID, timeout, 1)
if err != nil {
return fmt.Errorf("error waiting for VM creation: %w", err)
}
return nil
}
// CreateVMAsync creates a virtual machine asynchronously.
func (c *VirtualEnvironmentClient) CreateVMAsync(
ctx context.Context,
nodeName string,
d *VirtualEnvironmentVMCreateRequestBody,
) (*string, error) {
resBody := &VirtualEnvironmentVMCreateResponseBody{}
err := c.DoRequest(
ctx,
http.MethodPost,
fmt.Sprintf("nodes/%s/qemu", url.PathEscape(nodeName)),
d,
resBody,
)
if err != nil {
return nil, err
}
if resBody.Data == nil {
return nil, errors.New("the server did not include a data object in the response")
}
return resBody.Data, nil
}
// DeleteVM deletes a virtual machine.

View File

@ -303,6 +303,10 @@ type VirtualEnvironmentVMCreateRequestBody struct {
WatchdogDevice *CustomWatchdogDevice `json:"watchdog,omitempty" url:"watchdog,omitempty"`
}
type VirtualEnvironmentVMCreateResponseBody struct {
Data *string `json:"data,omitempty"`
}
// VirtualEnvironmentVMGetQEMUNetworkInterfacesResponseBody contains the body from a QEMU get network interfaces response.
type VirtualEnvironmentVMGetQEMUNetworkInterfacesResponseBody struct {
Data *VirtualEnvironmentVMGetQEMUNetworkInterfacesResponseData `json:"data,omitempty"`

View File

@ -228,6 +228,8 @@ const (
mkResourceVirtualEnvironmentVMVGAType = "type"
mkResourceVirtualEnvironmentVMVMID = "vm_id"
mkResourceVirtualEnvironmentVMSCSIHardware = "scsi_hardware"
vmCreateTimeoutSeconds = 10
)
func VM() *schema.Resource {
@ -2072,21 +2074,13 @@ func vmCreateCustom(ctx context.Context, d *schema.ResourceData, m interface{})
createBody.PoolID = &poolID
}
err = veClient.CreateVM(ctx, nodeName, createBody)
err = veClient.CreateVM(ctx, nodeName, createBody, vmCreateTimeoutSeconds)
if err != nil {
return diag.FromErr(err)
}
d.SetId(strconv.Itoa(vmID))
// NOTE: The VM creation is not atomic, and not synchronous. This means that the VM might not be
// available immediately after the creation, or its state reported by the API might not be
// up to date. This is a problem for the following operations, which rely on the VM information
// returned by API calls, particularly read-back to populate the Terraform state.
// Would it be possible to wait for the VM to be fully available, or to wait for the API to report
// the correct state?
// time.Sleep(5 * time.Second)
return vmCreateCustomDisks(ctx, d, m)
}