From 728eceb5e9fc342984218a0baf0155afae53fd71 Mon Sep 17 00:00:00 2001 From: Pavel Boldyrev <627562+bpg@users.noreply.github.com> Date: Wed, 29 Nov 2023 23:24:16 -0500 Subject: [PATCH] fix(vm): unable to clone as non-root due to `hook_script` (#756) * fix(vm): unable to clone as non-root due to `hook_script` Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> * fix linter error Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --------- Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com> --- .vscode/settings.json | 4 ++++ proxmoxtf/resource/vm.go | 16 +++++++++------- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/.vscode/settings.json b/.vscode/settings.json index 97eb39e49..f25b8d41b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -5,5 +5,9 @@ "qcow", "virtio" ], + "go.lintTool": "golangci-lint", + "go.lintFlags": [ + "--fast", + ], "go.testEnvFile": "${workspaceFolder}/test.env", } diff --git a/proxmoxtf/resource/vm.go b/proxmoxtf/resource/vm.go index 494b905dc..f05d7ad2b 100644 --- a/proxmoxtf/resource/vm.go +++ b/proxmoxtf/resource/vm.go @@ -2001,6 +2001,11 @@ func vmCreateClone(ctx context.Context, d *schema.ResourceData, m interface{}) d } } + vmConfig, err := vmAPI.GetVM(ctx) + if err != nil { + return diag.FromErr(err) + } + if len(initialization) > 0 { tflog.Trace(ctx, "Preparing the CloudInit configuration") @@ -2008,11 +2013,6 @@ func vmCreateClone(ctx context.Context, d *schema.ResourceData, m interface{}) d initializationDatastoreID := initializationBlock[mkResourceVirtualEnvironmentVMInitializationDatastoreID].(string) initializationInterface := initializationBlock[mkResourceVirtualEnvironmentVMInitializationInterface].(string) - vmConfig, err := vmAPI.GetVM(ctx) - if err != nil { - return diag.FromErr(err) - } - existingInterface := findExistingCloudInitDrive(vmConfig, vmID, "ide2") if initializationInterface == "" { initializationInterface = existingInterface @@ -2131,9 +2131,11 @@ func vmCreateClone(ctx context.Context, d *schema.ResourceData, m interface{}) d } hookScript := d.Get(mkResourceVirtualEnvironmentVMHookScriptFileID).(string) + currentHookScript := vmConfig.HookScript + if len(hookScript) > 0 { updateBody.HookScript = &hookScript - } else { + } else if currentHookScript != nil { del = append(del, "hookscript") } @@ -2147,7 +2149,7 @@ func vmCreateClone(ctx context.Context, d *schema.ResourceData, m interface{}) d disk := d.Get(mkResourceVirtualEnvironmentVMDisk).([]interface{}) efiDisk := d.Get(mkResourceVirtualEnvironmentVMEFIDisk).([]interface{}) - vmConfig, e := vmAPI.GetVM(ctx) + vmConfig, e = vmAPI.GetVM(ctx) if e != nil { if strings.Contains(e.Error(), "HTTP 404") || (strings.Contains(e.Error(), "HTTP 500") && strings.Contains(e.Error(), "does not exist")) {