Skip to content

SimpleProvider name fixes #302

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Nov 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 19 additions & 19 deletions lib/puppet/resource_api/simple_provider.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,45 +6,47 @@ module Puppet::ResourceApi
# This class provides a default implementation for set(), when your resource does not benefit from batching.
# Instead of processing changes yourself, the `create`, `update`, and `delete` functions, are called for you,
# with proper logging already set up.
# Note that your type needs to use `name` as its namevar, and `ensure` in the conventional way to signal presence
# and absence of resources.
# Note that your type needs to use `ensure` in the conventional way with values of `prsesent`
# and `absent` to signal presence and absence of resources.
class SimpleProvider
def set(context, changes)
namevars = context.type.namevars

changes.each do |name, change|
is = if context.type.feature?('simple_get_filter')
change.key?(:is) ? change[:is] : (get(context, [name]) || []).find { |r| r[:name] == name }
change.key?(:is) ? change[:is] : (get(context, [name]) || []).find { |r| SimpleProvider.build_name(namevars, r) == name }
else
change.key?(:is) ? change[:is] : (get(context) || []).find { |r| r[:name] == name }
change.key?(:is) ? change[:is] : (get(context) || []).find { |r| SimpleProvider.build_name(namevars, r) == name }
end
context.type.check_schema(is) unless change.key?(:is)

should = change[:should]

raise 'SimpleProvider cannot be used with a Type that is not ensurable' unless context.type.ensurable?

is = SimpleProvider.create_absent(:name, name) if is.nil?
should = SimpleProvider.create_absent(:name, name) if should.nil?
is_ensure = is.nil? ? 'absent' : is[:ensure].to_s
should_ensure = should.nil? ? 'absent' : should[:ensure].to_s

name_hash = if context.type.namevars.length > 1
name_hash = if namevars.length > 1
# pass a name_hash containing the values of all namevars
name_hash = {}
context.type.namevars.each do |namevar|
namevars.each do |namevar|
name_hash[namevar] = change[:should][namevar]
end
name_hash
else
name
end

if is[:ensure].to_s == 'absent' && should[:ensure].to_s == 'present'
if is_ensure == 'absent' && should_ensure == 'present'
context.creating(name) do
create(context, name_hash, should)
end
elsif is[:ensure].to_s == 'present' && should[:ensure].to_s == 'absent'
elsif is_ensure == 'present' && should_ensure == 'absent'
context.deleting(name) do
delete(context, name_hash)
end
elsif is[:ensure].to_s == 'present'
elsif is_ensure == 'present'
context.updating(name) do
update(context, name_hash, should)
end
Expand All @@ -65,14 +67,12 @@ def delete(_context, _name)
end

# @api private
def self.create_absent(namevar, title)
result = if title.is_a? Hash
title.dup
else
{ namevar => title }
end
result[:ensure] = 'absent'
result
def self.build_name(namevars, resource_hash)
if namevars.size > 1
Hash[namevars.map { |attr| [attr, resource_hash[attr]] }]
else
resource_hash[namevars[0]]
end
end
end
end
Loading