Skip to content

Commit

Permalink
✨ Add support for comma-separated values on the same key for EC2 tag …
Browse files Browse the repository at this point in the history
…filters (#4742)

* Add support for multiple comma-separated values on a include/exclude tag filter.

Signed-off-by: Vasil Sirakov <[email protected]>

* Added test cases for shouldExcludeInstance with multi-valued tag filters.

Signed-off-by: Vasil Sirakov <[email protected]>

* Fix assertions.

Signed-off-by: Vasil Sirakov <[email protected]>

---------

Signed-off-by: Vasil Sirakov <[email protected]>
  • Loading branch information
VasilSirakov authored Oct 14, 2024
1 parent 0b5e919 commit 767de8c
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
12 changes: 7 additions & 5 deletions providers/aws/resources/aws_ec2.go
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ func (a *mqlAwsEc2) getEc2Instances(ctx context.Context, svc *ec2.Client, filter
for k, v := range filters.Tags {
params.Filters = append(params.Filters, ec2types.Filter{
Name: aws.String(fmt.Sprintf("tag:%s", k)),
Values: []string{v},
Values: strings.Split(v, ","),
})
}
if len(filters.InstanceIds) > 0 {
Expand Down Expand Up @@ -1787,10 +1787,12 @@ func shouldExcludeInstance(instance ec2types.Instance, filters connection.Ec2Dis
}
}
for k, v := range filters.ExcludeTags {
for _, iTag := range instance.Tags {
if iTag.Key != nil && *iTag.Key == k &&
iTag.Value != nil && *iTag.Value == v {
return true
for _, tagValue := range strings.Split(v, ",") {
for _, iTag := range instance.Tags {
if iTag.Key != nil && *iTag.Key == k &&
iTag.Value != nil && *iTag.Value == tagValue {
return true
}
}
}
}
Expand Down
20 changes: 20 additions & 0 deletions providers/aws/resources/aws_ec2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,26 @@ func TestShouldExcludeInstance(t *testing.T) {
}
require.False(t, shouldExcludeInstance(instance, filters))
})

t.Run("should exclude instances with matching values for the same tag", func(t *testing.T) {
filters := connection.Ec2DiscoveryFilters{
ExcludeTags: map[string]string{
"key-1": "val-1,val-2,val-3",
},
}
require.True(t, shouldExcludeInstance(instance, filters))
})

t.Run("should not exclude instances when no tag values match", func(t *testing.T) {
filters := connection.Ec2DiscoveryFilters{
ExcludeTags: map[string]string{
"key-1": "val-2,val-3",
"key-2": "val-1,val-3",
"key-3": "val-1,val-2",
},
}
require.False(t, shouldExcludeInstance(instance, filters))
})
}

func TestDetermineApplicableRegions(t *testing.T) {
Expand Down

0 comments on commit 767de8c

Please sign in to comment.