From 62bd5562eb53a92d8cee13bd9690ee781144dcc6 Mon Sep 17 00:00:00 2001 From: Anthony Goddard Date: Fri, 5 Dec 2014 16:39:36 -0700 Subject: [PATCH 1/2] use mash for options, strings & symbols for consistency, add ip path attribute --- libraries/ipaddress.rb | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/libraries/ipaddress.rb b/libraries/ipaddress.rb index ba05209..766f2fe 100644 --- a/libraries/ipaddress.rb +++ b/libraries/ipaddress.rb @@ -29,15 +29,25 @@ def private_network_for_label(node, label) end def ipaddress(options = {}) - raise "Options must be a hash" unless - options.respond_to? :has_key? - raise "Options does not contain a node key" unless - options.has_key? :node - raise "Options does not contain a remote_node key" unless - options.has_key? :remote_node - raise "Options type is invalid" if - options.has_key? :type and not - [:local, :public, :label].include?(options[:type]) + options = Mash.new(options) + + unless options.respond_to? :has_key? + raise "Options must be a mash" + end + + unless options.has_key? :node + raise "Options does not contain a node key" + end + + unless options.has_key? :remote_node + raise "Options does not contain a remote_node key" + end + + if options.has_key? :type + unless ['local', 'public', 'label'].include?(options[:type].to_s) + raise "Options type is invalid" + end + end options[:type] ||= if provider_for_node(options[:remote_node]) == provider_for_node(options[:node]) @@ -56,7 +66,7 @@ def ipaddress(options = {}) Chef::Log.debug "ipaddress[#{options[:type]}]: attempting to determine ip address for #{options[:node].name}" - case options[:type] + case options[:type].to_sym when :label network = private_network_for_label(options[:remote_node], options[:label]) network[:ips][0][:ip] From eabb00ef05cffbbc4863a0ccd36b0dc4ddec18ea Mon Sep 17 00:00:00 2001 From: Anthony Goddard Date: Fri, 5 Dec 2014 17:39:50 -0700 Subject: [PATCH 2/2] allow ip address attribute --- libraries/ipaddress.rb | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/libraries/ipaddress.rb b/libraries/ipaddress.rb index 766f2fe..c711cc5 100644 --- a/libraries/ipaddress.rb +++ b/libraries/ipaddress.rb @@ -72,6 +72,11 @@ def ipaddress(options = {}) network[:ips][0][:ip] else cloud_ipv4 = options[:remote_node][:cloud]["#{options[:type]}_ipv4"] rescue nil + if options[:ipaddress_attribute] + cloud_ipv4 = options[:ipaddress_attribute].split('.').inject(options[:remote_node]) do |memo, key| + memo[key] || break + end + end cloud_ipv4 || options[:remote_node][:ipaddress] end end