-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(aws): Refactor AmazonCachingAgentFilter for better reuse (#5094
- Loading branch information
1 parent
c0c90db
commit eb9fb4b
Showing
7 changed files
with
196 additions
and
94 deletions.
There are no files selected for viewing
125 changes: 125 additions & 0 deletions
125
...groovy/com/netflix/spinnaker/clouddriver/aws/provider/agent/AmazonCachingAgentFilter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
package com.netflix.spinnaker.clouddriver.aws.provider.agent; | ||
|
||
import java.util.List; | ||
import org.apache.commons.lang3.StringUtils; | ||
import org.springframework.boot.context.properties.ConfigurationProperties; | ||
import org.springframework.stereotype.Component; | ||
|
||
@Component | ||
@ConfigurationProperties("aws.caching.filter") | ||
public class AmazonCachingAgentFilter { | ||
|
||
List<TagFilterOption> includeTags; | ||
List<TagFilterOption> excludeTags; | ||
|
||
public static class TagFilterOption { | ||
String name; | ||
String value; | ||
|
||
public TagFilterOption() {} | ||
|
||
public TagFilterOption(String name, String value) { | ||
this.name = name; | ||
this.value = value; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public String getValue() { | ||
return value; | ||
} | ||
|
||
public void setValue(String value) { | ||
this.value = value; | ||
} | ||
} | ||
|
||
public List<TagFilterOption> getIncludeTags() { | ||
return includeTags; | ||
} | ||
|
||
public void setIncludeTags(List<TagFilterOption> includeTags) { | ||
this.includeTags = includeTags; | ||
} | ||
|
||
public List<TagFilterOption> getExcludeTags() { | ||
return excludeTags; | ||
} | ||
|
||
public void setExcludeTags(List<TagFilterOption> excludeTags) { | ||
this.excludeTags = excludeTags; | ||
} | ||
|
||
public boolean hasTagFilter() { | ||
return this.hasIncludeTagFilter() || this.hasExcludeTagFilter(); | ||
} | ||
|
||
public boolean hasIncludeTagFilter() { | ||
return this.includeTags != null && this.includeTags.size() > 0; | ||
} | ||
|
||
public boolean hasExcludeTagFilter() { | ||
return this.excludeTags != null && this.excludeTags.size() > 0; | ||
} | ||
|
||
/** | ||
* ResourceTag is a helper wrapper for AWS resources which are taggable, to convert their specific | ||
* types from the AWS SDK into a generic type for comparison by this agent filter. | ||
*/ | ||
public static class ResourceTag { | ||
final String key; | ||
final String value; | ||
|
||
public ResourceTag(String key, String value) { | ||
this.key = key; | ||
this.value = value; | ||
} | ||
} | ||
|
||
/** | ||
* Determine if the resource with the given set of tags should be retained, that is, if the caller | ||
* should discard or keep it based on the include/exclude filters configured. | ||
*/ | ||
public boolean shouldRetainResource(List<ResourceTag> tags) { | ||
|
||
// retain the resource by default if there isn't an include filter setup | ||
boolean retainResource = !this.hasIncludeTagFilter(); | ||
|
||
if (this.hasIncludeTagFilter()) { | ||
retainResource = | ||
this.includeTags.stream() | ||
.anyMatch( | ||
filter -> | ||
tags.stream() | ||
.anyMatch(tag -> tagFilterOptionMatchesResourceTag(filter, tag))); | ||
} | ||
|
||
// exclude takes precedence over include so runs second if the resource is still being retained | ||
if (retainResource && this.hasExcludeTagFilter()) { | ||
retainResource = | ||
this.excludeTags.stream() | ||
.noneMatch( | ||
filter -> | ||
tags.stream() | ||
.anyMatch(tag -> tagFilterOptionMatchesResourceTag(filter, tag))); | ||
} | ||
|
||
return retainResource; | ||
} | ||
|
||
private boolean tagFilterOptionMatchesResourceTag( | ||
TagFilterOption tagFilterOption, ResourceTag resourceTag) { | ||
if (resourceTag.key == null || !resourceTag.key.matches(tagFilterOption.name)) { | ||
return false; | ||
} | ||
|
||
return StringUtils.isEmpty(tagFilterOption.getValue()) | ||
|| (resourceTag.value != null && resourceTag.value.matches(tagFilterOption.value)); | ||
} | ||
} |
57 changes: 0 additions & 57 deletions
57
...tflix/spinnaker/clouddriver/aws/provider/agent/AmazonCachingAgentFilterConfiguration.java
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
49 changes: 49 additions & 0 deletions
49
.../com/netflix/spinnaker/clouddriver/aws/provider/agent/AmazonCachingAgentFilterSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
package com.netflix.spinnaker.clouddriver.aws.provider.agent | ||
|
||
import spock.lang.Shared | ||
import spock.lang.Specification | ||
|
||
class AmazonCachingAgentFilterSpec extends Specification { | ||
|
||
@Shared | ||
AmazonCachingAgentFilter filter = new AmazonCachingAgentFilter() | ||
|
||
void "should retain based on tag criteria"() { | ||
given: | ||
filter.includeTags = includeTags | ||
filter.excludeTags = excludeTags | ||
|
||
when: | ||
def result = filter.shouldRetainResource(resourceTags) | ||
|
||
then: | ||
result == expected | ||
|
||
where: | ||
resourceTags | includeTags | excludeTags | expected | ||
[resourceTag("hello", "goodbye")] | null | null | true | ||
[resourceTag("hello", "goodbye")] | [filterTag("hello")] | null | true | ||
[resourceTag("hello", "goodbye")] | [filterTag("hello", "goodbye")] | null | true | ||
[resourceTag("hello", "goodbye")] | [filterTag("hello", "goo")] | null | false | ||
[resourceTag("hello", "goodbye")] | [filterTag("hello", ".*bye")] | null | true | ||
[resourceTag("hello", "goodbye")] | [filterTag(".*a.*")] | null | false | ||
[resourceTag("hello", "goodbye")] | null | [filterTag("hello")] | false | ||
[resourceTag("hello", "goodbye")] | null | [filterTag("hello", "goodbye")] | false | ||
[resourceTag("hello", "goodbye")] | null | [filterTag(".*a.*")] | true | ||
[resourceTag("hello", "goodbye")] | [filterTag("hello", "goodbye")] | [filterTag("Name")] | true | ||
[resourceTag("hello", "goodbye")] | [filterTag("hello", "goodbye")] | [filterTag("hello")] | false | ||
[resourceTag("hello", "goodbye")] | [filterTag(".*", "ciao")] | [filterTag("hello", ".*")] | false | ||
[resourceTag("hello", "goodbye"), | ||
resourceTag("Name", "primary"),] | [filterTag("hello")] | null | true | ||
[resourceTag("hello", "goodbye"), | ||
resourceTag("Name", "primary"),] | null | [filterTag("hello")] | false | ||
} | ||
|
||
private static def resourceTag(String name = null, String value = null) { | ||
return new AmazonCachingAgentFilter.ResourceTag(name, value) | ||
} | ||
|
||
private static def filterTag(String name = null, String value = null) { | ||
return new AmazonCachingAgentFilter.TagFilterOption(name, value) | ||
} | ||
} |
Oops, something went wrong.