Skip to content

Commit 5d360de

Browse files
authored
Merge pull request #316 from david22swan/CONT-761/main/custom_generate
(CAT-761) Add custom_generate as a feature
2 parents 8aa9884 + 35dc204 commit 5d360de

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

lib/puppet/resource_api.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,23 @@ def rsapi_canonicalized_target_state
136136
@rsapi_canonicalized_target_state
137137
end
138138

139+
# Method is used to custom generate resources which are then applied by Puppet
140+
def generate
141+
# If feature `custom_generate` has been set then call the generate function within the provider and return the given results
142+
return unless type_definition&.feature?('custom_generate')
143+
should_hash = rsapi_canonicalized_target_state
144+
is_hash = rsapi_current_state
145+
title = rsapi_title
146+
147+
# Ensure that a custom `generate` method has been created within the provider
148+
raise(Puppet::DevError, 'No generate method found within the types provider') unless my_provider.respond_to?(:generate)
149+
# Call the providers custom `generate` method
150+
rules_resources = my_provider.generate(context, title, is_hash, should_hash)
151+
152+
# Return array of resources
153+
rules_resources
154+
end
155+
139156
def rsapi_current_state
140157
refresh_current_state unless @rsapi_current_state
141158
@rsapi_current_state

lib/puppet/resource_api/type_definition.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def validate_schema(definition, attr_key)
3636
Puppet::ResourceApi::DataTypeHandling.validate_ensure(definition)
3737

3838
definition[:features] ||= []
39-
supported_features = %w[supports_noop canonicalize custom_insync remote_resource simple_get_filter].freeze
39+
supported_features = %w[supports_noop canonicalize custom_insync remote_resource simple_get_filter custom_generate].freeze
4040
unknown_features = definition[:features] - supported_features
4141
Puppet.warning("Unknown feature detected: #{unknown_features.inspect}") unless unknown_features.empty?
4242
end

spec/puppet/resource_api_spec.rb

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1690,6 +1690,61 @@ def set(_context, changes)
16901690
end
16911691
end
16921692

1693+
context 'with a provider that has a custom
1694+
generate', agent_test: true do
1695+
let(:definition) do
1696+
{
1697+
name: 'generator',
1698+
desc: 'some desc',
1699+
attributes: {
1700+
name: {
1701+
type: 'String',
1702+
desc: '',
1703+
behaviour: :namevar,
1704+
},
1705+
test_string: {
1706+
type: 'String',
1707+
desc: '',
1708+
},
1709+
},
1710+
features: ['custom_generate'],
1711+
}
1712+
end
1713+
let(:provider_class) do
1714+
Class.new do
1715+
def generate(_context, _title, _is, should)
1716+
# Unless purge is true, return an empty array
1717+
return [] unless should[:purge]
1718+
1719+
# gather a list of all rules present on the system
1720+
rules_resources = Puppet::Type.type(:iptables).instances
1721+
1722+
rules_resources
1723+
end
1724+
1725+
def get(_context)
1726+
[]
1727+
end
1728+
1729+
attr_reader :last_changes
1730+
def set(_context, changes)
1731+
@last_changes = changes
1732+
end
1733+
end
1734+
end
1735+
1736+
before(:each) do
1737+
stub_const('Puppet::Provider::Generator', Module.new)
1738+
stub_const('Puppet::Provider::Generator::Generator', provider_class)
1739+
end
1740+
1741+
it { expect { described_class.register_type(definition) }.not_to raise_error }
1742+
1743+
it 'is seen as a supported feature' do
1744+
expect(Puppet).not_to receive(:warning).with(%r{Unknown feature detected:.*})
1745+
end
1746+
end
1747+
16931748
context 'when retrieving instances' do
16941749
it('returns an Array') { expect(type.instances).to be_a Array }
16951750
it('returns an array of Type instances') { expect(type.instances[0]).to be_a Puppet::Type.type(:canonicalizer) }

0 commit comments

Comments
 (0)