This repository has been archived by the owner on Jul 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #98 from remind101/logs_filters
Blueprint for creating cloudwatch logs filters
- Loading branch information
Showing
5 changed files
with
154 additions
and
19 deletions.
There are no files selected for viewing
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,45 @@ | ||
from stacker.blueprints.base import Blueprint | ||
from stacker.blueprints.variables.types import TroposphereType | ||
|
||
from troposphere import logs, Output, Ref | ||
|
||
|
||
LOG_RETENTION_VALUES = [ | ||
0, 1, 3, 5, 7, 14, 30, 60, 90, 120, 150, 180, 365, 400, 545, 731, 1827, | ||
3653 | ||
] | ||
LOG_RETENTION_STRINGS = [str(x) for x in LOG_RETENTION_VALUES] | ||
|
||
|
||
def validate_cloudwatch_log_retention(value): | ||
if value not in LOG_RETENTION_VALUES: | ||
raise ValueError( | ||
"%d is not a valid retention period. Must be one of: %s" % ( | ||
value, | ||
', '.join(LOG_RETENTION_STRINGS) | ||
) | ||
) | ||
return value | ||
|
||
|
||
class SubscriptionFilters(Blueprint): | ||
|
||
VARIABLES = { | ||
"SubscriptionFilters": { | ||
"type": TroposphereType(logs.SubscriptionFilter, many=True), | ||
"description": "Subscription filters to create.", | ||
} | ||
} | ||
|
||
def create_template(self): | ||
t = self.template | ||
variables = self.get_variables() | ||
|
||
for _filter in variables["SubscriptionFilters"]: | ||
t.add_resource(_filter) | ||
t.add_output( | ||
Output( | ||
"%sName" % _filter.title, | ||
Value=Ref(_filter) | ||
) | ||
) |
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
46 changes: 46 additions & 0 deletions
46
tests/fixtures/blueprints/test_cloudwatch_logs_subscription_filters.json
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,46 @@ | ||
{ | ||
"Outputs": { | ||
"Filter1Name": { | ||
"Value": { | ||
"Ref": "Filter1" | ||
} | ||
}, | ||
"Filter2Name": { | ||
"Value": { | ||
"Ref": "Filter2" | ||
} | ||
} | ||
}, | ||
"Resources": { | ||
"Filter1": { | ||
"Properties": { | ||
"DestinationArn": { | ||
"Fn::GetAtt": [ | ||
"KinesisStream1", | ||
"Arn" | ||
] | ||
}, | ||
"FilterPattern": "{$.userIdentity.type = Root}", | ||
"LogGroupName": { | ||
"Ref": "LogGroup1" | ||
} | ||
}, | ||
"Type": "AWS::Logs::SubscriptionFilter" | ||
}, | ||
"Filter2": { | ||
"Properties": { | ||
"DestinationArn": { | ||
"Fn::GetAtt": [ | ||
"KinesisStream2", | ||
"Arn" | ||
] | ||
}, | ||
"FilterPattern": "{$.userIdentity.type = Root}", | ||
"LogGroupName": { | ||
"Ref": "LogGroup2" | ||
} | ||
}, | ||
"Type": "AWS::Logs::SubscriptionFilter" | ||
} | ||
} | ||
} |
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,46 @@ | ||
import unittest | ||
|
||
from stacker.blueprints.testutil import BlueprintTestCase | ||
from stacker.context import Context | ||
from stacker.variables import Variable | ||
|
||
from stacker_blueprints.cloudwatch_logs import SubscriptionFilters | ||
|
||
from troposphere import GetAtt, Ref | ||
|
||
|
||
class TestSubscriptionFilters(BlueprintTestCase): | ||
def setUp(self): | ||
self.ctx = Context({'namespace': 'test'}) | ||
|
||
def test_create_template(self): | ||
blueprint = SubscriptionFilters( | ||
'test_cloudwatch_logs_subscription_filters', | ||
self.ctx | ||
) | ||
|
||
blueprint.resolve_variables( | ||
[ | ||
Variable( | ||
"SubscriptionFilters", | ||
{ | ||
"Filter1": { | ||
"DestinationArn": GetAtt("KinesisStream1", "Arn"), | ||
"FilterPattern": "{$.userIdentity.type = Root}", | ||
"LogGroupName": Ref("LogGroup1"), | ||
}, | ||
"Filter2": { | ||
"DestinationArn": GetAtt("KinesisStream2", "Arn"), | ||
"FilterPattern": "{$.userIdentity.type = Root}", | ||
"LogGroupName": Ref("LogGroup2"), | ||
}, | ||
} | ||
) | ||
] | ||
) | ||
blueprint.create_template() | ||
self.assertRenderedBlueprint(blueprint) | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |