Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch the default VPC instead of a random one #13

Merged
merged 2 commits into from
Apr 1, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"
)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import order. You may have commented on this in a separate PR.


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