diff options
| -rw-r--r-- | README.markdown | 19 | ||||
| -rw-r--r-- | lib/puppet/parser/functions/getparam.rb | 33 | ||||
| -rw-r--r-- | spec/functions/getparam_spec.rb | 34 | 
3 files changed, 86 insertions, 0 deletions
diff --git a/README.markdown b/README.markdown index 21f7e7f..922383c 100644 --- a/README.markdown +++ b/README.markdown @@ -209,6 +209,25 @@ Example:  - *Type*: rvalue +getparam +-------- + +Takes a resource reference and name of the parameter and returns +value of resource's parameter. + +For example: + +    define example_resource($param) { +    } + +    example_resource { "example_resource_instance": +        param => "param_value" +    } + +    getparam(Example_resource["example_resource_instance"], "param") + +- *Type*: rvalue +  getvar  ------  Lookup a variable in a remote namespace. diff --git a/lib/puppet/parser/functions/getparam.rb b/lib/puppet/parser/functions/getparam.rb new file mode 100644 index 0000000..0962656 --- /dev/null +++ b/lib/puppet/parser/functions/getparam.rb @@ -0,0 +1,33 @@ +# Test whether a given class or definition is defined +require 'puppet/parser/functions' + +Puppet::Parser::Functions.newfunction(:getparam, +                                      :type => :rvalue, +                                      :doc => <<-'ENDOFDOC' +Takes a resource reference and name of the parameter and +returns value of resource's parameter. + +*Examples:* + +    define example_resource($param) { +    } + +    example_resource { "example_resource_instance": +        param => "param_value" +    } + +    getparam(Example_resource["example_resource_instance"], "param") + +Would return: param_value +ENDOFDOC +) do |vals| +  reference, param = vals +  raise(ArgumentError, 'Must specify a reference') unless reference +  raise(ArgumentError, 'Must specify name of a parameter') unless param and param.instance_of? String + +  if resource = findresource(reference.to_s) +    return resource[param] if resource[param] +  end + +  return '' +end diff --git a/spec/functions/getparam_spec.rb b/spec/functions/getparam_spec.rb new file mode 100644 index 0000000..d9c50a6 --- /dev/null +++ b/spec/functions/getparam_spec.rb @@ -0,0 +1,34 @@ +#! /usr/bin/env ruby -S rspec +require 'spec_helper' + +require 'rspec-puppet' +describe 'getparam' do +  describe 'when a resource is not specified' do +    it do +      should run.with_params().and_raise_error(ArgumentError) +      should run.with_params('User[dan]').and_raise_error(ArgumentError) +      should run.with_params('User[dan]', {}).and_raise_error(ArgumentError) +      should run.with_params('User[dan]', '').and_return('') +    end +  end +  describe 'when compared against a resource with no params' do +    let :pre_condition do +      'user { "dan": }' +    end +    it do +      should run.with_params('User[dan]', 'shell').and_return('') +    end +  end + +  describe 'when compared against a resource with params' do +    let :pre_condition do +      'user { "dan": ensure => present, shell => "/bin/sh", managehome => false}' +    end +    it do +      should run.with_params('User[dan]', 'shell').and_return('/bin/sh') +      should run.with_params('User[dan]', '').and_return('') +      should run.with_params('User[dan]', 'ensure').and_return('present') +      should run.with_params('User[dan]', 'managehome').and_return(false) +    end +  end +end  | 
