Skip to content

Commit

Permalink
Merge pull request #10 from gruntwork-io/subnets-fix
Browse files Browse the repository at this point in the history
Return raw subnet structs instead of just the ids
  • Loading branch information
brikis98 committed Mar 29, 2016
2 parents 6f69648 + afb8f1b commit 22b9e5f
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 13 deletions.
20 changes: 8 additions & 12 deletions aws/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@ import (
var VpcIdFilterName = "vpc-id"

type Vpc struct {
Id string // The ID of the VPC
Name string // The name of the VPC
SubnetIds []string // A list of subnet ids in the VPC
Id string // The ID of the VPC
Name string // The name of the VPC
Subnets []ec2.Subnet // A list of subnets in the VPC
}

func GetRandomVpc(awsRegion string) (Vpc, error) {
Expand All @@ -35,12 +35,8 @@ func GetRandomVpc(awsRegion string) (Vpc, error) {
vpc.Id = *randomVpc.VpcId
vpc.Name = FindVpcName(randomVpc)

vpc.SubnetIds, err = GetSubnetIdsForVpc(vpc.Id, awsRegion)
if err != nil {
return vpc, err
}

return vpc, nil
vpc.Subnets, err = GetSubnetsForVpc(vpc.Id, awsRegion)
return vpc, err
}

func FindVpcName(vpc *ec2.Vpc) string {
Expand All @@ -57,8 +53,8 @@ func FindVpcName(vpc *ec2.Vpc) string {
return ""
}

func GetSubnetIdsForVpc(vpcId string, awsRegion string) ([]string, error) {
subnets := []string{}
func GetSubnetsForVpc(vpcId string, awsRegion string) ([]ec2.Subnet, error) {
subnets := []ec2.Subnet{}

svc := ec2.New(session.New(), aws.NewConfig().WithRegion(awsRegion))

Expand All @@ -69,7 +65,7 @@ func GetSubnetIdsForVpc(vpcId string, awsRegion string) ([]string, error) {
}

for _, subnet := range subnetOutput.Subnets {
subnets = append(subnets, *subnet.SubnetId)
subnets = append(subnets, *subnet)
}
return subnets, nil
}
2 changes: 1 addition & 1 deletion rand_resources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ func TestGetRandomVpc(t *testing.T) {
t.Fatalf("GetRandomVpc returned a VPC without a name: %s", vpc)
}

if len(vpc.SubnetIds) == 0 {
if len(vpc.Subnets) == 0 {
t.Fatalf("GetRandomVpc returned a VPC with no subnets: %s", vpc)
}
}

0 comments on commit 22b9e5f

Please sign in to comment.