-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
22 changed files
with
549 additions
and
249 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
class HieraData | ||
class Lookup | ||
def initialize(hashes) | ||
@hashes = hashes | ||
end | ||
|
||
def lookup(key, merge_strategy: :first) | ||
applicable_values = extract_key_from_hashes(key) | ||
merge_values(applicable_values, merge_strategy) | ||
end | ||
|
||
private | ||
|
||
def extract_key_from_hashes(key) | ||
@hashes | ||
.select { |h| h.key?(key) } | ||
.pluck(key) | ||
end | ||
|
||
def merge_values(values, strategy) | ||
case strategy | ||
when :first | ||
values.first | ||
when :unique | ||
unique_array_merge(values) | ||
when :hash | ||
flat_hash_merge(values) | ||
when :deep | ||
deep_merge(values) | ||
end | ||
end | ||
|
||
def unique_array_merge(values) | ||
values.inject do |memo, value| | ||
memo ||= [] | ||
raise Hdm::Error, "Merge strategy `unique` is not applicable to a hash." if value.is_a?(Hash) | ||
|
||
memo + Array(value) | ||
end.uniq | ||
end | ||
|
||
def flat_hash_merge(values) | ||
values.inject do |memo, value| | ||
memo ||= {} | ||
raise Hdm::Error, "Merge strategy `hash` can only be used with hashes." unless value.is_a?(Hash) | ||
|
||
value.merge(memo) | ||
end | ||
end | ||
|
||
def deep_merge(values) | ||
values.inject do |memo, value| | ||
memo ||= {} | ||
raise Hdm::Error, "Merge strategy `deep` can only be used with hashes and arrays." unless value.is_a?(Hash) || value.is_a?(Array) | ||
|
||
DeepMerge.deep_merge!(memo, value, merge_hash_arrays: true) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
29 changes: 29 additions & 0 deletions
29
test/fixtures/files/puppet/environments/lookup_tests/data/common.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
--- | ||
hdm_integer: 93 | ||
hdm_unique_array: | ||
- common | ||
hdm_duplicates_in_array: | ||
- node | ||
- zone | ||
- common | ||
hdm_nested_array: | ||
- from_common | ||
- - nested | ||
- list | ||
hdm_simple_hash: | ||
origin: common | ||
added_by_common: common | ||
hdm_nested_hash: | ||
origin: common | ||
nested_hash: | ||
integer: 32 | ||
added_by_common: common | ||
lookup_options: | ||
'^hdm_duplicates': | ||
merge: hash | ||
hdm_duplicates_in_array: | ||
merge: unique | ||
hash_syntax: | ||
merge: | ||
strategy: deep | ||
|
16 changes: 16 additions & 0 deletions
16
.../fixtures/files/puppet/environments/lookup_tests/data/nodes/lookup.betadots.training.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
--- | ||
hdm_integer: 23 | ||
hdm_unique_array: | ||
- node | ||
hdm_duplicates_in_array: | ||
- node | ||
hdm_nested_array: | ||
- duplicate | ||
- nested_hash: | ||
node_integer: 2 | ||
hdm_simple_hash: | ||
origin: node | ||
hdm_nested_hash: | ||
origin: node | ||
nested_hash: | ||
integer: 5 |
18 changes: 18 additions & 0 deletions
18
test/fixtures/files/puppet/environments/lookup_tests/data/role/test_role.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
--- | ||
hdm_integer: 42 | ||
hdm_string: "role" | ||
hdm_unique_array: | ||
- role | ||
hdm_duplicates_in_array: | ||
- node | ||
- role | ||
hdm_nested_array: | ||
- from_role | ||
- nested_hash: | ||
role_integer: 2 | ||
hdm_simple_hash: | ||
origin: role | ||
added_by_role: role | ||
hdm_nested_hash: | ||
nested_hash: | ||
added_by_role: role |
15 changes: 15 additions & 0 deletions
15
test/fixtures/files/puppet/environments/lookup_tests/data/zone/test_zone.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
--- | ||
hdm_unique_array: | ||
- zone | ||
hdm_duplicates_in_array: | ||
- node | ||
- zone | ||
hdm_nested_array: | ||
- duplicate | ||
hdm_simple_hash: | ||
added_by_zone: zone | ||
hdm_nested_hash: | ||
origin: zone | ||
nested_hash: | ||
integer: 44 | ||
added_by_zone: zone |
16 changes: 16 additions & 0 deletions
16
test/fixtures/files/puppet/environments/lookup_tests/hiera.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
version: 5 | ||
defaults: | ||
datadir: data | ||
data_hash: yaml_data | ||
|
||
hierarchy: | ||
- name: "Host specific" | ||
path: "nodes/%{::facts.fqdn}.yaml" | ||
|
||
- name: "Role / zone specific" # Uses custom facts. | ||
paths: | ||
- "role/%{::facts.role}.yaml" | ||
- "zone/%{::facts.zone}.yaml" | ||
|
||
- name: "Global data" | ||
path: "common.yaml" |
5 changes: 5 additions & 0 deletions
5
test/fixtures/files/puppet/nodes/lookup.betadots.training_facts.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
fqdn: 'lookup.betadots.training' | ||
role: 'test_role' | ||
environment: 'lookup_tests' | ||
zone: 'test_zone' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.