add wait after AWS snapshot attr modification

This adds up to a 5 minute wait after issuing an add or remove request
to adjust a snapshot's createVolumePermission attribute.
This commit is contained in:
Jeremy Asher 2016-11-10 14:11:49 -08:00 committed by clint shryock
parent f20d1c3caa
commit 185ee439da
1 changed files with 60 additions and 12 deletions

View File

@ -2,8 +2,11 @@ package aws
import ( import (
"fmt" "fmt"
"time"
"github.com/aws/aws-sdk-go/aws" "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ec2" "github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema" "github.com/hashicorp/terraform/helper/schema"
) )
@ -53,10 +56,26 @@ func resourceAwsSnapshotCreateVolumePermissionCreate(d *schema.ResourceData, met
}, },
}) })
if err != nil { if err != nil {
return fmt.Errorf("error creating snapshot volume permission: %s", err) return fmt.Errorf("Error adding snapshot createVolumePermission: %s", err)
} }
d.SetId(fmt.Sprintf("%s-%s", snapshot_id, account_id)) d.SetId(fmt.Sprintf("%s-%s", snapshot_id, account_id))
// Wait for the account to appear in the permission list
stateConf := &resource.StateChangeConf{
Pending: []string{"denied"},
Target: []string{"granted"},
Refresh: resourceAwsSnapshotCreateVolumePermissionStateRefreshFunc(conn, snapshot_id, account_id),
Timeout: 5 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf(
"Error waiting for snapshot createVolumePermission (%s) to be added: %s",
d.Id(), err)
}
return nil return nil
} }
@ -80,25 +99,54 @@ func resourceAwsSnapshotCreateVolumePermissionDelete(d *schema.ResourceData, met
}, },
}) })
if err != nil { if err != nil {
return fmt.Errorf("error removing snapshot volume permission: %s", err) return fmt.Errorf("Error removing snapshot createVolumePermission: %s", err)
}
// Wait for the account to disappear from the permission list
stateConf := &resource.StateChangeConf{
Pending: []string{"granted"},
Target: []string{"denied"},
Refresh: resourceAwsSnapshotCreateVolumePermissionStateRefreshFunc(conn, snapshot_id, account_id),
Timeout: 5 * time.Minute,
Delay: 10 * time.Second,
MinTimeout: 10 * time.Second,
}
if _, err := stateConf.WaitForState(); err != nil {
return fmt.Errorf(
"Error waiting for snapshot createVolumePermission (%s) to be removed: %s",
d.Id(), err)
} }
return nil return nil
} }
func hasCreateVolumePermission(conn *ec2.EC2, snapshot_id string, account_id string) (bool, error) { func hasCreateVolumePermission(conn *ec2.EC2, snapshot_id string, account_id string) (bool, error) {
_, state, err := resourceAwsSnapshotCreateVolumePermissionStateRefreshFunc(conn, snapshot_id, account_id)()
if err != nil {
return false, err
}
if state == "granted" {
return true, nil
} else {
return false, nil
}
}
func resourceAwsSnapshotCreateVolumePermissionStateRefreshFunc(conn *ec2.EC2, snapshot_id string, account_id string) resource.StateRefreshFunc {
return func() (interface{}, string, error) {
attrs, err := conn.DescribeSnapshotAttribute(&ec2.DescribeSnapshotAttributeInput{ attrs, err := conn.DescribeSnapshotAttribute(&ec2.DescribeSnapshotAttributeInput{
SnapshotId: aws.String(snapshot_id), SnapshotId: aws.String(snapshot_id),
Attribute: aws.String("createVolumePermission"), Attribute: aws.String("createVolumePermission"),
}) })
if err != nil { if err != nil {
return false, err return nil, "", fmt.Errorf("Error refreshing snapshot createVolumePermission state: %s", err)
} }
for _, vp := range attrs.CreateVolumePermissions { for _, vp := range attrs.CreateVolumePermissions {
if *vp.UserId == account_id { if *vp.UserId == account_id {
return true, nil return attrs, "granted", nil
} }
} }
return false, nil return attrs, "denied", nil
}
} }