Why did you name it 'accepts'?
It seems like 'hash2resource', or at least something mentioning 'resource',
would be a better choice.
On Jan 11, 2011, at 5:10 PM, Dan Bode wrote:
> The accepts function takes 2 arguments:
> $type - the Resources Type
> $resoures - a hash of resources of the form:
> {title=>{attr=>value}, title2=>{attr2=>value,attr3=>value}}
>
> Signed-off-by: Dan Bode <[email protected]>
> ---
> lib/puppet/parser/functions/accepts.rb | 15 ++++++++
> spec/unit/parser/functions/accepts_spec.rb | 49 ++++++++++++++++++++++++++++
> 2 files changed, 64 insertions(+), 0 deletions(-)
> create mode 100644 lib/puppet/parser/functions/accepts.rb
> create mode 100644 spec/unit/parser/functions/accepts_spec.rb
>
> diff --git a/lib/puppet/parser/functions/accepts.rb
> b/lib/puppet/parser/functions/accepts.rb
> new file mode 100644
> index 0000000..0b54399
> --- /dev/null
> +++ b/lib/puppet/parser/functions/accepts.rb
> @@ -0,0 +1,15 @@
> +Puppet::Parser::Functions::newfunction(:accepts, :doc => '
> +Converts a hash into resources and adds them to the catalog.
> +Takes two parameters:
> + accepts($type, $resources)
> +Creates resources of type $type from the $resources hash. Assumes that hash
> is in the following form:
> + {title=>{attr=>value}}
> +') do |args|
> + raise ArgumentError, 'requires resource type and hash' unless args.size ==
> 2
> + args[1].each do |title, params|
> + # eventaully, I would like another argument that specifies constraints
> + raise ArgumentError, 'params should not contain title' if params['title']
> + resource =
> Puppet::Type.type(args[0].to_sym).hash2resource(params.merge(:title => title))
> + catalog.add_resource(resource)
> + end
> +end
> diff --git a/spec/unit/parser/functions/accepts_spec.rb
> b/spec/unit/parser/functions/accepts_spec.rb
> new file mode 100644
> index 0000000..25d9a7e
> --- /dev/null
> +++ b/spec/unit/parser/functions/accepts_spec.rb
> @@ -0,0 +1,49 @@
> +#!/usr/bin/env ruby
> +
> +require File.dirname(__FILE__) + '/../../../spec_helper'
> +
> +describe "the 'accepts' function" do
> +
> + before :each do
> + Puppet::Node::Environment.stubs(:current).returns(nil)
> + @compiler = Puppet::Parser::Compiler.new(Puppet::Node.new("foo"))
> + @scope = Puppet::Parser::Scope.new(:compiler => @compiler)
> + @main = Puppet::Resource.new('stage', 'main')
> + end
> +
> + it "should exist" do
> + Puppet::Parser::Functions.function("accepts").should ==
> "function_accepts"
> + end
> +
> + it "should create multiple resources" do
> + resources = {'foo'=>{'ensure'=>'present', 'gid'=>'1'},
> + 'bar'=>{'home'=>'/home/bar', 'noop'=>true}}
> + @scope.function_accepts(["user", resources])
> + resources.each do |title, params|
> + res =
> Puppet::Type.type('user').hash2resource(params.merge(:title=>title))
> + @scope.catalog.resource('user', title).should == res
> + end
> + end
> +
> + it "should create a single resource" do
> + resources = {'foo'=> {}}
> + @scope.function_accepts(["user", resources])
> + resources.each do |title, params|
> + res = Puppet::Resource.new('user', title, params)
> + @scope.catalog.resource('user', title).should == res
> + end
> + end
> +
> + it 'should fail if resource has title set in params' do
> + resources = {'foo'=> {'title'=>'bar'}}
> + lambda { @scope.function_accepts(["user", resources]) }.should
> raise_error(ArgumentError)
> + end
> +
> + it 'should accept an empty resource hash' do
> + resources = {}
> + @scope.function_accepts(["user", resources])
> + # should only have main stage
> + @scope.catalog.resources.size.should == 1
> + end
> +
> +end
> --
> 1.5.5.6
>
> --
> You received this message because you are subscribed to the Google Groups
> "Puppet Developers" group.
> To post to this group, send email to [email protected].
> To unsubscribe from this group, send email to
> [email protected].
> For more options, visit this group at
> http://groups.google.com/group/puppet-dev?hl=en.
>
--
Always be wary of any helpful item that weighs less than its operating
manual. -- Terry Pratchett
---------------------------------------------------------------------
Luke Kanies -|- http://puppetlabs.com -|- +1(615)594-8199
--
You received this message because you are subscribed to the Google Groups
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/puppet-dev?hl=en.