From 7890e01760a42cf5efb388cb8a6639d402860c96 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 21 Nov 2023 14:25:19 +0100 Subject: [PATCH] add an option to wait for tag to be created before starting an instance Signed-off-by: GnomeZworc --- outscale/resource_outscale_vm.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/outscale/resource_outscale_vm.go b/outscale/resource_outscale_vm.go index abf3f51ed..66a19f377 100644 --- a/outscale/resource_outscale_vm.go +++ b/outscale/resource_outscale_vm.go @@ -711,6 +711,11 @@ func resourceOutscaleOApiVM() *schema.Resource { Type: schema.TypeBool, Optional: true, }, + "wait_tag_before_start": { + Type: schema.TypeBool, + Optional: true, + Default: false, + }, "tags": tagsListOAPISchema(), }, } @@ -733,6 +738,10 @@ func resourceOAPIVMCreate(d *schema.ResourceData, meta interface{}) error { vmStateTarget[0] = "stopped" vmOpts.BootOnCreation = oscgo.PtrBool(false) } + vmWaitTag := d.Get("wait_tag_before_start").(bool) + if vmWaitTag { + vmOpts.BootOnCreation = oscgo.PtrBool(false) + } // Create the vm var resp oscgo.CreateVmsResponse @@ -780,6 +789,25 @@ func resourceOAPIVMCreate(d *schema.ResourceData, meta interface{}) error { return err } } + if vmWaitTag { + stateConf := &resource.StateChangeConf{ + Pending: []string{"pending"}, + Target: []string{"stopped"}, + Refresh: vmStateRefreshFunc(conn, vm.GetVmId(), "terminated"), + Timeout: d.Timeout(schema.TimeoutCreate), + Delay: 15 * time.Second, + MinTimeout: 3 * time.Second, + } + + _, err = stateConf.WaitForState() + if err != nil { + return fmt.Errorf( + "Error waiting for instance (%s) to become created: %s", d.Id(), err) + } + if err := startVM(vm.GetVmId(), conn, d.Timeout(schema.TimeoutCreate)); err != nil { + return err + } + } stateConf := &resource.StateChangeConf{ Pending: []string{"pending", "ending/wait"},