From 5bb3124d08531efb5c659d3c9b5639483b688dff Mon Sep 17 00:00:00 2001 From: Dan Petersen Date: Wed, 10 Feb 2021 22:54:27 +0100 Subject: [PATCH 1/3] feat: add support for waiting for specific network interfaces only --- CHANGELOG.md | 6 +++++ docs/resources/virtual_environment_vm.md | 1 + proxmox/virtual_environment_vm.go | 16 ++++++------ proxmoxtf/resource_virtual_environment_vm.go | 27 ++++++++++++++++++-- proxmoxtf/version.go | 2 +- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 991f28c..dd75ff7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +## v0.5.0 + +ENHANCEMENTS: + +* resource/virtual_environment_vm: Add `agent.prefixes` argument + ## v0.4.1 OTHER: diff --git a/docs/resources/virtual_environment_vm.md b/docs/resources/virtual_environment_vm.md index e1a8426..127bbe0 100644 --- a/docs/resources/virtual_environment_vm.md +++ b/docs/resources/virtual_environment_vm.md @@ -97,6 +97,7 @@ output "ubuntu_vm_public_key" { * `acpi` - (Optional) Whether to enable ACPI (defaults to `true`). * `agent` - (Optional) The QEMU agent configuration. * `enabled` - (Optional) Whether to enable the QEMU agent (defaults to `false`). + * `prefixes` - (Optional) The prefixes for the network interfaces which need to have an IP assigned before continuing (defaults to `["eno","enp","ens","eth","tun"]`). * `timeout` - (Optional) The maximum amount of time to wait for data from the QEMU agent to become available (defaults to `15m`). * `trim` - (Optional) Whether to enable the FSTRIM feature in the QEMU agent (defaults to `false`). * `type` - (Optional) The QEMU agent interface type (defaults to `virtio`). diff --git a/proxmox/virtual_environment_vm.go b/proxmox/virtual_environment_vm.go index d5bf739..b412a96 100644 --- a/proxmox/virtual_environment_vm.go +++ b/proxmox/virtual_environment_vm.go @@ -369,7 +369,7 @@ func (c *VirtualEnvironmentClient) UpdateVMAsync(nodeName string, vmID int, d *V } // WaitForNetworkInterfacesFromVMAgent waits for a virtual machine's QEMU agent to publish the network interfaces. -func (c *VirtualEnvironmentClient) WaitForNetworkInterfacesFromVMAgent(nodeName string, vmID int, timeout int, delay int, waitForIP bool) (*VirtualEnvironmentVMGetQEMUNetworkInterfacesResponseData, error) { +func (c *VirtualEnvironmentClient) WaitForNetworkInterfacesFromVMAgent(nodeName string, vmID int, timeout int, delay int, waitForIP bool, interfacePrefixes []string) (*VirtualEnvironmentVMGetQEMUNetworkInterfacesResponseData, error) { timeDelay := int64(delay) timeMax := float64(timeout) timeStart := time.Now() @@ -384,13 +384,13 @@ func (c *VirtualEnvironmentClient) WaitForNetworkInterfacesFromVMAgent(nodeName if waitForIP { for _, nic := range *data.Result { - if nic.Name == "lo" { - continue - } - - if nic.IPAddresses == nil || (nic.IPAddresses != nil && len(*nic.IPAddresses) == 0) { - missingIP = true - break + for _, prefix := range interfacePrefixes { + if strings.HasPrefix(nic.Name, prefix) { + if nic.IPAddresses == nil || (nic.IPAddresses != nil && len(*nic.IPAddresses) == 0) { + missingIP = true + break + } + } } } } diff --git a/proxmoxtf/resource_virtual_environment_vm.go b/proxmoxtf/resource_virtual_environment_vm.go index 48fba72..49644ef 100644 --- a/proxmoxtf/resource_virtual_environment_vm.go +++ b/proxmoxtf/resource_virtual_environment_vm.go @@ -98,6 +98,7 @@ const ( mkResourceVirtualEnvironmentVMACPI = "acpi" mkResourceVirtualEnvironmentVMAgent = "agent" mkResourceVirtualEnvironmentVMAgentEnabled = "enabled" + mkResourceVirtualEnvironmentVMAgentPrefixes = "prefixes" mkResourceVirtualEnvironmentVMAgentTimeout = "timeout" mkResourceVirtualEnvironmentVMAgentTrim = "trim" mkResourceVirtualEnvironmentVMAgentType = "type" @@ -235,6 +236,21 @@ func resourceVirtualEnvironmentVM() *schema.Resource { Optional: true, Default: dvResourceVirtualEnvironmentVMAgentEnabled, }, + mkResourceVirtualEnvironmentVMAgentPrefixes: { + Type: schema.TypeString, + Description: "The prefixes for the network interfaces which need to have an IP assigned before continuing", + Optional: true, + DefaultFunc: func() (interface{}, error) { + return []interface{}{ + "eno", + "enp", + "ens", + "eth", + "tun", + }, nil + }, + Elem: &schema.Schema{Type: schema.TypeString}, + }, mkResourceVirtualEnvironmentVMAgentTimeout: { Type: schema.TypeString, Description: "The maximum amount of time to wait for data from the QEMU agent to become available", @@ -1798,7 +1814,7 @@ func resourceVirtualEnvironmentVMCreateCustomDisks(d *schema.ResourceData, m int fileFormat, _ := block[mkResourceVirtualEnvironmentVMDiskFileFormat].(string) size, _ := block[mkResourceVirtualEnvironmentVMDiskSize].(int) speed := block[mkResourceVirtualEnvironmentVMDiskSpeed].([]interface{}) - diskInterface,_ := block[mkResourcevirtualEnvironmentVMDiskInterface].(string) + diskInterface, _ := block[mkResourcevirtualEnvironmentVMDiskInterface].(string) if len(speed) == 0 { diskSpeedDefault, err := diskSpeedResource.DefaultValue() @@ -3032,14 +3048,21 @@ func resourceVirtualEnvironmentVMReadNetworkValues(d *schema.ResourceData, m int return err } + agentPrefixes := agentBlock[mkResourceVirtualEnvironmentVMAgentPrefixes].([]interface{}) agentTimeout, err := time.ParseDuration(agentBlock[mkResourceVirtualEnvironmentVMAgentTimeout].(string)) if err != nil { return err } + interfacePrefixes := make([]string, len(agentPrefixes)) + + for i, v := range agentPrefixes { + interfacePrefixes[i] = v.(string) + } + macAddresses := []interface{}{} - networkInterfaces, err := veClient.WaitForNetworkInterfacesFromVMAgent(nodeName, vmID, int(agentTimeout.Seconds()), 5, true) + networkInterfaces, err := veClient.WaitForNetworkInterfacesFromVMAgent(nodeName, vmID, int(agentTimeout.Seconds()), 5, true, interfacePrefixes) if err == nil && networkInterfaces.Result != nil { ipv4Addresses = make([]interface{}, len(*networkInterfaces.Result)) diff --git a/proxmoxtf/version.go b/proxmoxtf/version.go index 31e9295..cd7f613 100644 --- a/proxmoxtf/version.go +++ b/proxmoxtf/version.go @@ -9,5 +9,5 @@ const ( TerraformProviderName = "terraform-provider-proxmox" // TerraformProviderVersion specifies the version number. - TerraformProviderVersion = "0.4.1" + TerraformProviderVersion = "0.5.0" ) From 55d52e3de1a919681225db053019d6dc13043627 Mon Sep 17 00:00:00 2001 From: Dan Petersen Date: Wed, 10 Feb 2021 22:57:43 +0100 Subject: [PATCH 2/3] fix: argument type and test case --- proxmoxtf/resource_virtual_environment_vm.go | 2 +- proxmoxtf/resource_virtual_environment_vm_test.go | 9 ++++++--- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/proxmoxtf/resource_virtual_environment_vm.go b/proxmoxtf/resource_virtual_environment_vm.go index 49644ef..cd83e68 100644 --- a/proxmoxtf/resource_virtual_environment_vm.go +++ b/proxmoxtf/resource_virtual_environment_vm.go @@ -237,7 +237,7 @@ func resourceVirtualEnvironmentVM() *schema.Resource { Default: dvResourceVirtualEnvironmentVMAgentEnabled, }, mkResourceVirtualEnvironmentVMAgentPrefixes: { - Type: schema.TypeString, + Type: schema.TypeList, Description: "The prefixes for the network interfaces which need to have an IP assigned before continuing", Optional: true, DefaultFunc: func() (interface{}, error) { diff --git a/proxmoxtf/resource_virtual_environment_vm_test.go b/proxmoxtf/resource_virtual_environment_vm_test.go index ddb5a5a..9303b9e 100644 --- a/proxmoxtf/resource_virtual_environment_vm_test.go +++ b/proxmoxtf/resource_virtual_environment_vm_test.go @@ -89,15 +89,18 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) { testOptionalArguments(t, agentSchema, []string{ mkResourceVirtualEnvironmentVMAgentEnabled, + mkResourceVirtualEnvironmentVMAgentPrefixes, mkResourceVirtualEnvironmentVMAgentTimeout, mkResourceVirtualEnvironmentVMAgentTrim, mkResourceVirtualEnvironmentVMAgentType, }) testValueTypes(t, agentSchema, map[string]schema.ValueType{ - mkResourceVirtualEnvironmentVMAgentEnabled: schema.TypeBool, - mkResourceVirtualEnvironmentVMAgentTrim: schema.TypeBool, - mkResourceVirtualEnvironmentVMAgentType: schema.TypeString, + mkResourceVirtualEnvironmentVMAgentEnabled: schema.TypeBool, + mkResourceVirtualEnvironmentVMAgentPrefixes: schema.TypeList, + mkResourceVirtualEnvironmentVMAgentTimeout: schema.TypeString, + mkResourceVirtualEnvironmentVMAgentTrim: schema.TypeBool, + mkResourceVirtualEnvironmentVMAgentType: schema.TypeString, }) audioDeviceSchema := testNestedSchemaExistence(t, s, mkResourceVirtualEnvironmentVMAudioDevice) From abe6e200a5caf6692f40133ce99f9ada7fbe2c8e Mon Sep 17 00:00:00 2001 From: Dan Petersen Date: Wed, 10 Feb 2021 23:00:12 +0100 Subject: [PATCH 3/3] fix: update changelog --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index dd75ff7..44d5011 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -## v0.5.0 +## v0.5.0 (UNRELEASED) ENHANCEMENTS: