Skip to content

Commit

Permalink
Added basic acceptance test.
Browse files Browse the repository at this point in the history
  • Loading branch information
Brad Sickles committed May 30, 2016
1 parent 03731d9 commit 75bf0ed
Show file tree
Hide file tree
Showing 2 changed files with 134 additions and 65 deletions.
130 changes: 65 additions & 65 deletions builtin/providers/aws/resource_aws_vpc_peering_connection_accept.go
Original file line number Diff line number Diff line change
@@ -1,65 +1,65 @@
package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsVpcPeeringConnectionAccept() *schema.Resource {
return &schema.Resource{
Create: resourceAwsVPCPeeringAcceptCreate,
Read: resourceAwsVPCPeeringAcceptRead,
Update: resourceAwsVPCPeeringAcceptUpdate,
Delete: resourceAwsVPCPeeringAcceptDelete,

Schema: map[string]*schema.Schema{
"peering_connection_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceAwsVPCPeeringAcceptCreate(d *schema.ResourceData, meta interface{}) error {
return resourceAwsVPCPeeringAcceptUpdate(d, meta)
}

func resourceAwsVPCPeeringAcceptRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
_, state, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())()
if err != nil {
return err
}
d.Set("state", state)
d.SetId(d.Get("peering_connection_id"))
return nil
}

func resourceAwsVPCPeeringAcceptUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

if cur, ok := d.Get("state").(string); ok && cur == ec2.VpcPeeringConnectionStateReasonCodeActive {
// already accepted
return nil
}

state, err := resourceVPCPeeringConnectionAccept(conn, d.Id())
if err != nil {
return err
}
d.Set("state", state)

// TODO: should we poll until this resolves? VpcPeeringConnectionStateReasonCodePendingAcceptance

if state != ec2.VpcPeeringConnectionStateReasonCodeActive {
return fmt.Errorf("Error accepting connection, state: %s", state)
}
return nil
}

func resourceAwsVPCPeeringAcceptDelete(d *schema.ResourceData, meta interface{}) error {
return nil
}
package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/schema"
)

func resourceAwsVpcPeeringConnectionAccept() *schema.Resource {
return &schema.Resource{
Create: resourceAwsVPCPeeringAcceptCreate,
Read: resourceAwsVPCPeeringAcceptRead,
Update: resourceAwsVPCPeeringAcceptUpdate,
Delete: resourceAwsVPCPeeringAcceptDelete,

Schema: map[string]*schema.Schema{
"peering_connection_id": &schema.Schema{
Type: schema.TypeString,
Required: true,
ForceNew: true,
},
},
}
}

func resourceAwsVPCPeeringAcceptCreate(d *schema.ResourceData, meta interface{}) error {
return resourceAwsVPCPeeringAcceptUpdate(d, meta)
}

func resourceAwsVPCPeeringAcceptRead(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn
_, state, err := resourceAwsVPCPeeringConnectionStateRefreshFunc(conn, d.Id())()
if err != nil {
return err
}
d.Set("state", state)
d.SetId(d.Get("peering_connection_id").(string))
return nil
}

func resourceAwsVPCPeeringAcceptUpdate(d *schema.ResourceData, meta interface{}) error {
conn := meta.(*AWSClient).ec2conn

if cur, ok := d.Get("state").(string); ok && cur == ec2.VpcPeeringConnectionStateReasonCodeActive {
// already accepted
return nil
}

state, err := resourceVPCPeeringConnectionAccept(conn, d.Id())
if err != nil {
return err
}
d.Set("state", state)

// TODO: should we poll until this resolves? VpcPeeringConnectionStateReasonCodePendingAcceptance

if state != ec2.VpcPeeringConnectionStateReasonCodeActive {
return fmt.Errorf("Error accepting connection, state: %s", state)
}
return nil
}

func resourceAwsVPCPeeringAcceptDelete(d *schema.ResourceData, meta interface{}) error {
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
package aws

import (
"fmt"
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/hashicorp/terraform/helper/resource"
"github.com/hashicorp/terraform/terraform"
"os"
"testing"
)

func TestAccAWSVPCPeeringConnectionAccept_basic(t *testing.T) {
var connection ec2.VpcPeeringConnection

resource.Test(t, resource.TestCase{
PreCheck: func() {
testAccPreCheck(t)
if os.Getenv("AWS_ACCOUNT_ID") == "" {
t.Fatal("AWS_ACCOUNT_ID must be set")
}
},

IDRefreshName: "aws_vpc_peering_connection.foo",
IDRefreshIgnore: []string{"auto_accept"},

Providers: testAccProviders,
Steps: []resource.TestStep{
resource.TestStep{
Config: testAccVpcPeeringAcceptConfig,
Check: resource.ComposeTestCheckFunc(
testAccCheckAWSVpcPeeringConnectionExists("aws_vpc_peering_connection.foo", &connection),
testAccCheckAWSVpcPeeringConnectionAccepted(&connection),
),
},
},
})
}

func testAccCheckAWSVpcPeeringConnectionAccepted(conn *ec2.VpcPeeringConnection) resource.TestCheckFunc {
return func(s *terraform.State) error {
if conn.Status == nil {
return fmt.Errorf("No vpc peering connection status")
}
if conn.Status.Code != ec2.VpcPeeringConnectionStateReasonCodeActive {
return fmt.Errorf("Vpc peering connection not accepted: %s", conn.Status.Code)
}
return nil
}
}

const testAccVpcPeeringAcceptConfig = `
resource "aws_vpc" "foo" {
cidr_block = "10.0.0.0/16"
}
resource "aws_vpc" "bar" {
cidr_block = "10.1.0.0/16"
}
resource "aws_vpc_peering_connection" "foo" {
vpc_id = "${aws_vpc.foo.id}"
peer_vpc_id = "${aws_vpc.bar.id}"
auto_accept = false
}
resource "aws_vpc_peering_connection_accept" "foo" {
peering_connection_id = "${aws_vpc_peering_connection.foo.id}"
}
`

0 comments on commit 75bf0ed

Please sign in to comment.