-
Notifications
You must be signed in to change notification settings - Fork 52
Hiera: Test for wrong interpolation syntax #143
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -20,6 +20,16 @@ def check_hiera_key(key) | |
| end | ||
| end | ||
|
|
||
| def check_hiera_data(_key, value) | ||
| # using filter_map to remove nil values | ||
| # there will be nil values if check_broken_function_call didn't return a string | ||
| # this is a shorthand for filter.compact | ||
| # https://blog.saeloun.com/2019/05/25/ruby-2-7-enumerable-filter-map/ | ||
| keys_and_values(value).filter_map do |element| | ||
| check_broken_function_call(element) | ||
| end | ||
| end | ||
|
|
||
| # Recurse through complex data structures. Return on first error. | ||
| def check_eyaml_data(name, val) | ||
| error = nil | ||
|
|
@@ -87,6 +97,11 @@ def check(filelist) | |
| key_msg = check_hiera_key(k) | ||
| errors << "WARNING: #{hiera_file}: Key :#{k}: #{key_msg}" if key_msg | ||
| end | ||
| if PuppetSyntax.check_hiera_data | ||
| check_hiera_data(k, v).each do |value_msg| | ||
| errors << "WARNING: #{hiera_file}: Key :#{k}: #{value_msg}" | ||
| end | ||
| end | ||
| eyaml_msg = check_eyaml_data(k, v) | ||
| errors << "WARNING: #{hiera_file}: #{eyaml_msg}" if eyaml_msg | ||
| end | ||
|
|
@@ -96,5 +111,51 @@ def check(filelist) | |
|
|
||
| errors | ||
| end | ||
|
|
||
| private | ||
|
|
||
| # you can call functions in hiera, like this: | ||
| # "%{lookup('this_is_ok')}" | ||
| # you can do this in everywhere in a hiera value | ||
| # you cannot do string concatenation within {}: | ||
| # "%{lookup('this_is_ok'):3306}" | ||
| # You can do string concatenation outside of {}: | ||
| # "%{lookup('this_is_ok')}:3306" | ||
| def check_broken_function_call(element) | ||
| 'string after a function call but before `}` in the value' if element.is_a?(String) && /%{.+\('.*'\).+}/.match?(element) | ||
| end | ||
|
|
||
| # gets a hash or array, returns all keys + values as array | ||
| def flatten_data(data, parent_key = []) | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this was written with the help of chatgpt |
||
| if data.is_a?(Hash) | ||
| data.flat_map do |key, value| | ||
| current_key = parent_key + [key.to_s] | ||
| if value.is_a?(Hash) || value.is_a?(Array) | ||
| flatten_data(value, current_key) | ||
| else | ||
| [current_key.join('.'), value] | ||
| end | ||
| end | ||
| elsif data.is_a?(Array) && !data.empty? | ||
| data.flat_map do |value| | ||
| if value.is_a?(Hash) || value.is_a?(Array) | ||
| flatten_data(value, parent_key) | ||
| else | ||
| [parent_key.join('.'), value] | ||
| end | ||
| end | ||
| else | ||
| [parent_key.join('.')] | ||
| end | ||
| end | ||
|
|
||
| # gets a string, hash or array, returns all keys + values as flattened + unique array | ||
| def keys_and_values(value) | ||
| if value.is_a?(Hash) || value.is_a?(Array) | ||
| flatten_data(value).flatten.uniq | ||
| else | ||
| [value] | ||
| end | ||
| end | ||
| end | ||
| end | ||
This file contains hidden or 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 hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.