Skip to content

Commit

Permalink
Add ownerID as fields to a variety of ec2/vpc resources (#964)
Browse files Browse the repository at this point in the history
* Add ownerID as fields to a variety of ec2/vpc resources

* Fix formatting

---------

Co-authored-by: Philipp Trulson <der-eismann@users.noreply.github.com>
Co-authored-by: Philipp Trulson <p.trulson@rebuy.com>
  • Loading branch information
3 people authored Aug 25, 2023
1 parent 6179e59 commit c0b549d
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 0 deletions.
15 changes: 15 additions & 0 deletions pkg/types/properties.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,21 @@ func (p Properties) SetTagWithPrefix(prefix string, tagKey *string, tagValue int
return p.Set(keyStr, tagValue)
}

func (p Properties) SetPropertyWithPrefix(prefix string, propertyKey string, propertyValue interface{}) Properties {
keyStr := strings.TrimSpace(propertyKey)
prefix = strings.TrimSpace(prefix)

if keyStr == "" {
return p
}

if prefix != "" {
keyStr = fmt.Sprintf("%s:%s", prefix, keyStr)
}

return p.Set(keyStr, propertyValue)
}

func (p Properties) Get(key string) string {
value, ok := p[key]
if !ok {
Expand Down
38 changes: 38 additions & 0 deletions pkg/types/properties_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,3 +161,41 @@ func TestPropertiesSetTagWithPrefix(t *testing.T) {
})
}
}

func TestPropertiesSetPropertiesWithPrefix(t *testing.T) {
cases := []struct {
name string
prefix string
key string
value interface{}
want string
}{
{
name: "empty",
prefix: "",
key: "OwnerID",
value: aws.String("123456789012"),
want: `[OwnerID: "123456789012"]`,
},
{
name: "nonempty",
prefix: "igw",
key: "OwnerID",
value: aws.String("123456789012"),
want: `[igw:OwnerID: "123456789012"]`,
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
p := types.NewProperties()

p.SetPropertyWithPrefix(tc.prefix, tc.key, tc.value)
have := p.String()

if tc.want != have {
t.Errorf("'%s' != '%s'", tc.want, have)
}
})
}
}
3 changes: 3 additions & 0 deletions resources/ec2-dhcp-options.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ type EC2DHCPOption struct {
id *string
tags []*ec2.Tag
defaultVPC bool
ownerID *string
}

func init() {
Expand All @@ -37,6 +38,7 @@ func ListEC2DHCPOptions(sess *session.Session) ([]Resource, error) {
id: out.DhcpOptionsId,
tags: out.Tags,
defaultVPC: defVpcDhcpOptsId == *out.DhcpOptionsId,
ownerID: out.OwnerId,
})
}

Expand All @@ -62,6 +64,7 @@ func (e *EC2DHCPOption) Properties() types.Properties {
properties.SetTag(tagValue.Key, tagValue.Value)
}
properties.Set("DefaultVPC", e.defaultVPC)
properties.Set("OwnerID", e.ownerID)
return properties
}

Expand Down
6 changes: 6 additions & 0 deletions resources/ec2-internet-gateway-attachments.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,10 @@ import (
type EC2InternetGatewayAttachment struct {
svc *ec2.EC2
vpcId *string
vpcOwnerID *string
vpcTags []*ec2.Tag
igwId *string
igwOwnerID *string
igwTags []*ec2.Tag
defaultVPC bool
}
Expand Down Expand Up @@ -50,8 +52,10 @@ func ListEC2InternetGatewayAttachments(sess *session.Session) ([]Resource, error
resources = append(resources, &EC2InternetGatewayAttachment{
svc: svc,
vpcId: vpc.VpcId,
vpcOwnerID: vpc.OwnerId,
vpcTags: vpc.Tags,
igwId: igw.InternetGatewayId,
igwOwnerID: igw.OwnerId,
igwTags: igw.Tags,
defaultVPC: *vpc.IsDefault,
})
Expand Down Expand Up @@ -84,6 +88,8 @@ func (e *EC2InternetGatewayAttachment) Properties() types.Properties {
properties.SetTagWithPrefix("vpc", tagValue.Key, tagValue.Value)
}
properties.Set("DefaultVPC", e.defaultVPC)
properties.SetPropertyWithPrefix("vpc", "OwnerID", e.vpcOwnerID)
properties.SetPropertyWithPrefix("igw", "OwnerID", e.igwOwnerID)
return properties
}

Expand Down
3 changes: 3 additions & 0 deletions resources/ec2-route-tables.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type EC2RouteTable struct {
svc *ec2.EC2
routeTable *ec2.RouteTable
defaultVPC bool
ownerID *string
}

func init() {
Expand All @@ -37,6 +38,7 @@ func ListEC2RouteTables(sess *session.Session) ([]Resource, error) {
svc: svc,
routeTable: out,
defaultVPC: defVpcId == *out.VpcId,
ownerID: out.OwnerId,
})
}

Expand Down Expand Up @@ -72,6 +74,7 @@ func (e *EC2RouteTable) Properties() types.Properties {
properties.SetTag(tagValue.Key, tagValue.Value)
}
properties.Set("DefaultVPC", e.defaultVPC)
properties.Set("OwnerID", e.ownerID)
return properties
}

Expand Down

0 comments on commit c0b549d

Please sign in to comment.