To me it seems a bit special-purpose and could be made available in a module 
pretty easily, which is what I'd recommend at this point.

How many others would use this?

On Feb 3, 2011, at 6:11 AM, Dean Wilson wrote:

> This was asked about on the puppet-users lists recently so I thought I'd
> have a go at writing it. Is this something we'd want in the core?
> 
> ---
> lib/puppet/parser/functions/ip_in_range.rb     |   15 +++++++++
> spec/unit/parser/functions/ip_in_range_spec.rb |   37 ++++++++++++++++++++++++
> 2 files changed, 52 insertions(+), 0 deletions(-)
> create mode 100644 lib/puppet/parser/functions/ip_in_range.rb
> create mode 100644 spec/unit/parser/functions/ip_in_range_spec.rb
> 
> diff --git a/lib/puppet/parser/functions/ip_in_range.rb 
> b/lib/puppet/parser/functions/ip_in_range.rb
> new file mode 100644
> index 0000000..b762b45
> --- /dev/null
> +++ b/lib/puppet/parser/functions/ip_in_range.rb
> @@ -0,0 +1,15 @@
> +require 'ipaddr'
> +
> +Puppet::Parser::Functions::newfunction(:ip_in_range, :type => :rvalue, :doc 
> => "Return true if the ip is within the network range.") do |args|
> +
> +    raise Puppet::ParseError, "ip_in_range requires two arguments, ip and 
> range" unless args.length == 2
> +
> +    begin
> +        ip    = IPAddr.new( args[0] )
> +        range = IPAddr.new( args[1] )
> +    rescue ArgumentError => e
> +        raise Puppet::ParseError, e.to_s
> +    end
> +
> +    return range.include?( ip )
> +end
> diff --git a/spec/unit/parser/functions/ip_in_range_spec.rb 
> b/spec/unit/parser/functions/ip_in_range_spec.rb
> new file mode 100644
> index 0000000..01e2947
> --- /dev/null
> +++ b/spec/unit/parser/functions/ip_in_range_spec.rb
> @@ -0,0 +1,37 @@
> +#! /usr/bin/env ruby
> +
> +require File.expand_path(File.dirname(__FILE__) + '/../../../spec_helper')
> +
> +describe "ip_in_range function" do
> +
> +  before :each do
> +    @scope = Puppet::Parser::Scope.new
> +  end
> +
> +  it "should exist" do
> +    Puppet::Parser::Functions.function("ip_in_range").should == 
> "function_ip_in_range"
> +  end
> +
> +  it "should raise a ParseError if there are less than 2 arguments" do
> +    lambda { @scope.function_ip_in_range([]) }.should( 
> raise_error(Puppet::ParseError))
> +  end
> +
> +  it "should return true when an ip is in the range" do
> +    @scope.function_ip_in_range( ["192.168.100.12", "192.168.100.0/24"] 
> ).should be_true
> +  end
> +
> +  it "should return false when an ip is not in the range" do
> +    @scope.function_ip_in_range( ["10.10.10.10", "192.168.100.0/24"] 
> ).should be_false
> +  end
> +
> +  it "should raise a ParseError when given an invalid IP address" do
> +    lambda { @scope.function_ip_in_range( ["a.b.c.d", "192.168.100.0/24"] ) 
> }.should( raise_error(Puppet::ParseError))
> +  end
> +
> +  it "should raise a ParseError when given an invalid IP range" do
> +    lambda { @scope.function_ip_in_range( ["192.168.100.12", 
> "192.168.100.0/CC"] ) }.should( raise_error(Puppet::ParseError))
> +    lambda { @scope.function_ip_in_range( ["192.168.100.12", "192.168/24"] ) 
>       }.should( raise_error(Puppet::ParseError))
> +    lambda { @scope.function_ip_in_range( ["192.168.100.12", 
> "aaa.168.100.0/CC"] ) }.should( raise_error(Puppet::ParseError))
> +  end
> +
> +end
> -- 
> 1.7.1
> 
> 
> -- 
> Dean Wilson
> http://www.unixdaemon.net       @unixdaemon
> http://www.puppetcookbook.com   @puppetcookbook
> 
> -- 
> 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.
> 


-- 
'Tis better to be silent and be thought a fool, than to speak and
remove all doubt.              --Abraham Lincoln
---------------------------------------------------------------------
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