Skip to content

Commit

Permalink
Config#config_from_ohai method refactored, Config::Ohai class created
Browse files Browse the repository at this point in the history
  • Loading branch information
zuazo committed Feb 7, 2014
1 parent 7568517 commit d7a0b80
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
21 changes: 9 additions & 12 deletions lib/chef/handler/sns/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# limitations under the License.
#

require 'chef/handler/sns/config/ohai'
require 'chef/mixin/params_validate'
require 'chef/exceptions'

Expand All @@ -29,18 +30,14 @@ module Config
REQUIRED = [ 'access_key', 'secret_key', 'topic_arn' ]

def config_from_ohai(node)
if node.attribute?('ec2')
if region.nil? and node.ec2.attribute?('placement_availability_zone')
region(node.ec2.placement_availability_zone.chop)
end
if node.ec2.attribute?('iam') and node.ec2.iam.attribute?('security-credentials')
iam_role, credentials = node.ec2.iam['security-credentials'].first
unless credentials.nil?
access_key(credentials['AccessKeyId']) if access_key.nil?
secret_key(credentials['SecretAccessKey']) if secret_key.nil?
token(credentials['Token']) if token.nil?
end
end
config_ohai = Config::Ohai.new(node)
[
:region,
:access_key,
:secret_key,
:token,
].each do |attr|
self.send(attr, config_ohai.send(attr)) if self.send(attr).nil?
end
end

Expand Down
66 changes: 66 additions & 0 deletions lib/chef/handler/sns/config/ohai.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
#
# Author:: Xabier de Zuazo (<xabier@onddo.com>)
# Copyright:: Copyright (c) 2014 Onddo Labs, SL.
# License:: Apache License, Version 2.0
#
# 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.
#

class Chef
class Handler
class Sns < ::Chef::Handler
module Config
class Ohai

def initialize(node)
read_config(node)
end

def region
@region
end

def access_key
@access_key
end

def secret_key
@secret_key
end

def token
@token
end

protected

def read_config(node)
return unless node.attribute?('ec2')
if node.ec2.attribute?('placement_availability_zone')
@region = node.ec2.placement_availability_zone.chop
end
if node.ec2.attribute?('iam') and node.ec2.iam.attribute?('security-credentials')
iam_role, credentials = node.ec2.iam['security-credentials'].first
unless credentials.nil?
@access_key = credentials['AccessKeyId']
@secret_key = credentials['SecretAccessKey']
@token = credentials['Token']
end
end
end

end
end
end
end
end

0 comments on commit d7a0b80

Please sign in to comment.