From 790115fe43979afdcf8742ac97a3efd5f285ae8d Mon Sep 17 00:00:00 2001 From: thetuxkeeper Date: Tue, 17 May 2016 20:35:06 +0200 Subject: [PATCH] provider/vsphere: wait for network enhanced (#6377) * - use WaitForNetIP - removed duplicate wait for network parts * gofmt fix * fixes --- .../resource_vsphere_virtual_machine.go | 87 ++----------------- .../vsphere/r/virtual_machine.html.markdown | 1 - 2 files changed, 8 insertions(+), 80 deletions(-) diff --git a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go index 74da959d27f3..ef4d70d3098f 100644 --- a/builtin/providers/vsphere/resource_vsphere_virtual_machine.go +++ b/builtin/providers/vsphere/resource_vsphere_virtual_machine.go @@ -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" @@ -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()) log.Printf("[INFO] Created virtual machine: %s", d.Id()) @@ -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 @@ -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 @@ -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()) @@ -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 } diff --git a/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown b/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown index 4268d6486271..4f80926bd55d 100644 --- a/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown +++ b/website/source/docs/providers/vsphere/r/virtual_machine.html.markdown @@ -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.