Skip to content

Commit

Permalink
Fixing ruby Symbol regex handle.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcos Machado committed Sep 25, 2020
1 parent bf4203d commit b6f728b
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 30 deletions.
1 change: 0 additions & 1 deletion manifests/eyaml.pp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@
$keysdir = dirname($_keysdir)

if ( $create_keys == true ) {

exec { 'createkeys':
user => $owner,
cwd => $keysdir,
Expand Down
46 changes: 22 additions & 24 deletions manifests/init.pp
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@
#Deprecated
$gem_source = undef,
) inherits ::hiera::params {

if $keysdir {
$_keysdir = $keysdir
} else {
Expand Down Expand Up @@ -164,7 +163,6 @@
}

if ( $eyaml_gpg ) or ( $eyaml ) {

$eyaml_real_datadir = empty($eyaml_datadir) ? {
false => $eyaml_datadir,
true => $datadir,
Expand All @@ -176,7 +174,6 @@
} else {
$requested_backends = unique(concat(['eyaml'], $backends))
}

} else {
$requested_backends = $backends
$eyaml_real_datadir = undef
Expand Down Expand Up @@ -207,14 +204,14 @@
# the above logic. This was neccessary in order to maintain compability
# with prior versions of this module
$eyaml_options = {
'eyaml' => delete_undef_values({
'datadir' => $eyaml_real_datadir,
'extension' => $eyaml_extension,
'pkcs7_private_key' => $_eyaml_pkcs7_private_key,
'pkcs7_public_key' => $_eyaml_pkcs7_public_key,
'encrypt_method' => $encrypt_method,
'gpg_gnupghome' => $gpg_gnupghome,
'gpg_recipients' => $eyaml_gpg_recipients,
'eyaml' => delete_undef_values( {
'datadir' => $eyaml_real_datadir,
'extension' => $eyaml_extension,
'pkcs7_private_key' => $_eyaml_pkcs7_private_key,
'pkcs7_public_key' => $_eyaml_pkcs7_public_key,
'encrypt_method' => $encrypt_method,
'gpg_gnupghome' => $gpg_gnupghome,
'gpg_recipients' => $eyaml_gpg_recipients,
}),
}
$yaml_options = { 'yaml' => { 'datadir' => $datadir } }
Expand All @@ -228,7 +225,7 @@
# catch that error here and notify the user
$missing_backends = difference($backends, keys($backend_data))
if count($missing_backends) > 0 {
fail("The supplied backends: ${missing_backends} are missing from the backend_options hash:\n ${backend_options}\n
fail("The supplied backends: ${missing_backends} are missing from the backend_options hash:\n ${backend_options}\n
or you might be using symbols in your hiera data")
}

Expand All @@ -250,18 +247,19 @@

# Determine hiera version
case $hiera_version {
'5': { if ($hierarchy !~ Hiera::Hiera5_hierarchy) {
fail('`hierarchy` should be an array of hash')
}
else
{ $hiera_template = epp('hiera/hiera.yaml.epp',
{
'hiera_version' => $hiera_version,
'hiera5_defaults' => $hiera5_defaults,
'hierarchy' => $hierarchy
})
}
} # Apply epp if hiera version is 5
'5': {
if ($hierarchy !~ Hiera::Hiera5_hierarchy) {
fail('`hierarchy` should be an array of hash')
}
else {
$hiera_template = epp('hiera/hiera.yaml.epp',
{
'hiera_version' => $hiera_version,
'hiera5_defaults' => $hiera5_defaults,
'hierarchy' => $hierarchy
})
}
} # Apply epp if hiera version is 5
default: { $hiera_template = template('hiera/hiera.yaml.erb') } # Apply erb for default version 3
}
file { $hiera_yaml:
Expand Down
1 change: 0 additions & 1 deletion manifests/install.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
$gem_source = undef,
$gem_install_options = $hiera::gem_install_options,
) {

# $gem_install_options is typically used for specifying a proxy
Package {
install_options => $gem_install_options,
Expand Down
2 changes: 1 addition & 1 deletion manifests/params.pp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
class hiera::params {
$confdir = $::settings::confdir
$hiera_version = '3'
$hiera5_defaults = {'datadir' => 'data', 'data_hash' => 'yaml_data'}
$hiera5_defaults = { 'datadir' => 'data', 'data_hash' => 'yaml_data' }
$package_ensure = 'present'
$package_name = 'hiera'
$hierarchy = []
Expand Down
15 changes: 13 additions & 2 deletions spec/classes/hiera_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,16 @@
merge_behavior: 'deeper',
eyaml: true,
datadir: '/etc/puppetlabs/code/environments/%{::environment}/hieradata',
backends: %w[yaml eyaml json yamll],
backends: %w[yaml eyaml json yamll redis],
'backend_options' => {
'json' => {
'datadir' => '/etc/puppet/json_data/data'
},
'yamll' => {
'datadir' => '/etc/puppet/yamll_data/data'
},
'redis' => {
'deserialize' => '!ruby/sym json'
}
}
}
Expand All @@ -78,9 +81,13 @@
expect(content).not_to include('!ruby/sym')
end

it 'testing gsub and regex' do
expect('!ruby/sym json'.to_yaml.gsub(/---\s+/, '').gsub(/(?:'|")(\!ruby\/sym\s)(.*)(?:'|")/, ':\2')).to eq ":json\n"
end

it 'include backends' do
backends = YAML.load(content)[:backends]
expect(backends).to eq(%w[yaml eyaml json yamll])
expect(backends).to eq(%w[yaml eyaml json yamll redis])
end
it 'include json backend' do
backend = YAML.load(content)[:json]
Expand All @@ -90,6 +97,10 @@
backend = YAML.load(content)[:yamll]
expect(backend[:datadir]).to eq('/etc/puppet/yamll_data/data')
end
it 'include redis backend with deserialize' do
redis_backend = YAML.load(content)[:redis]
expect(redis_backend[:deserialize]).to eq(:json)
end
# rubocop:disable RSpec/MultipleExpectations
it 'include eyaml backend' do
eyaml_backend = YAML.load(content)[:eyaml]
Expand Down
3 changes: 2 additions & 1 deletion templates/hiera.yaml.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@
<%- all_backends = @backend_data.inject({}){|data,(k,v)| data[k.to_sym] = v; data} -%>
<%# symobolize backend option keys because hiera backends assume the usage of symbols :( -%>
<%- b_data = all_backends[backend_name.to_sym].inject({}){|data,(k,v)| data[k.to_sym] = v; data} -%>
<%= { backend_name.to_sym => b_data }.to_yaml.gsub(/---\s+/, '').gsub(/\!ruby\/sym\s/, ':') -%>
<%# Replacing '!ruby/sym' with ':' and appending the string (value) between quotes to form the symbol -%>
<%= { backend_name.to_sym => b_data }.to_yaml.gsub(/---\s+/, '').gsub(/(?:'|")(\!ruby\/sym\s)(.*)(?:'|")/, ':\2') -%>
<%- end -%>

<% if @merge_behavior -%>
Expand Down

0 comments on commit b6f728b

Please sign in to comment.