github.com/recobe182/terraform@v0.8.5-0.20170117231232-49ab22a935b7/builtin/providers/aws/resource_aws_ebs_snapshot.go (about)

     1  package aws
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"time"
     7  
     8  	"github.com/aws/aws-sdk-go/aws"
     9  	"github.com/aws/aws-sdk-go/aws/awserr"
    10  	"github.com/aws/aws-sdk-go/service/ec2"
    11  	"github.com/hashicorp/terraform/helper/resource"
    12  	"github.com/hashicorp/terraform/helper/schema"
    13  )
    14  
    15  func resourceAwsEbsSnapshot() *schema.Resource {
    16  	return &schema.Resource{
    17  		Create: resourceAwsEbsSnapshotCreate,
    18  		Read:   resourceAwsEbsSnapshotRead,
    19  		Delete: resourceAwsEbsSnapshotDelete,
    20  
    21  		Schema: map[string]*schema.Schema{
    22  			"volume_id": {
    23  				Type:     schema.TypeString,
    24  				Required: true,
    25  				ForceNew: true,
    26  			},
    27  			"description": {
    28  				Type:     schema.TypeString,
    29  				Optional: true,
    30  				ForceNew: true,
    31  			},
    32  			"owner_id": {
    33  				Type:     schema.TypeString,
    34  				Computed: true,
    35  			},
    36  			"owner_alias": {
    37  				Type:     schema.TypeString,
    38  				Computed: true,
    39  			},
    40  			"encrypted": {
    41  				Type:     schema.TypeBool,
    42  				Computed: true,
    43  			},
    44  			"volume_size": {
    45  				Type:     schema.TypeInt,
    46  				Computed: true,
    47  			},
    48  			"kms_key_id": {
    49  				Type:     schema.TypeString,
    50  				Computed: true,
    51  			},
    52  			"data_encryption_key_id": {
    53  				Type:     schema.TypeString,
    54  				Computed: true,
    55  			},
    56  		},
    57  	}
    58  }
    59  
    60  func resourceAwsEbsSnapshotCreate(d *schema.ResourceData, meta interface{}) error {
    61  	conn := meta.(*AWSClient).ec2conn
    62  
    63  	request := &ec2.CreateSnapshotInput{
    64  		VolumeId: aws.String(d.Get("volume_id").(string)),
    65  	}
    66  	if v, ok := d.GetOk("description"); ok {
    67  		request.Description = aws.String(v.(string))
    68  	}
    69  
    70  	res, err := conn.CreateSnapshot(request)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	d.SetId(*res.SnapshotId)
    76  
    77  	err = resourceAwsEbsSnapshotWaitForAvailable(d.Id(), conn)
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	return resourceAwsEbsSnapshotRead(d, meta)
    83  }
    84  
    85  func resourceAwsEbsSnapshotRead(d *schema.ResourceData, meta interface{}) error {
    86  	conn := meta.(*AWSClient).ec2conn
    87  
    88  	req := &ec2.DescribeSnapshotsInput{
    89  		SnapshotIds: []*string{aws.String(d.Id())},
    90  	}
    91  	res, err := conn.DescribeSnapshots(req)
    92  	if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidSnapshotID.NotFound" {
    93  		log.Printf("Snapshot %q Not found - removing from state", d.Id())
    94  		d.SetId("")
    95  		return nil
    96  	}
    97  
    98  	snapshot := res.Snapshots[0]
    99  
   100  	d.Set("description", snapshot.Description)
   101  	d.Set("owner_id", snapshot.OwnerId)
   102  	d.Set("encrypted", snapshot.Encrypted)
   103  	d.Set("owner_alias", snapshot.OwnerAlias)
   104  	d.Set("volume_id", snapshot.VolumeId)
   105  	d.Set("data_encryption_key_id", snapshot.DataEncryptionKeyId)
   106  	d.Set("kms_keey_id", snapshot.KmsKeyId)
   107  	d.Set("volume_size", snapshot.VolumeSize)
   108  
   109  	return nil
   110  }
   111  
   112  func resourceAwsEbsSnapshotDelete(d *schema.ResourceData, meta interface{}) error {
   113  	conn := meta.(*AWSClient).ec2conn
   114  
   115  	return resource.Retry(5*time.Minute, func() *resource.RetryError {
   116  		request := &ec2.DeleteSnapshotInput{
   117  			SnapshotId: aws.String(d.Id()),
   118  		}
   119  		_, err := conn.DeleteSnapshot(request)
   120  		if err == nil {
   121  			return nil
   122  		}
   123  
   124  		ebsErr, ok := err.(awserr.Error)
   125  		if ebsErr.Code() == "SnapshotInUse" {
   126  			return resource.RetryableError(fmt.Errorf("EBS SnapshotInUse - trying again while it detaches"))
   127  		}
   128  
   129  		if !ok {
   130  			return resource.NonRetryableError(err)
   131  		}
   132  
   133  		return resource.NonRetryableError(err)
   134  	})
   135  }
   136  
   137  func resourceAwsEbsSnapshotWaitForAvailable(id string, conn *ec2.EC2) error {
   138  	log.Printf("Waiting for Snapshot %s to become available...", id)
   139  
   140  	req := &ec2.DescribeSnapshotsInput{
   141  		SnapshotIds: []*string{aws.String(id)},
   142  	}
   143  	err := conn.WaitUntilSnapshotCompleted(req)
   144  	return err
   145  }