provider/vsphere: wait for network enhanced (#6377)

* - use WaitForNetIP
- removed duplicate wait for network parts

* gofmt fix

* fixes
This commit is contained in:
thetuxkeeper 2016-05-17 20:35:06 +02:00 committed by Paul Stack
parent 6139b43c95
commit 790115fe43
2 changed files with 8 additions and 80 deletions

View File

@ -6,9 +6,7 @@ import (
"net" "net"
"strconv" "strconv"
"strings" "strings"
"time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/govmomi" "github.com/vmware/govmomi"
"github.com/vmware/govmomi/find" "github.com/vmware/govmomi/find"
@ -403,12 +401,6 @@ func resourceVSphereVirtualMachine() *schema.Resource {
}, },
}, },
}, },
"boot_delay": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
}, },
} }
} }
@ -711,32 +703,6 @@ func resourceVSphereVirtualMachineCreate(d *schema.ResourceData, meta interface{
} }
} }
if _, ok := d.GetOk("network_interface.0.ipv4_address"); !ok {
if v, ok := d.GetOk("boot_delay"); ok {
stateConf := &resource.StateChangeConf{
Pending: []string{"pending"},
Target: []string{"active"},
Refresh: waitForNetworkingActive(client, vm.datacenter, vm.Path()),
Timeout: 600 * time.Second,
Delay: time.Duration(v.(int)) * time.Second,
MinTimeout: 2 * time.Second,
}
_, err := stateConf.WaitForState()
if err != nil {
return err
}
}
}
if ip, ok := d.GetOk("network_interface.0.ipv4_address"); ok {
d.SetConnInfo(map[string]string{
"host": ip.(string),
})
} else {
log.Printf("[DEBUG] Could not get IP address for %s", d.Id())
}
d.SetId(vm.Path()) d.SetId(vm.Path())
log.Printf("[INFO] Created virtual machine: %s", d.Id()) log.Printf("[INFO] Created virtual machine: %s", d.Id())
@ -761,6 +727,12 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
var mvm mo.VirtualMachine var mvm mo.VirtualMachine
// wait for interfaces to appear
_, err = vm.WaitForNetIP(context.TODO(), true)
if err != nil {
return err
}
collector := property.DefaultCollector(client.Client) collector := property.DefaultCollector(client.Client)
if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore"}, &mvm); err != nil { if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore"}, &mvm); err != nil {
return err return err
@ -826,14 +798,10 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Invalid network interfaces to set: %#v", networkInterfaces) return fmt.Errorf("Invalid network interfaces to set: %#v", networkInterfaces)
} }
ip, err := vm.WaitForIP(context.TODO()) log.Printf("[DEBUG] ip address: %v", networkInterfaces[0]["ipv4_address"].(string))
if err != nil {
return err
}
log.Printf("[DEBUG] ip address: %v", ip)
d.SetConnInfo(map[string]string{ d.SetConnInfo(map[string]string{
"type": "ssh", "type": "ssh",
"host": ip, "host": networkInterfaces[0]["ipv4_address"].(string),
}) })
var rootDatastore string var rootDatastore string
@ -911,39 +879,6 @@ func resourceVSphereVirtualMachineDelete(d *schema.ResourceData, meta interface{
return nil return nil
} }
func waitForNetworkingActive(client *govmomi.Client, datacenter, name string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
dc, err := getDatacenter(client, datacenter)
if err != nil {
log.Printf("[ERROR] %#v", err)
return nil, "", err
}
finder := find.NewFinder(client.Client, true)
finder = finder.SetDatacenter(dc)
vm, err := finder.VirtualMachine(context.TODO(), name)
if err != nil {
log.Printf("[ERROR] %#v", err)
return nil, "", err
}
var mvm mo.VirtualMachine
collector := property.DefaultCollector(client.Client)
if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"summary"}, &mvm); err != nil {
log.Printf("[ERROR] %#v", err)
return nil, "", err
}
if mvm.Summary.Guest.IpAddress != "" {
log.Printf("[DEBUG] IP address with DHCP: %v", mvm.Summary.Guest.IpAddress)
return mvm.Summary, "active", err
} else {
log.Printf("[DEBUG] Waiting for IP address")
return nil, "pending", err
}
}
}
// addHardDisk adds a new Hard Disk to the VirtualMachine. // addHardDisk adds a new Hard Disk to the VirtualMachine.
func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, datastore *object.Datastore, diskPath string) error { func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, datastore *object.Datastore, diskPath string) error {
devices, err := vm.Device(context.TODO()) devices, err := vm.Device(context.TODO())
@ -1758,11 +1693,5 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error {
newVM.PowerOn(context.TODO()) newVM.PowerOn(context.TODO())
ip, err := newVM.WaitForIP(context.TODO())
if err != nil {
return err
}
log.Printf("[DEBUG] ip address: %v", ip)
return nil return nil
} }

View File

@ -76,7 +76,6 @@ The following arguments are supported:
* `network_interface` - (Required) Configures virtual network interfaces; see [Network Interfaces](#network-interfaces) below for details. * `network_interface` - (Required) Configures virtual network interfaces; see [Network Interfaces](#network-interfaces) below for details.
* `disk` - (Required) Configures virtual disks; see [Disks](#disks) below for details * `disk` - (Required) Configures virtual disks; see [Disks](#disks) below for details
* `cdrom` - (Optional) Configures a CDROM device and mounts an image as its media; see [CDROM](#cdrom) below for more details. * `cdrom` - (Optional) Configures a CDROM device and mounts an image as its media; see [CDROM](#cdrom) below for more details.
* `boot_delay` - (Optional) Time in seconds to wait for machine network to be ready.
* `windows_opt_config` - (Optional) Extra options for clones of Windows machines. * `windows_opt_config` - (Optional) Extra options for clones of Windows machines.
* `linked_clone` - (Optional) Specifies if the new machine is a [linked clone](https://www.vmware.com/support/ws5/doc/ws_clone_overview.html#wp1036396) of another machine or not. * `linked_clone` - (Optional) Specifies if the new machine is a [linked clone](https://www.vmware.com/support/ws5/doc/ws_clone_overview.html#wp1036396) of another machine or not.
* `custom_configuration_parameters` - (Optional) Map of values that is set as virtual machine custom configurations. * `custom_configuration_parameters` - (Optional) Map of values that is set as virtual machine custom configurations.