~bkerensa/ubuntu/raring/puppet/new-upstream-release

« back to all changes in this revision

Viewing changes to lib/puppet/parser/functions/create_resources.rb

  • Committer: Bazaar Package Importer
  • Author(s): Chuck Short
  • Date: 2011-07-25 01:00:37 UTC
  • mfrom: (1.1.24 upstream) (3.1.25 sid)
  • Revision ID: james.westby@ubuntu.com-20110725010037-875vuxs10eboqgw3
Tags: 2.7.1-1ubuntu1
* Merge from debian unstable.  Remaining changes:
  - debian/puppetmaster-passenger.postinst: Use cacrl instead of hostcrl to
    set the location of the CRL in apache2 configuration. Fix apache2
    configuration on upgrade as well (LP: #641001)
  - move all puppet dependencies to puppet-common since all the code
    actually located in puppet-common.
  - move libagueas from a recommend to a dependency.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
Puppet::Parser::Functions::newfunction(:create_resources, :doc => '
 
2
Converts a hash into a set of resources and adds them to the catalog.
 
3
Takes two parameters:
 
4
  create_resource($type, $resources)
 
5
    Creates resources of type $type from the $resources hash. Assumes that
 
6
    hash is in the following form:
 
7
     {title=>{parameters}}
 
8
  This is currently tested for defined resources, classes, as well as native types
 
9
') do |args|
 
10
  raise ArgumentError, ("create_resources(): wrong number of arguments (#{args.length}; must be 2)") if args.length != 2
 
11
  #raise ArgumentError, 'requires resource type and param hash' if args.size < 2
 
12
  # figure out what kind of resource we are
 
13
  type_of_resource = nil
 
14
  type_name = args[0].downcase
 
15
  if type_name == 'class'
 
16
    type_of_resource = :class
 
17
  else
 
18
    if resource = Puppet::Type.type(type_name.to_sym)
 
19
      type_of_resource = :type
 
20
    elsif resource = find_definition(type_name.downcase)
 
21
      type_of_resource = :define
 
22
    else 
 
23
      raise ArgumentError, "could not create resource of unknown type #{type_name}"
 
24
    end
 
25
  end
 
26
  # iterate through the resources to create
 
27
  args[1].each do |title, params|
 
28
    raise ArgumentError, 'params should not contain title' if(params['title'])
 
29
    case type_of_resource
 
30
    when :type
 
31
      res = resource.hash2resource(params.merge(:title => title))
 
32
      catalog.add_resource(res)
 
33
    when :define
 
34
      p_resource = Puppet::Parser::Resource.new(type_name, title, :scope => self, :source => resource)
 
35
      params.merge(:name => title).each do |k,v|
 
36
        p_resource.set_parameter(k,v)
 
37
      end
 
38
      resource.instantiate_resource(self, p_resource)
 
39
      compiler.add_resource(self, p_resource)
 
40
    when :class
 
41
      klass = find_hostclass(title)
 
42
      raise ArgumentError, "could not find hostclass #{title}" unless klass
 
43
      klass.ensure_in_catalog(self, params)
 
44
      compiler.catalog.add_class([title])
 
45
    end
 
46
  end
 
47
end