hiera 1.1.2 → 1.2.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of hiera might be problematic. Click here for more details.

data/lib/hiera.rb CHANGED
@@ -1,7 +1,7 @@
1
1
  require 'yaml'
2
2
 
3
3
  class Hiera
4
- VERSION = "1.1.2"
4
+ VERSION = "1.2.0-rc1"
5
5
 
6
6
  autoload :Config, "hiera/config"
7
7
  autoload :Util, "hiera/util"
data/lib/hiera/backend.rb CHANGED
@@ -1,4 +1,8 @@
1
1
  require 'hiera/util'
2
+ begin
3
+ require 'deep_merge'
4
+ rescue LoadError
5
+ end
2
6
 
3
7
  class Hiera
4
8
  module Backend
@@ -85,8 +89,9 @@ class Hiera
85
89
  # Puppet can return :undefined for unknown scope vars,
86
90
  # If it does then we still need to evaluate extra_data
87
91
  # before returning an empty string.
88
- if scope[var] && scope[var] != :undefined
89
- val = scope[var]
92
+ scope_val = scope[var]
93
+ if !scope_val.nil? && scope_val != :undefined
94
+ val = scope_val
90
95
  elsif extra_data[var]
91
96
  val = extra_data[var]
92
97
  end
@@ -136,6 +141,26 @@ class Hiera
136
141
  end
137
142
  end
138
143
 
144
+ # Merges two hashes answers with the configured merge behavior.
145
+ # :merge_behavior: {:native|:deep|:deeper}
146
+ #
147
+ # Deep merge options use the Hash utility function provided by [deep_merge](https://github.com/peritor/deep_merge)
148
+ #
149
+ # :native => Native Hash.merge
150
+ # :deep => Use Hash.deep_merge
151
+ # :deeper => Use Hash.deep_merge!
152
+ #
153
+ def merge_answer(left,right)
154
+ case Config[:merge_behavior]
155
+ when :deeper,'deeper'
156
+ left.deep_merge!(right)
157
+ when :deep,'deep'
158
+ left.deep_merge(right)
159
+ else # Native and undefined
160
+ left.merge(right)
161
+ end
162
+ end
163
+
139
164
  # Calls out to all configured backends in the order they
140
165
  # were specified. The first one to answer will win.
141
166
  #
@@ -167,7 +192,7 @@ class Hiera
167
192
  when :hash
168
193
  raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
169
194
  answer ||= {}
170
- answer = new_answer.merge answer
195
+ answer = merge_answer(new_answer,answer)
171
196
  else
172
197
  answer = new_answer
173
198
  break
@@ -30,10 +30,15 @@ class Hiera
30
30
  new_answer = Backend.parse_answer(data[key], scope)
31
31
  case resolution_type
32
32
  when :array
33
+ raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
33
34
  answer ||= []
34
35
  answer << new_answer
36
+ when :hash
37
+ raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
38
+ answer ||= {}
39
+ answer = Backend.merge_answer(new_answer,answer)
35
40
  else
36
- answer = Backend.parse_answer(data[key], scope)
41
+ answer = new_answer
37
42
  break
38
43
  end
39
44
  end
@@ -52,7 +52,7 @@ class Hiera
52
52
  when :hash
53
53
  raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
54
54
  answer ||= {}
55
- answer = new_answer.merge answer
55
+ answer = Backend.merge_answer(new_answer,answer)
56
56
  else
57
57
  answer = new_answer
58
58
  break
data/lib/hiera/config.rb CHANGED
@@ -11,7 +11,8 @@ class Hiera::Config
11
11
  # {:backends => "yaml", :hierarchy => "common"}
12
12
  def load(source)
13
13
  @config = {:backends => "yaml",
14
- :hierarchy => "common"}
14
+ :hierarchy => "common",
15
+ :merge_behavior => :native }
15
16
 
16
17
  if source.is_a?(String)
17
18
  if File.exist?(source)
@@ -5,6 +5,7 @@ class Hiera
5
5
  module Backend
6
6
  describe Yaml_backend do
7
7
  before do
8
+ Config.load({})
8
9
  Hiera.stubs(:debug)
9
10
  Hiera.stubs(:warn)
10
11
  @backend = Yaml_backend.new
@@ -333,5 +333,30 @@ class Hiera
333
333
  Backend.lookup("key", {"test" => "value"}, {}, nil, :hash).should == {"test" => "value"}
334
334
  end
335
335
  end
336
+ describe '#merge_answer' do
337
+ before do
338
+ Hiera.stubs(:debug)
339
+ Hiera.stubs(:warn)
340
+ end
341
+
342
+ it "should use Hash.merge when configured with :merge_behavior => :native" do
343
+ Config.load({:merge_behavior => :native})
344
+ Hash.any_instance.expects(:merge).with({"b" => "bnswer"}).returns({"a" => "answer", "b" => "bnswer"})
345
+ Backend.merge_answer({"a" => "answer"},{"b" => "bnswer"}).should == {"a" => "answer", "b" => "bnswer"}
346
+ end
347
+
348
+ it "should use deep_merge! when configured with :merge_behavior => :deeper" do
349
+ Config.load({:merge_behavior => :deeper})
350
+ Hash.any_instance.expects('deep_merge!').with({"b" => "bnswer"}).returns({"a" => "answer", "b" => "bnswer"})
351
+ Backend.merge_answer({"a" => "answer"},{"b" => "bnswer"}).should == {"a" => "answer", "b" => "bnswer"}
352
+ end
353
+
354
+ it "should use deep_merge when configured with :merge_behavior => :deep" do
355
+ Config.load({:merge_behavior => :deep})
356
+ Hash.any_instance.expects('deep_merge').with({"b" => "bnswer"}).returns({"a" => "answer", "b" => "bnswer"})
357
+ Backend.merge_answer({"a" => "answer"},{"b" => "bnswer"}).should == {"a" => "answer", "b" => "bnswer"}
358
+ end
359
+
360
+ end
336
361
  end
337
362
  end
@@ -7,7 +7,8 @@ class Hiera
7
7
  {
8
8
  :backends => ["yaml"],
9
9
  :hierarchy => "common",
10
- :logger => "console"
10
+ :logger => "console",
11
+ :merge_behavior=>:native
11
12
  }
12
13
  end
13
14
 
@@ -43,11 +44,11 @@ class Hiera
43
44
 
44
45
  it "should merge defaults with the loaded or supplied config" do
45
46
  config = Config.load({})
46
- config.should == {:backends => ["yaml"], :hierarchy => "common", :logger => "console"}
47
+ config.should == {:backends => ["yaml"], :hierarchy => "common", :logger => "console", :merge_behavior=>:native}
47
48
  end
48
49
 
49
50
  it "should force :backends to be a flattened array" do
50
- Config.load({:backends => [["foo", ["bar"]]]}).should == {:backends => ["foo", "bar"], :hierarchy => "common", :logger => "console"}
51
+ Config.load({:backends => [["foo", ["bar"]]]}).should == {:backends => ["foo", "bar"], :hierarchy => "common", :logger => "console", :merge_behavior=>:native}
51
52
  end
52
53
 
53
54
  it "should load the supplied logger" do
metadata CHANGED
@@ -1,13 +1,15 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera
3
3
  version: !ruby/object:Gem::Version
4
- hash: 23
5
- prerelease:
4
+ hash: 1822012835
5
+ prerelease: 6
6
6
  segments:
7
7
  - 1
8
- - 1
9
8
  - 2
10
- version: 1.1.2
9
+ - 0
10
+ - rc
11
+ - 1
12
+ version: 1.2.0.rc1
11
13
  platform: ruby
12
14
  authors:
13
15
  - Puppet Labs
@@ -15,7 +17,7 @@ autorequire:
15
17
  bindir: bin
16
18
  cert_chain: []
17
19
 
18
- date: 2012-12-27 00:00:00 Z
20
+ date: 2013-03-05 00:00:00 Z
19
21
  dependencies:
20
22
  - !ruby/object:Gem::Dependency
21
23
  name: json_pure
@@ -41,26 +43,26 @@ extra_rdoc_files: []
41
43
 
42
44
  files:
43
45
  - bin/hiera
44
- - lib/hiera/backend/json_backend.rb
45
- - lib/hiera/backend/yaml_backend.rb
46
- - lib/hiera/backend.rb
47
- - lib/hiera/config.rb
48
- - lib/hiera/console_logger.rb
49
- - lib/hiera/noop_logger.rb
50
46
  - lib/hiera/puppet_logger.rb
47
+ - lib/hiera/console_logger.rb
51
48
  - lib/hiera/util.rb
49
+ - lib/hiera/backend.rb
50
+ - lib/hiera/noop_logger.rb
51
+ - lib/hiera/config.rb
52
+ - lib/hiera/backend/yaml_backend.rb
53
+ - lib/hiera/backend/json_backend.rb
52
54
  - lib/hiera.rb
53
55
  - COPYING
54
56
  - README.md
55
57
  - LICENSE
56
- - spec/spec_helper.rb
57
- - spec/unit/backend/json_backend_spec.rb
58
- - spec/unit/backend/yaml_backend_spec.rb
59
- - spec/unit/backend_spec.rb
58
+ - spec/unit/util_spec.rb
60
59
  - spec/unit/config_spec.rb
61
- - spec/unit/console_logger_spec.rb
60
+ - spec/unit/backend_spec.rb
62
61
  - spec/unit/hiera_spec.rb
63
- - spec/unit/util_spec.rb
62
+ - spec/unit/console_logger_spec.rb
63
+ - spec/unit/backend/yaml_backend_spec.rb
64
+ - spec/unit/backend/json_backend_spec.rb
65
+ - spec/spec_helper.rb
64
66
  homepage: https://github.com/puppetlabs/hiera
65
67
  licenses: []
66
68
 
@@ -81,12 +83,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
81
83
  required_rubygems_version: !ruby/object:Gem::Requirement
82
84
  none: false
83
85
  requirements:
84
- - - ">="
86
+ - - ">"
85
87
  - !ruby/object:Gem::Version
86
- hash: 3
88
+ hash: 25
87
89
  segments:
88
- - 0
89
- version: "0"
90
+ - 1
91
+ - 3
92
+ - 1
93
+ version: 1.3.1
90
94
  requirements: []
91
95
 
92
96
  rubyforge_project:
@@ -95,11 +99,11 @@ signing_key:
95
99
  specification_version: 3
96
100
  summary: Light weight hierarchical data store
97
101
  test_files:
98
- - spec/spec_helper.rb
99
- - spec/unit/backend/json_backend_spec.rb
100
- - spec/unit/backend/yaml_backend_spec.rb
101
- - spec/unit/backend_spec.rb
102
+ - spec/unit/util_spec.rb
102
103
  - spec/unit/config_spec.rb
103
- - spec/unit/console_logger_spec.rb
104
+ - spec/unit/backend_spec.rb
104
105
  - spec/unit/hiera_spec.rb
105
- - spec/unit/util_spec.rb
106
+ - spec/unit/console_logger_spec.rb
107
+ - spec/unit/backend/yaml_backend_spec.rb
108
+ - spec/unit/backend/json_backend_spec.rb
109
+ - spec/spec_helper.rb