From 3392d8e5c067748d3e7f28811609e94f44cdde11 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Oto=20Pet=C5=99=C3=ADk?= Date: Sat, 30 Jul 2022 04:15:13 +0200 Subject: [PATCH] Provide correct outputs from stopped VM (#106) Outputs 'ipv4_addresses', 'ipv6_addresses', 'network_interface_names' of stopped VM are empty and break plans - e.g. search for "eth0" in empty list. On update, mark them 'Computed' if there is planned change in 'started' or 'network_device' of the VM. This makes it possible to depend on e.g. 'network_interface_names' of VM which is not running at the time of 'terraform plan', but which will be started during 'terraform apply'. --- proxmoxtf/resource_virtual_environment_vm.go | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/proxmoxtf/resource_virtual_environment_vm.go b/proxmoxtf/resource_virtual_environment_vm.go index 6dbdc6a9..280406e6 100644 --- a/proxmoxtf/resource_virtual_environment_vm.go +++ b/proxmoxtf/resource_virtual_environment_vm.go @@ -9,6 +9,7 @@ import ( "errors" "fmt" "github.com/hashicorp/terraform-plugin-sdk/v2/diag" + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff" "sort" "strconv" "strings" @@ -1057,6 +1058,17 @@ func resourceVirtualEnvironmentVM() *schema.Resource { ReadContext: resourceVirtualEnvironmentVMRead, UpdateContext: resourceVirtualEnvironmentVMUpdate, DeleteContext: resourceVirtualEnvironmentVMDelete, + CustomizeDiff: customdiff.All( + customdiff.ComputedIf(mkResourceVirtualEnvironmentVMIPv4Addresses, func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) bool { + return d.HasChange(mkResourceVirtualEnvironmentVMStarted) || d.HasChange(mkResourceVirtualEnvironmentVMNetworkDevice) + }), + customdiff.ComputedIf(mkResourceVirtualEnvironmentVMIPv6Addresses, func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) bool { + return d.HasChange(mkResourceVirtualEnvironmentVMStarted) || d.HasChange(mkResourceVirtualEnvironmentVMNetworkDevice) + }), + customdiff.ComputedIf(mkResourceVirtualEnvironmentVMNetworkInterfaceNames, func(ctx context.Context, d *schema.ResourceDiff, meta interface{}) bool { + return d.HasChange(mkResourceVirtualEnvironmentVMStarted) || d.HasChange(mkResourceVirtualEnvironmentVMNetworkDevice) + }), + ), } }