puppetlabs / puppetlabs/puppet-resource_api

Enhance SimpleProvider extensibility

Open
#201 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

enhancement
Dominant language
Ruby
Stars
27
Forks
42
PR merge metrics
No merged PRs in 30d

Description

Use Case

In a SimpleProvider The caller of update can always compute the old state. In some cases this information would be useful to the provider.

Describe the Solution You Would Like

Extract the common bits of the set implementation of SimpleProvider (see quoted bits below) into a re-usable utility method that makes it easier to implement a custom set method with custom handling.

https://github.com/puppetlabs/puppet-resource_api/blob/7e89026a7d629756da518c05e2bf62cf724af6ae/lib/puppet/resource_api/simple_provider.rb#L11-L35

A custom set method then could look like this:

def set(context, changes)
  changes.each do |name, change|
    is, should, name_hash = SimpleProvider.unpack_state(context, self, name, change)

    # trivial example also passing through `should`
    # more elaborate changes would be possible
    if is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'present'
      context.creating(name) do
        create(context, name_hash, is, should)
      end
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'present'
      context.updating(name) do
        update(context, name_hash, is, should)
      end
    elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'absent'
      context.deleting(name) do
        delete(context, name_hash, is)
      end
    end
  end
end

with unpack_state being defined in the SimpleProvider:

def self.unpack_state(context, provider, name, change)
  # paste https://github.com/puppetlabs/puppet-resource_api/blob/7e89026a7d629756da518c05e2bf62cf724af6ae/lib/puppet/resource_api/simple_provider.rb#L11-L35
  # here, using the provided `context` and `provider`
  [is, should, name_hash]
end

Describe Alternatives You've Considered

  • create a feature to select this behaviour - hard to make fit in naturally, and will still be limiting for other edge-cases
  • pass is down always - would break backwards compatibility
  • create a NotSoSimpleProvider - would require less work by implementors, but would only solve the specific issue.
  • create separate retrieve_current_state, retrieve_should_state, create_name_hash methods. Adding to the SimpleProvider instance's namespace is risky both for breaking backwards-compatibility and for allowing implementors to override those methods.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with lib/puppet/resource_api/simple_provider.rb, especially the set implementation at the linked lines 11-35, and trace how context, provider, name, and change are used. Done means the shared state-unpacking behavior is reusable by a custom set method while preserving the existing SimpleProvider behavior and backward compatibility.

Written by the indexing model from the issue text.

Assessment

Tech stack
ruby
Domain
backend-api-design
Issue type
Refactor
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.