From 7890e01760a42cf5efb388cb8a6639d402860c96 Mon Sep 17 00:00:00 2001 From: GnomeZworc Date: Tue, 21 Nov 2023 14:25:19 +0100 Subject: [PATCH 1/2] 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"}, From bd798148158d8ed477d5f8b9b5feaa9a6d6db79a Mon Sep 17 00:00:00 2001 From: Thiery Ouattara Date: Fri, 12 Jan 2024 11:00:11 +0000 Subject: [PATCH 2/2] remove Default option --- outscale/resource_outscale_vm.go | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/outscale/resource_outscale_vm.go b/outscale/resource_outscale_vm.go index 66a19f377..cc8cb5a6a 100644 --- a/outscale/resource_outscale_vm.go +++ b/outscale/resource_outscale_vm.go @@ -714,7 +714,6 @@ func resourceOutscaleOApiVM() *schema.Resource { "wait_tag_before_start": { Type: schema.TypeBool, Optional: true, - Default: false, }, "tags": tagsListOAPISchema(), }, @@ -738,8 +737,8 @@ 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 { + vmWaitTag, ok := d.GetOk("wait_tag_before_start") + if ok && vmWaitTag.(bool) { vmOpts.BootOnCreation = oscgo.PtrBool(false) } @@ -789,7 +788,7 @@ func resourceOAPIVMCreate(d *schema.ResourceData, meta interface{}) error { return err } } - if vmWaitTag { + if vmWaitTag.(bool) { stateConf := &resource.StateChangeConf{ Pending: []string{"pending"}, Target: []string{"stopped"},