terra-farm / terraform-provider-virtualbox

VirtualBox provider for Terraform
https://terra-farm.github.io/provider-virtualbox/
MIT License
323 stars 134 forks source link

Sometime it cannot find NICs address respectively #79

Closed nevill closed 4 years ago

nevill commented 4 years ago

Suppose my Virtualbox have following output.

From showvminfo

$ VBoxManage showvminfo "master-01" --machinereadable
name="master-01"
memory=1024
cpus=2
pae="on"
VMState="running"
hostonlyadapter1="vboxnet1"
macaddress1="0800275BAF21"
cableconnected1="on"
nic1="hostonly"
nictype1="82545EM"
nicspeed1="0"
natnet2="nat"
macaddress2="0800274A2EA2"
cableconnected2="on"
nic2="nat"
nictype2="82545EM"
nicspeed2="0"
nic3="none"
nic4="none"
nic5="none"
nic6="none"
nic7="none"
nic8="none"
GuestMemoryBalloon=0
GuestOSType="Linux26_64"
GuestAdditionsRunLevel=2
GuestAdditionsVersion="6.0.14 r133895"
GuestAdditionsFacility_VirtualBox Base Driver=50,1571759712632
GuestAdditionsFacility_VirtualBox System Service=50,1571759720710
GuestAdditionsFacility_Seamless Mode=0,1571759712629
GuestAdditionsFacility_Graphics Mode=0,1571759712629

... more properties are cut off

From guest property

$ VBoxManage guestproperty enumerate master-01 --patterns "/VirtualBox/GuestInfo/Net*"
Name: /VirtualBox/GuestInfo/Net/0/MAC, value: 0800274A2EA2, timestamp: 1571759720723377000, flags:
Name: /VirtualBox/GuestInfo/Net/0/V4/IP, value: 10.0.3.15, timestamp: 1571759720723226000, flags:
Name: /VirtualBox/GuestInfo/Net/0/V4/Netmask, value: 255.255.255.0, timestamp: 1571759720723342000, flags:
Name: /VirtualBox/GuestInfo/Net/0/Status, value: Up, timestamp: 1571759720723419000, flags:
Name: /VirtualBox/GuestInfo/Net/0/Name, value: eth0, timestamp: 1571759720723448000, flags:
Name: /VirtualBox/GuestInfo/Net/0/V4/Broadcast, value: 10.0.3.255, timestamp: 1571759720723308000, flags:

Name: /VirtualBox/GuestInfo/Net/1/Name, value: docker0, timestamp: 1571759720723876000, flags:
Name: /VirtualBox/GuestInfo/Net/1/MAC, value: 0242BF5B1B9D, timestamp: 1571759720723772000, flags:
Name: /VirtualBox/GuestInfo/Net/1/V4/IP, value: 172.17.0.1, timestamp: 1571759720723477000, flags:
Name: /VirtualBox/GuestInfo/Net/1/V4/Netmask, value: 255.255.0.0, timestamp: 1571759720723594000, flags:
Name: /VirtualBox/GuestInfo/Net/1/Status, value: Up, timestamp: 1571759720723842000, flags:
Name: /VirtualBox/GuestInfo/Net/1/V4/Broadcast, value: 172.17.255.255, timestamp: 1571759720723514000, flags:

Name: /VirtualBox/GuestInfo/Net/2/Status, value: Up, timestamp: 1571759720724350000, flags:
Name: /VirtualBox/GuestInfo/Net/2/Name, value: eth1, timestamp: 1571759720724414000, flags:
Name: /VirtualBox/GuestInfo/Net/2/MAC, value: 0800275BAF21, timestamp: 1571759720724187000, flags:
Name: /VirtualBox/GuestInfo/Net/2/V4/IP, value: 192.168.99.112, timestamp: 1571759720723908000, flags:
Name: /VirtualBox/GuestInfo/Net/2/V4/Netmask, value: 255.255.255.0, timestamp: 1571759720724096000, flags:
Name: /VirtualBox/GuestInfo/Net/2/V4/Broadcast, value: 192.168.99.255, timestamp: 1571759720723946000, flags:

Name: /VirtualBox/GuestInfo/Net/Count, value: 3, timestamp: 1571760671544746000, flags:

Notice that one more interface docker0 created after VM started.

here in code https://github.com/terra-farm/terraform-provider-virtualbox/blob/39689431d146f06ebb608b73d53c5a48387362a0/virtualbox/resource_vm.go#L631

we only iterate over the number of physical interfaces, which possibly cannot find the interface IP address in the 3rd place. Also, it results in returning early and resource network_adapter cannot be set. https://github.com/terra-farm/terraform-provider-virtualbox/blob/39689431d146f06ebb608b73d53c5a48387362a0/virtualbox/resource_vm.go#L684-L687

My proposal would be

  1. ~in terra-farm/go-virtualbox, add a new method to enumerate all the runtime interfaces based on~ get the number of NICs via guestproperty /VirtualBox/GuestInfo/Net/Count
  2. fix method netVboxToTf by calling the new method
nevill commented 4 years ago

Updated my proposal, it's not necessary to touch terra-farm/go-virtualbox.