Skip to content
This repository has been archived by the owner on Jun 13, 2019. It is now read-only.

Commit

Permalink
Compatibility with puppet 3.0, hiera 1.1 and hiera-puppet 1.0.
Browse files Browse the repository at this point in the history
  • Loading branch information
amfranz committed Oct 19, 2012
1 parent b47fbab commit 46630bd
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 110 deletions.
10 changes: 9 additions & 1 deletion lib/hiera/backend/rspec_backend.rb
Expand Up @@ -6,7 +6,7 @@ def initialize
end

def lookup(key, scope, order_override, resolution_type)
answer = Backend.empty_answer(resolution_type)
answer = nil

Hiera.debug("Looking up #{key} in RSpec backend")

Expand All @@ -26,6 +26,12 @@ def lookup(key, scope, order_override, resolution_type)
next
end

# Extra logging that we found the key. This can be outputted
# multiple times if the resolution type is array or hash but that
# should be expected as the logging will then tell the user ALL the
# places where the key is found.
Hiera.debug("Found #{key} in #{source}")

# for array resolution we just append to the array whatever
# we find, we then goes onto the next file and keep adding to
# the array
Expand All @@ -35,9 +41,11 @@ def lookup(key, scope, order_override, resolution_type)
case resolution_type
when :array
raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
answer ||= []
answer << new_answer
when :hash
raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
answer ||= {}
answer = new_answer.merge answer
else
answer = new_answer
Expand Down
117 changes: 12 additions & 105 deletions lib/rspec-hiera-puppet/puppet.rb
Expand Up @@ -18,126 +18,33 @@ def compile

def register_function_hiera(spec)
Puppet::Parser::Functions.newfunction(:hiera, :type => :rvalue) do |*args|
# Functions called from puppet manifests that look like this:
# lookup("foo", "bar")
# internally in puppet are invoked: func(["foo", "bar"])
#
# where as calling from templates should work like this:
# scope.function_lookup("foo", "bar")
#
# Therefore, declare this function with args '*args' to accept any number
# of arguments and deal with puppet's special calling mechanism now:
if args[0].is_a?(Array)
args = args[0]
end

key = args[0]
default = args[1]
override = args[2]

require 'hiera'
require 'hiera/scope'

hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))

if self.respond_to?("[]")
hiera_scope = self
else
hiera_scope = Hiera::Scope.new(self)
end

answer = hiera.lookup(key, default, hiera_scope, override, :priority)

raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.nil?

return answer
require 'hiera_puppet'
key, default, override = HieraPuppet.parse_args(args)
HieraPuppet.lookup(key, default, self, override, :priority)
end
end

def register_function_hiera_array(spec)
Puppet::Parser::Functions.newfunction(:hiera_array, :type => :rvalue) do |*args|
if args[0].is_a?(Array)
args = args[0]
end

key = args[0]
default = args[1]
override = args[2]

require 'hiera'
require 'hiera/scope'

hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))

if self.respond_to?("[]")
hiera_scope = self
else
hiera_scope = Hiera::Scope.new(self)
end

answer = hiera.lookup(key, default, hiera_scope, override, :array)

raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?

answer
require 'hiera_puppet'
key, default, override = HieraPuppet.parse_args(args)
HieraPuppet.lookup(key, default, self, override, :array)
end
end

def register_function_hiera_hash(spec)
Puppet::Parser::Functions.newfunction(:hiera_hash, :type => :rvalue) do |*args|
if args[0].is_a?(Array)
args = args[0]
end

raise(Puppet::ParseError, "Please supply a parameter to perform a Hiera lookup") if args.empty?

key = args[0]
default = args[1]
override = args[2]

require 'hiera'
require 'hiera/scope'

hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))

if self.respond_to?("{}")
hiera_scope = self
else
hiera_scope = Hiera::Scope.new(self)
end

answer = hiera.lookup(key, default, hiera_scope, override, :hash)

raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?

answer
require 'hiera_puppet'
key, default, override = HieraPuppet.parse_args(args)
HieraPuppet.lookup(key, default, self, override, :hash)
end
end

def register_function_hiera_include(spec)
Puppet::Parser::Functions.newfunction(:hiera_include) do |*args|
if args[0].is_a?(Array)
args = args[0]
end

key = args[0]
default = args[1]
override = args[2]

require 'hiera'
require 'hiera/scope'

hiera = Hiera.new(:config => spec.hiera_config.merge(:logger => 'puppet'))

if self.respond_to?("[]")
hiera_scope = self
else
hiera_scope = Hiera::Scope.new(self)
end

answer = hiera.lookup(key, default, hiera_scope, override, :array)

raise(Puppet::ParseError, "Could not find data item #{key} in any Hiera data file and no default supplied") if answer.empty?
require 'hiera_puppet'
key, default, override = HieraPuppet.parse_args(args)
answer = HieraPuppet.lookup(key, default, self, override, :array)

method = Puppet::Parser::Functions.function(:include)
send(method, answer)
Expand Down
2 changes: 1 addition & 1 deletion lib/rspec-hiera-puppet/version.rb
@@ -1,7 +1,7 @@
module Rspec
module Hiera
module Puppet
VERSION = "0.3.0"
VERSION = "1.0.0"
end
end
end
6 changes: 3 additions & 3 deletions rspec-hiera-puppet.gemspec
Expand Up @@ -15,9 +15,9 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.version = Rspec::Hiera::Puppet::VERSION

gem.add_dependency('puppet', '>= 2.7')
gem.add_dependency('hiera', '>= 0.3')
gem.add_dependency('hiera-puppet', '>= 0.3')
gem.add_dependency('puppet', '>= 3.0')
gem.add_dependency('hiera', '>= 1.0')
gem.add_dependency('hiera-puppet', '>= 1.0')
gem.add_dependency('rspec')
gem.add_dependency('rspec-puppet')
end

1 comment on commit 46630bd

@johnl
Copy link

@johnl johnl commented on 46630bd Mar 25, 2013

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Does this really now depend on puppet >= 3.0 ? Or does it work with 2.7 still too?

Please sign in to comment.