Skip to content
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

(PDK-946) Passes ensure values to puppet as symbols. #74

Merged
merged 2 commits into from
May 4, 2018
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
36 changes: 28 additions & 8 deletions lib/puppet/resource_api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ def register_type(definition)
raise Puppet::DevError, 'requires a Hash as definition, not %{other_type}' % { other_type: definition.class } unless definition.is_a? Hash
raise Puppet::DevError, 'requires a name' unless definition.key? :name
raise Puppet::DevError, 'requires attributes' unless definition.key? :attributes
validate_ensure(definition)

definition[:features] ||= []
supported_features = %w[supports_noop canonicalize remote_resource simple_get_filter].freeze
Expand Down Expand Up @@ -82,6 +83,7 @@ def type_definition
@missing_params = []
definition[:attributes].each do |name, options|
type = Puppet::Pops::Types::TypeParser.singleton.parse(options[:type])

# skip read only vars and the namevar
next if [:read_only, :namevar].include? options[:behaviour]

Expand Down Expand Up @@ -145,12 +147,20 @@ def type_definition
end
end

if name == :ensure
def insync?(is)
rs_value.to_s == is.to_s
end
end

type = Puppet::Pops::Types::TypeParser.singleton.parse(options[:type])
if param_or_property == :newproperty
define_method(:should) do
if type.is_a? Puppet::Pops::Types::PBooleanType
# work around https://tickets.puppetlabs.com/browse/PUP-2368
rs_value ? :true : :false # rubocop:disable Lint/BooleanSymbol
elsif name == :ensure && rs_value.is_a?(String)
rs_value.to_sym
else
rs_value
end
Expand Down Expand Up @@ -205,12 +215,11 @@ def type_definition
Puppet::ResourceApi.def_newvalues(self, param_or_property, %r{})
when Puppet::Pops::Types::PBooleanType
Puppet::ResourceApi.def_newvalues(self, param_or_property, 'true', 'false')
# rubocop:disable Lint/BooleanSymbol
aliasvalue true, 'true'
aliasvalue false, 'false'
aliasvalue :true, 'true'
aliasvalue :false, 'false'
# rubocop:enable Lint/BooleanSymbol
aliasvalue :true, 'true' # rubocop:disable Lint/BooleanSymbol
aliasvalue :false, 'false' # rubocop:disable Lint/BooleanSymbol

when Puppet::Pops::Types::PIntegerType
Puppet::ResourceApi.def_newvalues(self, param_or_property, %r{^-?\d+$})
when Puppet::Pops::Types::PFloatType, Puppet::Pops::Types::PNumericType
Expand Down Expand Up @@ -266,9 +275,12 @@ def call_provider(value); end
end
else
result[namevar_name] = title
result[:ensure] = 'absent' if definition[:attributes].key?(:ensure)
result[:ensure] = :absent if type_definition.ensurable?
end

# puppet needs ensure to be a symbol
result[:ensure] = result[:ensure].to_sym if type_definition.ensurable? && result[:ensure].is_a?(String)

raise_missing_attrs

@rapi_current_state = current_state
Expand All @@ -286,7 +298,6 @@ def call_provider(value); end

retrieve unless @rapi_current_state

# require 'pry'; binding.pry
return if @rapi_current_state == target_state

Puppet.debug("Target State: #{target_state.inspect}")
Expand Down Expand Up @@ -315,7 +326,7 @@ def call_provider(value); end

define_method(:raise_missing_attrs) do
error_msg = "The following mandatory attributes were not provided:\n * " + @missing_attrs.join(", \n * ")
raise Puppet::ResourceError, error_msg if @missing_attrs.any? && (value(:ensure) != 'absent' && !value(:ensure).nil?)
raise Puppet::ResourceError, error_msg if @missing_attrs.any? && (value(:ensure) != :absent && !value(:ensure).nil?)
end

define_method(:raise_missing_params) do
Expand Down Expand Up @@ -489,6 +500,15 @@ def self.try_mungify(type, value, error_msg_prefix)
# an error :-(
inferred_type = Puppet::Pops::Types::TypeCalculator.infer_set(value)
error_msg = Puppet::Pops::Types::TypeMismatchDescriber.new.describe_mismatch(error_msg_prefix, type, inferred_type)
return [nil, error_msg] # the entire function is using returns for clarity # rubocop:disable Style/RedundantReturn
[nil, error_msg]
end

def self.validate_ensure(definition)
return unless definition[:attributes].key? :ensure
options = definition[:attributes][:ensure]
type = Puppet::Pops::Types::TypeParser.singleton.parse(options[:type])

return if type.is_a?(Puppet::Pops::Types::PEnumType) && type.values.sort == %w[absent present].sort
raise Puppet::DevError, '`:ensure` attribute must have a type of: `Enum[present, absent]`'
end
end
2 changes: 1 addition & 1 deletion spec/acceptance/device_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
end
it 'manages resources on the target system' do
stdout_str, status = Open3.capture2e("puppet resource #{common_args} device_provider foo ensure=present #{default_type_values}")
expect(stdout_str).to match %r{Notice: /Device_provider\[foo\]/ensure: ensure changed 'absent' to 'present'}
expect(stdout_str).to match %r{Notice: /Device_provider\[foo\]/ensure: defined 'ensure' as 'present'}
expect(status).to eq 0
end

Expand Down
12 changes: 9 additions & 3 deletions spec/acceptance/validation_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
it 'allows removing' do
output, status = Open3.capture2e("puppet resource #{common_args} test_validation foo ensure=absent param=2")
expect(output.strip).to match %r{^test_validation}
expect(output.strip).to match %r{Test_validation\[foo\]/ensure: ensure changed 'present' to 'absent'}
expect(output.strip).to match %r{Test_validation\[foo\]/ensure: undefined 'ensure' from 'present'}
expect(status.exitstatus).to eq 0
end

Expand All @@ -52,6 +52,12 @@
expect(status.exitstatus).to eq 1
end

it 'does not change attribute values on delete' do
output, status = Open3.capture2e("puppet resource #{common_args} test_validation foo ensure=absent param=2 prop=4")
expect(output.strip).not_to match %r{prop changed}
expect(status.exitstatus).to eq 0
end

context 'when passing a value to a read_only property' do
context 'with an existing resource' do
it 'throws' do
Expand Down Expand Up @@ -80,7 +86,7 @@

it 'allows creating' do
output, status = Open3.capture2e("puppet apply #{common_args} -e \"test_validation{ new: prop => 2, param => 3 }\"")
expect(output.strip).to match %r{Test_validation\[new\]/ensure: ensure changed 'absent' to 'present'}
expect(output.strip).to match %r{Test_validation\[new\]/ensure: defined 'ensure' as 'present'}
expect(status.exitstatus).to eq 0
end

Expand All @@ -92,7 +98,7 @@

it 'allows removing' do
output, status = Open3.capture2e("puppet apply #{common_args} -e \"test_validation{ foo: ensure => absent, param => 3 }\"")
expect(output.strip).to match %r{Test_validation\[foo\]/ensure: ensure changed 'present' to 'absent'}
expect(output.strip).to match %r{Test_validation\[foo\]/ensure: undefined 'ensure' from 'present'}
expect(status.exitstatus).to eq 0
end

Expand Down
Loading