Skip to content

Commit

Permalink
feat(file): ensure upload of ISO/VSTMPL is completed upon resource cr…
Browse files Browse the repository at this point in the history
…eation (#471)

* feat(provider): ensure upload of ISO/VSTMPL completes before starting VM and add timeout to config for this

* remove `ForceNew: true` for the timeout attribute

* minor docs update

---------

Co-authored-by: dandaolrian <dandaolrian@users.noreply.github.com>
Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
  • Loading branch information
3 people authored Aug 10, 2023
1 parent 8a5a533 commit f901e71
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 2 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ example: example-build example-init example-apply example-destroy

example-apply:
export TF_CLI_CONFIG_FILE="$(shell pwd -P)/example.tfrc" \
&& export TF_REATTACH_PROVIDERS='{"registry.terraform.io/bpg/proxmox":{"Protocol":"grpc","ProtocolVersion":6,"Pid":82711,"Test":true,"Addr":{"Network":"unix","String":"/var/folders/xx/4plpx_3133lbctp7b7v1yfch0000gn/T/plugin3121225613"}}}' \
&& export TF_DISABLE_CHECKPOINT="true" \
&& export TF_PLUGIN_CACHE_DIR="$(TERRAFORM_PLUGIN_CACHE_DIRECTORY)" \
&& cd ./example \
Expand Down
5 changes: 4 additions & 1 deletion docs/resources/virtual_environment_file.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ EOF
- `data` - (Required) The raw data.
- `file_name` - (Required) The file name.
- `resize` - (Optional) The number of bytes to resize the file to.
- `timeout_upload` - (Optional) Timeout for uploading ISO/VSTMPL files in
seconds (defaults to 1800).

## Attribute Reference

Expand All @@ -96,7 +98,8 @@ created as another temporary file).

## Import

Instances can be imported using the `node_name`, `datastore_id`, `content_type` and the `file_name`, e.g.,
Instances can be imported using the `node_name`, `datastore_id`, `content_type`
and the `file_name`, e.g.,

```bash
$ terraform import proxmox_virtual_environment_file.cloud_config pve/local/snippets/example.cloud-config.yaml
Expand Down
11 changes: 11 additions & 0 deletions proxmox/nodes/storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func (c *Client) APIUpload(
ctx context.Context,
datastoreID string,
d *api.FileUploadRequest,
uploadTimeout int,
) (*DatastoreUploadResponseBody, error) {
tflog.Debug(ctx, "uploading file to datastore using PVE API", map[string]interface{}{
"file_name": d.FileName,
Expand Down Expand Up @@ -276,5 +277,15 @@ func (c *Client) APIUpload(
return nil, fmt.Errorf("error uploading file to datastore %s: %w", datastoreID, err)
}

if resBody.UploadID == nil {
return nil, fmt.Errorf("error uploading file to datastore %s: no uploadID", datastoreID)
}

err = c.Tasks().WaitForTask(ctx, *resBody.UploadID, uploadTimeout, 5)

if err != nil {
return nil, fmt.Errorf("error uploading file to datastore %s: failed waiting for upload - %w", datastoreID, err)
}

return resBody, nil
}
11 changes: 10 additions & 1 deletion proxmoxtf/resource/file.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const (
dvResourceVirtualEnvironmentFileSourceFileFileName = ""
dvResourceVirtualEnvironmentFileSourceFileInsecure = false
dvResourceVirtualEnvironmentFileSourceRawResize = 0
dvResourceVirtualEnvironmentFileTimeoutUpload = 1800

mkResourceVirtualEnvironmentFileContentType = "content_type"
mkResourceVirtualEnvironmentFileDatastoreID = "datastore_id"
Expand All @@ -58,6 +59,7 @@ const (
mkResourceVirtualEnvironmentFileSourceRawData = "data"
mkResourceVirtualEnvironmentFileSourceRawFileName = "file_name"
mkResourceVirtualEnvironmentFileSourceRawResize = "resize"
mkResourceVirtualEnvironmentFileTimeoutUpload = "timeout_upload"
)

// File returns a resource that manages files on a node.
Expand Down Expand Up @@ -190,6 +192,12 @@ func File() *schema.Resource {
MaxItems: 1,
MinItems: 0,
},
mkResourceVirtualEnvironmentFileTimeoutUpload: {
Type: schema.TypeInt,
Description: "Timeout for uploading ISO/VSTMPL files in seconds",
Optional: true,
Default: dvResourceVirtualEnvironmentFileTimeoutUpload,
},
},
CreateContext: fileCreate,
ReadContext: fileRead,
Expand Down Expand Up @@ -425,7 +433,8 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag

switch *contentType {
case "iso", "vztmpl":
_, err = capi.Node(nodeName).APIUpload(ctx, datastoreID, request)
uploadTimeout := d.Get(mkResourceVirtualEnvironmentFileTimeoutUpload).(int)
_, err = capi.Node(nodeName).APIUpload(ctx, datastoreID, request, uploadTimeout)
default:
// For all other content types, we need to upload the file to the node's
// datastore using SFTP.
Expand Down
2 changes: 2 additions & 0 deletions proxmoxtf/resource/file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ func TestFileSchema(t *testing.T) {
mkResourceVirtualEnvironmentFileContentType,
mkResourceVirtualEnvironmentFileSourceFile,
mkResourceVirtualEnvironmentFileSourceRaw,
mkResourceVirtualEnvironmentFileTimeoutUpload,
})

test.AssertComputedAttributes(t, s, []string{
Expand All @@ -59,6 +60,7 @@ func TestFileSchema(t *testing.T) {
mkResourceVirtualEnvironmentFileNodeName: schema.TypeString,
mkResourceVirtualEnvironmentFileSourceFile: schema.TypeList,
mkResourceVirtualEnvironmentFileSourceRaw: schema.TypeList,
mkResourceVirtualEnvironmentFileTimeoutUpload: schema.TypeInt,
})

sourceFileSchema := test.AssertNestedSchemaExistence(t, s, mkResourceVirtualEnvironmentFileSourceFile)
Expand Down

0 comments on commit f901e71

Please sign in to comment.