Skip to content

Commit

Permalink
Refs #31964 - Introduce foreman::to_symbolized_yaml
Browse files Browse the repository at this point in the history
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
ekohl committed Apr 7, 2021
1 parent 0b3507b commit e2e5d35
Show file tree
Hide file tree
Showing 5 changed files with 104 additions and 10 deletions.
38 changes: 38 additions & 0 deletions lib/puppet/functions/foreman/to_symbolized_yaml.rb
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
7 changes: 6 additions & 1 deletion manifests/dynflow/worker.pp
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@
if $ensure == 'present' {
assert_type(Array[String[1], 1], $queues)

$config = {
'concurrency' => $concurrency,
'queues' => $queues,
}

file { $filename:
ensure => file,
owner => $config_owner,
group => pick($config_group, $foreman::group),
mode => $config_mode,
content => template('foreman/dynflow_worker.yml.erb'),
content => foreman::to_symbolized_yaml($config),
}
~> service { $service:
ensure => running,
Expand Down
53 changes: 49 additions & 4 deletions spec/defines/foreman_dynflow_worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,21 +42,58 @@ class { 'foreman':
{queues: ['default', 'remote_execution']}
end

let(:expected_content) do
<<~CONTENT
---
:concurrency: 1
:queues:
- default
- remote_execution
CONTENT
end

it { should compile.with_all_deps }
it {
should contain_file('/etc/foreman/dynflow/test_worker.yml')
.with_ensure('file')
.with_owner('root')
.with_group('foreman')
.with_mode('0644')
.with_content(/:concurrency: 1/)
.with_content(/:queues:\n - default\n - remote_execution/)
.with_content(expected_content)
}
it {
should contain_service('dynflow-sidekiq@test_worker')
.with_ensure('running')
.with_enable(true)
}

context 'and priorities' do
let(:params) do
{queues: [['default', 1], ['remote_execution', 1]]}
end

let(:expected_content) do
<<~CONTENT
---
:concurrency: 1
:queues:
- - default
- 1
- - remote_execution
- 1
CONTENT
end

it { should compile.with_all_deps }
it do
should contain_file('/etc/foreman/dynflow/test_worker.yml')
.with_ensure('file')
.with_owner('root')
.with_group('foreman')
.with_mode('0644')
.with_content(expected_content)
end
end
end

context 'with custom concurrency and queues' do
Expand All @@ -67,14 +104,22 @@ class { 'foreman':
}
end

let(:expected_content) do
<<~CONTENT
---
:concurrency: 10
:queues:
- katello
CONTENT
end

it { should compile.with_all_deps }
it { should contain_file('/etc/foreman/dynflow/test_worker.yml')
.with_ensure('file')
.with_owner('root')
.with_group('foreman')
.with_mode('0644')
.with_content(/:concurrency: 10/)
.with_content(/:queues:\n - katello\n$/) }
.with_content(expected_content) }
end
end
end
Expand Down
11 changes: 11 additions & 0 deletions spec/functions/foreman_to_symbolized_yaml_spec.rb
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
5 changes: 0 additions & 5 deletions templates/dynflow_worker.yml.erb

This file was deleted.

0 comments on commit e2e5d35

Please sign in to comment.