From 4e1ce30619ccf7db141874f4daa5873ca9f012f1 Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Sat, 24 Jun 2023 01:38:24 -0400 Subject: [PATCH] fix(file): add check for supported content types when uploading file to a storage (#379) --- proxmoxtf/resource/file.go | 24 ++++++++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/proxmoxtf/resource/file.go b/proxmoxtf/resource/file.go index 34e44dcc..ba566017 100644 --- a/proxmoxtf/resource/file.go +++ b/proxmoxtf/resource/file.go @@ -24,6 +24,7 @@ import ( "github.com/hashicorp/terraform-plugin-log/tflog" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" + "golang.org/x/exp/slices" "github.com/bpg/terraform-provider-proxmox/proxmox/api" "github.com/bpg/terraform-provider-proxmox/proxmoxtf" @@ -403,6 +404,18 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag return diag.Errorf("failed to determine the datastore path") } + _, found := slices.BinarySearch(datastore.Content, *contentType) + if !found { + diags = append(diags, diag.Diagnostics{ + diag.Diagnostic{ + Severity: diag.Warning, + Summary: fmt.Sprintf("the datastore %q does not support content type %q", + *datastore.Storage, *contentType, + ), + }, + }...) + } + remoteFileDir := *datastore.Path err = capi.SSH().NodeUpload(ctx, nodeName, remoteFileDir, request) @@ -412,14 +425,21 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag return diag.FromErr(err) } - volumeID, diags := fileGetVolumeID(d) + volumeID, di := fileGetVolumeID(d) + diags = append(diags, di...) if diags.HasError() { return diags } d.SetId(*volumeID) - return fileRead(ctx, d, m) + diags = append(diags, fileRead(ctx, d, m)...) + + if d.Id() == "" { + diags = append(diags, diag.Errorf("failed to read file from %q", *volumeID)...) + } + + return diags } func fileGetContentType(d *schema.ResourceData) (*string, diag.Diagnostics) {