Skip to content

Commit

Permalink
feat: add support for network_device MTU (#176)
Browse files Browse the repository at this point in the history
* add support for network_device MTU

* add mtu to the example templates

* change default mtu 1500 -> 0, update docs

Co-authored-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
  • Loading branch information
abdo-farag and bpg authored Dec 13, 2022
1 parent 2907346 commit 3c02cb1
Show file tree
Hide file tree
Showing 9 changed files with 76 additions and 2 deletions.
1 change: 1 addition & 0 deletions docs/resources/virtual_environment_container.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ output "ubuntu_container_public_key" {
* `bridge` - (Optional) The name of the network bridge (defaults to `vmbr0`).
* `enabled` - (Optional) Whether to enable the network device (defaults to `true`).
* `mac_address` - (Optional) The MAC address.
* `mtu` - (Optional) Maximum transfer unit of the interface. Cannot be larger than the bridge's MTU.
* `name` - (Required) The network interface name.
* `rate_limit` - (Optional) The rate limit in megabytes per second.
* `vlan_id` - (Optional) The VLAN identifier.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/virtual_environment_vm.md
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,7 @@ output "ubuntu_vm_public_key" {
* `rtl8139` - Realtek RTL8139.
* `virtio` - VirtIO (paravirtualized).
* `vmxnet3` - VMware vmxnet3.
* `mtu` - (Optional) Force MTU, for VirtIO only. Set to 1 to use the bridge MTU. Cannot be larger than the bridge MTU.
* `rate_limit` - (Optional) The rate limit in megabytes per second.
* `vlan_id` - (Optional) The VLAN identifier.
* `node_name` - (Required) The name of the node to assign the virtual machine to.
Expand Down
1 change: 1 addition & 0 deletions example/resource_virtual_environment_container.tf
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ resource "proxmox_virtual_environment_container" "example_template" {

network_interface {
name = "veth0"
mtu = 1450
}

node_name = data.proxmox_virtual_environment_nodes.example.names[0]
Expand Down
4 changes: 3 additions & 1 deletion example/resource_virtual_environment_vm.tf
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,9 @@ resource "proxmox_virtual_environment_vm" "example_template" {

name = "terraform-provider-proxmox-example-template"

network_device {}
network_device {
mtu = 1450
}

network_device {
vlan_id = 1024
Expand Down
12 changes: 12 additions & 0 deletions proxmox/virtual_environment_vm_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ type CustomNetworkDevice struct {
Queues *int `json:"queues,omitempty" url:"queues,omitempty"`
RateLimit *float64 `json:"rate,omitempty" url:"rate,omitempty"`
Tag *int `json:"tag,omitempty" url:"tag,omitempty"`
MTU *int `json:"mtu,omitempty" url:"mtu,omitempty"`
Trunks []int `json:"trunks,omitempty" url:"trunks,omitempty"`
}

Expand Down Expand Up @@ -802,6 +803,9 @@ func (r CustomNetworkDevice) EncodeValues(key string, v *url.Values) error {
if r.Tag != nil {
values = append(values, fmt.Sprintf("tag=%d", *r.Tag))
}
if r.MTU != nil {
values = append(values, fmt.Sprintf("mtu=%d", *r.MTU))
}

if len(r.Trunks) > 0 {
trunks := make([]string, len(r.Trunks))
Expand Down Expand Up @@ -1462,6 +1466,14 @@ func (r *CustomNetworkDevice) UnmarshalJSON(b []byte) error {
}
r.RateLimit = &fv

case "mtu":
iv, err := strconv.Atoi(v[1])

if err != nil {
return err
}
r.MTU = &iv

case "tag":
iv, err := strconv.Atoi(v[1])

Expand Down
37 changes: 36 additions & 1 deletion proxmoxtf/resource_virtual_environment_container.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ package proxmoxtf
import (
"context"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"strconv"
"strings"

"github.com/hashicorp/terraform-plugin-sdk/v2/diag"

"github.com/bpg/terraform-provider-proxmox/proxmox"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
Expand Down Expand Up @@ -42,6 +43,7 @@ const (
dvResourceVirtualEnvironmentContainerNetworkInterfaceMACAddress = ""
dvResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit = 0
dvResourceVirtualEnvironmentContainerNetworkInterfaceVLANID = 0
dvResourceVirtualEnvironmentContainerNetworkInterfaceMTU = 0
dvResourceVirtualEnvironmentContainerOperatingSystemType = "unmanaged"
dvResourceVirtualEnvironmentContainerPoolID = ""
dvResourceVirtualEnvironmentContainerStarted = true
Expand Down Expand Up @@ -91,6 +93,7 @@ const (
mkResourceVirtualEnvironmentContainerNetworkInterfaceName = "name"
mkResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit = "rate_limit"
mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID = "vlan_id"
mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU = "mtu"
mkResourceVirtualEnvironmentContainerNodeName = "node_name"
mkResourceVirtualEnvironmentContainerOperatingSystem = "operating_system"
mkResourceVirtualEnvironmentContainerOperatingSystemTemplateFileID = "template_file_id"
Expand Down Expand Up @@ -481,6 +484,12 @@ func resourceVirtualEnvironmentContainer() *schema.Resource {
Optional: true,
Default: dvResourceVirtualEnvironmentContainerNetworkInterfaceVLANID,
},
mkResourceVirtualEnvironmentVMNetworkDeviceMTU: {
Type: schema.TypeInt,
Description: "Maximum transmission unit (MTU)",
Optional: true,
Default: dvResourceVirtualEnvironmentVMNetworkDeviceMTU,
},
},
},
MaxItems: maxResourceVirtualEnvironmentContainerNetworkInterfaces,
Expand Down Expand Up @@ -791,6 +800,7 @@ func resourceVirtualEnvironmentContainerCreateClone(ctx context.Context, d *sche
name := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceName].(string)
rateLimit := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit].(float64)
vlanID := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID].(int)
mtu, _ := networkInterfaceMap[mkResourceVirtualEnvironmentVMNetworkDeviceMTU].(int)

if bridge != "" {
networkInterfaceObject.Bridge = &bridge
Expand Down Expand Up @@ -830,6 +840,10 @@ func resourceVirtualEnvironmentContainerCreateClone(ctx context.Context, d *sche
networkInterfaceObject.Tag = &vlanID
}

if mtu != 0 {
networkInterfaceObject.MTU = &mtu
}

networkInterfaceArray[ni] = networkInterfaceObject
}

Expand Down Expand Up @@ -1003,6 +1017,7 @@ func resourceVirtualEnvironmentContainerCreateCustom(ctx context.Context, d *sch
name := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceName].(string)
rateLimit := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit].(float64)
vlanID := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID].(int)
mtu := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU].(int)

if bridge != "" {
networkInterfaceObject.Bridge = &bridge
Expand Down Expand Up @@ -1041,6 +1056,9 @@ func resourceVirtualEnvironmentContainerCreateCustom(ctx context.Context, d *sch
if vlanID != 0 {
networkInterfaceObject.Tag = &vlanID
}
if mtu != 0 {
networkInterfaceObject.MTU = &mtu
}

networkInterfaceArray[ni] = networkInterfaceObject
}
Expand Down Expand Up @@ -1237,6 +1255,12 @@ func resourceVirtualEnvironmentContainerGetExistingNetworkInterface(ctx context.
networkInterface[mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID] = 0
}

if nv.MTU != nil {
networkInterface[mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU] = *nv.MTU
} else {
networkInterface[mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU] = 0
}

networkInterfaces = append(networkInterfaces, networkInterface)
}

Expand Down Expand Up @@ -1552,6 +1576,12 @@ func resourceVirtualEnvironmentContainerRead(ctx context.Context, d *schema.Reso
networkInterface[mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID] = 0
}

if nv.MTU != nil {
networkInterface[mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU] = *nv.MTU
} else {
networkInterface[mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU] = 0
}

networkInterfaceList = append(networkInterfaceList, networkInterface)
}

Expand Down Expand Up @@ -1833,6 +1863,7 @@ func resourceVirtualEnvironmentContainerUpdate(ctx context.Context, d *schema.Re
name := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceName].(string)
rateLimit := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit].(float64)
vlanID := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID].(int)
mtu := networkInterfaceMap[mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU].(int)

if bridge != "" {
networkInterfaceObject.Bridge = &bridge
Expand Down Expand Up @@ -1872,6 +1903,10 @@ func resourceVirtualEnvironmentContainerUpdate(ctx context.Context, d *schema.Re
networkInterfaceObject.Tag = &vlanID
}

if mtu != 0 {
networkInterfaceObject.MTU = &mtu
}

networkInterfaceArray[ni] = networkInterfaceObject
}

Expand Down
2 changes: 2 additions & 0 deletions proxmoxtf/resource_virtual_environment_container_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,7 @@ func TestResourceVirtualEnvironmentContainerSchema(t *testing.T) {
mkResourceVirtualEnvironmentContainerNetworkInterfaceMACAddress,
mkResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit,
mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID,
mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU,
})

testValueTypes(t, networkInterfaceSchema, map[string]schema.ValueType{
Expand All @@ -203,6 +204,7 @@ func TestResourceVirtualEnvironmentContainerSchema(t *testing.T) {
mkResourceVirtualEnvironmentContainerNetworkInterfaceName: schema.TypeString,
mkResourceVirtualEnvironmentContainerNetworkInterfaceRateLimit: schema.TypeFloat,
mkResourceVirtualEnvironmentContainerNetworkInterfaceVLANID: schema.TypeInt,
mkResourceVirtualEnvironmentContainerNetworkInterfaceMTU: schema.TypeInt,
})

operatingSystemSchema := testNestedSchemaExistence(t, s, mkResourceVirtualEnvironmentContainerOperatingSystem)
Expand Down
18 changes: 18 additions & 0 deletions proxmoxtf/resource_virtual_environment_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const (
dvResourceVirtualEnvironmentVMNetworkDeviceModel = "virtio"
dvResourceVirtualEnvironmentVMNetworkDeviceRateLimit = 0
dvResourceVirtualEnvironmentVMNetworkDeviceVLANID = 0
dvResourceVirtualEnvironmentVMNetworkDeviceMTU = 0
dvResourceVirtualEnvironmentVMOperatingSystemType = "other"
dvResourceVirtualEnvironmentVMPoolID = ""
dvResourceVirtualEnvironmentVMSerialDeviceDevice = "socket"
Expand Down Expand Up @@ -181,6 +182,7 @@ const (
mkResourceVirtualEnvironmentVMNetworkDeviceModel = "model"
mkResourceVirtualEnvironmentVMNetworkDeviceRateLimit = "rate_limit"
mkResourceVirtualEnvironmentVMNetworkDeviceVLANID = "vlan_id"
mkResourceVirtualEnvironmentVMNetworkDeviceMTU = "mtu"
mkResourceVirtualEnvironmentVMNetworkInterfaceNames = "network_interface_names"
mkResourceVirtualEnvironmentVMNodeName = "node_name"
mkResourceVirtualEnvironmentVMOperatingSystem = "operating_system"
Expand Down Expand Up @@ -903,6 +905,12 @@ func resourceVirtualEnvironmentVM() *schema.Resource {
Optional: true,
Default: dvResourceVirtualEnvironmentVMNetworkDeviceVLANID,
},
mkResourceVirtualEnvironmentVMNetworkDeviceMTU: {
Type: schema.TypeInt,
Description: "Maximum transmission unit (MTU)",
Optional: true,
Default: dvResourceVirtualEnvironmentVMNetworkDeviceMTU,
},
},
},
MaxItems: maxResourceVirtualEnvironmentVMNetworkDevices,
Expand Down Expand Up @@ -2324,6 +2332,7 @@ func resourceVirtualEnvironmentVMGetNetworkDeviceObjects(d *schema.ResourceData)
model, _ := block[mkResourceVirtualEnvironmentVMNetworkDeviceModel].(string)
rateLimit, _ := block[mkResourceVirtualEnvironmentVMNetworkDeviceRateLimit].(float64)
vlanID, _ := block[mkResourceVirtualEnvironmentVMNetworkDeviceVLANID].(int)
mtu, _ := block[mkResourceVirtualEnvironmentVMNetworkDeviceMTU].(int)

device := proxmox.CustomNetworkDevice{
Enabled: enabled,
Expand All @@ -2346,6 +2355,10 @@ func resourceVirtualEnvironmentVMGetNetworkDeviceObjects(d *schema.ResourceData)
device.Tag = &vlanID
}

if mtu != 0 {
device.MTU = &mtu
}

networkDeviceObjects[i] = device
}

Expand Down Expand Up @@ -3080,6 +3093,11 @@ func resourceVirtualEnvironmentVMReadCustom(ctx context.Context, d *schema.Resou
} else {
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceVLANID] = 0
}
if nd.MTU != nil {
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceMTU] = nd.MTU
} else {
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceMTU] = 0
}
} else {
macAddresses[ni] = ""
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceEnabled] = false
Expand Down
2 changes: 2 additions & 0 deletions proxmoxtf/resource_virtual_environment_vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) {
mkResourceVirtualEnvironmentVMNetworkDeviceModel,
mkResourceVirtualEnvironmentVMNetworkDeviceRateLimit,
mkResourceVirtualEnvironmentVMNetworkDeviceVLANID,
mkResourceVirtualEnvironmentVMNetworkDeviceMTU,
})

testValueTypes(t, networkDeviceSchema, map[string]schema.ValueType{
Expand All @@ -305,6 +306,7 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) {
mkResourceVirtualEnvironmentVMNetworkDeviceModel: schema.TypeString,
mkResourceVirtualEnvironmentVMNetworkDeviceRateLimit: schema.TypeFloat,
mkResourceVirtualEnvironmentVMNetworkDeviceVLANID: schema.TypeInt,
mkResourceVirtualEnvironmentVMNetworkDeviceMTU: schema.TypeInt,
})

operatingSystemSchema := testNestedSchemaExistence(t, s, mkResourceVirtualEnvironmentVMOperatingSystem)
Expand Down

0 comments on commit 3c02cb1

Please sign in to comment.