Skip to content

Commit

Permalink
Merge pull request #2 from easybiblabs/master
Browse files Browse the repository at this point in the history
Filter Notifications by Opsworks Activity
  • Loading branch information
zuazo committed Mar 11, 2014
2 parents b795f8b + 0548a7b commit f3f7235
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 5 deletions.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,16 @@ chef_handler "Chef::Handler::Sns" do
end
```

##### Opsworks: Filter Notifications by Activity
An optional array of opsworks activities can be supplied. If the array is set, notifications will
only be triggered for the activities in the array, everything else will be discarded.

```ruby
argument_array = [
:filter_opsworks_activities => ['deploy','configure']
]
```

## Handler Configuration Options

The following options are available to configure the handler:
Expand Down
23 changes: 19 additions & 4 deletions lib/chef/handler/sns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def initialize(config={})

def report
config_check(node)
sns.topics[topic_arn].publish(
sns_body,
{ :subject => sns_subject }
)
if allow_publish(node)
sns.topics[topic_arn].publish(
sns_body,
{ :subject => sns_subject }
)
end
end

def get_region
Expand All @@ -45,6 +47,19 @@ def get_region

protected

def allow_publish(node)
if filter_opsworks_activity.nil?
return true
end

if node.attribute?("opsworks") && node["opsworks"].attribute?("activity")
return filter_opsworks_activity.include?(node["opsworks"]["activity"])
end

Chef::Log.debug("You supplied opsworks activity filters, but node attr was not found. Returning false")
return false
end

def sns
@sns ||= begin
params = {
Expand Down
10 changes: 9 additions & 1 deletion lib/chef/handler/sns/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,15 @@ def body_template(arg=nil)
:kind_of => String
)
end


def filter_opsworks_activity(arg=nil)
arg = Array(arg) if arg.is_a? String
set_or_return(
:filter_opsworks_activity,
arg,
:kind_of => Array
)
end
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions test/test_chef_handler_sns.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,4 +175,37 @@ def get_sns_body
assert_equal @fake_sns_handler.get_sns_body, body_msg
end

it 'should publish messages if node["opsworks"]["activity"] does not exist' do
@sns_handler = Chef::Handler::Sns.new(@config)
AWS::SNS::Topic.any_instance.expects(:publish).once

@sns_handler.run_report_safely(@run_status)
end

it 'should publish messages if node["opsworks"]["activity"] matches allowed acvities' do
@node.set['opsworks']['activity'] = 'deploy'
@config[:filter_opsworks_activity] = ['deploy', 'setup']

@sns_handler = Chef::Handler::Sns.new(@config)
AWS::SNS::Topic.any_instance.expects(:publish).once
@sns_handler.run_report_safely(@run_status)
end

it 'should not publish messages if node["opsworks"]["activity"] differs from allowed acvities' do
@node.set['opsworks']['activity'] = 'configure'
@config[:filter_opsworks_activity] = ['deploy', 'setup']

@sns_handler = Chef::Handler::Sns.new(@config)
AWS::SNS::Topic.any_instance.expects(:publish).never
@sns_handler.run_report_safely(@run_status)
end

it 'should not publish messages if node["opsworks"]["activity"] is set, but the node attribute is missing' do
@config[:filter_opsworks_activity] = ['deploy', 'setup']

@sns_handler = Chef::Handler::Sns.new(@config)
AWS::SNS::Topic.any_instance.expects(:publish).never
@sns_handler.run_report_safely(@run_status)
end

end

0 comments on commit f3f7235

Please sign in to comment.