mirror of
https://github.com/opentofu/opentofu.git
synced 2025-01-18 12:42:58 -06:00
d9a2e0dbb3
* provider/scaleway: fix scaleway_volume_attachment with count > 1 since scaleway requires servers to be powered off to attach volumes to, we need to make sure that we don't power down a server twice, or power up a server while it's supposed to be modified. sadly terraform doesn't seem to sport serialization primitives for usecases like this, but putting the code in question behind a `sync.Mutex` does the trick, too fixes #9417 * provider/scaleway: use mutexkv to lock per-resource following @dcharbonnier suggestion. thanks! * provider/scaleway: cleanup waitForServerState signature * provider/scaleway: store serverID in var * provider/scaleway: correct imports * provider/scaleway: increase timeouts
85 lines
2.1 KiB
Go
85 lines
2.1 KiB
Go
package scaleway
|
|
|
|
import (
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/hashicorp/terraform/helper/resource"
|
|
"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
|
|
}
|
|
|
|
// 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")
|
|
|
|
if err != nil {
|
|
if serr, ok := err.(api.ScalewayAPIError); ok {
|
|
if serr.StatusCode == 404 {
|
|
return nil
|
|
}
|
|
}
|
|
|
|
return err
|
|
}
|
|
|
|
return resource.Retry(20*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 := 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(scaleway *api.ScalewayAPI, serverID, targetState string) error {
|
|
return resource.Retry(20*time.Minute, func() *resource.RetryError {
|
|
s, err := scaleway.GetServer(serverID)
|
|
|
|
if err != nil {
|
|
return resource.NonRetryableError(err)
|
|
}
|
|
|
|
if s.State != targetState {
|
|
return resource.RetryableError(fmt.Errorf("Waiting for server to enter %q state", targetState))
|
|
}
|
|
|
|
return nil
|
|
})
|
|
}
|