From 04d7532bf73c486d12e0ec2377b7ce932d013c20 Mon Sep 17 00:00:00 2001 From: Brett Mack Date: Thu, 18 Feb 2016 17:36:36 +0000 Subject: [PATCH] When creating a new VApp wait for the VM to be given an IP address if using DHCP --- builtin/providers/vcd/resource_vcd_vapp.go | 32 ++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/builtin/providers/vcd/resource_vcd_vapp.go b/builtin/providers/vcd/resource_vcd_vapp.go index 8c98ecf21..65055ce00 100644 --- a/builtin/providers/vcd/resource_vcd_vapp.go +++ b/builtin/providers/vcd/resource_vcd_vapp.go @@ -297,17 +297,45 @@ func resourceVcdVAppRead(d *schema.ResourceData, meta interface{}) error { return fmt.Errorf("Error refreshing vdc: %#v", err) } - vapp, err := vcdClient.OrgVdc.FindVAppByName(d.Id()) + _, err = vcdClient.OrgVdc.FindVAppByName(d.Id()) if err != nil { log.Printf("[DEBUG] Unable to find vapp. Removing from tfstate") d.SetId("") return nil } - d.Set("ip", vapp.VApp.Children.VM[0].NetworkConnectionSection.NetworkConnection.IPAddress) + + ip, err := getVAppIPAddress(d, meta) + if err != nil { + return err + } + d.Set("ip", ip) return nil } +func getVAppIPAddress(d *schema.ResourceData, meta interface{}) (string, error) { + vcdClient := meta.(*VCDClient) + var ip string + + err := retryCall(vcdClient.MaxRetryTimeout, func() error { + err := vcdClient.OrgVdc.Refresh() + if err != nil { + return fmt.Errorf("Error refreshing vdc: %#v", err) + } + vapp, err := vcdClient.OrgVdc.FindVAppByName(d.Id()) + if err != nil { + return fmt.Errorf("Unable to find vapp.") + } + ip = vapp.VApp.Children.VM[0].NetworkConnectionSection.NetworkConnection.IPAddress + if ip == "" { + return fmt.Errorf("Timeout: VM did not aquire IP address") + } + return nil + }) + + return ip, err +} + func resourceVcdVAppDelete(d *schema.ResourceData, meta interface{}) error { vcdClient := meta.(*VCDClient) vapp, err := vcdClient.OrgVdc.FindVAppByName(d.Id())