hiera-router 0.3.3 → 0.3.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43963a8b7ae64ae4df6930f042a96c9d72f41de4
4
- data.tar.gz: a352c0f02f8e8093d1a2cb99551984ab9493c6aa
3
+ metadata.gz: 46665fadd9ea12e6890a66c4525b2d86ab21487c
4
+ data.tar.gz: 79f78ce15b8018a4652b26f0b502b4918a813589
5
5
  SHA512:
6
- metadata.gz: 601162d82e78f61c06b6f1cc131c1621e9278992419b53d412e188e3ff9f305f6e148e24c08f4978aa7c79785f273bd4aad7d6713e8da867a13fa9ad510b0821
7
- data.tar.gz: 90e18f7bb03116bf38a62c61918353841387dfc23a3705fb881d8270f89d03285a85e42db9189ed647df32a41449351fa0b2dbe5114528a7141716f836b02919
6
+ metadata.gz: de1dcade846df314da397119fb5a519d267288737037e1492dafd6adf13d4f914c5d309687bec760e4445e6c87598de2ce6bb21bc0875f131377232d813b8d31
7
+ data.tar.gz: 08cd3d6d6bd6e0ccc81ec6853fb2c50f984b2d8dfa3b5d3d73a69d2b46c34728d5cd58096d7c80b9727ce8017015683d5fa1d7127b9efda2d73d896d51feaf36
@@ -2,167 +2,167 @@ require 'hiera/filecache'
2
2
  require 'yaml'
3
3
 
4
4
  class Hiera
5
- class Config
6
- class << self
7
- def config
8
- @config
9
- end
10
- end
11
- end
12
-
13
- module Backend
14
- class Router_backend
15
- attr_reader :backends
16
- attr_accessor :config
17
-
18
- def initialize(cache = nil)
19
- @cache = cache || Filecache.new
20
- @backends = {}
21
- Hiera.debug("[hiera-router] I'm here!")
22
- self.config = Config.config
23
-
24
- if backend_list = Config[:router][:backends]
25
- Hiera.debug("[hiera-router] Initializing backends: #{backend_list.keys.join(',')}")
26
- backend_list.each do |backend, backend_config|
27
- Hiera.debug("[hiera-router] Initializing backend '#{backend}'")
28
- backend_classname = backend_config['backend_class'] || backend_config[:backend_class] || backend
29
- full_backend_classname = "#{backend_classname.capitalize}_backend"
30
- backend_config_override = backend_config['backend_key'] || backend_config[:backend_key] || backend_classname
31
- Hiera.debug("[hiera-router] Backend class for '#{backend}' will be '#{backend_classname}'")
32
-
33
- backend_config_override_config = Config[backend_config_override.to_sym] || Config[:router][backend_config_override.to_sym] || {}
34
- backend_config = Config[:router].merge({
35
- :hierarchy => Config[:hierarchy],
36
- :backends => [backend_classname],
37
- backend_classname.to_sym => backend_config_override_config,
38
- })
39
-
40
- if backend_config_override_config[:hierarchy]
41
- backend_config[:hierarchy] = backend_config_override_config[:hierarchy]
42
- end
43
-
44
- Config.load(backend_config)
45
- require "hiera/backend/#{full_backend_classname.downcase}"
46
- backend_inst = Hiera::Backend.const_get(full_backend_classname).new
47
- Config.load(config)
48
- @backends[backend.to_sym] = {
49
- :instance => backend_inst,
50
- :config => backend_config,
51
- }
52
- end
53
- end
54
-
55
- Hiera.debug("[hiera-router] hiera router initialized")
56
- end
57
- def lookup(key, scope, order_override, resolution_type)
58
- options = {
59
- :key => key,
60
- :scope => scope,
61
- :order_override => order_override,
62
- :resolution_type => resolution_type,
63
- }
64
- answer = nil
65
-
66
- Hiera.debug("[hiera-router] Looking up #{key} in yaml backend")
67
-
68
- Backend.datasources(scope, order_override) do |source|
69
- yaml_file = Backend.datafile(:router, scope, source, 'yaml') || next
70
-
71
- data = @cache.read(yaml_file, Hash) do |cached_data|
72
- begin
73
- Hiera.debug("[hiera-router] Looking + loading data source #{source} ('#{yaml_file}')")
74
- YAML.load(cached_data) || {}
75
- rescue
76
- Hiera.debug("[hiera-router] something wrong with source #{source} '#{yaml_file}' -- returning an empty result")
77
- {}
78
- end
79
- end
80
-
81
- next if data.empty?
82
- next unless data.include?(key)
83
-
84
- Hiera.debug("[hiera-router] Found #{key} in #{source}")
85
-
86
- new_answer = parse_answer(data[key], scope, options)
87
- next if new_answer.nil?
88
-
89
- case resolution_type
90
- when :array
91
- raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
92
- answer ||= []
93
- answer << new_answer
94
- when :hash
95
- raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
96
- answer ||= {}
97
- answer = Backend.merge_answer(new_answer,answer)
98
- else
99
- answer = new_answer
100
- break
101
- end
102
- end
103
-
104
- return answer
105
- end
106
-
107
- def recursive_key_from_hash(hash, path)
108
- focus = hash
109
- path.each do |key|
110
- if focus.is_a?(Hash) and focus.include?(key)
111
- focus = focus[key]
112
- else
113
- return nil
114
- end
115
- end
116
-
117
- return focus
118
- end
119
-
120
- def parse_answer(data, scope, options, path = [])
121
- if data.is_a?(Numeric) or data.is_a?(TrueClass) or data.is_a?(FalseClass)
122
- return data
123
- elsif data.is_a?(String)
124
- return parse_string(data, scope, options, path)
125
- elsif data.is_a?(Hash)
126
- answer = {}
127
- data.each_pair do |key, val|
128
- interpolated_key = Backend.parse_string(key, scope)
129
- subpath = path + [interpolated_key]
130
- answer[interpolated_key] = parse_answer(val, scope, options, subpath)
131
- end
132
-
133
- return answer
134
- elsif data.is_a?(Array)
135
- answer = []
136
- data.each do |item|
137
- answer << parse_answer(item, scope, options)
138
- end
139
-
140
- return answer
141
- end
142
- end
143
-
144
- def parse_string(data, scope, options, path = [])
145
- if match = data.match(/^backend\[([^,]+)(?:,(.*))?\]$/)
146
- backend_name, backend_parameters = match.captures
147
- backend_options = options
148
- backend_options = backend_options.merge(backend_parameters) if backend_parameters
149
- Hiera.debug("[hiera-router] Calling hiera with '#{backend_name}'...")
150
- if backend = self.backends[backend_name.to_sym]
151
- backend_instance = backend[:instance]
152
- Hiera.debug("[hiera-router] Backend class: #{backend_instance.class.name}")
153
- Config.load(backend[:config])
154
- result = backend_instance.lookup(backend_options[:key], backend_options[:scope], nil, backend_options[:resolution_type])
155
- Config.load(self.config)
156
- else
157
- Hiera.warn "Backend '#{backend_name}' was not configured; returning the data as-is."
158
- result = data
159
- end
160
- Hiera.debug("[hiera-router] Call to '#{backend_name}' finished.")
161
- return recursive_key_from_hash(result, path)
162
- else
163
- Backend.parse_string(data, scope)
164
- end
165
- end
166
- end
167
- end
5
+ class Config
6
+ class << self
7
+ def config
8
+ @config
9
+ end
10
+ end
11
+ end
12
+
13
+ module Backend
14
+ class Router_backend
15
+ attr_reader :backends
16
+ attr_accessor :config
17
+
18
+ def initialize(cache = nil)
19
+ @cache = cache || Filecache.new
20
+ @backends = {}
21
+ Hiera.debug("[hiera-router] I'm here!")
22
+ self.config = Config.config
23
+
24
+ if backend_list = Config[:router][:backends]
25
+ Hiera.debug("[hiera-router] Initializing backends: #{backend_list.keys.join(',')}")
26
+ backend_list.each do |backend, backend_config|
27
+ Hiera.debug("[hiera-router] Initializing backend '#{backend}'")
28
+ backend_classname = backend_config['backend_class'] || backend_config[:backend_class] || backend
29
+ full_backend_classname = "#{backend_classname.capitalize}_backend"
30
+ backend_config_override = backend_config['backend_key'] || backend_config[:backend_key] || backend_classname
31
+ Hiera.debug("[hiera-router] Backend class for '#{backend}' will be '#{backend_classname}'")
32
+
33
+ backend_config_override_config = Config[backend_config_override.to_sym] || Config[:router][backend_config_override.to_sym] || {}
34
+
35
+ backend_config = self.config.clone
36
+ backend_config.delete(:router)
37
+ if backend_config_override_config[:hierarchy]
38
+ backend_config[:hierarchy] = backend_config_override_config[:hierarchy]
39
+ end
40
+
41
+ backend_config[:backends] = [backend_classname]
42
+ backend_config[backend_classname.to_sym] = backend_config_override_config
43
+
44
+ Config.load(backend_config)
45
+ require "hiera/backend/#{full_backend_classname.downcase}"
46
+ backend_inst = Hiera::Backend.const_get(full_backend_classname).new
47
+ Config.load(config)
48
+ @backends[backend.to_sym] = {
49
+ :instance => backend_inst,
50
+ :config => backend_config,
51
+ }
52
+ end
53
+ end
54
+
55
+ Hiera.debug("[hiera-router] hiera router initialized")
56
+ end
57
+ def lookup(key, scope, order_override, resolution_type)
58
+ options = {
59
+ :key => key,
60
+ :scope => scope,
61
+ :order_override => order_override,
62
+ :resolution_type => resolution_type,
63
+ }
64
+ answer = nil
65
+
66
+ Hiera.debug("[hiera-router] Looking up #{key} in yaml backend")
67
+
68
+ Backend.datasources(scope, order_override) do |source|
69
+ yaml_file = Backend.datafile(:router, scope, source, 'yaml') || next
70
+
71
+ data = @cache.read(yaml_file, Hash) do |cached_data|
72
+ begin
73
+ Hiera.debug("[hiera-router] Looking + loading data source #{source} ('#{yaml_file}')")
74
+ YAML.load(cached_data) || {}
75
+ rescue
76
+ Hiera.debug("[hiera-router] something wrong with source #{source} '#{yaml_file}' -- returning an empty result")
77
+ {}
78
+ end
79
+ end
80
+
81
+ next if data.empty?
82
+ next unless data.include?(key)
83
+
84
+ Hiera.debug("[hiera-router] Found #{key} in #{source}")
85
+
86
+ new_answer = parse_answer(data[key], scope, options)
87
+ next if new_answer.nil?
88
+
89
+ case resolution_type
90
+ when :array
91
+ raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
92
+ answer ||= []
93
+ answer << new_answer
94
+ when :hash
95
+ raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
96
+ answer ||= {}
97
+ answer = Backend.merge_answer(new_answer,answer)
98
+ else
99
+ answer = new_answer
100
+ break
101
+ end
102
+ end
103
+
104
+ return answer
105
+ end
106
+
107
+ def recursive_key_from_hash(hash, path)
108
+ focus = hash
109
+ path.each do |key|
110
+ if focus.is_a?(Hash) and focus.include?(key)
111
+ focus = focus[key]
112
+ else
113
+ return nil
114
+ end
115
+ end
116
+
117
+ return focus
118
+ end
119
+
120
+ def parse_answer(data, scope, options, path = [])
121
+ if data.is_a?(Numeric) or data.is_a?(TrueClass) or data.is_a?(FalseClass)
122
+ return data
123
+ elsif data.is_a?(String)
124
+ return parse_string(data, scope, options, path)
125
+ elsif data.is_a?(Hash)
126
+ answer = {}
127
+ data.each_pair do |key, val|
128
+ interpolated_key = Backend.parse_string(key, scope)
129
+ subpath = path + [interpolated_key]
130
+ answer[interpolated_key] = parse_answer(val, scope, options, subpath)
131
+ end
132
+
133
+ return answer
134
+ elsif data.is_a?(Array)
135
+ answer = []
136
+ data.each do |item|
137
+ answer << parse_answer(item, scope, options)
138
+ end
139
+
140
+ return answer
141
+ end
142
+ end
143
+
144
+ def parse_string(data, scope, options, path = [])
145
+ if match = data.match(/^backend\[([^,]+)(?:,(.*))?\]$/)
146
+ backend_name, backend_parameters = match.captures
147
+ backend_options = options
148
+ backend_options = backend_options.merge(backend_parameters) if backend_parameters
149
+ Hiera.debug("[hiera-router] Calling hiera with '#{backend_name}'...")
150
+ if backend = self.backends[backend_name.to_sym]
151
+ backend_instance = backend[:instance]
152
+ Hiera.debug("[hiera-router] Backend class: #{backend_instance.class.name}")
153
+ Config.load(backend[:config])
154
+ result = backend_instance.lookup(backend_options[:key], backend_options[:scope], nil, backend_options[:resolution_type])
155
+ Config.load(self.config)
156
+ else
157
+ Hiera.warn "Backend '#{backend_name}' was not configured; returning the data as-is."
158
+ result = data
159
+ end
160
+ Hiera.debug("[hiera-router] Call to '#{backend_name}' finished.")
161
+ return recursive_key_from_hash(result, path)
162
+ else
163
+ Backend.parse_string(data, scope)
164
+ end
165
+ end
166
+ end
167
+ end
168
168
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hiera-router
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.3
4
+ version: 0.3.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jo Vandeginste