-
Notifications
You must be signed in to change notification settings - Fork 271
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refs #31964 - Introduce foreman::to_symbolized_yaml
This function symbolizes keys in YAML which is typically what Foreman config files use. This is hard to achieve with pure Puppet so a Ruby function is used.
- Loading branch information
Showing
5 changed files
with
104 additions
and
10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
# frozen_string_literal: true | ||
|
||
require 'yaml' | ||
# @summary | ||
# Convert a data structure and output it as YAML while symbolizing keys | ||
# | ||
# In Foreman often YAML files have symbols as keys. Since it's hard to do that | ||
# from Puppet, this function does it for you. | ||
# | ||
# @example How to output YAML | ||
# # output yaml to a file | ||
# file { '/tmp/my.yaml': | ||
# ensure => file, | ||
# content => foreman::to_symbolized_yaml($myhash), | ||
# } | ||
# @example Use options control the output format | ||
# file { '/tmp/my.yaml': | ||
# ensure => file, | ||
# content => foreman::to_symbolized_yaml($myhash, {indentation: 4}) | ||
# } | ||
Puppet::Functions.create_function(:'foreman::to_symbolized_yaml') do | ||
# @param data | ||
# @param options | ||
# | ||
# @return [String] | ||
dispatch :to_symbolized_yaml do | ||
param 'Any', :data | ||
optional_param 'Hash', :options | ||
end | ||
|
||
def to_symbolized_yaml(data, options = {}) | ||
if data.is_a?(Hash) | ||
data = Hash[data.map { |k, v| [k.to_sym, v] }] | ||
end | ||
|
||
data.to_yaml(options) | ||
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
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,11 @@ | ||
require 'spec_helper' | ||
|
||
describe 'foreman::to_symbolized_yaml' do | ||
it 'should exist' do | ||
is_expected.not_to eq(nil) | ||
end | ||
|
||
it 'should symbolize keys' do | ||
is_expected.to run.with_params({'a' => 'b'}).and_return("---\n:a: b\n") | ||
end | ||
end |
This file was deleted.
Oops, something went wrong.