Browse Source

Merge "Add undef2nil function"

Jenkins 2 years ago
parent
commit
8abe8a95c7

+ 19
- 0
deployment/puppet/merge_yaml/lib/puppet/provider/merge_yaml_settings/ruby.rb View File

@@ -59,6 +59,24 @@ Puppet::Type.type(:merge_yaml_settings).provide(:ruby) do
59 59
     result
60 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 80
   # Produce the merged data structure by merging
63 81
   # the original data data with the override data.
64 82
   # @return [Hash]
@@ -68,6 +86,7 @@ Puppet::Type.type(:merge_yaml_settings).provide(:ruby) do
68 86
     debug "Merge override: #{override_data.inspect}" if merge_debug
69 87
     original_data_clone = Marshal.load Marshal.dump original_data
70 88
     YamlDeepMerge.deep_merge! override_data, original_data_clone, deep_merge_options
89
+    original_data_clone = undef2nil original_data_clone
71 90
     debug "Result: #{original_data_clone.inspect}" if merge_debug
72 91
     original_data_clone
73 92
   end

+ 6
- 0
deployment/puppet/merge_yaml/spec/unit/puppet/provider/merge_yaml_settings/ruby_spec.rb View File

@@ -105,6 +105,12 @@ describe Puppet::Type.type(:merge_yaml_settings).provider(:ruby) do
105 105
       expect(provider.merged_data).to eq('a' => %w(2))
106 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 114
   end
109 115
 
110 116
   context 'transaction' do

+ 31
- 0
deployment/puppet/osnailyfacter/lib/puppet/parser/functions/undef2nil.rb View File

@@ -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

+ 48
- 0
deployment/puppet/osnailyfacter/spec/functions/undef2nil_spec.rb View File

@@ -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

Loading…
Cancel
Save