From 9261dcbf075e6afddfc44f7f2b94627b2a5b9d25 Mon Sep 17 00:00:00 2001 From: Dan Petersen Date: Fri, 27 Dec 2019 23:25:35 +0100 Subject: [PATCH] Initial support for multiple VLANs --- README.md | 2 +- proxmoxtf/resource_virtual_environment_vm.go | 27 +++++++++++-------- .../resource_virtual_environment_vm_test.go | 6 ++--- proxmoxtf/utils.go | 21 ++++++++++----- 4 files changed, 35 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 20e03c2e..deaaedca 100644 --- a/README.md +++ b/README.md @@ -361,7 +361,7 @@ This resource doesn't expose any additional attributes. * `enabled` - (Optional) Whether to enable the network device (defaults to `true`) * `mac_address` - (Optional) The MAC address * `model` - (Optional) The network device model (defaults to `virtio`) - * `vlan_id` - (Optional) The VLAN identifier + * `vlan_ids` - (Optional) The VLAN identifiers * `node_name` - (Required) The name of the node to assign the virtual machine to * `os_type` - (Optional) The OS type (defaults to `other`) * `pool_id` - (Optional) The ID of a pool to assign the virtual machine to diff --git a/proxmoxtf/resource_virtual_environment_vm.go b/proxmoxtf/resource_virtual_environment_vm.go index 200f0e30..43f9454c 100644 --- a/proxmoxtf/resource_virtual_environment_vm.go +++ b/proxmoxtf/resource_virtual_environment_vm.go @@ -43,7 +43,6 @@ const ( dvResourceVirtualEnvironmentVMNetworkDeviceEnabled = true dvResourceVirtualEnvironmentVMNetworkDeviceMACAddress = "" dvResourceVirtualEnvironmentVMNetworkDeviceModel = "virtio" - dvResourceVirtualEnvironmentVMNetworkDeviceVLANID = -1 dvResourceVirtualEnvironmentVMOSType = "other" dvResourceVirtualEnvironmentVMPoolID = "" dvResourceVirtualEnvironmentVMVMID = -1 @@ -95,7 +94,7 @@ const ( mkResourceVirtualEnvironmentVMNetworkDeviceEnabled = "enabled" mkResourceVirtualEnvironmentVMNetworkDeviceMACAddress = "mac_address" mkResourceVirtualEnvironmentVMNetworkDeviceModel = "model" - mkResourceVirtualEnvironmentVMNetworkDeviceVLANID = "vlan_id" + mkResourceVirtualEnvironmentVMNetworkDeviceVLANIDs = "vlan_ids" mkResourceVirtualEnvironmentVMNodeName = "node_name" mkResourceVirtualEnvironmentVMOSType = "os_type" mkResourceVirtualEnvironmentVMPoolID = "pool_id" @@ -552,12 +551,14 @@ func resourceVirtualEnvironmentVM() *schema.Resource { Default: dvResourceVirtualEnvironmentVMNetworkDeviceModel, ValidateFunc: getNetworkDeviceModelValidator(), }, - mkResourceVirtualEnvironmentVMNetworkDeviceVLANID: { - Type: schema.TypeInt, - Optional: true, - Description: "The VLAN identifier", - Default: dvResourceVirtualEnvironmentVMNetworkDeviceVLANID, - ValidateFunc: getVLANIDValidator(), + mkResourceVirtualEnvironmentVMNetworkDeviceVLANIDs: { + Type: schema.TypeList, + Optional: true, + Description: "The VLAN identifiers", + DefaultFunc: func() (interface{}, error) { + return make([]interface{}, 0), nil + }, + Elem: &schema.Schema{Type: schema.TypeInt}, }, }, }, @@ -838,7 +839,7 @@ func resourceVirtualEnvironmentVMCreate(d *schema.ResourceData, m interface{}) e enabled, _ := block[mkResourceVirtualEnvironmentVMNetworkDeviceEnabled].(bool) macAddress, _ := block[mkResourceVirtualEnvironmentVMNetworkDeviceMACAddress].(string) model, _ := block[mkResourceVirtualEnvironmentVMNetworkDeviceModel].(string) - vlanID, _ := block[mkResourceVirtualEnvironmentVMNetworkDeviceVLANID].(int) + vlanIDs, _ := block[mkResourceVirtualEnvironmentVMNetworkDeviceVLANIDs].([]interface{}) device := proxmox.CustomNetworkDevice{ Enabled: enabled, @@ -853,8 +854,12 @@ func resourceVirtualEnvironmentVMCreate(d *schema.ResourceData, m interface{}) e device.MACAddress = &macAddress } - if vlanID != -1 { - device.Trunks = []int{vlanID} + if len(vlanIDs) > 0 { + device.Trunks = make([]int, len(vlanIDs)) + + for vi, vv := range vlanIDs { + device.Trunks[vi] = vv.(int) + } } networkDeviceObjects[i] = device diff --git a/proxmoxtf/resource_virtual_environment_vm_test.go b/proxmoxtf/resource_virtual_environment_vm_test.go index 8176d1cf..6c6d9c35 100644 --- a/proxmoxtf/resource_virtual_environment_vm_test.go +++ b/proxmoxtf/resource_virtual_environment_vm_test.go @@ -287,7 +287,7 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) { mkResourceVirtualEnvironmentVMNetworkDeviceEnabled, mkResourceVirtualEnvironmentVMNetworkDeviceMACAddress, mkResourceVirtualEnvironmentVMNetworkDeviceModel, - mkResourceVirtualEnvironmentVMNetworkDeviceVLANID, + mkResourceVirtualEnvironmentVMNetworkDeviceVLANIDs, }) testSchemaValueTypes(t, networkDeviceSchema, []string{ @@ -295,12 +295,12 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) { mkResourceVirtualEnvironmentVMNetworkDeviceEnabled, mkResourceVirtualEnvironmentVMNetworkDeviceMACAddress, mkResourceVirtualEnvironmentVMNetworkDeviceModel, - mkResourceVirtualEnvironmentVMNetworkDeviceVLANID, + mkResourceVirtualEnvironmentVMNetworkDeviceVLANIDs, }, []schema.ValueType{ schema.TypeString, schema.TypeBool, schema.TypeString, schema.TypeString, - schema.TypeInt, + schema.TypeList, }) } diff --git a/proxmoxtf/utils.go b/proxmoxtf/utils.go index dcc48a4d..b9f363a2 100644 --- a/proxmoxtf/utils.go +++ b/proxmoxtf/utils.go @@ -118,23 +118,32 @@ func getQEMUAgentTypeValidator() schema.SchemaValidateFunc { return validation.StringInSlice([]string{"isa", "virtio"}, false) } -func getVLANIDValidator() schema.SchemaValidateFunc { +func getVLANIDsValidator() schema.SchemaValidateFunc { return func(i interface{}, k string) (ws []string, es []error) { min := 1 max := 4094 - v, ok := i.(int) + list, ok := i.([]interface{}) if !ok { - es = append(es, fmt.Errorf("expected type of %s to be int", k)) + es = append(es, fmt.Errorf("expected type of %s to be []interface{}", k)) return } - if v != -1 { - if v < min || v > max { - es = append(es, fmt.Errorf("expected %s to be in the range (%d - %d), got %d", k, min, max, v)) + for li, lv := range list { + v, ok := lv.(int) + + if !ok { + es = append(es, fmt.Errorf("expected type of %s[%d] to be int", k, li)) return } + + if v != -1 { + if v < min || v > max { + es = append(es, fmt.Errorf("expected %s[%d] to be in the range (%d - %d), got %d", k, li, min, max, v)) + return + } + } } return