2016-07-13 22:03:41 +02:00
|
|
|
package scaleway
|
|
|
|
|
|
|
|
import (
|
2016-10-27 13:37:40 +02:00
|
|
|
"fmt"
|
2016-07-13 22:03:41 +02:00
|
|
|
"time"
|
|
|
|
|
2016-10-27 13:37:40 +02:00
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
2016-07-13 22:03:41 +02:00
|
|
|
"github.com/scaleway/scaleway-cli/pkg/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
// Bool returns a pointer to of the bool value passed in.
|
|
|
|
func Bool(val bool) *bool {
|
|
|
|
return &val
|
|
|
|
}
|
|
|
|
|
|
|
|
// String returns a pointer to of the string value passed in.
|
|
|
|
func String(val string) *string {
|
|
|
|
return &val
|
|
|
|
}
|
|
|
|
|
2016-10-29 13:07:35 +02:00
|
|
|
func validateVolumeType(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(string)
|
|
|
|
if value != "l_ssd" {
|
|
|
|
errors = append(errors, fmt.Errorf("%q must be l_ssd", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func validateVolumeSize(v interface{}, k string) (ws []string, errors []error) {
|
|
|
|
value := v.(int)
|
|
|
|
if value < 1 || value > 150 {
|
|
|
|
errors = append(errors, fmt.Errorf("%q be more than 1 and less than 150", k))
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:37:40 +02:00
|
|
|
// 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")
|
2016-07-13 22:03:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2016-10-27 13:37:40 +02:00
|
|
|
if serr, ok := err.(api.ScalewayAPIError); ok {
|
|
|
|
if serr.StatusCode == 404 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-07-13 22:03:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2016-10-27 17:51:34 +02:00
|
|
|
return resource.Retry(20*time.Minute, func() *resource.RetryError {
|
2016-10-27 13:37:40 +02:00
|
|
|
_, err := scaleway.GetServer(server.Identifier)
|
|
|
|
|
|
|
|
if err == nil {
|
|
|
|
return resource.RetryableError(fmt.Errorf("Waiting for server %q to be deleted", server.Identifier))
|
2016-07-13 22:03:41 +02:00
|
|
|
}
|
2016-10-27 13:37:40 +02:00
|
|
|
|
|
|
|
if serr, ok := err.(api.ScalewayAPIError); ok {
|
|
|
|
if serr.StatusCode == 404 {
|
|
|
|
return nil
|
|
|
|
}
|
2016-07-13 22:03:41 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 13:37:40 +02:00
|
|
|
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 {
|
2016-07-13 22:03:41 +02:00
|
|
|
return err
|
|
|
|
}
|
2016-10-27 13:37:40 +02:00
|
|
|
|
2016-07-13 22:03:41 +02:00
|
|
|
if rootVolume, ok := server.Volumes["0"]; ok {
|
2016-10-27 13:37:40 +02:00
|
|
|
if err := scaleway.DeleteVolume(rootVolume.Identifier); err != nil {
|
2016-07-13 22:03:41 +02:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2016-10-27 13:37:40 +02:00
|
|
|
// 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
|
|
|
|
|
2016-10-27 17:51:34 +02:00
|
|
|
func waitForServerState(scaleway *api.ScalewayAPI, serverID, targetState string) error {
|
2016-11-17 14:08:05 +01:00
|
|
|
return resource.Retry(60*time.Minute, func() *resource.RetryError {
|
|
|
|
scaleway.ClearCache()
|
|
|
|
|
2016-10-27 17:51:34 +02:00
|
|
|
s, err := scaleway.GetServer(serverID)
|
2016-07-13 22:03:41 +02:00
|
|
|
|
|
|
|
if err != nil {
|
2016-10-27 17:51:34 +02:00
|
|
|
return resource.NonRetryableError(err)
|
2016-07-13 22:03:41 +02:00
|
|
|
}
|
2016-10-27 17:51:34 +02:00
|
|
|
|
|
|
|
if s.State != targetState {
|
|
|
|
return resource.RetryableError(fmt.Errorf("Waiting for server to enter %q state", targetState))
|
2016-07-13 22:03:41 +02:00
|
|
|
}
|
|
|
|
|
2016-10-27 17:51:34 +02:00
|
|
|
return nil
|
|
|
|
})
|
2016-07-13 22:03:41 +02:00
|
|
|
}
|