Skip to content

Commit

Permalink
provider/vsphere: wait for network enhanced (#6377)
Browse files Browse the repository at this point in the history
* - use WaitForNetIP
- removed duplicate wait for network parts

* gofmt fix

* fixes
  • Loading branch information
thetuxkeeper authored and stack72 committed May 17, 2016
1 parent 6139b43 commit 790115f
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 80 deletions.
87 changes: 8 additions & 79 deletions builtin/providers/vsphere/resource_vsphere_virtual_machine.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,7 @@ import (
"net"
"strconv"
"strings"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
"github.com/vmware/govmomi"
"github.com/vmware/govmomi/find"
Expand Down Expand Up @@ -403,12 +401,6 @@ func resourceVSphereVirtualMachine() *schema.Resource {
},
},
},

"boot_delay": &schema.Schema{
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
},
},
}
}
Expand Down Expand Up @@ -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())
log.Printf("[INFO] Created virtual machine: %s", d.Id())

Expand All @@ -761,6 +727,12 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})

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)
if err := collector.RetrieveOne(context.TODO(), vm.Reference(), []string{"guest", "summary", "datastore"}, &mvm); err != nil {
return err
Expand Down Expand Up @@ -826,14 +798,10 @@ func resourceVSphereVirtualMachineRead(d *schema.ResourceData, meta interface{})
return fmt.Errorf("Invalid network interfaces to set: %#v", networkInterfaces)
}

ip, err := vm.WaitForIP(context.TODO())
if err != nil {
return err
}
log.Printf("[DEBUG] ip address: %v", ip)
log.Printf("[DEBUG] ip address: %v", networkInterfaces[0]["ipv4_address"].(string))
d.SetConnInfo(map[string]string{
"type": "ssh",
"host": ip,
"host": networkInterfaces[0]["ipv4_address"].(string),
})

var rootDatastore string
Expand Down Expand Up @@ -911,39 +879,6 @@ func resourceVSphereVirtualMachineDelete(d *schema.ResourceData, meta interface{
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.
func addHardDisk(vm *object.VirtualMachine, size, iops int64, diskType string, datastore *object.Datastore, diskPath string) error {
devices, err := vm.Device(context.TODO())
Expand Down Expand Up @@ -1758,11 +1693,5 @@ func (vm *virtualMachine) deployVirtualMachine(c *govmomi.Client) error {

newVM.PowerOn(context.TODO())

ip, err := newVM.WaitForIP(context.TODO())
if err != nil {
return err
}
log.Printf("[DEBUG] ip address: %v", ip)

return nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ The following arguments are supported:
* `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
* `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.
* `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.
Expand Down

0 comments on commit 790115f

Please sign in to comment.