]> gitweb.fluxo.info Git - puppet-stdlib.git/commitdiff
Add getparam function to get defined resource parameters
authorJaka Hudoklin <jakahudoklin@gmail.com>
Wed, 2 Jan 2013 12:10:43 +0000 (12:10 +0000)
committerJeff McCune <jeff@puppetlabs.com>
Thu, 10 Jan 2013 01:51:12 +0000 (17:51 -0800)
As far as i know there's no other puppet-dsl-like way to get parameter of
defined resource, so that's why i implemented getparam function, which takes
resource reference and parameter name and returns parameter value.

    Here's another example why this function is really useful:

    define config($path, $config_param1, $config_param2) { }

    define example_resource($config) {
        $path = getparam($config, "path")

        notice("Path is $path")
    }

    define example_resource2($example_resource, $config = getparam($example_resource, "config")) {
        $config_param1 = getparam($config, "config_param1")

        notice("Config parameter is $config_param1")
    }

    define example_resource3($example_resource, $config = getparam($example_resource, "config")) {
        $config_param2 = getparam($config, "config_param2")

        notice("Config parameter is $config_param2")
    }

    class test_getparam {

        config { "config_instance":
            path => "/some/config/path",
            config_param1 => "someconfigtext1",
            config_param2 => "someconfigtext2",
        }

        example_resource { "example_resource_instance":
            config => Config["config_instance"]
        }

        example_resource2 { "example_resource_instance":
            example_resource => Example_resource["example_resource_instance"]
        }

        example_resource3 { "example_resource_instance":
            example_resource => Example_resource2["example_resource_instance"]
        }
    }

    class { "test_getparam": }

README.markdown
lib/puppet/parser/functions/getparam.rb [new file with mode: 0644]
spec/functions/getparam_spec.rb [new file with mode: 0644]

index 21f7e7f9a9a75e55798d2ec1abaae25889f69a8e..922383cbd35a4282f106dcdf86294ab8f251b7d2 100644 (file)
@@ -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 (file)
index 0000000..0962656
--- /dev/null
@@ -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 (file)
index 0000000..d9c50a6
--- /dev/null
@@ -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