Ask Your Question
0

Why can't I find @resource in a custom provider?

asked 2014-09-04 18:31:30 -0500

st33v gravatar image

updated 2014-09-04 23:57:54 -0500

I’m trying to extend the puppetlabs splunk module so support the installation of splunk to directories other than /opt/splunk or /opt/splunkforwarder. My plan was to set a ‘prefix’ in the params.pp file like ‘/apps/my-‘ so that splunk can find the config files in /apps/my-splunk/etc/local/inputs.conf for example. The tweaks to inputs and outputs are handled by the providers in the splunk module.

So I added something like this:

  newparam(:prefix) do
    desc 'the prefix to the splunk installations'
  end

to the splunk input type and then I modified the corresponding provider like this:

Puppet::Type.type(:splunk_input).provide(
  :ini_setting,
  # set ini_setting as the parent provider
  :parent => Puppet::Type.type(:ini_setting).provider(:ruby)
) do
  # hard code the file path (this allows purging)
  def self.file_path
    case Facter.value(:osfamily)
    when 'windows'
      'C:\Program Files\Splunk\etc\system\local\inputs.conf'
    else
      “#{@resource[:prefix]}splunk/etc/system/local/inputs.conf”
    end
  end
end

This results in ‘Could not evaluate: undefined method `[]' for nil:NilClass’ relating to @resource when I try to use it like this:

splunk_input { 'default_host':
  prefix  => '/apps/mrc-',
  section => 'default',
  setting => 'host',
  value   => $::clientcert,
  tag     => 'splunk_server',
}

It’s as if resource doesn’t exist in this context. This provider has a parent that is simply the provider ini_setting module so I took a look at the readme for that module. Looking at this section, there’s an example of exactly what I’m trying to do (minus the prefix part). There is also evidence of a @resource hash that can be used pull variables in from the declaration but I haven’t been able to access any @resource or resource[:prefix] via any permutation of 'resource[:prefix]','@resource', self.resource, etc.

So what’s the deal? Do I not have the ability to access @resource from within self.filepath? I've used @resource in other providers so is the problem that this is a child class of inisetting? Some kind of class scope conflict?

--steve

edit retag flag offensive close merge delete

2 Answers

Sort by » oldest newest most voted
0

answered 2014-09-05 07:04:55 -0500

domcleal gravatar image

updated 2014-09-06 09:52:36 -0500

Defining the method using "self." means it's a class, rather than instance method. In Puppet terms, the file path is defined at the provider rather than individual resource level, so it doesn't make sense to access details of a resource.

You should be able to define the path like this instead:

def file_path
  case Facter.value(:osfamily)
  when 'windows'
    'C:\Program Files\Splunk\etc\system\local\inputs.conf'
  else
    "#{@resource[:prefix]}splunk/etc/system/local/inputs.conf"
  end
end

This will override the filepath method defined at the resource/instance level in the base inifile provider: ruby.rb#filepath

The downside of this means that you won't get purging support, so will be unable to purge unmanaged inifile entries. This is because the prefix is specified per resource, so Puppet would be unable to find the file and retrieve the inifile entry resources inside it.

edit flag offensive delete link more
0

answered 2014-09-09 09:16:41 -0500

st33v gravatar image

Thank you,

Your explanation certainly clears things up for me. However, the code you provided doesn't work as it still cannot find @resource. Is there simply no good way to make the path configurable? I was thinking that I might use a fact that finds the location of splunk and pass in the path that way but I'm not sure if that would even work... it would likely require multiple puppet runs to configure splunk... install it the first time and then the path fact would exist on the second run. Blech... not a good solution either.

I've worked around the problem by creating symlinks in /etc/splunk and /etc/splunkforwarder so that I have a predictable path to the configuration but it seems like a really awful hack to me.

edit flag offensive delete link more

Comments

Can you share the full error message and the code please?

domcleal gravatar imagedomcleal ( 2014-09-10 09:03:19 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

2 followers

Stats

Asked: 2014-09-04 18:31:30 -0500

Seen: 549 times

Last updated: Sep 09 '14