github.com/vtorhonen/terraform@v0.9.0-beta2.0.20170307220345-5d894e4ffda7/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  		ValidateFunc: validateArn,
    40  	}
    41  
    42  	return &schema.Resource{
    43  		Create: resourceAwsAmiCopyCreate,
    44  
    45  		Schema: resourceSchema,
    46  
    47  		// The remaining operations are shared with the generic aws_ami resource,
    48  		// since the aws_ami_copy resource only differs in how it's created.
    49  		Read:   resourceAwsAmiRead,
    50  		Update: resourceAwsAmiUpdate,
    51  		Delete: resourceAwsAmiDelete,
    52  	}
    53  }
    54  
    55  func resourceAwsAmiCopyCreate(d *schema.ResourceData, meta interface{}) error {
    56  	client := meta.(*AWSClient).ec2conn
    57  
    58  	req := &ec2.CopyImageInput{
    59  		Name:          aws.String(d.Get("name").(string)),
    60  		Description:   aws.String(d.Get("description").(string)),
    61  		SourceImageId: aws.String(d.Get("source_ami_id").(string)),
    62  		SourceRegion:  aws.String(d.Get("source_ami_region").(string)),
    63  		Encrypted:     aws.Bool(d.Get("encrypted").(bool)),
    64  	}
    65  
    66  	if v, ok := d.GetOk("kms_key_id"); ok {
    67  		req.KmsKeyId = aws.String(v.(string))
    68  	}
    69  
    70  	res, err := client.CopyImage(req)
    71  	if err != nil {
    72  		return err
    73  	}
    74  
    75  	id := *res.ImageId
    76  	d.SetId(id)
    77  	d.Partial(true) // make sure we record the id even if the rest of this gets interrupted
    78  	d.Set("id", id)
    79  	d.Set("manage_ebs_snapshots", true)
    80  	d.SetPartial("id")
    81  	d.SetPartial("manage_ebs_snapshots")
    82  	d.Partial(false)
    83  
    84  	_, err = resourceAwsAmiWaitForAvailable(id, client)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	return resourceAwsAmiUpdate(d, meta)
    90  }