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

Add mac_addresses attribute to VM resource

This commit is contained in:
Dan Petersen 2019-12-29 23:24:22 +01:00
parent 1dfe979e9e
commit d29a0a7ef6
5 changed files with 23 additions and 2 deletions

View File

@ -428,6 +428,7 @@ This resource doesn't expose any additional attributes.
###### Attributes
* `ipv4_addresses` - The IPv4 addresses per network interface published by the QEMU agent (empty list when `agent.enabled` is `false`)
* `ipv6_addresses` - The IPv6 addresses per network interface published by the QEMU agent (empty list when `agent.enabled` is `false`)
* `mac_addresses` - The MAC addresses assigned to the network devices
* `network_interface_names` - The network interface names published by the QEMU agent (empty list when `agent.enabled` is `false`)
## Developing the Provider

View File

@ -77,6 +77,10 @@ output "resource_proxmox_virtual_environment_vm_example_ipv6_addresses" {
value = "${proxmox_virtual_environment_vm.example.ipv6_addresses}"
}
output "resource_proxmox_virtual_environment_vm_example_mac_addresses" {
value = "${proxmox_virtual_environment_vm.example.mac_addresses}"
}
output "resource_proxmox_virtual_environment_vm_example_network_interface_names" {
value = "${proxmox_virtual_environment_vm.example.network_interface_names}"
}

View File

@ -1095,6 +1095,7 @@ func (r *CustomNetworkDevice) UnmarshalJSON(b []byte) error {
r.Trunks[i] = iv
}
default:
r.MACAddress = &v[1]
r.Model = v[0]
}
}

View File

@ -92,6 +92,7 @@ const (
mkResourceVirtualEnvironmentVMIPv4Addresses = "ipv4_addresses"
mkResourceVirtualEnvironmentVMIPv6Addresses = "ipv6_addresses"
mkResourceVirtualEnvironmentVMKeyboardLayout = "keyboard_layout"
mkResourceVirtualEnvironmentVMMACAddresses = "mac_addresses"
mkResourceVirtualEnvironmentVMMemory = "memory"
mkResourceVirtualEnvironmentVMMemoryDedicated = "dedicated"
mkResourceVirtualEnvironmentVMMemoryFloating = "floating"
@ -515,6 +516,12 @@ func resourceVirtualEnvironmentVM() *schema.Resource {
Default: dvResourceVirtualEnvironmentVMKeyboardLayout,
ValidateFunc: getKeyboardLayoutValidator(),
},
mkResourceVirtualEnvironmentVMMACAddresses: {
Type: schema.TypeList,
Computed: true,
Description: "The MAC addresses for the network interfaces",
Elem: &schema.Schema{Type: schema.TypeString},
},
mkResourceVirtualEnvironmentVMMemory: &schema.Schema{
Type: schema.TypeList,
Description: "The memory allocation",
@ -1465,6 +1472,7 @@ func resourceVirtualEnvironmentVMRead(d *schema.ResourceData, m interface{}) err
// Compare the network devices to those stored in the state.
currentNetworkDeviceList := d.Get(mkResourceVirtualEnvironmentVMNetworkDevice).([]interface{})
macAddresses := make([]interface{}, 8)
networkDeviceLast := -1
networkDeviceList := make([]interface{}, 8)
networkDeviceObjects := []*proxmox.CustomNetworkDevice{
@ -1493,20 +1501,24 @@ func resourceVirtualEnvironmentVMRead(d *schema.ResourceData, m interface{}) err
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceEnabled] = nd.Enabled
if nd.MACAddress != nil {
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceMACAddress] = *nd.MACAddress
macAddresses[ni] = *nd.MACAddress
} else {
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceMACAddress] = ""
macAddresses[ni] = ""
}
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceMACAddress] = macAddresses[ni]
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceModel] = nd.Model
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceVLANIDs] = nd.Trunks
} else {
macAddresses[ni] = ""
networkDevice[mkResourceVirtualEnvironmentVMNetworkDeviceEnabled] = false
}
networkDeviceList[ni] = networkDevice
}
d.Set(mkResourceVirtualEnvironmentVMMACAddresses, macAddresses[0:len(currentNetworkDeviceList)])
if len(currentNetworkDeviceList) > 0 || networkDeviceLast > -1 {
d.Set(mkResourceVirtualEnvironmentVMNetworkDevice, networkDeviceList[0:networkDeviceLast+1])
}

View File

@ -46,6 +46,7 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) {
testComputedAttributes(t, s, []string{
mkResourceVirtualEnvironmentVMIPv4Addresses,
mkResourceVirtualEnvironmentVMIPv6Addresses,
mkResourceVirtualEnvironmentVMMACAddresses,
mkResourceVirtualEnvironmentVMNetworkInterfaceNames,
})
@ -61,6 +62,7 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) {
mkResourceVirtualEnvironmentVMMemory,
mkResourceVirtualEnvironmentVMName,
mkResourceVirtualEnvironmentVMNetworkDevice,
mkResourceVirtualEnvironmentVMMACAddresses,
mkResourceVirtualEnvironmentVMNetworkInterfaceNames,
mkResourceVirtualEnvironmentVMOSType,
mkResourceVirtualEnvironmentVMPoolID,
@ -79,6 +81,7 @@ func TestResourceVirtualEnvironmentVMSchema(t *testing.T) {
schema.TypeString,
schema.TypeList,
schema.TypeList,
schema.TypeList,
schema.TypeString,
schema.TypeString,
schema.TypeBool,