mirror of
				https://github.com/optim-enterprises-bv/puppetlabs-create_resources.git
				synced 2025-11-04 04:07:46 +00:00 
			
		
		
		
	This commit is intended to resolve some of the issues
related with create_resources support for
multiple Puppet versions.
Basically, create_resources often needs to be an
external dependency if it's used in modules, so that
those modules can work with 2.6.x.
In this case, it needs to be added to the fixtures.yaml
file. Since there is no way to specify that
certain dependant modules should only be added for
certain versions of Puppet, then create_resources will
even override the native one for versions >= 2.7.0
in all unit tests.
For this reason, the module needs to be able to
simultaneously support the targeted versions:
2.6.x ,2.7.x, 3.0.x, and 3.1.x.
This commit does the following:
   - back ports all 2.7.x changes to create_resources
(so that it will work with 3.0)
   - modifies tests, unit tests so they pass with
     all targeted versions.
   - update Rakefile and spec_helper to use
     puppetlabs_spec_helper
of coarse, there is a bigger question of if I should
even be doing this, it has several pitfalls:
  - modules will be tested with an external create_resoures
    which may not be in sync with the one in core
  - I would recommend that modules not pull in
    create_resources as a dep in their module file,
    which means
       1. it becomes an extra step for 2.6.x
       2. module's are not tested and used consistenly
		
	
		
			
				
	
	
		
			76 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
			
		
		
	
	
			76 lines
		
	
	
		
			2.9 KiB
		
	
	
	
		
			Ruby
		
	
	
	
	
	
Puppet::Parser::Functions::newfunction(:create_resources, :doc => <<-'ENDHEREDOC') do |args|
 | 
						|
    Converts a hash into a set of resources and adds them to the catalog.
 | 
						|
 | 
						|
    This function takes two mandatory arguments: a resource type, and a hash describing
 | 
						|
    a set of resources. The hash should be in the form `{title => {parameters} }`:
 | 
						|
 | 
						|
        # A hash of user resources:
 | 
						|
        $myusers = {
 | 
						|
          'nick' => { uid    => '1330',
 | 
						|
                      group  => allstaff,
 | 
						|
                      groups => ['developers', 'operations', 'release'], }
 | 
						|
          'dan'  => { uid    => '1308',
 | 
						|
                      group  => allstaff,
 | 
						|
                      groups => ['developers', 'prosvc', 'release'], }
 | 
						|
        }
 | 
						|
 | 
						|
        create_resources(user, $myusers)
 | 
						|
 | 
						|
    A third, optional parameter may be given, also as a hash:
 | 
						|
 | 
						|
        $defaults = {
 | 
						|
          'ensure'   => present,
 | 
						|
          'provider' => 'ldap',
 | 
						|
        }
 | 
						|
 | 
						|
        create_resources(user, $myusers, $defaults)
 | 
						|
 | 
						|
    The values given on the third argument are added to the parameters of each resource
 | 
						|
    present in the set given on the second argument. If a parameter is present on both
 | 
						|
    the second and third arguments, the one on the second argument takes precedence.
 | 
						|
 | 
						|
    This function can be used to create defined resources and classes, as well
 | 
						|
    as native resources.
 | 
						|
  ENDHEREDOC
 | 
						|
  raise ArgumentError, ("create_resources(): wrong number of arguments (#{args.length}; must be 2 or 3)") if args.length < 2 || args.length > 3
 | 
						|
 | 
						|
  # figure out what kind of resource we are
 | 
						|
  type_of_resource = nil
 | 
						|
  type_name = args[0].downcase
 | 
						|
  if type_name == 'class'
 | 
						|
    type_of_resource = :class
 | 
						|
  else
 | 
						|
    if resource = Puppet::Type.type(type_name.to_sym)
 | 
						|
      type_of_resource = :type
 | 
						|
    elsif resource = find_definition(type_name.downcase)
 | 
						|
      type_of_resource = :define
 | 
						|
    else
 | 
						|
      raise ArgumentError, "could not create resource of unknown type #{type_name}"
 | 
						|
    end
 | 
						|
  end
 | 
						|
  # iterate through the resources to create
 | 
						|
  defaults = args[2] || {}
 | 
						|
  args[1].each do |title, params|
 | 
						|
    params = Puppet::Util.symbolizehash(defaults.merge(params))
 | 
						|
    raise ArgumentError, 'params should not contain title' if(params[:title])
 | 
						|
    case type_of_resource
 | 
						|
    # JJM The only difference between a type and a define is the call to instantiate_resource
 | 
						|
    # for a defined type.
 | 
						|
    when :type, :define
 | 
						|
      p_resource = Puppet::Parser::Resource.new(type_name, title, :scope => self, :source => resource)
 | 
						|
      {:name => title}.merge(params).each do |k,v|
 | 
						|
        p_resource.set_parameter(k,v)
 | 
						|
      end
 | 
						|
      if type_of_resource == :define then
 | 
						|
        resource.instantiate_resource(self, p_resource)
 | 
						|
      end
 | 
						|
      compiler.add_resource(self, p_resource)
 | 
						|
    when :class
 | 
						|
      klass = find_hostclass(title)
 | 
						|
      raise ArgumentError, "could not find hostclass #{title}" unless klass
 | 
						|
      klass.ensure_in_catalog(self, params)
 | 
						|
      compiler.catalog.add_class(title)
 | 
						|
    end
 | 
						|
  end
 | 
						|
end
 |