Skip to content

Commit

Permalink
Merge pull request #13 from gruntwork-io/default-vpc
Browse files Browse the repository at this point in the history
Fetch the default VPC instead of a random one
  • Loading branch information
josh-padnick committed Apr 1, 2016
2 parents 86deb14 + 00e565f commit bc28ab8
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 13 deletions.
22 changes: 13 additions & 9 deletions aws/vpc.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
package aws

import (
"github.com/aws/aws-sdk-go/service/ec2"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/aws"
"errors"
"github.com/gruntwork-io/terratest/util"
"strconv"
)

var VpcIdFilterName = "vpc-id"
Expand All @@ -15,25 +16,28 @@ type Vpc struct {
Subnets []ec2.Subnet // A list of subnets in the VPC
}

func GetRandomVpc(awsRegion string) (Vpc, error) {
var IS_DEFAULT_FILTER_NAME = "isDefault"
var IS_DEFAULT_FILTER_VALUE = "true"

func GetDefaultVpc(awsRegion string) (Vpc, error) {
vpc := Vpc{}

svc := ec2.New(session.New(), aws.NewConfig().WithRegion(awsRegion))
vpcs, err := svc.DescribeVpcs(&ec2.DescribeVpcsInput{})
defaultVpcFilter := ec2.Filter{Name: &IS_DEFAULT_FILTER_NAME, Values: []*string{&IS_DEFAULT_FILTER_VALUE}}
vpcs, err := svc.DescribeVpcs(&ec2.DescribeVpcsInput{Filters: []*ec2.Filter{&defaultVpcFilter}})
if err != nil {
return vpc, err
}

numVpcs := len(vpcs.Vpcs)
if numVpcs == 0 {
return vpc, errors.New("No VPCs found in region " + awsRegion)
if numVpcs != 1 {
return vpc, errors.New("Expected to find one default VPC in region " + awsRegion + " but found " + strconv.Itoa(numVpcs))
}

randomIndex := util.Random(0, numVpcs)
randomVpc := vpcs.Vpcs[randomIndex]
defaultVpc := vpcs.Vpcs[0]

vpc.Id = *randomVpc.VpcId
vpc.Name = FindVpcName(randomVpc)
vpc.Id = *defaultVpc.VpcId
vpc.Name = FindVpcName(defaultVpc)

vpc.Subnets, err = GetSubnetsForVpc(vpc.Id, awsRegion)
return vpc, err
Expand Down
4 changes: 2 additions & 2 deletions rand_resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,6 @@ func (r *RandomResourceCollection) GetRandomPrivateCidrBlock(prefix int) string
return util.GetRandomPrivateCidrBlock(prefix)
}

func (r *RandomResourceCollection) GetRandomVpc() (aws.Vpc, error) {
return aws.GetRandomVpc(r.AwsRegion)
func (r *RandomResourceCollection) GetDefaultVpc() (aws.Vpc, error) {
return aws.GetDefaultVpc(r.AwsRegion)
}
4 changes: 2 additions & 2 deletions rand_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ func TestAllParametersSet(t *testing.T) {
}
}

func TestGetRandomVpc(t *testing.T) {
func TestGetDefaultVpc(t *testing.T) {
t.Parallel()

ro := NewRandomResourceCollectionOptions()
Expand All @@ -130,7 +130,7 @@ func TestGetRandomVpc(t *testing.T) {
t.Fatalf("Failed to create RandomResourceCollection: %s", err.Error())
}

vpc, err := rand.GetRandomVpc()
vpc, err := rand.GetDefaultVpc()
if err != nil {
t.Fatalf("Failed to get random VPC: %s", err.Error())
}
Expand Down

0 comments on commit bc28ab8

Please sign in to comment.