provider/scaleway speedup server deletion (#9491)

* provider/scaleway speedup server deletion

using `terminate` instead of `poweroff` leads to a faster shutdown

fixes #9430

* provider/scaleway: extract server shutdown code
This commit is contained in:
Raphael Randschau 2016-10-27 13:37:40 +02:00 committed by Paul Stack
parent e2f96ed092
commit ec20f800b2
2 changed files with 48 additions and 29 deletions

View File

@ -1,9 +1,11 @@
package scaleway package scaleway
import ( import (
"fmt"
"log" "log"
"time" "time"
"github.com/hashicorp/terraform/helper/resource"
"github.com/scaleway/scaleway-cli/pkg/api" "github.com/scaleway/scaleway-cli/pkg/api"
) )
@ -17,36 +19,55 @@ func String(val string) *string {
return &val return &val
} }
// NOTE copied from github.com/scaleway/scaleway-cli/pkg/api/helpers.go // deleteRunningServer terminates the server and waits until it is removed.
// the helpers.go file pulls in quite a lot dependencies, and they're just convenience wrappers anyway func deleteRunningServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) error {
err := scaleway.PostServerAction(server.Identifier, "terminate")
func deleteServerSafe(s *api.ScalewayAPI, serverID string) error {
server, err := s.GetServer(serverID)
if err != nil { if err != nil {
if serr, ok := err.(api.ScalewayAPIError); ok {
if serr.StatusCode == 404 {
return nil
}
}
return err return err
} }
if server.State != "stopped" { return resource.Retry(5*time.Minute, func() *resource.RetryError {
if err := s.PostServerAction(serverID, "poweroff"); err != nil { _, err := scaleway.GetServer(server.Identifier)
return err
}
if err := waitForServerState(s, serverID, "stopped"); err != nil {
return err
}
}
if err := s.DeleteServer(serverID); err != nil { if err == nil {
return resource.RetryableError(fmt.Errorf("Waiting for server %q to be deleted", server.Identifier))
}
if serr, ok := err.(api.ScalewayAPIError); ok {
if serr.StatusCode == 404 {
return nil
}
}
return resource.RetryableError(err)
})
}
// deleteStoppedServer needs to cleanup attached root volumes. this is not done
// automatically by Scaleway
func deleteStoppedServer(scaleway *api.ScalewayAPI, server *api.ScalewayServer) error {
if err := scaleway.DeleteServer(server.Identifier); err != nil {
return err return err
} }
if rootVolume, ok := server.Volumes["0"]; ok { if rootVolume, ok := server.Volumes["0"]; ok {
if err := s.DeleteVolume(rootVolume.Identifier); err != nil { if err := scaleway.DeleteVolume(rootVolume.Identifier); err != nil {
return err return err
} }
} }
return nil 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
func waitForServerState(s *api.ScalewayAPI, serverID string, targetState string) error { func waitForServerState(s *api.ScalewayAPI, serverID string, targetState string) error {
var server *api.ScalewayServer var server *api.ScalewayServer
var err error var err error

View File

@ -205,22 +205,20 @@ func resourceScalewayServerUpdate(d *schema.ResourceData, m interface{}) error {
func resourceScalewayServerDelete(d *schema.ResourceData, m interface{}) error { func resourceScalewayServerDelete(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway scaleway := m.(*Client).scaleway
def, err := scaleway.GetServer(d.Id()) s, err := scaleway.GetServer(d.Id())
if err != nil {
if serr, ok := err.(api.ScalewayAPIError); ok {
if serr.StatusCode == 404 {
d.SetId("")
return nil
}
}
return err
}
err = deleteServerSafe(scaleway, def.Identifier)
if err != nil { if err != nil {
return err return err
} }
d.SetId("") if s.State == "stopped" {
return nil return deleteStoppedServer(scaleway, s)
}
err = deleteRunningServer(scaleway, s)
if err == nil {
d.SetId("")
}
return err
} }