hiera-router 0.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/lib/hiera/backend/router_backend.rb +109 -0
- metadata +50 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: a12b6ae4e10045577873f02b0b2425cace302ea5
|
4
|
+
data.tar.gz: 065154f07b55c5db435f02ab23e2c1bf096e5057
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 870db9815df5d7f3288ad6556f784aa1d2fe8b5d12d990412a02c5970f56ce95a925b2905420c51ff5b564852fd92a6acffc2966a50c8dac58f8e2c8daf59be5
|
7
|
+
data.tar.gz: d9fbb8ac71a1999729ecdd2238f6df585d2d50b3482876e584049e755c7502e1c02913d1e5f3a00bbca58d87d7f01d43c6e29360f0a81df65ab93b8b78dcf994
|
@@ -0,0 +1,109 @@
|
|
1
|
+
class Hiera
|
2
|
+
module Backend
|
3
|
+
class Router_backend
|
4
|
+
attr_reader :backends
|
5
|
+
|
6
|
+
def initialize(cache = nil)
|
7
|
+
require 'hiera/filecache'
|
8
|
+
require 'yaml'
|
9
|
+
@cache = cache || Filecache.new
|
10
|
+
@backends = {}
|
11
|
+
if backend_list = Config[:router][:backends]
|
12
|
+
backend_list.each do |backend|
|
13
|
+
require "hiera/backend/#{backend.downcase}_backend"
|
14
|
+
@backends[backend] = Hiera::Backend.const_get("#{backend.capitalize}_backend").new
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
Hiera.debug("hiera router initialized")
|
19
|
+
end
|
20
|
+
def lookup(key, scope, order_override, resolution_type)
|
21
|
+
options = {
|
22
|
+
:key => key,
|
23
|
+
:scope => scope,
|
24
|
+
:order_override => order_override,
|
25
|
+
:resolution_type => resolution_type,
|
26
|
+
}
|
27
|
+
answer = nil
|
28
|
+
|
29
|
+
Hiera.debug("Looking up #{key} in yaml backend")
|
30
|
+
|
31
|
+
Backend.datasources(scope, order_override) do |source|
|
32
|
+
Hiera.debug("Looking for data source #{source}")
|
33
|
+
yaml_file = Backend.datafile(:router, scope, source, 'yaml') || next
|
34
|
+
|
35
|
+
next unless File.exists?(yaml_file)
|
36
|
+
|
37
|
+
data = @cache.read(yaml_file, Hash) do |cached_data|
|
38
|
+
YAML.load(cached_data) || {}
|
39
|
+
end
|
40
|
+
|
41
|
+
next if data.empty?
|
42
|
+
next unless data.include?(key)
|
43
|
+
|
44
|
+
Hiera.debug("Found #{key} in #{source}")
|
45
|
+
|
46
|
+
new_answer = parse_answer(data[key], scope, options)
|
47
|
+
next if new_answer.nil?
|
48
|
+
|
49
|
+
case resolution_type
|
50
|
+
when :array
|
51
|
+
raise Exception, "Hiera type mismatch: expected Array and got #{new_answer.class}" unless new_answer.kind_of? Array or new_answer.kind_of? String
|
52
|
+
answer ||= []
|
53
|
+
answer << new_answer
|
54
|
+
when :hash
|
55
|
+
raise Exception, "Hiera type mismatch: expected Hash and got #{new_answer.class}" unless new_answer.kind_of? Hash
|
56
|
+
answer ||= {}
|
57
|
+
answer = Backend.merge_answer(new_answer,answer)
|
58
|
+
else
|
59
|
+
answer = new_answer
|
60
|
+
break
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
return answer
|
65
|
+
end
|
66
|
+
|
67
|
+
def parse_answer(data, scope, options)
|
68
|
+
if data.is_a?(Numeric) or data.is_a?(TrueClass) or data.is_a?(FalseClass)
|
69
|
+
return data
|
70
|
+
elsif data.is_a?(String)
|
71
|
+
return parse_string(data, scope, options)
|
72
|
+
elsif data.is_a?(Hash)
|
73
|
+
answer = {}
|
74
|
+
data.each_pair do |key, val|
|
75
|
+
interpolated_key = Backend.parse_string(key, scope)
|
76
|
+
answer[interpolated_key] = parse_answer(val, scope, options)
|
77
|
+
end
|
78
|
+
|
79
|
+
return answer
|
80
|
+
elsif data.is_a?(Array)
|
81
|
+
answer = []
|
82
|
+
data.each do |item|
|
83
|
+
answer << parse_answer(item, scope, options)
|
84
|
+
end
|
85
|
+
|
86
|
+
return answer
|
87
|
+
end
|
88
|
+
end
|
89
|
+
|
90
|
+
def parse_string(data, scope, options)
|
91
|
+
if match = data.match(/^backend\[([^,]+)(?:,(.*))?\]$/)
|
92
|
+
backend_name, backend_parameters = match.captures
|
93
|
+
backend_options = options
|
94
|
+
backend_options = backend_options.merge(backend_parameters) if backend_parameters
|
95
|
+
Hiera.debug "Calling hiera with '#{backend_name}'..."
|
96
|
+
if backend = backends[backend_name]
|
97
|
+
result = backend.lookup(backend_options[:key], backend_options[:scope], nil, backend_options[:resolution_type])
|
98
|
+
else
|
99
|
+
raise "Backend '#{backend_name}' was not configured."
|
100
|
+
end
|
101
|
+
Hiera.debug "Call to '#{backend_name}' finished."
|
102
|
+
return result
|
103
|
+
else
|
104
|
+
Backend.parse_string(data, scope)
|
105
|
+
end
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
metadata
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: hiera-router
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.6
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Jo Vandeginste
|
8
|
+
- Tom Leuse
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2016-06-23 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: |
|
15
|
+
This hiera backend replaces the default yaml backend, but will resend queries to other hiera backends based on the value returned by the yaml files.
|
16
|
+
|
17
|
+
When hiera-router gets a string matching "BACKEND[otherbackendname]", it will resend the same query to "otherbackendname".
|
18
|
+
email:
|
19
|
+
- jo.vandeginste@kuleuven.be
|
20
|
+
- tom.leuse@kuleuven.be
|
21
|
+
executables: []
|
22
|
+
extensions: []
|
23
|
+
extra_rdoc_files: []
|
24
|
+
files:
|
25
|
+
- lib/hiera/backend/router_backend.rb
|
26
|
+
homepage: https://github.com/jovandeginste/hiera-router
|
27
|
+
licenses:
|
28
|
+
- Apache-2.0
|
29
|
+
metadata: {}
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
require_paths:
|
33
|
+
- lib
|
34
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
35
|
+
requirements:
|
36
|
+
- - ">="
|
37
|
+
- !ruby/object:Gem::Version
|
38
|
+
version: '0'
|
39
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
40
|
+
requirements:
|
41
|
+
- - ">="
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
requirements: []
|
45
|
+
rubyforge_project:
|
46
|
+
rubygems_version: 2.4.5
|
47
|
+
signing_key:
|
48
|
+
specification_version: 4
|
49
|
+
summary: This hiera backend to selectively forward requests to different hiera backends
|
50
|
+
test_files: []
|