Skip to content

Commit

Permalink
replace waiting on server results with task api
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolai86 committed Mar 31, 2018
1 parent 492a5f3 commit 867df97
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 48 deletions.
42 changes: 9 additions & 33 deletions scaleway/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func validateVolumeType(v interface{}, k string) (ws []string, errors []error) {

// deleteRunningServer terminates the server and waits until it is removed.
func deleteRunningServer(scaleway *api.API, server *api.Server) error {
_, err := scaleway.PostServerAction(server.Identifier, "terminate")
task, err := scaleway.PostServerAction(server.Identifier, "terminate")

if err != nil {
if serr, ok := err.(api.APIError); ok {
Expand All @@ -59,7 +59,7 @@ func deleteRunningServer(scaleway *api.API, server *api.Server) error {
return err
}

return waitForServerShutdown(scaleway, server.Identifier)
return waitForTaskCompletion(scaleway, task.Identifier)
}

// deleteStoppedServer needs to cleanup attached root volumes. this is not done
Expand All @@ -77,40 +77,16 @@ func deleteStoppedServer(scaleway *api.API, server *api.Server) error {
return nil
}

// NOTE copied from github.com/scaleway/scaleway-cli/pkg/api/helpers.go
// the helpers.go file pulls in quite a lot dependencies, and they're just convenience wrappers anyway

var allStates = []string{"starting", "running", "stopping", "stopped"}

func waitForServerStartup(scaleway *api.API, serverID string) error {
return waitForServerState(scaleway, serverID, "running", []string{"running", "starting"})
}

func waitForServerShutdown(scaleway *api.API, serverID string) error {
return waitForServerState(scaleway, serverID, "stopped", []string{"stopped", "stopping"})
}

func waitForServerState(scaleway *api.API, serverID, targetState string, pendingStates []string) error {
func waitForTaskCompletion(scaleway *api.API, taskID string) error {
stateConf := &resource.StateChangeConf{
Pending: pendingStates,
Target: []string{targetState},
Pending: []string{"pending"},
Target: []string{"success"},
Refresh: func() (interface{}, string, error) {
s, err := scaleway.GetServer(serverID)

if err == nil {
return 42, s.State, nil
}

if serr, ok := err.(api.APIError); ok {
if serr.StatusCode == 404 {
return 42, "stopped", nil
}
}

if s != nil {
return 42, s.State, err
task, err := scaleway.GetTask(taskID)
if err != nil {
return 42, "error", err
}
return 42, "error", err
return 42, task.Status, nil
},
Timeout: 60 * time.Minute,
MinTimeout: 10 * time.Second,
Expand Down
4 changes: 2 additions & 2 deletions scaleway/resource_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,12 @@ func resourceScalewayServerCreate(d *schema.ResourceData, m interface{}) error {

d.SetId(server.Identifier)
if d.Get("state").(string) != "stopped" {
_, err = scaleway.PostServerAction(server.Identifier, "poweron")
task, err := scaleway.PostServerAction(server.Identifier, "poweron")
if err != nil {
return err
}

err = waitForServerStartup(scaleway, server.Identifier)
err = waitForTaskCompletion(scaleway, task.Identifier)

if v, ok := d.GetOk("public_ip"); ok {
if err := attachIP(scaleway, d.Id(), v.(string)); err != nil {
Expand Down
30 changes: 17 additions & 13 deletions scaleway/resource_volume_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,13 @@ func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{
if server.State != "stopped" {
startServerAgain = true

if _, err := scaleway.PostServerAction(server.Identifier, "poweroff"); err != nil {
task, err := scaleway.PostServerAction(server.Identifier, "poweroff")
if err != nil {
return err
}
if err := waitForTaskCompletion(scaleway, task.Identifier); err != nil {
return err
}
}

if err := waitForServerShutdown(scaleway, server.Identifier); err != nil {
return err
}

volumes := make(map[string]api.Volume)
Expand Down Expand Up @@ -115,10 +115,11 @@ func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{
}

if startServerAgain {
if _, err := scaleway.PostServerAction(serverID, "poweron"); err != nil {
task, err := scaleway.PostServerAction(serverID, "poweron")
if err != nil {
return err
}
if err := waitForServerStartup(scaleway, serverID); err != nil {
if err := waitForTaskCompletion(scaleway, task.Identifier); err != nil {
return err
}
}
Expand Down Expand Up @@ -187,12 +188,14 @@ func resourceScalewayVolumeAttachmentDelete(d *schema.ResourceData, m interface{
// volumes can only be modified when the server is powered off
if server.State != "stopped" {
startServerAgain = true
if _, err := scaleway.PostServerAction(server.Identifier, "poweroff"); err != nil {

task, err := scaleway.PostServerAction(server.Identifier, "poweroff")
if err != nil {
return err
}
if err := waitForTaskCompletion(scaleway, task.Identifier); err != nil {
return err
}
}
if err := waitForServerShutdown(scaleway, server.Identifier); err != nil {
return err
}

volumes := make(map[string]api.Volume)
Expand Down Expand Up @@ -239,10 +242,11 @@ func resourceScalewayVolumeAttachmentDelete(d *schema.ResourceData, m interface{
}

if startServerAgain {
if _, err := scaleway.PostServerAction(serverID, "poweron"); err != nil {
task, err := scaleway.PostServerAction(serverID, "poweron")
if err != nil {
return err
}
if err := waitForServerStartup(scaleway, serverID); err != nil {
if err := waitForTaskCompletion(scaleway, task.Identifier); err != nil {
return err
}
}
Expand Down

0 comments on commit 867df97

Please sign in to comment.