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:
parent
e2f96ed092
commit
ec20f800b2
|
@ -1,9 +1,11 @@
|
|||
package scaleway
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"log"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/terraform/helper/resource"
|
||||
"github.com/scaleway/scaleway-cli/pkg/api"
|
||||
)
|
||||
|
||||
|
@ -17,36 +19,55 @@ func String(val string) *string {
|
|||
return &val
|
||||
}
|
||||
|
||||
// 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
|
||||
// deleteRunningServer terminates the server and waits until it is removed.
|
||||
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 {
|
||||
return err
|
||||
}
|
||||
|
||||
if server.State != "stopped" {
|
||||
if err := s.PostServerAction(serverID, "poweroff"); err != nil {
|
||||
return err
|
||||
}
|
||||
if err := waitForServerState(s, serverID, "stopped"); err != nil {
|
||||
return err
|
||||
if serr, ok := err.(api.ScalewayAPIError); ok {
|
||||
if serr.StatusCode == 404 {
|
||||
return nil
|
||||
}
|
||||
}
|
||||
|
||||
if err := s.DeleteServer(serverID); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
return resource.Retry(5*time.Minute, func() *resource.RetryError {
|
||||
_, err := scaleway.GetServer(server.Identifier)
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
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 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 {
|
||||
var server *api.ScalewayServer
|
||||
var err error
|
||||
|
|
|
@ -205,22 +205,20 @@ func resourceScalewayServerUpdate(d *schema.ResourceData, m interface{}) error {
|
|||
func resourceScalewayServerDelete(d *schema.ResourceData, m interface{}) error {
|
||||
scaleway := m.(*Client).scaleway
|
||||
|
||||
def, 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)
|
||||
s, err := scaleway.GetServer(d.Id())
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if s.State == "stopped" {
|
||||
return deleteStoppedServer(scaleway, s)
|
||||
}
|
||||
|
||||
err = deleteRunningServer(scaleway, s)
|
||||
|
||||
if err == nil {
|
||||
d.SetId("")
|
||||
return nil
|
||||
}
|
||||
|
||||
return err
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue