Skip to content

Commit

Permalink
Merge pull request #934 from wyardley/btisdall-allow-array-for-map-re…
Browse files Browse the repository at this point in the history
…source-mappings

Allow mappings to be supplied as array of hashes.
  • Loading branch information
wyardley authored Oct 19, 2016
2 parents 3e9ea24 + 3f9b12d commit dec3293
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 5 deletions.
25 changes: 24 additions & 1 deletion manifests/resource/map.pp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@
# }
# }
#
# Sample Usage (preserving input of order of mappings):
#
# nginx::resource::map { 'backend_pool':
# ...
# mappings => [
# { 'key' => '*.sf.example.com', 'value' => 'sf-pool-1' },
# { 'key' => '*.nyc.example.com', 'value' => 'ny-pool-1' },
# ]
# }
#
# Sample Hiera usage:
#
# nginx::string_mappings:
Expand All @@ -39,6 +49,17 @@
# mappings:
# '*.nyc.example.com': 'ny-pool-1'
# '*.sf.example.com': 'sf-pool-1'
#
# Sample Hiera usage (preserving input of order of mappings):
#
# nginx::string_mappings:
# client_network:
# ...
# mappings:
# - key: '*.sf.example.com'
# value: 'sf-pool-1'
# - key: '*.nyc.example.com'
# value: 'ny-pool-1'


define nginx::resource::map (
Expand All @@ -51,7 +72,9 @@
validate_string($string)
validate_re($string, '^.{2,}$',
"Invalid string value [${string}]. Expected a minimum of 2 characters.")
validate_hash($mappings)
if ! ( is_array($mappings) or is_hash($mappings) ) {
fail("\$mappings must be a hash of the form { 'foo' => 'pool_b' } or array of hashes of form [{ 'key' => 'foo', 'value' => 'pool_b' }, ...]")
}
validate_bool($hostnames)
validate_re($ensure, '^(present|absent)$',
"Invalid ensure value '${ensure}'. Expected 'present' or 'absent'")
Expand Down
30 changes: 27 additions & 3 deletions spec/defines/resource_map_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,31 @@
match: [' default pool_a;']
},
{
title: 'should contain ordered mappings',
title: 'should contain ordered mappings when supplied as a hash',
attr: 'mappings',
value: {
'foo' => 'pool_b',
'bar' => 'pool_c',
'baz' => 'pool_d'
},
match: [
' foo pool_b;',
' bar pool_c;',
' baz pool_d;',
' foo pool_b;'
' baz pool_d;'
]
},
{
title: 'should contain mappings in input order when supplied as an array of hashes',
attr: 'mappings',
value: [
{ 'key' => 'foo', 'value' => 'pool_b' },
{ 'key' => 'bar', 'value' => 'pool_c' },
{ 'key' => 'baz', 'value' => 'pool_d' }
],
match: [
' foo pool_b;',
' bar pool_c;',
' baz pool_d;'
]
}
].each do |param|
Expand All @@ -89,6 +103,16 @@

it { is_expected.to contain_file("/etc/nginx/conf.d/#{title}-map.conf").with_ensure('absent') }
end

context 'when mappings is a string' do
let :params do
default_params.merge(
mappings: 'foo pool_b'
)
end

it { is_expected.to raise_error(Puppet::Error, %r[mappings must be a hash of the form { 'foo' => 'pool_b' }]) }
end
end
end
end
8 changes: 7 additions & 1 deletion templates/conf.d/map.erb
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ map <%= @string %> $<%= @name %> {
default <%= @default %>;
<% end -%>

<% if @mappings -%>
<% if @mappings.is_a?(Hash) -%>
<%- field_width = @mappings.inject(0) { |l,(k,v)| k.size > l ? k.size : l } -%>
<%- @mappings.sort_by{|k,v| k}.each do |key,value| -%>
<%= sprintf("%-*s", field_width, key) %> <%= value %>;
<%- end -%>
<% end -%>
<% if @mappings.is_a?(Array) -%>
<%- field_width = @mappings.inject(0) { |l,(h)| h['key'].size > l ? h['key'].size : l } -%>
<%- @mappings.each do |h| -%>
<%= sprintf("%-*s", field_width, h['key']) %> <%= h['value'] %>;
<%- end -%>
<% end -%>
}

0 comments on commit dec3293

Please sign in to comment.