Skip to content

Commit

Permalink
add an option to wait for tag to be created before starting an instance
Browse files Browse the repository at this point in the history
Signed-off-by: GnomeZworc <[email protected]>
  • Loading branch information
GnomeZworc committed Dec 12, 2023
1 parent e4751de commit 7890e01
Showing 1 changed file with 28 additions and 0 deletions.
28 changes: 28 additions & 0 deletions outscale/resource_outscale_vm.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
}
Expand All @@ -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
Expand Down Expand Up @@ -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"},
Expand Down

0 comments on commit 7890e01

Please sign in to comment.