0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-03 20:12:59 +00:00
terraform-provider-proxmox/proxmox/nodes/tasks/tasks.go
Rafał Safin 58347c09fe
feat(storage): add new resource proxmox_virtual_environment_download_file (#837)
* feat(core): add query url metadata endpoint to nodes client api

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(core): move storage api code to nodestorage folder

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(core): add download url api

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(storage): add  resource_download_file

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(storage): finish new resource_download_file

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(storage): generate docs for new download file resource, update other docs and examples

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(storage): add basic acc tests for resource download_file

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* fix(test): lint new test file

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(storage): warning instead of error when file already exists on resource download file

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(storage): better validation in resource download file, delete upload task on error

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(storage): better err message in resource download file

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* fix(core): removed unnecessary toint in custom type bool

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* fix(storage): typo in resource download file error

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(storage): download file resource review - 1

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* feat(storage): finish resource download file after review

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* fix(storage): error instead of warnings in parse int errors in sizeRequiresReplaceModifier

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* fix(docs): remove unwanted changes in virtual_environment_file.md

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* fix(test): fix download file base acceptance tests

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* fix(test): fix resource download file acc tests

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* fix(test): last fix resource download file acc test

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>

* fix: use PVE-compatible jammy LXC image, fix few typos

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>

---------

Signed-off-by: Rafał Safin <rafal.safin@rafsaf.pl>
Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
Co-authored-by: Rafał Safin <rafal.safin@rafsaf.pl>
Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
2024-01-02 21:29:13 -05:00

161 lines
3.5 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"
"errors"
"fmt"
"net/http"
"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{}
path, err := c.BuildPath(upid, "status")
if err != nil {
return nil, fmt.Errorf("error building path for task status: %w", err)
}
err = c.DoRequest(
ctx,
http.MethodGet,
path,
nil,
resBody,
)
if err != nil {
return nil, fmt.Errorf("error retrieving task status: %w", err)
}
if resBody.Data == nil {
return nil, api.ErrNoDataObjectInResponse
}
return resBody.Data, nil
}
// GetTaskLog retrieves the log of a task. The log is returned as an array of
// lines. Each line is an object with a line number and the text of the line.
// Reads first 50 lines by default.
func (c *Client) GetTaskLog(ctx context.Context, upid string) ([]string, error) {
resBody := &GetTaskLogResponseBody{}
lines := []string{}
path, err := c.BuildPath(upid, "log")
if err != nil {
return lines, fmt.Errorf("error building path for task status: %w", err)
}
err = c.DoRequest(
ctx,
http.MethodGet,
path,
nil,
resBody,
)
if err != nil {
return lines, fmt.Errorf("error retrieving task status: %w", err)
}
if resBody.Data == nil {
return lines, api.ErrNoDataObjectInResponse
}
for _, line := range resBody.Data {
lines = append(lines, line.LineText)
}
return lines, nil
}
// DeleteTask deletes specific task.
func (c *Client) DeleteTask(ctx context.Context, upid string) error {
path, err := c.baseTaskPath(upid)
if err != nil {
return fmt.Errorf("error creating task path: %w", err)
}
err = c.DoRequest(
ctx,
http.MethodDelete,
path,
nil,
nil,
)
if err != nil {
return fmt.Errorf("error deleting task: %w", err)
}
return 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)
isCriticalError := func(err error) bool {
var target *api.HTTPError
if errors.As(err, &target) {
if target.Code != http.StatusBadRequest {
// this is a special case to account for eventual consistency
// when creating a task -- the task may not be available via status API
// immediately after creation
return true
}
}
return err != nil
}
for timeElapsed.Seconds() < timeMax {
if int64(timeElapsed.Seconds())%timeDelay == 0 {
status, err := c.GetTaskStatus(ctx, upid)
if isCriticalError(err) {
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(
"timeout while waiting for task \"%s\" to complete",
upid,
)
}