diff --git a/c7n/resources/swf.py b/c7n/resources/swf.py index 277cdfe88dc..27e2bb7bfe3 100644 --- a/c7n/resources/swf.py +++ b/c7n/resources/swf.py @@ -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): @@ -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 diff --git a/tests/data/placebo/test_swf_domain_config/swf.DescribeDomain_1.json b/tests/data/placebo/test_swf_domain_config/swf.DescribeDomain_1.json new file mode 100644 index 00000000000..58e136df4f4 --- /dev/null +++ b/tests/data/placebo/test_swf_domain_config/swf.DescribeDomain_1.json @@ -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": {} + } +} \ No newline at end of file diff --git a/tests/data/placebo/test_swf_domain_config/swf.ListDomains_1.json b/tests/data/placebo/test_swf_domain_config/swf.ListDomains_1.json new file mode 100644 index 00000000000..d7495a0f88a --- /dev/null +++ b/tests/data/placebo/test_swf_domain_config/swf.ListDomains_1.json @@ -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": {} + } +} \ No newline at end of file diff --git a/tests/data/placebo/test_swf_domain_config/tagging.GetResources_1.json b/tests/data/placebo/test_swf_domain_config/tagging.GetResources_1.json new file mode 100644 index 00000000000..64d5f4925be --- /dev/null +++ b/tests/data/placebo/test_swf_domain_config/tagging.GetResources_1.json @@ -0,0 +1,13 @@ +{ + "status_code": 200, + "data": { + "PaginationToken": "", + "ResourceTagMappingList": [ + { + "ResourceARN": "arn:aws:swf:us-east-1:123456789012:/domain/c7n-test", + "Tags": [] + } + ], + "ResponseMetadata": {} + } +} \ No newline at end of file diff --git a/tests/data/placebo/test_swf_domain_tag/swf.ListDomains_1.json b/tests/data/placebo/test_swf_domain_tag/swf.ListDomains_1.json new file mode 100644 index 00000000000..d7495a0f88a --- /dev/null +++ b/tests/data/placebo/test_swf_domain_tag/swf.ListDomains_1.json @@ -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": {} + } +} \ No newline at end of file diff --git a/tests/data/placebo/test_swf_domain_tag/swf.ListDomains_2.json b/tests/data/placebo/test_swf_domain_tag/swf.ListDomains_2.json new file mode 100644 index 00000000000..d7495a0f88a --- /dev/null +++ b/tests/data/placebo/test_swf_domain_tag/swf.ListDomains_2.json @@ -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": {} + } +} \ No newline at end of file diff --git a/tests/data/placebo/test_swf_domain_tag/swf.ListTagsForResource_1.json b/tests/data/placebo/test_swf_domain_tag/swf.ListTagsForResource_1.json new file mode 100644 index 00000000000..103c766035a --- /dev/null +++ b/tests/data/placebo/test_swf_domain_tag/swf.ListTagsForResource_1.json @@ -0,0 +1,12 @@ +{ + "status_code": 200, + "data": { + "tags": [ + { + "key": "TestKey", + "value": "TestValue" + } + ], + "ResponseMetadata": {} + } +} \ No newline at end of file diff --git a/tests/data/placebo/test_swf_domain_tag/swf.ListTagsForResource_2.json b/tests/data/placebo/test_swf_domain_tag/swf.ListTagsForResource_2.json new file mode 100644 index 00000000000..c42bb257a30 --- /dev/null +++ b/tests/data/placebo/test_swf_domain_tag/swf.ListTagsForResource_2.json @@ -0,0 +1,7 @@ +{ + "status_code": 200, + "data": { + "tags": [], + "ResponseMetadata": {} + } +} \ No newline at end of file diff --git a/tests/data/placebo/test_swf_domain_tag/tagging.GetResources_1.json b/tests/data/placebo/test_swf_domain_tag/tagging.GetResources_1.json new file mode 100644 index 00000000000..64d5f4925be --- /dev/null +++ b/tests/data/placebo/test_swf_domain_tag/tagging.GetResources_1.json @@ -0,0 +1,13 @@ +{ + "status_code": 200, + "data": { + "PaginationToken": "", + "ResourceTagMappingList": [ + { + "ResourceARN": "arn:aws:swf:us-east-1:123456789012:/domain/c7n-test", + "Tags": [] + } + ], + "ResponseMetadata": {} + } +} \ No newline at end of file diff --git a/tests/data/placebo/test_swf_domain_tag/tagging.GetResources_2.json b/tests/data/placebo/test_swf_domain_tag/tagging.GetResources_2.json new file mode 100644 index 00000000000..27ac375e65e --- /dev/null +++ b/tests/data/placebo/test_swf_domain_tag/tagging.GetResources_2.json @@ -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": {} + } +} \ No newline at end of file diff --git a/tests/data/placebo/test_swf_domain_tag/tagging.TagResources_1.json b/tests/data/placebo/test_swf_domain_tag/tagging.TagResources_1.json new file mode 100644 index 00000000000..bd2dead8598 --- /dev/null +++ b/tests/data/placebo/test_swf_domain_tag/tagging.TagResources_1.json @@ -0,0 +1,7 @@ +{ + "status_code": 200, + "data": { + "FailedResourcesMap": {}, + "ResponseMetadata": {} + } +} \ No newline at end of file diff --git a/tests/data/placebo/test_swf_domain_tag/tagging.UntagResources_1.json b/tests/data/placebo/test_swf_domain_tag/tagging.UntagResources_1.json new file mode 100644 index 00000000000..bd2dead8598 --- /dev/null +++ b/tests/data/placebo/test_swf_domain_tag/tagging.UntagResources_1.json @@ -0,0 +1,7 @@ +{ + "status_code": 200, + "data": { + "FailedResourcesMap": {}, + "ResponseMetadata": {} + } +} \ No newline at end of file diff --git a/tests/test_swf.py b/tests/test_swf.py index 3f866d86f64..70cb62e3c59 100644 --- a/tests/test_swf.py +++ b/tests/test_swf.py @@ -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)