github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/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 return &schema.Resource{ 28 Create: resourceAwsAmiCopyCreate, 29 30 Schema: resourceSchema, 31 32 // The remaining operations are shared with the generic aws_ami resource, 33 // since the aws_ami_copy resource only differs in how it's created. 34 Read: resourceAwsAmiRead, 35 Update: resourceAwsAmiUpdate, 36 Delete: resourceAwsAmiDelete, 37 } 38 } 39 40 func resourceAwsAmiCopyCreate(d *schema.ResourceData, meta interface{}) error { 41 client := meta.(*AWSClient).ec2conn 42 43 req := &ec2.CopyImageInput{ 44 Name: aws.String(d.Get("name").(string)), 45 Description: aws.String(d.Get("description").(string)), 46 SourceImageId: aws.String(d.Get("source_ami_id").(string)), 47 SourceRegion: aws.String(d.Get("source_ami_region").(string)), 48 } 49 50 res, err := client.CopyImage(req) 51 if err != nil { 52 return err 53 } 54 55 id := *res.ImageId 56 d.SetId(id) 57 d.Partial(true) // make sure we record the id even if the rest of this gets interrupted 58 d.Set("id", id) 59 d.Set("manage_ebs_snapshots", true) 60 d.SetPartial("id") 61 d.SetPartial("manage_ebs_snapshots") 62 d.Partial(false) 63 64 _, err = resourceAwsAmiWaitForAvailable(id, client) 65 if err != nil { 66 return err 67 } 68 69 return resourceAwsAmiUpdate(d, meta) 70 }