diff --git a/builtin/providers/aws/resource_aws_vpc_peering_connection.go b/builtin/providers/aws/resource_aws_vpc_peering_connection.go index 0c8001c4d..a0ff9a331 100644 --- a/builtin/providers/aws/resource_aws_vpc_peering_connection.go +++ b/builtin/providers/aws/resource_aws_vpc_peering_connection.go @@ -74,8 +74,8 @@ func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error d.Id()) stateConf := &resource.StateChangeConf{ Pending: []string{"pending"}, - Target: "ready", - Refresh: resourceAwsVpcPeeringConnectionStateRefreshFunc(conn, d.Id()), + Target: "pending-acceptance", + Refresh: resourceAwsVpcPeeringConnectionStateRefreshFunc(ec2conn, d.Id()), Timeout: 1 * time.Minute, } if _, err := stateConf.WaitForState(); err != nil { @@ -100,38 +100,26 @@ func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error { pc := pcRaw.(*ec2.VPCPeeringConnection) - code := pc.Status.Code - if _, ok := d.GetOk("auto_accept"); ok { - updatedCode, err := resourceVpcPeeringConnectionAccept(ec2conn, pc, d.Id()) - if err != nil { - return fmt.Errorf("Error accepting vpc peering connection: %s", err) - } - - code = updatedCode - } - - d.Set("accept_status", code) - - d.Set("peer_owner_id", pc.AccepterVpcInfo.OwnerId) - d.Set("peer_vpc_id", pc.AccepterVpcInfo.VpcId) - d.Set("vpc_id", pc.RequesterVpcInfo.VpcId) - d.Set("tags", tagsToMap(pc.Tags)) + d.Set("accept_status", *pc.Status.Code) + d.Set("peer_owner_id", pc.AccepterVPCInfo.OwnerID) + d.Set("peer_vpc_id", pc.AccepterVPCInfo.VPCID) + d.Set("vpc_id", pc.RequesterVPCInfo.VPCID) + d.Set("tags", tagsToMapSDK(pc.Tags)) return nil } -func resourceVpcPeeringConnectionAccept(conn *ec2.EC2, oldPc *ec2.VpcPeeringConnection, id string) (string, error) { - //func resourceVpcPeeringConnectionAccept(conn *ec2.EC2, oldPc *ec2.VpcPeeringConnection, d *schema.ResourceData) error { - if oldPc.Status.Code == "pending-acceptance" { - log.Printf("[INFO] Accept Vpc Peering Connection with id: %s", id) - _, err := conn.AcceptVpcPeeringConnection(id) +func resourceVpcPeeringConnectionAccept(conn *ec2.EC2, id string) (string, error) { - pcRaw, _, err := resourceAwsVpcPeeringConnectionStateRefreshFunc(conn, id)() - pc := pcRaw.(*ec2.VpcPeeringConnection) - return pc.Status.Code, err + log.Printf("[INFO] Accept Vpc Peering Connection with id: %s", id) + + req := &ec2.AcceptVPCPeeringConnectionRequest{ + VPCPeeringConnectionID: aws.String(id), } - return oldPc.Status.Code, nil + resp, err := conn.AcceptVPCPeeringConnection(req) + pc := resp.VPCPeeringConnection + return *pc.Status.Code, err } func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error { @@ -143,6 +131,32 @@ func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error d.SetPartial("tags") } + if _, ok := d.GetOk("auto_accept"); ok { + + pcRaw, _, err := resourceAwsVpcPeeringConnectionStateRefreshFunc(ec2conn, d.Id())() + + if err != nil { + return err + } + if pcRaw == nil { + d.SetId("") + return nil + } + pc := pcRaw.(*ec2.VPCPeeringConnection) + + if *pc.Status.Code == "pending-acceptance" { + + status, err := resourceVpcPeeringConnectionAccept(ec2conn, d.Id()) + + log.Printf( + "[DEBUG] Vpc Peering connection accept status %s", + status) + if err != nil { + return err + } + } + } + return resourceAwsVpcPeeringRead(d, meta) } @@ -181,6 +195,6 @@ func resourceAwsVpcPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) r pc := resp.VPCPeeringConnections[0] - return pc, "ready", nil + return pc, *pc.Status.Code, nil } }