-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
CloudWatch events - Fixes #52 #69
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
4af416b
Add CloudWatchEventSource
coopernurse dc26849
Added 'cron' sample to demo CloudWatch events
coopernurse 7d79fa7
add .gitignore to 'cron' sample
coopernurse 3019dc6
Remove event_source.py - accidentally re-added when I rebased
coopernurse a285ae4
cloudwatch: eliminate 'else' before return in _to_status
coopernurse File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,113 @@ | ||
# -*- coding: utf-8 -*- | ||
# Copyright (c) 2014, 2015 Mitch Garnaat | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# http://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
import kappa.event_source.base | ||
import logging | ||
import uuid | ||
|
||
LOG = logging.getLogger(__name__) | ||
|
||
|
||
class CloudWatchEventSource(kappa.event_source.base.EventSource): | ||
|
||
def __init__(self, context, config): | ||
super(CloudWatchEventSource, self).__init__(context, config) | ||
self._events = kappa.awsclient.create_client('events', context.session) | ||
self._lambda = kappa.awsclient.create_client('lambda', context.session) | ||
self._name = config['arn'].split('/')[-1] | ||
self._context = context | ||
self._config = config | ||
|
||
def get_rule(self): | ||
response = self._events.call('list_rules', NamePrefix=self._name) | ||
LOG.debug(response) | ||
if 'Rules' in response: | ||
for r in response['Rules']: | ||
if r['Name'] == self._name: | ||
return r | ||
return None | ||
|
||
def add(self, function): | ||
kwargs = { | ||
'Name': self._name, | ||
'State': 'ENABLED' if self.enabled else 'DISABLED' | ||
} | ||
if 'schedule' in self._config: | ||
kwargs['ScheduleExpression'] = self._config['schedule'] | ||
if 'pattern' in self._config: | ||
kwargs['EventPattern'] = self._config['pattern'] | ||
if 'description' in self._config: | ||
kwargs['Description'] = self._config['description'] | ||
if 'role_arn' in self._config: | ||
kwargs['RoleArn'] = self._config['role_arn'] | ||
try: | ||
response = self._events.call('put_rule', **kwargs) | ||
LOG.debug(response) | ||
self._config['arn'] = response['RuleArn'] | ||
response = self._lambda.call('add_permission', | ||
FunctionName=function.name, | ||
StatementId=str(uuid.uuid4()), | ||
Action='lambda:InvokeFunction', | ||
Principal='events.amazonaws.com', | ||
SourceArn=response['RuleArn']) | ||
LOG.debug(response) | ||
response = self._events.call('put_targets', | ||
Rule=self._name, | ||
Targets=[{ | ||
'Id': function.name, | ||
'Arn': function.arn | ||
}]) | ||
LOG.debug(response) | ||
except Exception: | ||
LOG.exception('Unable to put CloudWatch event source') | ||
|
||
def update(self, function): | ||
self.add(function) | ||
|
||
def remove(self, function): | ||
LOG.debug('removing CloudWatch event source') | ||
try: | ||
rule = self.get_rule() | ||
if rule: | ||
response = self._events.call('remove_targets', | ||
Rule=self._name, | ||
Ids=[function.name]) | ||
LOG.debug(response) | ||
response = self._events.call('delete_rule', | ||
Name=self._name) | ||
LOG.debug(response) | ||
except Exception: | ||
LOG.exception('Unable to remove CloudWatch event source %s', self._name) | ||
|
||
def status(self, function): | ||
LOG.debug('status for CloudWatch event for %s', function.name) | ||
return self._to_status(self.get_rule()) | ||
|
||
def enable(self, function): | ||
if self.get_rule(): | ||
self._events.call('enable_rule', Name=self._name) | ||
|
||
def disable(self, function): | ||
if self.get_rule(): | ||
self._events.call('disable_rule', Name=self._name) | ||
|
||
def _to_status(self, rule): | ||
if rule: | ||
return { | ||
'EventSourceArn': rule['Arn'], | ||
'State': rule['State'] | ||
} | ||
else: | ||
return None | ||
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,4 @@ | ||
.kappa/ | ||
kappa.yml | ||
*.zip | ||
|
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,10 @@ | ||
import logging | ||
import time | ||
|
||
LOG = logging.getLogger() | ||
LOG.setLevel(logging.DEBUG) | ||
|
||
|
||
def handler(event, context): | ||
LOG.debug(event) | ||
return {'status': 'success', 'time': time.time()} |
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,4 @@ | ||
{ | ||
"foo": "bar", | ||
"fie": "baz" | ||
} |
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,23 @@ | ||
--- | ||
name: kappa-cron | ||
environments: | ||
dev: | ||
profile: <your profile here> | ||
region: <your region here> | ||
policy: | ||
resources: | ||
- arn: arn:aws:logs:*:*:* | ||
actions: | ||
- "*" | ||
event_sources: | ||
- arn: arn:aws:events:<your region here>:<your account id>:rule/kappa-cron-dev | ||
schedule: rate(1 minute) | ||
description: cron to run this lambda function every minute | ||
enabled: true | ||
lambda: | ||
description: Kappa sample lambda that runs every minute | ||
handler: simple.handler | ||
runtime: python2.7 | ||
memory_size: 128 | ||
timeout: 3 | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can drop the else clause here and just
return None
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok, that's done.