0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-06-29 18:21:10 +00:00
terraform-provider-proxmox/proxmox/nodes/tasks/tasks.go
Pavel Boldyrev 343e8045c1
feat(vm): add network device resources (#376)
* wip

* experimenting with terraform plugin framework

* cleaning up poc and adding tests

* adding read / update / delete

* update bridge_vlan_aware and MTU

* add ipv6 and simplify IP support

* fix provider's schema

* add docs

* run linter from cmdline

* disable TF acceptance tests

* add VLAN

* update docs

* add examole

* cleanup
2023-06-23 22:47:25 +00:00

87 lines
1.8 KiB
Go

/*
* 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 tasks
import (
"context"
"fmt"
"net/http"
"net/url"
"time"
"github.com/bpg/terraform-provider-proxmox/proxmox/api"
)
// GetTaskStatus retrieves the status of a task.
func (c *Client) GetTaskStatus(ctx context.Context, upid string) (*GetTaskStatusResponseData, error) {
resBody := &GetTaskStatusResponseBody{}
err := c.DoRequest(
ctx,
http.MethodGet,
c.ExpandPath(fmt.Sprintf("%s/status", url.PathEscape(upid))),
nil,
resBody,
)
if err != nil {
return nil, fmt.Errorf("error retrievinf task status: %w", err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
return resBody.Data, nil
}
// WaitForTask waits for a specific task to complete.
func (c *Client) WaitForTask(ctx context.Context, upid string, timeoutSec, delaySec int) error {
timeDelay := int64(delaySec)
timeMax := float64(timeoutSec)
timeStart := time.Now()
timeElapsed := timeStart.Sub(timeStart)
for timeElapsed.Seconds() < timeMax {
if int64(timeElapsed.Seconds())%timeDelay == 0 {
status, err := c.GetTaskStatus(ctx, upid)
if err != nil {
return err
}
if status.Status != "running" {
if status.ExitCode != "OK" {
return fmt.Errorf(
"task \"%s\" failed to complete with exit code: %s",
upid,
status.ExitCode,
)
}
return nil
}
time.Sleep(1 * time.Second)
}
time.Sleep(200 * time.Millisecond)
timeElapsed = time.Since(timeStart)
if ctx.Err() != nil {
return fmt.Errorf(
"context error while waiting for task \"%s\" to complete: %w",
upid, ctx.Err(),
)
}
}
return fmt.Errorf(
"timeoutSec while waiting for task \"%s\" to complete",
upid,
)
}