0
0
mirror of https://github.com/bpg/terraform-provider-proxmox.git synced 2025-07-04 12:32:59 +00:00

feat(vm): add support for disconnected attribute in network interface (#1129)

Signed-off-by: Pavel Boldyrev <627562+bpg@users.noreply.github.com>
This commit is contained in:
Pavel Boldyrev 2024-03-15 21:41:53 -04:00 committed by GitHub
parent 29b5438faf
commit 11ca880f28
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 79 additions and 31 deletions

View File

@ -408,20 +408,17 @@ output "ubuntu_vm_public_key" {
it (defaults to `false`). it (defaults to `false`).
- `name` - (Optional) The virtual machine name. - `name` - (Optional) The virtual machine name.
- `network_device` - (Optional) A network device (multiple blocks supported). - `network_device` - (Optional) A network device (multiple blocks supported).
- `bridge` - (Optional) The name of the network bridge (defaults - `bridge` - (Optional) The name of the network bridge (defaults to `vmbr0`).
to `vmbr0`). - `disconnected` - (Optional) Whether to disconnect the network device from the network (defaults to `false`).
- `enabled` - (Optional) Whether to enable the network device (defaults - `enabled` - (Optional) Whether to enable the network device (defaults to `true`).
to `true`). - `firewall` - (Optional) Whether this interface's firewall rules should be used (defaults to `false`).
- `firewall` - (Optional) Whether this interface's firewall rules should be
used (defaults to `false`).
- `mac_address` - (Optional) The MAC address. - `mac_address` - (Optional) The MAC address.
- `model` - (Optional) The network device model (defaults to `virtio`). - `model` - (Optional) The network device model (defaults to `virtio`).
- `e1000` - Intel E1000. - `e1000` - Intel E1000.
- `rtl8139` - Realtek RTL8139. - `rtl8139` - Realtek RTL8139.
- `virtio` - VirtIO (paravirtualized). - `virtio` - VirtIO (paravirtualized).
- `vmxnet3` - VMware vmxnet3. - `vmxnet3` - VMware vmxnet3.
- `mtu` - (Optional) Force MTU, for VirtIO only. Set to 1 to use the bridge - `mtu` - (Optional) Force MTU, for VirtIO only. Set to 1 to use the bridge MTU. Cannot be larger than the bridge MTU.
MTU. Cannot be larger than the bridge MTU.
- `queues` - (Optional) The number of queues for VirtIO (1..64). - `queues` - (Optional) The number of queues for VirtIO (1..64).
- `rate_limit` - (Optional) The rate limit in megabytes per second. - `rate_limit` - (Optional) The rate limit in megabytes per second.
- `vlan_id` - (Optional) The VLAN identifier. - `vlan_id` - (Optional) The VLAN identifier.

View File

@ -109,9 +109,9 @@ func TestAccResourceVMNetwork(t *testing.T) {
tests := []struct { tests := []struct {
name string name string
step resource.TestStep step []resource.TestStep
}{ }{
{"network interfaces", resource.TestStep{ {"network interfaces", []resource.TestStep{{
Config: providerConfig + ` Config: providerConfig + `
resource "proxmox_virtual_environment_file" "cloud_config" { resource "proxmox_virtual_environment_file" "cloud_config" {
content_type = "snippets" content_type = "snippets"
@ -179,7 +179,41 @@ EOF
"network_device.0.trunks": "10;20;30", "network_device.0.trunks": "10;20;30",
}), }),
), ),
}}, }}},
{"network device disconnected", []resource.TestStep{{
Config: `
resource "proxmox_virtual_environment_vm" "test_vm_network2" {
node_name = "pve"
started = false
network_device {
bridge = "vmbr0"
}
}`,
Check: resource.ComposeTestCheckFunc(
testResourceAttributes("proxmox_virtual_environment_vm.test_vm_network2", map[string]string{
"network_device.0.bridge": "vmbr0",
"network_device.0.disconnected": "false",
}),
),
}, {
Config: `
resource "proxmox_virtual_environment_vm" "test_vm_network2" {
node_name = "pve"
started = false
network_device {
bridge = "vmbr0"
disconnected = true
}
}`,
Check: resource.ComposeTestCheckFunc(
testResourceAttributes("proxmox_virtual_environment_vm.test_vm_network2", map[string]string{
"network_device.0.bridge": "vmbr0",
"network_device.0.disconnected": "true",
}),
),
}}},
} }
accProviders := testAccMuxProviders(context.Background(), t) accProviders := testAccMuxProviders(context.Background(), t)
@ -191,7 +225,7 @@ EOF
resource.Test(t, resource.TestCase{ resource.Test(t, resource.TestCase{
ProtoV6ProviderFactories: accProviders, ProtoV6ProviderFactories: accProviders,
Steps: []resource.TestStep{tt.step}, Steps: tt.step,
}) })
}) })
} }

View File

@ -89,16 +89,16 @@ type CustomEFIDisk struct {
// CustomNetworkDevice handles QEMU network device parameters. // CustomNetworkDevice handles QEMU network device parameters.
type CustomNetworkDevice struct { type CustomNetworkDevice struct {
Model string `json:"model" url:"model"`
Bridge *string `json:"bridge,omitempty" url:"bridge,omitempty"`
Enabled bool `json:"-" url:"-"` Enabled bool `json:"-" url:"-"`
Bridge *string `json:"bridge,omitempty" url:"bridge,omitempty"`
Firewall *types.CustomBool `json:"firewall,omitempty" url:"firewall,omitempty,int"` Firewall *types.CustomBool `json:"firewall,omitempty" url:"firewall,omitempty,int"`
LinkDown *types.CustomBool `json:"link_down,omitempty" url:"link_down,omitempty,int"` LinkDown *types.CustomBool `json:"link_down,omitempty" url:"link_down,omitempty,int"`
MACAddress *string `json:"macaddr,omitempty" url:"macaddr,omitempty"` MACAddress *string `json:"macaddr,omitempty" url:"macaddr,omitempty"`
MTU *int `json:"mtu,omitempty" url:"mtu,omitempty"`
Model string `json:"model" url:"model"`
Queues *int `json:"queues,omitempty" url:"queues,omitempty"` Queues *int `json:"queues,omitempty" url:"queues,omitempty"`
RateLimit *float64 `json:"rate,omitempty" url:"rate,omitempty"` RateLimit *float64 `json:"rate,omitempty" url:"rate,omitempty"`
Tag *int `json:"tag,omitempty" url:"tag,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"` Trunks []int `json:"trunks,omitempty" url:"trunks,omitempty"`
} }

View File

@ -23,15 +23,16 @@ func GetNetworkDeviceObjects(d *schema.ResourceData) (vms.CustomNetworkDevices,
block := networkDeviceEntry.(map[string]interface{}) block := networkDeviceEntry.(map[string]interface{})
bridge := block[mkNetworkDeviceBridge].(string) bridge := block[mkNetworkDeviceBridge].(string)
disconnected := types.CustomBool(block[mkNetworkDeviceDisconnected].(bool))
enabled := block[mkNetworkDeviceEnabled].(bool) enabled := block[mkNetworkDeviceEnabled].(bool)
firewall := types.CustomBool(block[mkNetworkDeviceFirewall].(bool)) firewall := types.CustomBool(block[mkNetworkDeviceFirewall].(bool))
macAddress := block[mkNetworkDeviceMACAddress].(string) macAddress := block[mkNetworkDeviceMACAddress].(string)
model := block[mkNetworkDeviceModel].(string) model := block[mkNetworkDeviceModel].(string)
mtu := block[mkNetworkDeviceMTU].(int)
queues := block[mkNetworkDeviceQueues].(int) queues := block[mkNetworkDeviceQueues].(int)
rateLimit := block[mkNetworkDeviceRateLimit].(float64) rateLimit := block[mkNetworkDeviceRateLimit].(float64)
vlanID := block[mkNetworkDeviceVLANID].(int)
trunks := block[mkNetworkDeviceTrunks].(string) trunks := block[mkNetworkDeviceTrunks].(string)
mtu := block[mkNetworkDeviceMTU].(int) vlanID := block[mkNetworkDeviceVLANID].(int)
device := vms.CustomNetworkDevice{ device := vms.CustomNetworkDevice{
Enabled: enabled, Enabled: enabled,
@ -43,6 +44,10 @@ func GetNetworkDeviceObjects(d *schema.ResourceData) (vms.CustomNetworkDevices,
device.Bridge = &bridge device.Bridge = &bridge
} }
if disconnected {
device.LinkDown = &disconnected
}
if macAddress != "" { if macAddress != "" {
device.MACAddress = &macAddress device.MACAddress = &macAddress
} }
@ -145,6 +150,12 @@ func ReadNetworkDeviceObjects(d *schema.ResourceData, vmConfig *vms.GetResponseD
networkDevice[mkNetworkDeviceEnabled] = nd.Enabled networkDevice[mkNetworkDeviceEnabled] = nd.Enabled
if nd.LinkDown != nil {
networkDevice[mkNetworkDeviceDisconnected] = *nd.LinkDown
} else {
networkDevice[mkNetworkDeviceDisconnected] = false
}
if nd.Firewall != nil { if nd.Firewall != nil {
networkDevice[mkNetworkDeviceFirewall] = *nd.Firewall networkDevice[mkNetworkDeviceFirewall] = *nd.Firewall
} else { } else {

View File

@ -17,30 +17,31 @@ const (
dvNetworkDeviceBridge = "vmbr0" dvNetworkDeviceBridge = "vmbr0"
dvNetworkDeviceEnabled = true dvNetworkDeviceEnabled = true
dvNetworkDeviceFirewall = false dvNetworkDeviceFirewall = false
dvNetworkDeviceMTU = 0
dvNetworkDeviceModel = "virtio" dvNetworkDeviceModel = "virtio"
dvNetworkDeviceQueues = 0 dvNetworkDeviceQueues = 0
dvNetworkDeviceRateLimit = 0 dvNetworkDeviceRateLimit = 0
dvNetworkDeviceVLANID = 0
dvNetworkDeviceTrunks = "" dvNetworkDeviceTrunks = ""
dvNetworkDeviceMTU = 0 dvNetworkDeviceVLANID = 0
mkIPv4Addresses = "ipv4_addresses" mkIPv4Addresses = "ipv4_addresses"
mkIPv6Addresses = "ipv6_addresses" mkIPv6Addresses = "ipv6_addresses"
mkMACAddresses = "mac_addresses" mkMACAddresses = "mac_addresses"
// MkNetworkDevice is the name of the network device. // MkNetworkDevice is the name of the network device.
MkNetworkDevice = "network_device" MkNetworkDevice = "network_device"
mkNetworkDeviceBridge = "bridge" mkNetworkDeviceBridge = "bridge"
mkNetworkDeviceEnabled = "enabled" mkNetworkDeviceDisconnected = "disconnected"
mkNetworkDeviceFirewall = "firewall" mkNetworkDeviceEnabled = "enabled"
mkNetworkDeviceMACAddress = "mac_address" mkNetworkDeviceFirewall = "firewall"
mkNetworkDeviceModel = "model" mkNetworkDeviceMACAddress = "mac_address"
mkNetworkDeviceQueues = "queues" mkNetworkDeviceMTU = "mtu"
mkNetworkDeviceRateLimit = "rate_limit" mkNetworkDeviceModel = "model"
mkNetworkDeviceVLANID = "vlan_id" mkNetworkDeviceQueues = "queues"
mkNetworkDeviceTrunks = "trunks" mkNetworkDeviceRateLimit = "rate_limit"
mkNetworkDeviceMTU = "mtu" mkNetworkDeviceTrunks = "trunks"
mkNetworkInterfaceNames = "network_interface_names" mkNetworkDeviceVLANID = "vlan_id"
mkNetworkInterfaceNames = "network_interface_names"
) )
// Schema returns the schema for the network resource. // Schema returns the schema for the network resource.
@ -86,6 +87,11 @@ func Schema() map[string]*schema.Schema {
Optional: true, Optional: true,
Default: dvNetworkDeviceBridge, Default: dvNetworkDeviceBridge,
}, },
mkNetworkDeviceDisconnected: {
Type: schema.TypeBool,
Description: "Whether the network device should be disconnected from the network",
Optional: true,
},
mkNetworkDeviceEnabled: { mkNetworkDeviceEnabled: {
Type: schema.TypeBool, Type: schema.TypeBool,
Description: "Whether to enable the network device", Description: "Whether to enable the network device",