What is the best way to implement the get method in a new custom provider 
where information from the resource is required when using the Resource API?

Using the management of public or private keys as an example, the get 
method cannot return a list of resources without certain information from 
the resource, like filename.

I have managed to work around this by declaring certain parameters as 
:namevar so they are available in the provider's get method, but this 
doesn't seem like the intended use.  Here is a working example:

class Puppet::Provider::Pkey::Pkey < Puppet::ResourceApi::SimpleProvider
  def get(context, name)
    context.debug("Getting '#{name.inspect}'")
    result = [{}]
    name.each_index do |x|
      name[x].each do |key, val|
        result[x][key] = val unless key.to_s.eql? 'path'
        result[x][key] = val if (key.to_s.eql? 'path') && 
Pathname.new(val.to_s).exist?
      end
      result[x][:ensure] = 'present'
    end
    result
  end
...


This basically iterates over the :namevar values and returns them in a hash 
if the resource exists.
This is the complementary type for pkey:

require 'puppet/resource_api'


Puppet::ResourceApi.register_type(
  name: 'pkey',
  docs: <<-EOS,
@summary a pkey type
@example
pkey { 'localhost.key':
  ensure    => 'present',

       path      => '/etc/ssl/private/localhost.key'

  outform   => pem,
  algorithm => rsa,
  keysize   => 2048,
}

This type provides Puppet with the capabilities to manage private keys
EOS
  features: ['simple_get_filter'],
  attributes: {
    ensure: {
      type:     'Enum[present, absent]',
      desc:     'Whether this resource should be present or absent on the 
target system.',
      default:  'present',
    },
    name: {
      type:     'String',
      desc:     'The name of the key',
      behavior: :namevar,
    },
    path: {
      type:     'String',
      desc:     'The absolute path of the key file.',
      behavior: :namevar,
    },
...



The net effect of the code above is that the key shows to not exist if the 
file is not found, which is the desired behavior, but this seems like a 
misuse of :namevar.

Is there are better way?

This is going to be the case for any provider where state cannot be 
generated without user provided information, such as a filename (ini file, 
private key, certificate).

Thanks,
Axton

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to puppet-users+unsubscr...@googlegroups.com.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/puppet-users/1279a793-5a8b-4e66-83d2-10e821f633b6%40googlegroups.com.

Reply via email to