Skip to content

Commit

Permalink
aws - swf - domain configuration filter (cloud-custodian#9850)
Browse files Browse the repository at this point in the history
  • Loading branch information
mattheidelbaugh authored Nov 27, 2024
1 parent 40a16cf commit c303cb9
Show file tree
Hide file tree
Showing 13 changed files with 211 additions and 0 deletions.
24 changes: 24 additions & 0 deletions c7n/resources/swf.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Copyright The Cloud Custodian Authors.
# SPDX-License-Identifier: Apache-2.0
from c7n.filters import ValueFilter
from c7n.manager import resources
from c7n.query import ConfigSource, DescribeSource, QueryResourceManager, TypeInfo
from c7n.tags import universal_augment
from c7n.utils import local_session, type_schema


class DescribeSimpleWorkflow(DescribeSource):
Expand All @@ -25,3 +27,25 @@ class resource_type(TypeInfo):
'describe': DescribeSimpleWorkflow,
'config': ConfigSource
}


@SimpleWorkflowDomain.filter_registry.register('configuration')
class SWFConfigurationFilter(ValueFilter):

annotation_key = "c7n:configuration"
permissions = ("swf:DescribeDomain",)
schema = type_schema('configuration', rinherit=ValueFilter.schema)

def process(self, resources, event=None):
client = local_session(self.manager.session_factory).client('swf')
results = []
for r in resources:
if self.annotation_key not in r:
config = self.manager.retry(client.describe_domain, name=r["name"])["configuration"]
config["workflowExecutionRetentionPeriodInDays"] = int(
config["workflowExecutionRetentionPeriodInDays"])
r[self.annotation_key] = config

if self.match(r[self.annotation_key]):
results.append(r)
return results
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"status_code": 200,
"data": {
"domainInfo": {
"name": "c7n-test",
"status": "REGISTERED",
"arn": "arn:aws:swf:us-east-1:123456789012:/domain/c7n-test"
},
"configuration": {
"workflowExecutionRetentionPeriodInDays": "90"
},
"ResponseMetadata": {}
}
}
13 changes: 13 additions & 0 deletions tests/data/placebo/test_swf_domain_config/swf.ListDomains_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"status_code": 200,
"data": {
"domainInfos": [
{
"name": "c7n-test",
"status": "REGISTERED",
"arn": "arn:aws:swf:us-east-1:123456789012:/domain/c7n-test"
}
],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"status_code": 200,
"data": {
"PaginationToken": "",
"ResourceTagMappingList": [
{
"ResourceARN": "arn:aws:swf:us-east-1:123456789012:/domain/c7n-test",
"Tags": []
}
],
"ResponseMetadata": {}
}
}
13 changes: 13 additions & 0 deletions tests/data/placebo/test_swf_domain_tag/swf.ListDomains_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"status_code": 200,
"data": {
"domainInfos": [
{
"name": "c7n-test",
"status": "REGISTERED",
"arn": "arn:aws:swf:us-east-1:123456789012:/domain/c7n-test"
}
],
"ResponseMetadata": {}
}
}
13 changes: 13 additions & 0 deletions tests/data/placebo/test_swf_domain_tag/swf.ListDomains_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"status_code": 200,
"data": {
"domainInfos": [
{
"name": "c7n-test",
"status": "REGISTERED",
"arn": "arn:aws:swf:us-east-1:123456789012:/domain/c7n-test"
}
],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"status_code": 200,
"data": {
"tags": [
{
"key": "TestKey",
"value": "TestValue"
}
],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"tags": [],
"ResponseMetadata": {}
}
}
13 changes: 13 additions & 0 deletions tests/data/placebo/test_swf_domain_tag/tagging.GetResources_1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"status_code": 200,
"data": {
"PaginationToken": "",
"ResourceTagMappingList": [
{
"ResourceARN": "arn:aws:swf:us-east-1:123456789012:/domain/c7n-test",
"Tags": []
}
],
"ResponseMetadata": {}
}
}
18 changes: 18 additions & 0 deletions tests/data/placebo/test_swf_domain_tag/tagging.GetResources_2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
{
"status_code": 200,
"data": {
"PaginationToken": "",
"ResourceTagMappingList": [
{
"ResourceARN": "arn:aws:swf:us-east-1:123456789012:/domain/c7n-test",
"Tags": [
{
"Key": "TestKey",
"Value": "TestValue"
}
]
}
],
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"FailedResourcesMap": {},
"ResponseMetadata": {}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"status_code": 200,
"data": {
"FailedResourcesMap": {},
"ResponseMetadata": {}
}
}
57 changes: 57 additions & 0 deletions tests/test_swf.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,60 @@ def test_swf_domain_value_filter(self):
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]['name'], 'test-custodian-swf-domain')
self.assertEqual(resources[0]['c7n:MatchedFilters'], ['name'])

def test_swf_domain_tag(self):
session_factory = self.replay_flight_data('test_swf_domain_tag')
client = session_factory().client("swf")
p = self.load_policy(
{
"name": "test-swf-domain-tag",
"resource": "swf-domain",
"actions": [{
"type": "tag",
"key": "TestKey",
"value": "TestValue"
}]
},
session_factory=session_factory,
)

resources = p.run()
tags = client.list_tags_for_resource(resourceArn=resources[0]["arn"])["tags"]
self.assertEqual(tags[0]["key"], "TestKey")

p = self.load_policy(
{
"name": "test-swf-domain-untag",
"resource": "swf-domain",
"actions": [{
"type": "remove-tag",
"tags": ["TestKey"]
}]
},
session_factory=session_factory,
)

resources = p.run()
tags = client.list_tags_for_resource(resourceArn=resources[0]["arn"])["tags"]
self.assertEqual(len(tags), 0)

def test_swf_domain_config(self):
session_factory = self.replay_flight_data('test_swf_domain_config')
p = self.load_policy(
{
"name": "test-swf-domain-config",
"resource": "swf-domain",
"filters": [{
"type": "configuration",
"key": "workflowExecutionRetentionPeriodInDays",
"op": "gt",
"value": 45
}]
},
session_factory=session_factory,
)

resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["c7n:configuration"][
"workflowExecutionRetentionPeriodInDays"], 90)

0 comments on commit c303cb9

Please sign in to comment.