Given that this is a generic function, I think a different name would be far 
better suited.

On Jan 16, 2011, at 5:50 PM, Dan Bode wrote:

> 
> 
> On Sun, Jan 16, 2011 at 3:51 PM, Luke Kanies <[email protected]> wrote:
> Why did you name it 'accepts'?
> 
> It seems like 'hash2resource', or at least something mentioning 'resource', 
> would be a better choice.
> 
> form the ticket:
> The word accepts is Teyo’s fault ;) The reason that I like accepts for this 
> is because I see it as a way of delegating that certain classes can accept 
> certain resources.
> 
> As an example, lets say that we have a nice data abstraction layer in our 
> ENC, and we want to allow operators to create host resources.
> 
> In puppet, we can specify that our platform interface accepts host resources:
> 
> class platform(
>   ...
>   $hosts ={},
> ) {
>   accepts('hosts', $hosts)
> }
> now the operators can add hosts entries when they need to, because the 
> platform class (which they have the permission to declare) accepts them.
> 
> Soon, ENC’s will allow param classes, then we can start auto generating forms 
> for the resources that certain classes accept.
> 
> I am not opposed to changing the name to be something else, I just wanted to 
> explain why I chose this name.
> 
> 
> 
>  
> 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.
> 
> 
> 
> -- 
> 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.


-- 
The only really good place to buy lumber is at a store where the lumber
has already been cut and attached together in the form of furniture,
finished, and put inside boxes.         --Dave Barry
---------------------------------------------------------------------
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.

Reply via email to