From 4a0ceb9735270e41b4d75c71d3864cc9a6f29e21 Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Mon, 5 Feb 2024 22:23:22 -0500 Subject: [PATCH] Merge remote-tracking branch 'origin/main' into breackdown-vm-code Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --- proxmoxtf/resource/file.go | 5 +++-- proxmoxtf/resource/{ => ssh}/sudo.go | 6 +++--- proxmoxtf/resource/vm/disk.go | 9 +++++---- 3 files changed, 11 insertions(+), 9 deletions(-) rename proxmoxtf/resource/{ => ssh}/sudo.go (69%) diff --git a/proxmoxtf/resource/file.go b/proxmoxtf/resource/file.go index d67c2f80..636bbc5a 100644 --- a/proxmoxtf/resource/file.go +++ b/proxmoxtf/resource/file.go @@ -12,6 +12,7 @@ import ( "crypto/sha256" "crypto/tls" "fmt" + "github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/ssh" "io" "net/http" "net/url" @@ -594,7 +595,7 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag _, err := capi.SSH().ExecuteNodeCommands(ctx, nodeName, []string{ // the `mv` command should be scoped to the specific directories in sudoers! fmt.Sprintf(`%s; try_sudo "mv %s/%s %s/%s" && rm %s/%s && rmdir -p %s`, - trySudo, + ssh.TrySudo, srcDir, *fileName, dstDir, *fileName, srcDir, *fileName, @@ -603,7 +604,7 @@ func fileCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag }) if err != nil { if matches, e := regexp.MatchString(`cannot move .* Permission denied`, err.Error()); e == nil && matches { - return diag.FromErr(newErrSSHUserNoPermission(capi.SSH().Username())) + return diag.FromErr(ssh.NewErrSSHUserNoPermission(capi.SSH().Username())) } diags = append(diags, diag.Errorf("error moving file: %s", err.Error())...) diff --git a/proxmoxtf/resource/sudo.go b/proxmoxtf/resource/ssh/sudo.go similarity index 69% rename from proxmoxtf/resource/sudo.go rename to proxmoxtf/resource/ssh/sudo.go index d9598a14..e827996a 100644 --- a/proxmoxtf/resource/sudo.go +++ b/proxmoxtf/resource/ssh/sudo.go @@ -1,14 +1,14 @@ -package resource +package ssh import ( "fmt" ) const ( - trySudo = `try_sudo(){ if [ $(sudo -n pvesm apiinfo 2>&1 | grep "APIVER" | wc -l) -gt 0 ]; then sudo $1; else $1; fi }` + TrySudo = `try_sudo(){ if [ $(sudo -n pvesm apiinfo 2>&1 | grep "APIVER" | wc -l) -gt 0 ]; then sudo $1; else $1; fi }` ) -func newErrSSHUserNoPermission(username string) error { +func NewErrSSHUserNoPermission(username string) error { return fmt.Errorf("the SSH user '%s' does not have required permissions. "+ "Make sure 'sudo' is installed and the user is configured in sudoers file. "+ "Refer to the documentation for more details", username) diff --git a/proxmoxtf/resource/vm/disk.go b/proxmoxtf/resource/vm/disk.go index 8fbebc02..c07f39e9 100644 --- a/proxmoxtf/resource/vm/disk.go +++ b/proxmoxtf/resource/vm/disk.go @@ -3,6 +3,8 @@ package vm import ( "context" "fmt" + "github.com/bpg/terraform-provider-proxmox/proxmoxtf/resource/ssh" + "regexp" "strconv" "strings" @@ -370,7 +372,7 @@ func vmImportCustomDisks(ctx context.Context, d *schema.ResourceData, m interfac commands = append( commands, `set -e`, - `try_sudo(){ if [ $(sudo -n echo tfpve 2>&1 | grep "tfpve" | wc -l) -gt 0 ]; then sudo $1; else $1; fi }`, + ssh.TrySudo, fmt.Sprintf(`file_id="%s"`, *d.FileID), fmt.Sprintf(`file_format="%s"`, *d.Format), fmt.Sprintf(`datastore_id_target="%s"`, *d.ID), @@ -403,9 +405,8 @@ func vmImportCustomDisks(ctx context.Context, d *schema.ResourceData, m interfac out, err := api.SSH().ExecuteNodeCommands(ctx, nodeName, commands) if err != nil { - if strings.Contains(err.Error(), "pvesm: not found") { - return fmt.Errorf("The configured SSH user '%s' does not have the required permissions to import disks. "+ - "Make sure `sudo` is installed and the user is a member of sudoers.", api.SSH().Username()) + if matches, e := regexp.Match(`pvesm: .* not found`, out); e == nil && matches { + return ssh.NewErrSSHUserNoPermission(api.SSH().Username()) } return err