Merge pull request #1174 from hashicorp/aws-go-vpc-peering
providers/aws: Convert AWS VPC Peering to aws-sdk-go
This commit is contained in:
commit
e4a298fdfb
|
@ -5,9 +5,10 @@ import (
|
||||||
"log"
|
"log"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/hashicorp/aws-sdk-go/aws"
|
||||||
|
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/helper/schema"
|
"github.com/hashicorp/terraform/helper/schema"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func resourceAwsVpcPeeringConnection() *schema.Resource {
|
func resourceAwsVpcPeeringConnection() *schema.Resource {
|
||||||
|
@ -40,23 +41,23 @@ func resourceAwsVpcPeeringConnection() *schema.Resource {
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error {
|
||||||
ec2conn := meta.(*AWSClient).ec2conn
|
ec2conn := meta.(*AWSClient).awsEC2conn
|
||||||
|
|
||||||
// Create the vpc peering connection
|
// Create the vpc peering connection
|
||||||
createOpts := &ec2.CreateVpcPeeringConnection{
|
createOpts := &ec2.CreateVPCPeeringConnectionRequest{
|
||||||
PeerOwnerId: d.Get("peer_owner_id").(string),
|
PeerOwnerID: aws.String(d.Get("peer_owner_id").(string)),
|
||||||
PeerVpcId: d.Get("peer_vpc_id").(string),
|
PeerVPCID: aws.String(d.Get("peer_vpc_id").(string)),
|
||||||
VpcId: d.Get("vpc_id").(string),
|
VPCID: aws.String(d.Get("vpc_id").(string)),
|
||||||
}
|
}
|
||||||
log.Printf("[DEBUG] VpcPeeringCreate create config: %#v", createOpts)
|
log.Printf("[DEBUG] VpcPeeringCreate create config: %#v", createOpts)
|
||||||
resp, err := ec2conn.CreateVpcPeeringConnection(createOpts)
|
resp, err := ec2conn.CreateVPCPeeringConnection(createOpts)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("Error creating vpc peering connection: %s", err)
|
return fmt.Errorf("Error creating vpc peering connection: %s", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Get the ID and store it
|
// Get the ID and store it
|
||||||
rt := &resp.VpcPeeringConnection
|
rt := resp.VPCPeeringConnection
|
||||||
d.SetId(rt.VpcPeeringConnectionId)
|
d.SetId(*rt.VPCPeeringConnectionID)
|
||||||
log.Printf("[INFO] Vpc Peering Connection ID: %s", d.Id())
|
log.Printf("[INFO] Vpc Peering Connection ID: %s", d.Id())
|
||||||
|
|
||||||
// Wait for the vpc peering connection to become available
|
// Wait for the vpc peering connection to become available
|
||||||
|
@ -79,7 +80,7 @@ func resourceAwsVpcPeeringCreate(d *schema.ResourceData, meta interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
ec2conn := meta.(*AWSClient).ec2conn
|
ec2conn := meta.(*AWSClient).awsEC2conn
|
||||||
pcRaw, _, err := resourceAwsVpcPeeringConnectionStateRefreshFunc(ec2conn, d.Id())()
|
pcRaw, _, err := resourceAwsVpcPeeringConnectionStateRefreshFunc(ec2conn, d.Id())()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -89,20 +90,20 @@ func resourceAwsVpcPeeringRead(d *schema.ResourceData, meta interface{}) error {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
pc := pcRaw.(*ec2.VpcPeeringConnection)
|
pc := pcRaw.(*ec2.VPCPeeringConnection)
|
||||||
|
|
||||||
d.Set("peer_owner_id", pc.AccepterVpcInfo.OwnerId)
|
d.Set("peer_owner_id", pc.AccepterVPCInfo.OwnerID)
|
||||||
d.Set("peer_vpc_id", pc.AccepterVpcInfo.VpcId)
|
d.Set("peer_vpc_id", pc.AccepterVPCInfo.VPCID)
|
||||||
d.Set("vpc_id", pc.RequesterVpcInfo.VpcId)
|
d.Set("vpc_id", pc.RequesterVPCInfo.VPCID)
|
||||||
d.Set("tags", tagsToMap(pc.Tags))
|
d.Set("tags", tagsToMapSDK(pc.Tags))
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error {
|
||||||
ec2conn := meta.(*AWSClient).ec2conn
|
ec2conn := meta.(*AWSClient).awsEC2conn
|
||||||
|
|
||||||
if err := setTags(ec2conn, d); err != nil {
|
if err := setTagsSDK(ec2conn, d); err != nil {
|
||||||
return err
|
return err
|
||||||
} else {
|
} else {
|
||||||
d.SetPartial("tags")
|
d.SetPartial("tags")
|
||||||
|
@ -112,9 +113,12 @@ func resourceAwsVpcPeeringUpdate(d *schema.ResourceData, meta interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
func resourceAwsVpcPeeringDelete(d *schema.ResourceData, meta interface{}) error {
|
func resourceAwsVpcPeeringDelete(d *schema.ResourceData, meta interface{}) error {
|
||||||
ec2conn := meta.(*AWSClient).ec2conn
|
ec2conn := meta.(*AWSClient).awsEC2conn
|
||||||
|
|
||||||
_, err := ec2conn.DeleteVpcPeeringConnection(d.Id())
|
_, err := ec2conn.DeleteVPCPeeringConnection(
|
||||||
|
&ec2.DeleteVPCPeeringConnectionRequest{
|
||||||
|
VPCPeeringConnectionID: aws.String(d.Id()),
|
||||||
|
})
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -123,9 +127,11 @@ func resourceAwsVpcPeeringDelete(d *schema.ResourceData, meta interface{}) error
|
||||||
func resourceAwsVpcPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
func resourceAwsVpcPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) resource.StateRefreshFunc {
|
||||||
return func() (interface{}, string, error) {
|
return func() (interface{}, string, error) {
|
||||||
|
|
||||||
resp, err := conn.DescribeVpcPeeringConnection([]string{id}, ec2.NewFilter())
|
resp, err := conn.DescribeVPCPeeringConnections(&ec2.DescribeVPCPeeringConnectionsRequest{
|
||||||
|
VPCPeeringConnectionIDs: []string{id},
|
||||||
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
if ec2err, ok := err.(*ec2.Error); ok && ec2err.Code == "InvalidVpcPeeringConnectionID.NotFound" {
|
if ec2err, ok := err.(aws.APIError); ok && ec2err.Code == "InvalidVpcPeeringConnectionID.NotFound" {
|
||||||
resp = nil
|
resp = nil
|
||||||
} else {
|
} else {
|
||||||
log.Printf("Error on VpcPeeringConnectionStateRefresh: %s", err)
|
log.Printf("Error on VpcPeeringConnectionStateRefresh: %s", err)
|
||||||
|
@ -139,7 +145,7 @@ func resourceAwsVpcPeeringConnectionStateRefreshFunc(conn *ec2.EC2, id string) r
|
||||||
return nil, "", nil
|
return nil, "", nil
|
||||||
}
|
}
|
||||||
|
|
||||||
pc := &resp.VpcPeeringConnections[0]
|
pc := &resp.VPCPeeringConnections[0]
|
||||||
|
|
||||||
return pc, "ready", nil
|
return pc, "ready", nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,9 +4,9 @@ import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/hashicorp/aws-sdk-go/gen/ec2"
|
||||||
"github.com/hashicorp/terraform/helper/resource"
|
"github.com/hashicorp/terraform/helper/resource"
|
||||||
"github.com/hashicorp/terraform/terraform"
|
"github.com/hashicorp/terraform/terraform"
|
||||||
"github.com/mitchellh/goamz/ec2"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestAccAWSVPCPeeringConnection_normal(t *testing.T) {
|
func TestAccAWSVPCPeeringConnection_normal(t *testing.T) {
|
||||||
|
@ -28,17 +28,20 @@ func TestAccAWSVPCPeeringConnection_normal(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error {
|
func testAccCheckAWSVpcPeeringConnectionDestroy(s *terraform.State) error {
|
||||||
conn := testAccProvider.Meta().(*AWSClient).ec2conn
|
conn := testAccProvider.Meta().(*AWSClient).awsEC2conn
|
||||||
|
|
||||||
for _, rs := range s.RootModule().Resources {
|
for _, rs := range s.RootModule().Resources {
|
||||||
if rs.Type != "aws_vpc_peering_connection" {
|
if rs.Type != "aws_vpc_peering_connection" {
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
describe, err := conn.DescribeVpcPeeringConnection([]string{rs.Primary.ID}, ec2.NewFilter())
|
describe, err := conn.DescribeVPCPeeringConnections(
|
||||||
|
&ec2.DescribeVPCPeeringConnectionsRequest{
|
||||||
|
VPCPeeringConnectionIDs: []string{rs.Primary.ID},
|
||||||
|
})
|
||||||
|
|
||||||
if err == nil {
|
if err == nil {
|
||||||
if len(describe.VpcPeeringConnections) != 0 {
|
if len(describe.VPCPeeringConnections) != 0 {
|
||||||
return fmt.Errorf("vpc peering connection still exists")
|
return fmt.Errorf("vpc peering connection still exists")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue