github.com/turtlemonvh/terraform@v0.6.9-0.20151204001754-8e40b6b855e8/builtin/providers/aws/resource_aws_vpc_peering_connection.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 resourceAwsVpcPeeringConnection() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceAwsVPCPeeringCreate, 18 Read: resourceAwsVPCPeeringRead, 19 Update: resourceAwsVPCPeeringUpdate, 20 Delete: resourceAwsVPCPeeringDelete, 21 22 Schema: map[string]*schema.Schema{ 23 "peer_owner_id": &schema.Schema{ 24 Type: schema.TypeString, 25 Required: true, 26 ForceNew: true, 27 DefaultFunc: schema.EnvDefaultFunc("AWS_ACCOUNT_ID", nil), 28 }, 29 "peer_vpc_id": &schema.Schema{ 30 Type: schema.TypeString, 31 Required: true, 32 ForceNew: true, 33 }, 34 "vpc_id": &schema.Schema{ 35 Type: schema.TypeString, 36 Required: true, 37 ForceNew: true, 38 }, 39 "auto_accept": &schema.Schema{ 40 Type: schema.TypeBool, 41 Optional: true, 42 }, 43 "accept_status": &schema.Schema{ 44 Type: schema.TypeString, 45 Computed: true, 46 }, 47 "tags": tagsSchema(), 48 }, 49 } 50 } 51 52 func resourceAwsVPCPeeringCreate(d *schema.ResourceData, meta interface{}) error { 53 conn := meta.(*AWSClient).ec2conn 54 55 // Create the vpc peering connection 56 createOpts := &ec2.CreateVpcPeeringConnectionInput{ 57 PeerOwnerId: aws.String(d.Get("peer_owner_id").(string)), 58 PeerVpcId: aws.String(d.Get("peer_vpc_id").(string)), 59 VpcId: aws.String(d.Get("vpc_id").(string)), 60 } 61 log.Printf("[DEBUG] VPCPeeringCreate create config: %#v", createOpts) 62 resp, err := conn.CreateVpcPeeringConnection(createOpts) 63 if err != nil { 64 return fmt.Errorf("Error creating vpc peering connection: %s", err) 65 } 66 67 // Get the ID and store it 68 rt := resp.VpcPeeringConnection 69 d.SetId(*rt.VpcPeeringConnectionId) 70 log.Printf("[INFO] VPC Peering Connection ID: %s", d.Id()) 71 72 // Wait for the vpc peering connection to become available 73 log.Printf( 74 "[DEBUG] Waiting for vpc peering connection (%s) to become available", 75 d.Id()) 76 stateConf := &resource.StateChangeConf{ 77 Pending: []string{"pending"}, 78 Target: "pending-acceptance", 79 Refresh: resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id()), 80 Timeout: 1 * time.Minute, 81 } 82 if _, err := stateConf.WaitForState(); err != nil { 83 return fmt.Errorf( 84 "Error waiting for vpc peering (%s) to become available: %s", 85 d.Id(), err) 86 } 87 88 return resourceAwsVPCPeeringUpdate(d, meta) 89 } 90 91 func resourceAwsVPCPeeringRead(d *schema.ResourceData, meta interface{}) error { 92 conn := meta.(*AWSClient).ec2conn 93 pcRaw, _, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())() 94 if err != nil { 95 return err 96 } 97 if pcRaw == nil { 98 d.SetId("") 99 return nil 100 } 101 102 pc := pcRaw.(*ec2.VpcPeeringConnection) 103 104 // The failed status is a status that we can assume just means the 105 // connection is gone. Destruction isn't allowed, and it eventually 106 // just "falls off" the console. See GH-2322 107 if *pc.Status.Code == "failed" { 108 d.SetId("") 109 return nil 110 } 111 112 d.Set("accept_status", *pc.Status.Code) 113 d.Set("peer_owner_id", pc.AccepterVpcInfo.OwnerId) 114 d.Set("peer_vpc_id", pc.AccepterVpcInfo.VpcId) 115 d.Set("vpc_id", pc.RequesterVpcInfo.VpcId) 116 d.Set("tags", tagsToMap(pc.Tags)) 117 118 return nil 119 } 120 121 func resourceVPCPeeringConnectionAccept(conn *ec2.EC2, id string) (string, error) { 122 123 log.Printf("[INFO] Accept VPC Peering Connection with id: %s", id) 124 125 req := &ec2.AcceptVpcPeeringConnectionInput{ 126 VpcPeeringConnectionId: aws.String(id), 127 } 128 129 resp, err := conn.AcceptVpcPeeringConnection(req) 130 if err != nil { 131 return "", err 132 } 133 pc := resp.VpcPeeringConnection 134 return *pc.Status.Code, err 135 } 136 137 func resourceAwsVPCPeeringUpdate(d *schema.ResourceData, meta interface{}) error { 138 conn := meta.(*AWSClient).ec2conn 139 140 if err := setTags(conn, d); err != nil { 141 return err 142 } else { 143 d.SetPartial("tags") 144 } 145 146 if _, ok := d.GetOk("auto_accept"); ok { 147 148 pcRaw, _, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())() 149 150 if err != nil { 151 return err 152 } 153 if pcRaw == nil { 154 d.SetId("") 155 return nil 156 } 157 pc := pcRaw.(*ec2.VpcPeeringConnection) 158 159 if pc.Status != nil && *pc.Status.Code == "pending-acceptance" { 160 161 status, err := resourceVPCPeeringConnectionAccept(conn, d.Id()) 162 if err != nil { 163 return err 164 } 165 log.Printf( 166 "[DEBUG] VPC Peering connection accept status: %s", 167 status) 168 } 169 } 170 171 return resourceAwsVPCPeeringRead(d, meta) 172 } 173 174 func resourceAwsVPCPeeringDelete(d *schema.ResourceData, meta interface{}) error { 175 conn := meta.(*AWSClient).ec2conn 176 177 _, err := conn.DeleteVpcPeeringConnection( 178 &ec2.DeleteVpcPeeringConnectionInput{ 179 VpcPeeringConnectionId: aws.String(d.Id()), 180 }) 181 return err 182 } 183 184 // resourceAwsVPCPeeringConnectionStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch 185 // a VPCPeeringConnection. 186 func resourceAwsVPCPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc { 187 return func() (interface{}, string, error) { 188 189 resp, err := conn.DescribeVpcPeeringConnections(&ec2.DescribeVpcPeeringConnectionsInput{ 190 VpcPeeringConnectionIds: []*string{aws.String(id)}, 191 }) 192 if err != nil { 193 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpcPeeringConnectionID.NotFound" { 194 resp = nil 195 } else { 196 log.Printf("Error on VPCPeeringConnectionStateRefresh: %s", err) 197 return nil, "", err 198 } 199 } 200 201 if resp == nil { 202 // Sometimes AWS just has consistency issues and doesn't see 203 // our instance yet. Return an empty state. 204 return nil, "", nil 205 } 206 207 pc := resp.VpcPeeringConnections[0] 208 209 return pc, *pc.Status.Code, nil 210 } 211 }