terraform/builtin/providers/aws/resource_aws_ebs_snapshot.go

146 lines
3.4 KiB
Go
Raw Normal View History

provider/aws: New Resource aws_ebs_snapshot (#10017) * provider/aws: Add ability to create aws_ebs_snapshot ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSEBSSnapshot_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/11/10 14:18:36 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSEBSSnapshot_ -timeout 120m === RUN TestAccAWSEBSSnapshot_basic --- PASS: TestAccAWSEBSSnapshot_basic (31.56s) === RUN TestAccAWSEBSSnapshot_withDescription --- PASS: TestAccAWSEBSSnapshot_withDescription (189.35s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws220.928s ``` * docs/aws: Addition of the docs for aws_ebs_snapshot resource * provider/aws: Creation of shared schema funcs for common AWS data source patterns * provider/aws: Create aws_ebs_snapshot datasource Fixes #8828 This data source will use a number of filters, owner_ids, snapshot_ids and restorable_by_user_ids in order to find the correct snapshot. The data source has no real use case for most_recent and will error on no snapshots found or greater than 1 snapshot found ``` % make testacc TEST=./builtin/providers/aws TESTARGS='-run=TestAccAWSEbsSnapshotDataSource_' ==> Checking that code complies with gofmt requirements... go generate $(go list ./... | grep -v /terraform/vendor/) 2016/11/10 14:34:33 Generated command/internal_plugin_list.go TF_ACC=1 go test ./builtin/providers/aws -v -run=TestAccAWSEbsSnapshotDataSource_ -timeout 120m === RUN TestAccAWSEbsSnapshotDataSource_basic --- PASS: TestAccAWSEbsSnapshotDataSource_basic (192.66s) === RUN TestAccAWSEbsSnapshotDataSource_multipleFilters --- PASS: TestAccAWSEbsSnapshotDataSource_multipleFilters (33.84s) PASS ok github.com/hashicorp/terraform/builtin/providers/aws226.522s ``` * docs/aws: Addition of docs for the aws_ebs_snapshot data source Adds the new resource `aws_ebs_snapshot`
2016-11-21 11:40:22 +01:00
package aws
import (
"fmt"
"log"
"time"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/helper/schema"
)
func resourceAwsEbsSnapshot() *schema.Resource {
return &schema.Resource{
Create: resourceAwsEbsSnapshotCreate,
Read: resourceAwsEbsSnapshotRead,
Delete: resourceAwsEbsSnapshotDelete,
Schema: map[string]*schema.Schema{
"volume_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
"description": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
},
"owner_id": {
Type: schema.TypeString,
Computed: true,
},
"owner_alias": {
Type: schema.TypeString,
Computed: true,
},
"encrypted": {
Type: schema.TypeBool,
Computed: true,
},
"volume_size": {
Type: schema.TypeInt,
Computed: true,
},
"kms_key_id": {
Type: schema.TypeString,
Computed: true,
},
"data_encryption_key_id": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceAwsEbsSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
request := &ec2.CreateSnapshotInput{
VolumeId: aws.String(d.Get("volume_id").(string)),
}
if v, ok := d.GetOk("description"); ok {
request.Description = aws.String(v.(string))
}
res, err := conn.CreateSnapshot(request)
if err != nil {
return err
}
d.SetId(*res.SnapshotId)
err = resourceAwsEbsSnapshotWaitForAvailable(d.Id(), conn)
if err != nil {
return err
}
return resourceAwsEbsSnapshotRead(d, meta)
}
func resourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
req := &ec2.DescribeSnapshotsInput{
SnapshotIds: []*string{aws.String(d.Id())},
}
res, err := conn.DescribeSnapshots(req)
if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSnapshotID.NotFound" {
log.Printf("Snapshot %q Not found - removing from state", d.Id())
d.SetId("")
return nil
}
snapshot := res.Snapshots[0]
d.Set("description", snapshot.Description)
d.Set("owner_id", snapshot.OwnerId)
d.Set("encrypted", snapshot.Encrypted)
d.Set("owner_alias", snapshot.OwnerAlias)
d.Set("volume_id", snapshot.VolumeId)
d.Set("data_encryption_key_id", snapshot.DataEncryptionKeyId)
d.Set("kms_keey_id", snapshot.KmsKeyId)
d.Set("volume_size", snapshot.VolumeSize)
return nil
}
func resourceAwsEbsSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
return resource.Retry(5*time.Minute, func() *resource.RetryError {
request := &ec2.DeleteSnapshotInput{
SnapshotId: aws.String(d.Id()),
}
_, err := conn.DeleteSnapshot(request)
if err == nil {
return nil
}
ebsErr, ok := err.(awserr.Error)
if ebsErr.Code() == "SnapshotInUse" {
return resource.RetryableError(fmt.Errorf("EBS SnapshotInUse - trying again while it detaches"))
}
if !ok {
return resource.NonRetryableError(err)
}
return resource.NonRetryableError(err)
})
}
func resourceAwsEbsSnapshotWaitForAvailable(id string, conn *ec2.EC2) error {
log.Printf("Waiting for Snapshot %s to become available...", id)
req := &ec2.DescribeSnapshotsInput{
SnapshotIds: []*string{aws.String(id)},
}
err := conn.WaitUntilSnapshotCompleted(req)
return err
}