github.com/mohanarpit/terraform@v0.6.16-0.20160909104007-291f29853544/builtin/providers/aws/resource_aws_ami_copy.go (about)

     1  package aws
     2  
     3  import (
     4  	"github.com/aws/aws-sdk-go/aws"
     5  	"github.com/aws/aws-sdk-go/service/ec2"
     6  
     7  	"github.com/hashicorp/terraform/helper/schema"
     8  )
     9  
    10  func resourceAwsAmiCopy() *schema.Resource {
    11  	// Inherit all of the common AMI attributes from aws_ami, since we're
    12  	// implicitly creating an aws_ami resource.
    13  	resourceSchema := resourceAwsAmiCommonSchema(true)
    14  
    15  	// Additional attributes unique to the copy operation.
    16  	resourceSchema["source_ami_id"] = &schema.Schema{
    17  		Type:     schema.TypeString,
    18  		Required: true,
    19  		ForceNew: true,
    20  	}
    21  	resourceSchema["source_ami_region"] = &schema.Schema{
    22  		Type:     schema.TypeString,
    23  		Required: true,
    24  		ForceNew: true,
    25  	}
    26  
    27  	resourceSchema["encrypted"] = &schema.Schema{
    28  		Type:     schema.TypeBool,
    29  		Optional: true,
    30  		Default:  false,
    31  		ForceNew: true,
    32  	}
    33  
    34  	resourceSchema["kms_key_id"] = &schema.Schema{
    35  		Type:     schema.TypeString,
    36  		Optional: true,
    37  		Computed: true,
    38  		ForceNew: true,
    39  	}
    40  
    41  	return &schema.Resource{
    42  		Create: resourceAwsAmiCopyCreate,
    43  
    44  		Schema: resourceSchema,
    45  
    46  		// The remaining operations are shared with the generic aws_ami resource,
    47  		// since the aws_ami_copy resource only differs in how it's created.
    48  		Read:   resourceAwsAmiRead,
    49  		Update: resourceAwsAmiUpdate,
    50  		Delete: resourceAwsAmiDelete,
    51  	}
    52  }
    53  
    54  func resourceAwsAmiCopyCreate(d *schema.ResourceData, meta interface{}) error {
    55  	client := meta.(*AWSClient).ec2conn
    56  
    57  	req := &ec2.CopyImageInput{
    58  		Name:          aws.String(d.Get("name").(string)),
    59  		Description:   aws.String(d.Get("description").(string)),
    60  		SourceImageId: aws.String(d.Get("source_ami_id").(string)),
    61  		SourceRegion:  aws.String(d.Get("source_ami_region").(string)),
    62  		Encrypted:     aws.Bool(d.Get("encrypted").(bool)),
    63  	}
    64  
    65  	if v, ok := d.GetOk("kms_key_id"); ok {
    66  		req.KmsKeyId = aws.String(v.(string))
    67  	}
    68  
    69  	res, err := client.CopyImage(req)
    70  	if err != nil {
    71  		return err
    72  	}
    73  
    74  	id := *res.ImageId
    75  	d.SetId(id)
    76  	d.Partial(true) // make sure we record the id even if the rest of this gets interrupted
    77  	d.Set("id", id)
    78  	d.Set("manage_ebs_snapshots", true)
    79  	d.SetPartial("id")
    80  	d.SetPartial("manage_ebs_snapshots")
    81  	d.Partial(false)
    82  
    83  	_, err = resourceAwsAmiWaitForAvailable(id, client)
    84  	if err != nil {
    85  		return err
    86  	}
    87  
    88  	return resourceAwsAmiUpdate(d, meta)
    89  }