From c1a6f0386f2cd1c4c7877e8dfb5ac5c6e6cfb659 Mon Sep 17 00:00:00 2001 From: "Paul J. Wilcox" Date: Fri, 1 Oct 2021 18:49:29 +0100 Subject: [PATCH] Added 'Tags' and supporting functions to VPC struct (#1000) * 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)' --- modules/aws/vpc.go | 44 +++++++++++++++++++++++++++++++++++++---- modules/aws/vpc_test.go | 23 +++++++++++++++++++++ 2 files changed, 63 insertions(+), 4 deletions(-) diff --git a/modules/aws/vpc.go b/modules/aws/vpc.go index e80f90537..d4a56638f 100644 --- a/modules/aws/vpc.go +++ b/modules/aws/vpc.go @@ -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. @@ -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" @@ -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 @@ -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) diff --git a/modules/aws/vpc_test.go b/modules/aws/vpc_test.go index a1cbb6963..efa9fa739 100644 --- a/modules/aws/vpc_test.go +++ b/modules/aws/vpc_test.go @@ -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)