Module: Chef::Handler::Sns::Config

Extended by:
Config
Includes:
Mixin::ParamsValidate
Included in:
Chef::Handler::Sns, Config
Defined in:
lib/chef/handler/sns/config.rb,
lib/chef/handler/sns/config/ohai.rb

Overview

Reads Chef Handler SNS configuration options or calculate them if not set.

Defined Under Namespace

Classes: Ohai

Constant Summary collapse

REQUIRED =

Required configuration options.

%w(access_key secret_key topic_arn).freeze

Instance Method Summary collapse

Instance Method Details

#access_key(arg = nil) ⇒ String

Gets or sets AWS access key.

Parameters:

  • arg (String) (defaults to: nil)

    Access key.

Returns:

  • (String)

    Access Key.



127
128
129
# File 'lib/chef/handler/sns/config.rb', line 127

def access_key(arg = nil)
  set_or_return(:access_key, arg, kind_of: String)
end

#body_template(arg = nil) ⇒ String

Gets or sets SNS message body template file path.

Parameters:

  • arg (String) (defaults to: nil)

    SNS body template.

Returns:

  • (String)

    SNS body template.



226
227
228
# File 'lib/chef/handler/sns/config.rb', line 226

def body_template(arg = nil)
  set_or_return(:body_template, arg, kind_of: String)
end

#config_check(node = nil) ⇒ Object

Checks if any required configuration option is not set.

Tries to read some configuration options from Ohai before checking them.

Parameters:

  • node (Chef::Node) (defaults to: nil)

    Node to read Ohai information from.

Returns:

  • void

Raises:

  • (Exceptions::ValidationFailed)

    When any required configuration option is not set.



105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/chef/handler/sns/config.rb', line 105

def config_check(node = nil)
  config_from_ohai(node) if node
  REQUIRED.each do |key|
    next unless send(key).nil?
    raise Exceptions::ValidationFailed,
          "Required argument #{key} is missing!"
  end

  return unless body_template && !::File.exist?(body_template)
  raise Exceptions::ValidationFailed,
        "Template file not found: #{body_template}."
end

#config_from_ohai(node) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Reads some configuration options from Ohai information.

Called from #config_check.

Parameters:

  • node (Chef::Node)

    No objects to read the information from.

Returns:

  • void



58
59
60
61
62
63
64
65
# File 'lib/chef/handler/sns/config.rb', line 58

def config_from_ohai(node)
  config_ohai = Config::Ohai.new(node)
  [
    :access_key, :secret_key, :token
  ].each do |attr|
    send(attr, config_ohai.send(attr)) if send(attr).nil?
  end
end

#config_init(config = {}) ⇒ Object

Sets configuration reading it from a Hash.

Parameters:

  • config (Hash) (defaults to: {})

    Configuration options to set.

Returns:

  • void

See Also:



78
79
80
81
82
83
84
85
86
87
88
# File 'lib/chef/handler/sns/config.rb', line 78

def config_init(config = {})
  config.each do |key, value|
    if Config.respond_to?(key) && !key.to_s.match(/^config_/)
      send(key, value)
    else
      Chef::Log.warn(
        "#{self.class}: configuration method not found: #{key}."
      )
    end
  end
end

#filter_opsworks_activity(arg = nil) ⇒ Array

Gets or sets [OpsWorks](aws.amazon.com/opsworks/) activities.

Notifications will only be triggered for the activities in the array, everything else will be discarded.

Parameters:

  • arg (Array) (defaults to: nil)

    Activities list.

Returns:

  • (Array)

    Activities list.



242
243
244
245
# File 'lib/chef/handler/sns/config.rb', line 242

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

#message_structure(arg = nil) ⇒ String

Gets or sets MessageStructure SNS request parameter.

Parameters:

  • arg (String) (defaults to: nil)

    MessageStructure.

Returns:

  • (String)

    MessageStructure.



200
201
202
# File 'lib/chef/handler/sns/config.rb', line 200

def message_structure(arg = nil)
  set_or_return(:message_structure, arg, kind_of: String)
end

#region(arg = nil) ⇒ String

Gets or sets AWS region.

Parameters:

  • arg (String) (defaults to: nil)

    Region.

Returns:

  • (String)

    Region.



153
154
155
# File 'lib/chef/handler/sns/config.rb', line 153

def region(arg = nil)
  set_or_return(:region, arg, kind_of: String)
end

#secret_key(arg = nil) ⇒ String

Gets or sets AWS secret key.

Parameters:

  • arg (String) (defaults to: nil)

    Secret key.

Returns:

  • (String)

    Secret Key.



140
141
142
# File 'lib/chef/handler/sns/config.rb', line 140

def secret_key(arg = nil)
  set_or_return(:secret_key, arg, kind_of: String)
end

#subject(arg = nil) ⇒ String

Gets or sets SNS message subject.

Parameters:

  • arg (String) (defaults to: nil)

    SNS subject.

Returns:

  • (String)

    SNS subject.



213
214
215
# File 'lib/chef/handler/sns/config.rb', line 213

def subject(arg = nil)
  set_or_return(:subject, arg, kind_of: String)
end

#token(arg = nil) ⇒ String

Gets or sets AWS token.

Parameters:

  • arg (String) (defaults to: nil)

    Token.

Returns:

  • (String)

    Token.



166
167
168
# File 'lib/chef/handler/sns/config.rb', line 166

def token(arg = nil)
  set_or_return(:token, arg, kind_of: [String, FalseClass])
end

#topic_arn(arg = nil) ⇒ String

Gets or sets AWS Topic ARN.

It also tries to set the AWS region reading it from the ARN string.

Parameters:

  • arg (String) (defaults to: nil)

    Topic ARN.

Returns:

  • (String)

    Topic ARN.



181
182
183
184
185
186
187
188
189
# File 'lib/chef/handler/sns/config.rb', line 181

def topic_arn(arg = nil)
  set_or_return(
    :topic_arn, arg, kind_of: String
  ).tap do |arn|
    # Get the region from the ARN:
    next if arn.nil? || !region.nil?
    region(arn.split(':', 5)[3])
  end
end