github.com/danrjohnson/terraform@v0.7.0-rc2.0.20160627135212-d0fc1fa086ff/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: []string{"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 != nil { 108 if *pc.Status.Code == "failed" || *pc.Status.Code == "deleted" { 109 log.Printf("[DEBUG] VPC Peering Connect (%s) in state (%s), removing", d.Id(), *pc.Status.Code) 110 d.SetId("") 111 return nil 112 } 113 } 114 115 d.Set("accept_status", *pc.Status.Code) 116 d.Set("peer_owner_id", pc.AccepterVpcInfo.OwnerId) 117 d.Set("peer_vpc_id", pc.AccepterVpcInfo.VpcId) 118 d.Set("vpc_id", pc.RequesterVpcInfo.VpcId) 119 d.Set("tags", tagsToMap(pc.Tags)) 120 121 return nil 122 } 123 124 func resourceVPCPeeringConnectionAccept(conn *ec2.EC2, id string) (string, error) { 125 126 log.Printf("[INFO] Accept VPC Peering Connection with id: %s", id) 127 128 req := &ec2.AcceptVpcPeeringConnectionInput{ 129 VpcPeeringConnectionId: aws.String(id), 130 } 131 132 resp, err := conn.AcceptVpcPeeringConnection(req) 133 if err != nil { 134 return "", err 135 } 136 pc := resp.VpcPeeringConnection 137 return *pc.Status.Code, err 138 } 139 140 func resourceAwsVPCPeeringUpdate(d *schema.ResourceData, meta interface{}) error { 141 conn := meta.(*AWSClient).ec2conn 142 143 if err := setTags(conn, d); err != nil { 144 return err 145 } else { 146 d.SetPartial("tags") 147 } 148 149 if _, ok := d.GetOk("auto_accept"); ok { 150 pcRaw, _, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())() 151 152 if err != nil { 153 return err 154 } 155 if pcRaw == nil { 156 d.SetId("") 157 return nil 158 } 159 pc := pcRaw.(*ec2.VpcPeeringConnection) 160 161 if pc.Status != nil && *pc.Status.Code == "pending-acceptance" { 162 status, err := resourceVPCPeeringConnectionAccept(conn, d.Id()) 163 if err != nil { 164 return err 165 } 166 log.Printf( 167 "[DEBUG] VPC Peering connection accept status: %s", 168 status) 169 } 170 } 171 172 return resourceAwsVPCPeeringRead(d, meta) 173 } 174 175 func resourceAwsVPCPeeringDelete(d *schema.ResourceData, meta interface{}) error { 176 conn := meta.(*AWSClient).ec2conn 177 178 _, err := conn.DeleteVpcPeeringConnection( 179 &ec2.DeleteVpcPeeringConnectionInput{ 180 VpcPeeringConnectionId: aws.String(d.Id()), 181 }) 182 return err 183 } 184 185 // resourceAwsVPCPeeringConnectionStateRefreshFunc returns a resource.StateRefreshFunc that is used to watch 186 // a VPCPeeringConnection. 187 func resourceAwsVPCPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc { 188 return func() (interface{}, string, error) { 189 190 resp, err := conn.DescribeVpcPeeringConnections(&ec2.DescribeVpcPeeringConnectionsInput{ 191 VpcPeeringConnectionIds: []*string{aws.String(id)}, 192 }) 193 if err != nil { 194 if ec2err, ok := err.(awserr.Error); ok && ec2err.Code() == "InvalidVpcPeeringConnectionID.NotFound" { 195 resp = nil 196 } else { 197 log.Printf("Error on VPCPeeringConnectionStateRefresh: %s", err) 198 return nil, "", err 199 } 200 } 201 202 if resp == nil { 203 // Sometimes AWS just has consistency issues and doesn't see 204 // our instance yet. Return an empty state. 205 return nil, "", nil 206 } 207 208 pc := resp.VpcPeeringConnections[0] 209 210 return pc, *pc.Status.Code, nil 211 } 212 }