Skip to content

Commit

Permalink
Added 'Tags' and supporting functions to VPC struct (#1000)
Browse files Browse the repository at this point in the history
* Added 'TestGetTagsForVpc' and 'TestGetTagValueForVpcE' to 'vpc_test.go'

* Added 'GetTagsForVpc' and 'GetTagsForVpcE' functions. Added 'Tags' to 'Vpc' struct and 'GetVpcsE' function.

* Added 'GetTagValueForVpc' and 'GetTagValueForVpcE' functions to 'vpc.go'

* Added 'TestGetTagValueForVpc' function to 'vpc_test.go'

* Corrected use of 'client.DescribeTags' within 'GetTagsForVpcE' function.

* Removed 'GetTagValueForVpc(E)' functions and related tests.

* Updated 'GetTagsForVpc' and 'GetTagsForVpcE' to make use of 'require.NoError(t, err)'
  • Loading branch information
SphenicPaul authored Oct 1, 2021
1 parent 2053af4 commit c1a6f03
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 4 deletions.
44 changes: 40 additions & 4 deletions modules/aws/vpc.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import (

// Vpc is an Amazon Virtual Private Cloud.
type Vpc struct {
Id string // The ID of the VPC
Name string // The name of the VPC
Subnets []Subnet // A list of subnets in the VPC
Id string // The ID of the VPC
Name string // The name of the VPC
Subnets []Subnet // A list of subnets in the VPC
Tags map[string]string // The tags associated with the VPC
}

// Subnet is a subnet in an availability zone.
Expand All @@ -26,6 +27,9 @@ type Subnet struct {
}

var vpcIDFilterName = "vpc-id"
var vpcResourceIdFilterName = "resource-id"
var vpcResourceTypeFilterName = "resource-type"
var vpcResourceTypeFilterValue = "vpc"
var isDefaultFilterName = "isDefault"
var isDefaultFilterValue = "true"

Expand Down Expand Up @@ -89,7 +93,13 @@ func GetVpcsE(t testing.TestingT, filters []*ec2.Filter, region string) ([]*Vpc,
if err != nil {
return nil, err
}
retVal[i] = &Vpc{Id: aws.StringValue(vpc.VpcId), Name: FindVpcName(vpc), Subnets: subnets}

tags, err := GetTagsForVpcE(t, aws.StringValue(vpc.VpcId), region)
if err != nil {
return nil, err
}

retVal[i] = &Vpc{Id: aws.StringValue(vpc.VpcId), Name: FindVpcName(vpc), Subnets: subnets, Tags: tags}
}

return retVal, nil
Expand Down Expand Up @@ -143,6 +153,32 @@ func GetSubnetsForVpcE(t testing.TestingT, vpcID string, region string) ([]Subne
return subnets, nil
}

// GetTagsForVpc gets the tags for the specified VPC.
func GetTagsForVpc(t testing.TestingT, vpcID string, region string) map[string]string {
tags, err := GetTagsForVpcE(t, vpcID, region)
require.NoError(t, err)

return tags
}

// GetTagsForVpcE gets the tags for the specified VPC.
func GetTagsForVpcE(t testing.TestingT, vpcID string, region string) (map[string]string, error) {
client, err := NewEc2ClientE(t, region)
require.NoError(t, err)

vpcResourceTypeFilter := ec2.Filter{Name: &vpcResourceTypeFilterName, Values: []*string{&vpcResourceTypeFilterValue}}
vpcResourceIdFilter := ec2.Filter{Name: &vpcResourceIdFilterName, Values: []*string{&vpcID}}
tagsOutput, err := client.DescribeTags(&ec2.DescribeTagsInput{Filters: []*ec2.Filter{&vpcResourceTypeFilter, &vpcResourceIdFilter}})
require.NoError(t, err)

tags := map[string]string{}
for _, tag := range tagsOutput.Tags {
tags[aws.StringValue(tag.Key)] = aws.StringValue(tag.Value)
}

return tags, nil
}

// IsPublicSubnet returns True if the subnet identified by the given id in the provided region is public.
func IsPublicSubnet(t testing.TestingT, subnetId string, region string) bool {
isPublic, err := IsPublicSubnetE(t, subnetId, region)
Expand Down
23 changes: 23 additions & 0 deletions modules/aws/vpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,29 @@ func TestIsPublicSubnet(t *testing.T) {
assert.True(t, IsPublicSubnet(t, *subnet.SubnetId, region))
}

func TestGetTagsForVpc(t *testing.T) {
t.Parallel()

region := GetRandomStableRegion(t, nil, nil)
vpc := createVpc(t, region)
defer deleteVpc(t, *vpc.VpcId, region)

noTags := GetTagsForVpc(t, *vpc.VpcId, region)
assert.True(t, len(vpc.Tags) == 0)
assert.True(t, len(noTags) == 0)

testTags := make(map[string]string)
testTags["TagKey1"] = "TagValue1"
testTags["TagKey2"] = "TagValue2"

AddTagsToResource(t, region, *vpc.VpcId, testTags)
vpcWithTags := GetVpcById(t, *vpc.VpcId, region)
tags := GetTagsForVpc(t, *vpc.VpcId, region)

assert.True(t, len(vpcWithTags.Tags) == len(testTags))
assert.True(t, len(tags) == len(testTags))
}

func createPublicRoute(t *testing.T, vpcId string, routeTableId string, region string) {
ec2Client := NewEc2Client(t, region)

Expand Down

0 comments on commit c1a6f03

Please sign in to comment.