diff options
author | Jenkins <jenkins@review.openstack.org> | 2017-03-14 08:35:36 +0000 |
---|---|---|
committer | Gerrit Code Review <review@openstack.org> | 2017-03-14 08:35:36 +0000 |
commit | 8abe8a95c76b8ba66099fbb67f712e6b22b0f422 (patch) | |
tree | ab56cb9906d96b7b14ce24481b265e4e3b762100 | |
parent | 798ee3a00064a929f77e127a1bca094bdc9491c7 (diff) | |
parent | 25bda8d2da8ce9785464c899024b88bba0017000 (diff) |
Merge "Add undef2nil function"
4 files changed, 104 insertions, 0 deletions
diff --git a/deployment/puppet/merge_yaml/lib/puppet/provider/merge_yaml_settings/ruby.rb b/deployment/puppet/merge_yaml/lib/puppet/provider/merge_yaml_settings/ruby.rb index 625cccf..ccf7d59 100644 --- a/deployment/puppet/merge_yaml/lib/puppet/provider/merge_yaml_settings/ruby.rb +++ b/deployment/puppet/merge_yaml/lib/puppet/provider/merge_yaml_settings/ruby.rb | |||
@@ -59,6 +59,24 @@ Puppet::Type.type(:merge_yaml_settings).provide(:ruby) do | |||
59 | result | 59 | result |
60 | end | 60 | end |
61 | 61 | ||
62 | # Purge the :undef values which have been introduced with Puppet4 | ||
63 | def undef2nil(structure) | ||
64 | if structure.is_a? Array | ||
65 | structure.map do |element| | ||
66 | undef2nil element | ||
67 | end | ||
68 | elsif structure.is_a? Hash | ||
69 | hash = {} | ||
70 | structure.each do |key, value| | ||
71 | hash[key] = undef2nil value | ||
72 | end | ||
73 | hash | ||
74 | else | ||
75 | return nil if structure == :undef | ||
76 | structure | ||
77 | end | ||
78 | end | ||
79 | |||
62 | # Produce the merged data structure by merging | 80 | # Produce the merged data structure by merging |
63 | # the original data data with the override data. | 81 | # the original data data with the override data. |
64 | # @return [Hash] | 82 | # @return [Hash] |
@@ -68,6 +86,7 @@ Puppet::Type.type(:merge_yaml_settings).provide(:ruby) do | |||
68 | debug "Merge override: #{override_data.inspect}" if merge_debug | 86 | debug "Merge override: #{override_data.inspect}" if merge_debug |
69 | original_data_clone = Marshal.load Marshal.dump original_data | 87 | original_data_clone = Marshal.load Marshal.dump original_data |
70 | YamlDeepMerge.deep_merge! override_data, original_data_clone, deep_merge_options | 88 | YamlDeepMerge.deep_merge! override_data, original_data_clone, deep_merge_options |
89 | original_data_clone = undef2nil original_data_clone | ||
71 | debug "Result: #{original_data_clone.inspect}" if merge_debug | 90 | debug "Result: #{original_data_clone.inspect}" if merge_debug |
72 | original_data_clone | 91 | original_data_clone |
73 | end | 92 | end |
diff --git a/deployment/puppet/merge_yaml/spec/unit/puppet/provider/merge_yaml_settings/ruby_spec.rb b/deployment/puppet/merge_yaml/spec/unit/puppet/provider/merge_yaml_settings/ruby_spec.rb index dbca159..cbb1cf7 100644 --- a/deployment/puppet/merge_yaml/spec/unit/puppet/provider/merge_yaml_settings/ruby_spec.rb +++ b/deployment/puppet/merge_yaml/spec/unit/puppet/provider/merge_yaml_settings/ruby_spec.rb | |||
@@ -105,6 +105,12 @@ describe Puppet::Type.type(:merge_yaml_settings).provider(:ruby) do | |||
105 | expect(provider.merged_data).to eq('a' => %w(2)) | 105 | expect(provider.merged_data).to eq('a' => %w(2)) |
106 | end | 106 | end |
107 | 107 | ||
108 | it 'will replace the :undef values with nil values' do | ||
109 | expect(provider).to receive(:original_data).and_return('a' => :undef, 'b' => '1').at_least(:once) | ||
110 | expect(provider).to receive(:override_data).and_return('c' => '2').at_least(:once) | ||
111 | expect(provider.merged_data).to eq('a' => nil, 'b' => '1', 'c' => '2') | ||
112 | end | ||
113 | |||
108 | end | 114 | end |
109 | 115 | ||
110 | context 'transaction' do | 116 | context 'transaction' do |
diff --git a/deployment/puppet/osnailyfacter/lib/puppet/parser/functions/undef2nil.rb b/deployment/puppet/osnailyfacter/lib/puppet/parser/functions/undef2nil.rb new file mode 100644 index 0000000..3840554 --- /dev/null +++ b/deployment/puppet/osnailyfacter/lib/puppet/parser/functions/undef2nil.rb | |||
@@ -0,0 +1,31 @@ | |||
1 | module Puppet::Parser::Functions | ||
2 | newfunction( | ||
3 | :undef2nil, | ||
4 | :type => :rvalue, | ||
5 | :doc => <<-EOS | ||
6 | Replaces all :undef values with the "nil" values. | ||
7 | Useful for Puppet 4 compatibility problems. | ||
8 | EOS | ||
9 | ) do |argument| | ||
10 | argument = argument.first | ||
11 | |||
12 | undef2nil = lambda do |structure| | ||
13 | if structure.is_a? Array | ||
14 | structure.map do |element| | ||
15 | undef2nil.call element | ||
16 | end | ||
17 | elsif structure.is_a? Hash | ||
18 | hash = {} | ||
19 | structure.each do |key, value| | ||
20 | hash.store key, undef2nil.call(value) | ||
21 | end | ||
22 | hash | ||
23 | else | ||
24 | break nil if structure == :undef | ||
25 | structure | ||
26 | end | ||
27 | end | ||
28 | |||
29 | undef2nil.call argument | ||
30 | end | ||
31 | end | ||
diff --git a/deployment/puppet/osnailyfacter/spec/functions/undef2nil_spec.rb b/deployment/puppet/osnailyfacter/spec/functions/undef2nil_spec.rb new file mode 100644 index 0000000..d2e2927 --- /dev/null +++ b/deployment/puppet/osnailyfacter/spec/functions/undef2nil_spec.rb | |||
@@ -0,0 +1,48 @@ | |||
1 | require 'spec_helper' | ||
2 | |||
3 | describe 'undef2nil' do | ||
4 | |||
5 | it 'should exist' do | ||
6 | is_expected.not_to be_nil | ||
7 | end | ||
8 | |||
9 | it 'should not modify normal values' do | ||
10 | is_expected.to run.with_params('test').and_return('test') | ||
11 | is_expected.to run.with_params(nil).and_return(nil) | ||
12 | end | ||
13 | |||
14 | it 'should change :undef to nil in a simple value' do | ||
15 | is_expected.to run.with_params(:undef).and_return(nil) | ||
16 | end | ||
17 | |||
18 | it 'should be able to process arrays' do | ||
19 | is_expected.to run.with_params(['1', 2, :undef, nil, {}, [1]]).and_return(['1', 2, nil, nil, {}, [1]]) | ||
20 | end | ||
21 | |||
22 | it 'should be able to process hashes' do | ||
23 | is_expected.to run.with_params( | ||
24 | { | ||
25 | 'a' => 'b', | ||
26 | 'c' => ['d'], | ||
27 | 'e' => :undef, | ||
28 | :undef => 'f', | ||
29 | 'g' => { | ||
30 | 'a' => :undef, | ||
31 | 'b' => [1, :undef], | ||
32 | } | ||
33 | } | ||
34 | ).and_return( | ||
35 | { | ||
36 | 'a' => 'b', | ||
37 | 'c' => ['d'], | ||
38 | 'e' => nil, | ||
39 | :undef => 'f', | ||
40 | 'g' => { | ||
41 | 'a' => nil, | ||
42 | 'b' => [1, nil], | ||
43 | } | ||
44 | } | ||
45 | ) | ||
46 | end | ||
47 | |||
48 | end | ||