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

fix(volume_attachment): fix volume_attachment deletion #13

Merged
merged 1 commit into from
Jul 3, 2017
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
3 changes: 0 additions & 3 deletions scaleway/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package scaleway
import (
"sync"

"github.com/hashicorp/terraform/helper/mutexkv"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/terraform/terraform"
)
Expand Down Expand Up @@ -62,8 +61,6 @@ func Provider() terraform.ResourceProvider {
}
}

var scalewayMutexKV = mutexkv.NewMutexKV()

func providerConfigure(d *schema.ResourceData) (interface{}, error) {
apiKey := ""
if v, ok := d.Get("token").(string); ok {
Expand Down
24 changes: 9 additions & 15 deletions scaleway/resource_volume_attachment.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ func resourceScalewayVolumeAttachment() *schema.Resource {
var errVolumeAlreadyAttached = fmt.Errorf("Scaleway volume already attached")

func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{}) error {
mu.Lock()
defer mu.Unlock()

scaleway := m.(*Client).scaleway

vol, err := scaleway.GetVolume(d.Get("volume").(string))
Expand All @@ -46,10 +49,7 @@ func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{
return errVolumeAlreadyAttached
}

// guard against server shutdown/ startup race conditiond
serverID := d.Get("server").(string)
scalewayMutexKV.Lock(serverID)
defer scalewayMutexKV.Unlock(serverID)

server, err := scaleway.GetServer(serverID)
if err != nil {
Expand Down Expand Up @@ -90,14 +90,11 @@ func resourceScalewayVolumeAttachmentCreate(d *schema.ResourceData, m interface{
volumes[k] = v
}

if err := resource.Retry(5*time.Minute, func() *resource.RetryError {

if err := resource.Retry(serverWaitTimeout, func() *resource.RetryError {
var req = api.ScalewayServerPatchDefinition{
Volumes: &volumes,
}
mu.Lock()
err := scaleway.PatchServer(serverID, req)
mu.Unlock()

if err == nil {
return nil
Expand Down Expand Up @@ -169,18 +166,17 @@ func resourceScalewayVolumeAttachmentRead(d *schema.ResourceData, m interface{})
return nil
}

func resourceScalewayVolumeAttachmentDelete(d *schema.ResourceData, m interface{}) error {
scaleway := m.(*Client).scaleway
const serverWaitTimeout = 5 * time.Minute

func resourceScalewayVolumeAttachmentDelete(d *schema.ResourceData, m interface{}) error {
mu.Lock()
defer mu.Unlock()

scaleway := m.(*Client).scaleway

var startServerAgain = false

// guard against server shutdown/ startup race conditiond
serverID := d.Get("server").(string)
scalewayMutexKV.Lock(serverID)
defer scalewayMutexKV.Unlock(serverID)

server, err := scaleway.GetServer(serverID)
if err != nil {
Expand Down Expand Up @@ -218,13 +214,11 @@ func resourceScalewayVolumeAttachmentDelete(d *schema.ResourceData, m interface{
volumes[k] = v
}

if err := resource.Retry(5*time.Minute, func() *resource.RetryError {
if err := resource.Retry(serverWaitTimeout, func() *resource.RetryError {
var req = api.ScalewayServerPatchDefinition{
Volumes: &volumes,
}
mu.Lock()
err := scaleway.PatchServer(serverID, req)
mu.Unlock()

if err == nil {
return nil
Expand Down