Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

provider/scaleway speedup server deletion #9491

Merged
merged 2 commits into from
Oct 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 34 additions & 13 deletions builtin/providers/scaleway/helpers.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
package scaleway

import (
"fmt"
"log"
"time"

"github.com/hashicorp/terraform/helper/resource"
"github.com/scaleway/scaleway-cli/pkg/api"
)

Expand All @@ -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 {
if serr, ok := err.(api.ScalewayAPIError); ok {
if serr.StatusCode == 404 {
return nil
}
}

return err
}

if server.State != "stopped" {
if err := s.PostServerAction(serverID, "poweroff"); err != nil {
return err
return resource.Retry(5*time.Minute, func() *resource.RetryError {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

5 minutes seams very optimistic considering the current performances of scaleway, any way to make this with config at the provider level and/or to put 10 minutes ?

_, err := scaleway.GetServer(server.Identifier)

if err == nil {
return resource.RetryableError(fmt.Errorf("Waiting for server %q to be deleted", server.Identifier))
}
if err := waitForServerState(s, serverID, "stopped"); err != nil {
return err

if serr, ok := err.(api.ScalewayAPIError); ok {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

at some point we could consider an helper method like isRessouceMissingError(err) bool no ?

if serr.StatusCode == 404 {
return nil
}
}
}

if err := s.DeleteServer(serverID); err != 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
Expand Down
22 changes: 10 additions & 12 deletions builtin/providers/scaleway/resource_scaleway_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
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 {
return err
if s.State == "stopped" {
return deleteStoppedServer(scaleway, s)
}

d.SetId("")
return nil
err = deleteRunningServer(scaleway, s)

if err == nil {
d.SetId("")
}

return err
}