puppetlabs / puppet-resource_api

This library provides a simple way to write new native resources for https://puppet.com.
Apache License 2.0
27 stars 41 forks source link

Find a way to pass information from the manifest to the `get` method. #242

Open DavidS opened 5 years ago

DavidS commented 5 years ago

Example for how this is currently used/implemented:

https://github.com/puppetlabs/puppetlabs-sqlserver/blob/b36eecf3fdadb3d45c9691fcac2e92c66fa3ad30/lib/puppet/provider/sqlserver_tsql/mssql.rb#L16-L26

  def get_config(instance = resource[:instance])
    config_resc = resource.catalog.resources.find do |resc|
      resc.title =~ %r{Sqlserver::Config} &&
        resc.original_parameters[:instance_name] =~ %r{#{instance}}i
    end
    if config_resc.nil?
      raise("Sqlserver_tsql[#{resource.title}] was unable to retrieve the config, please ensure the catalog contains sqlserver::config{'#{resource[:instance]}':}")
    end

    config_resc.original_parameters
  end

Puppet::Type's resource.catalog.resources provides access to other resources in the current catalog.

Possible implementation:

chadh commented 4 years ago

I don't need full manifest information, but I would love the get() method for a resource to be able to get the full context on all of that type of resource specified in the manifest. With simple_get_filter I can get a list of resource names, but not the arguments.

My use case is the puppetlabs-git git_config provider. The get() method would ostensibly be able to enumerate all git configs on a system, but the provider supports setting git configs per user. So enumerating all configs means iterating over all users, which in some environments is not an option. If the set of resources from the catalog was available, I could at least enumerate the current status of those. I realize this means that we can't ensure absent in an idempotent manner. It would also allow me to match the functionality of the low-level provider (which specifies value(), value=(), and mk_resource_methods()).

chadh commented 4 years ago

my use case above does not apply here after all. I misunderstood that name and names parameters are actually hashes with all namevars, which is sufficient for my case.

DavidS commented 3 years ago

Edited the description with a possible implementation path.

kreeuwijk commented 3 years ago

Implemented in #284