From 740ca7dc8053be93e43392ca61b2f308c0596d19 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Fri, 7 Apr 2017 15:13:59 -0700 Subject: (FACT-932) Add new function, fact() The fact() function allows dot-notation reference to facts. It is an alternative to using $facts directly with array-indexing. Array-indexing is often onerous to use since it doesn't align with how structured facts are accessed elsewhere in the ecosystem and if any element in a multi-step path doesn't exist, array indexing can cause a compilation failure. Example usage: fact('os.family') --- lib/puppet/functions/fact.rb | 58 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 58 insertions(+) create mode 100644 lib/puppet/functions/fact.rb diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb new file mode 100644 index 0000000..dfb048b --- /dev/null +++ b/lib/puppet/functions/fact.rb @@ -0,0 +1,58 @@ +# Digs into the facts hash using dot-notation +# +# Example usage: +# +# fact('osfamily') +# fact('os.architecture') +# +# Array indexing: +# +# fact('mountpoints."/dev".options.1') +# +# Fact containing a "." in the name: +# +# fact('vmware."VRA.version"') +# +Puppet::Functions.create_function(:fact) do + dispatch :fact do + param 'String', :fact_name + end + + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') + end + + def fact(fact_name) + facts = closure_scope['facts'] + + # Transform the dot-notation string into an array of paths to walk. Make + # sure to correctly extract double-quoted values containing dots as single + # elements in the path. + path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } + + walked_path = [] + path.reduce(facts) do |d, k| + return nil if d.nil? || k.nil? + + case + when d.is_a?(Array) + begin + result = d[Integer(k)] + rescue ArgumentError => e + Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is an array; cannot index to '#{k}'") + result = nil + end + when d.is_a?(Hash) + result = d[k] + else + Puppet.warning("fact request for #{fact_name} returning nil: '#{to_dot_syntax(walked_path)}' is not a collection; cannot walk to '#{k}'") + result = nil + end + + walked_path << k + result + end + end +end -- cgit v1.2.3 From 409a974095a3f5b637e091494b5d14b451c5de78 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Fri, 30 Jun 2017 14:01:41 -0700 Subject: (FACT-932) Allow use of fact() on other hashes Because sometimes people want to use an alternative data set, but treat it like it's a set of facts. --- lib/puppet/functions/fact.rb | 23 +++++++++++++++++------ 1 file changed, 17 insertions(+), 6 deletions(-) diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb index dfb048b..48d0d3c 100644 --- a/lib/puppet/functions/fact.rb +++ b/lib/puppet/functions/fact.rb @@ -18,22 +18,27 @@ Puppet::Functions.create_function(:fact) do param 'String', :fact_name end - def to_dot_syntax(array_path) - array_path.map do |string| - string.include?('.') ? %Q{"#{string}"} : string - end.join('.') + dispatch :alternative do + param 'Hash', :fact_hash + param 'String', :fact_name end def fact(fact_name) - facts = closure_scope['facts'] + dot_dig(closure_scope['facts'], fact_name) + end + + def alternative(alternative_hash, fact_name) + dot_dig(alternative_hash, fact_name) + end + def dot_dig(fact_hash, fact_name) # Transform the dot-notation string into an array of paths to walk. Make # sure to correctly extract double-quoted values containing dots as single # elements in the path. path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } walked_path = [] - path.reduce(facts) do |d, k| + path.reduce(fact_hash) do |d, k| return nil if d.nil? || k.nil? case @@ -55,4 +60,10 @@ Puppet::Functions.create_function(:fact) do result end end + + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') + end end -- cgit v1.2.3 From 0f35700487368357adec8a535b5c50437b208264 Mon Sep 17 00:00:00 2001 From: Reid Vandewiele Date: Mon, 3 Jul 2017 11:33:15 -0700 Subject: Revert "Allow use of fact() on other hashes" This reverts commit 409a974095a3f5b637e091494b5d14b451c5de78. After thinking about this, use case, and function naming, I think it's probably best for now to keep things simple and let `fact()` be single-purpose for looking up facts with no potentially confusing extensions. The only use case where the name makes sense is where it's being used on the `$facts` hash, and I think we'd rather in that circumstance promote the raw use of `fact()`. --- lib/puppet/functions/fact.rb | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/lib/puppet/functions/fact.rb b/lib/puppet/functions/fact.rb index 48d0d3c..dfb048b 100644 --- a/lib/puppet/functions/fact.rb +++ b/lib/puppet/functions/fact.rb @@ -18,27 +18,22 @@ Puppet::Functions.create_function(:fact) do param 'String', :fact_name end - dispatch :alternative do - param 'Hash', :fact_hash - param 'String', :fact_name + def to_dot_syntax(array_path) + array_path.map do |string| + string.include?('.') ? %Q{"#{string}"} : string + end.join('.') end def fact(fact_name) - dot_dig(closure_scope['facts'], fact_name) - end - - def alternative(alternative_hash, fact_name) - dot_dig(alternative_hash, fact_name) - end + facts = closure_scope['facts'] - def dot_dig(fact_hash, fact_name) # Transform the dot-notation string into an array of paths to walk. Make # sure to correctly extract double-quoted values containing dots as single # elements in the path. path = fact_name.scan(/([^."]+)|(?:")([^"]+)(?:")/).map {|x| x.compact.first } walked_path = [] - path.reduce(fact_hash) do |d, k| + path.reduce(facts) do |d, k| return nil if d.nil? || k.nil? case @@ -60,10 +55,4 @@ Puppet::Functions.create_function(:fact) do result end end - - def to_dot_syntax(array_path) - array_path.map do |string| - string.include?('.') ? %Q{"#{string}"} : string - end.join('.') - end end -- cgit v1.2.3