chef-handler-sns 0.2.0 → 0.2.1

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -8,6 +8,7 @@ This Chef Handler is heavily based on [Joshua Timberman](https://github.com/jtim
8
8
 
9
9
  [![Gem Version](https://badge.fury.io/rb/chef-handler-sns.png)](http://badge.fury.io/rb/chef-handler-sns)
10
10
  [![Dependency Status](https://gemnasium.com/onddo/chef-handler-sns.png)](https://gemnasium.com/onddo/chef-handler-sns)
11
+ [![Code Climate](https://codeclimate.com/github/onddo/chef-handler-sns.png)](https://codeclimate.com/github/onddo/chef-handler-sns)
11
12
 
12
13
  ## Requirements
13
14
 
@@ -0,0 +1,113 @@
1
+ #
2
+ # Author:: Xabier de Zuazo (<xabier@onddo.com>)
3
+ # Copyright:: Copyright (c) 2013 Onddo Labs, SL.
4
+ # License:: Apache License, Version 2.0
5
+ #
6
+ # Licensed under the Apache License, Version 2.0 (the "License");
7
+ # you may not use this file except in compliance with the License.
8
+ # You may obtain a copy of the License at
9
+ #
10
+ # http://www.apache.org/licenses/LICENSE-2.0
11
+ #
12
+ # Unless required by applicable law or agreed to in writing, software
13
+ # distributed under the License is distributed on an "AS IS" BASIS,
14
+ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ # See the License for the specific language governing permissions and
16
+ # limitations under the License.
17
+ #
18
+
19
+ require 'chef/mixin/params_validate'
20
+ require 'chef/exceptions'
21
+
22
+ class Chef
23
+ class Handler
24
+ class Sns
25
+ module Config
26
+ include ::Chef::Mixin::ParamsValidate
27
+
28
+ def config_init(config={})
29
+ config.each do |key, value|
30
+ if self.respond_to?(key)
31
+ self.send(key, value)
32
+ else
33
+ Chef::Log.warn("#{self.class.to_s}: cnofiguration method not found: #{key}.")
34
+ end
35
+ end
36
+ end
37
+
38
+ def config_check
39
+ required = [ 'access_key', 'secret_key', 'topic_arn' ]
40
+ opts = {}
41
+ map = {}
42
+ required.each do |key|
43
+ opts[key] = self.send(key)
44
+ map[key] = { :required => true }
45
+ end
46
+ validate(opts, map)
47
+
48
+ if body_template and not ::File.exists?(body_template)
49
+ raise Exceptions::ValidationFailed,
50
+ "Template file not found: #{body_template}."
51
+ end
52
+ end
53
+
54
+ def access_key(arg=nil)
55
+ set_or_return(
56
+ :access_key,
57
+ arg,
58
+ :kind_of => String
59
+ )
60
+ end
61
+
62
+ def secret_key(arg=nil)
63
+ set_or_return(
64
+ :secret_key,
65
+ arg,
66
+ :kind_of => String
67
+ )
68
+ end
69
+
70
+ def region(arg=nil)
71
+ set_or_return(
72
+ :region,
73
+ arg,
74
+ :kind_of => String
75
+ )
76
+ end
77
+
78
+ def token(arg=nil)
79
+ set_or_return(
80
+ :token,
81
+ arg,
82
+ :kind_of => String
83
+ )
84
+ end
85
+
86
+ def topic_arn(arg=nil)
87
+ set_or_return(
88
+ :topic_arn,
89
+ arg,
90
+ :kind_of => String
91
+ )
92
+ end
93
+
94
+ def subject(arg=nil)
95
+ set_or_return(
96
+ :subject,
97
+ arg,
98
+ :kind_of => String
99
+ )
100
+ end
101
+
102
+ def body_template(arg=nil)
103
+ set_or_return(
104
+ :body_template,
105
+ arg,
106
+ :kind_of => String
107
+ )
108
+ end
109
+
110
+ end
111
+ end
112
+ end
113
+ end
@@ -1,7 +1,7 @@
1
1
  class Chef
2
2
  class Handler
3
3
  class Sns
4
- VERSION = '0.2.0'
4
+ VERSION = '0.2.1'
5
5
  end
6
6
  end
7
7
  end
@@ -23,56 +23,36 @@ require 'erubis'
23
23
  class Chef
24
24
  class Handler
25
25
  class Sns < ::Chef::Handler
26
- attr_writer :access_key, :secret_key, :region, :token, :topic_arn, :subject, :body_template
26
+ require 'chef/handler/sns/config'
27
+ include ::Chef::Handler::Sns::Config
27
28
 
28
29
  def initialize(config={})
29
30
  Chef::Log.debug("#{self.class.to_s} initialized.")
30
- @access_key = config[:access_key]
31
- @secret_key = config[:secret_key]
32
- @region = config[:region] if config.has_key?(:region)
33
- @token = config[:token] if config.has_key?(:token)
34
- @topic_arn = config[:topic_arn]
35
- @subject = config[:subject] if config.has_key?(:subject)
36
- @body_template = config[:body_template] if config.has_key?(:body_template)
31
+ config_init(config)
37
32
  end
38
33
 
39
34
  def report
40
- check_config
41
- Chef::Log.debug("#{self.class.to_s} reporting.")
42
- sns.publish(@topic_arn, sns_body, sns_subject)
35
+ config_check
36
+ sns.publish(topic_arn, sns_body, sns_subject)
43
37
  end
44
38
 
45
39
  protected
46
-
47
- def check_config
48
- Chef::Log.debug("#{self.class.to_s} checking handler configuration.")
49
- raise "access_key not properly set" unless @access_key.kind_of?(String)
50
- raise "secret_key not properly set" unless @secret_key.kind_of?(String)
51
- raise "region not properly set" unless @region.kind_of?(String) or @region.nil?
52
- raise "token not properly set" unless @token.kind_of?(String) or @token.nil?
53
- raise "topic_arn not properly set" unless @topic_arn.kind_of?(String)
54
- raise "subject not properly set" unless @subject.kind_of?(String) or @subject.nil?
55
- unless @body_template.nil?
56
- raise "body_template not properly set" unless @body_template.kind_of?(String)
57
- raise "body_template file not found: #{@body_template}" unless ::File.exists?(@body_template)
58
- end
59
- end
60
40
 
61
41
  def sns
62
42
  @sns ||= begin
63
43
  params = {
64
44
  :logger => Chef::Log,
65
- :region => @region || node.ec2.placement_availability_zone.chop
45
+ :region => region || node.ec2.placement_availability_zone.chop
66
46
  }
67
- params[:token] = @token if @token
68
- RightAws::SnsInterface.new(@access_key, @secret_key, params)
47
+ params[:token] = token if token
48
+ RightAws::SnsInterface.new(access_key, secret_key, params)
69
49
  end
70
50
  end
71
51
 
72
52
  def sns_subject
73
- if @subject
53
+ if subject
74
54
  context = self
75
- eruby = Erubis::Eruby.new(@subject)
55
+ eruby = Erubis::Eruby.new(subject)
76
56
  eruby.evaluate(context)
77
57
  else
78
58
  chef_client = Chef::Config[:solo] ? 'Chef Solo' : 'Chef Client'
@@ -82,7 +62,7 @@ class Chef
82
62
  end
83
63
 
84
64
  def sns_body
85
- template = IO.read(@body_template || "#{File.dirname(__FILE__)}/sns/templates/body.erb")
65
+ template = IO.read(body_template || "#{File.dirname(__FILE__)}/sns/templates/body.erb")
86
66
  context = self
87
67
  eruby = Erubis::Eruby.new(template)
88
68
  eruby.evaluate(context)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: chef-handler-sns
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.1
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -51,6 +51,7 @@ extra_rdoc_files: []
51
51
  files:
52
52
  - LICENSE
53
53
  - README.md
54
+ - lib/chef/handler/sns/config.rb
54
55
  - lib/chef/handler/sns/templates/body.erb
55
56
  - lib/chef/handler/sns/version.rb
56
57
  - lib/chef/handler/sns.rb