Skip to content

Commit

Permalink
Merge pull request #953 from wyardley/issues_717_upstream_cfg_append
Browse files Browse the repository at this point in the history
Add upstream_cfg_append (to match prepend)
  • Loading branch information
jyaworski authored Nov 1, 2016
2 parents 77c5051 + 7f6dc88 commit 77397e1
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 9 deletions.
23 changes: 14 additions & 9 deletions manifests/resource/upstream.pp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
# [*members*] - Array of member URIs for NGINX to connect to. Must follow valid NGINX syntax.
# If omitted, individual members should be defined with nginx::resource::upstream::member
# [*ensure*] - Enables or disables the specified location (present|absent)
# [*upstream_cfg_append*] - Hash of custom directives to put after other directives in upstream
# [*upstream_cfg_prepend*] - It expects a hash with custom directives to put before anything else inside upstream
# [*upstream_fail_timeout*] - Set the fail_timeout for the upstream. Default is 10 seconds - As that is what Nginx does normally.
# [*upstream_max_fails*] - Set the max_fails for the upstream. Default is to use nginx default value which is 1.
Expand Down Expand Up @@ -40,13 +41,14 @@
# upstream_cfg_prepend => $my_config,
# }
define nginx::resource::upstream (
$members = undef,
$members_tag = undef,
$ensure = 'present',
$upstream_cfg_prepend = undef,
$members = undef,
$members_tag = undef,
$ensure = 'present',
$upstream_cfg_append = undef,
$upstream_cfg_prepend = undef,
$upstream_fail_timeout = '10s',
$upstream_max_fails = undef,
$upstream_context = 'http',
$upstream_max_fails = undef,
$upstream_context = 'http',
) {

if $members != undef {
Expand All @@ -56,6 +58,9 @@
"${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.")
validate_re($upstream_context, '^(http|stream)$',
"${upstream_context} is not supported for upstream_context. Allowed values are 'http' and 'stream'.")
if ($upstream_cfg_append != undef) {
validate_hash($upstream_cfg_append)
}
if ($upstream_cfg_prepend != undef) {
validate_hash($upstream_cfg_prepend)
}
Expand Down Expand Up @@ -87,15 +92,15 @@
concat::fragment { "${name}_upstream_header":
target => "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf",
order => '10',
content => template('nginx/conf.d/upstream_header.erb'),
content => template('nginx/upstream/upstream_header.erb'),
}

if $members != undef {
# Uses: $members, $upstream_fail_timeout
concat::fragment { "${name}_upstream_members":
target => "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf",
order => '50',
content => template('nginx/conf.d/upstream_members.erb'),
content => template('nginx/upstream/upstream_members.erb'),
}
} else {
# Collect exported members:
Expand All @@ -109,6 +114,6 @@
concat::fragment { "${name}_upstream_footer":
target => "${::nginx::config::conf_dir}/${conf_dir_real}/${name}-upstream.conf",
order => '90',
content => "}\n",
content => template('nginx/upstream/upstream_footer.erb'),
}
}
23 changes: 23 additions & 0 deletions spec/defines/resource_upstream_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,29 @@
' server test1 fail_timeout=10s;',
' server test2 fail_timeout=10s;'
]
},
{
title: 'should contain ordered appended directives',
attr: 'upstream_cfg_append',
fragment: 'footer',
value: {
'test3' => 'test value 3',
'test6' => { 'subkey1' => %w(subvalue1 subvalue2) },
'test1' => 'test value 1',
'test2' => 'test value 2',
'test5' => { 'subkey1' => 'subvalue1' },
'test4' => ['test value 1', 'test value 2']
},
match: [
' test1 test value 1;',
' test2 test value 2;',
' test3 test value 3;',
' test4 test value 1;',
' test4 test value 2;',
' test5 subkey1 subvalue1;',
' test6 subkey1 subvalue1;',
' test6 subkey1 subvalue2;'
]
}
].each do |param|
context "when #{param[:attr]} is #{param[:value]}" do
Expand Down
12 changes: 12 additions & 0 deletions templates/upstream/upstream_footer.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<% if @upstream_cfg_append -%><% @upstream_cfg_append.sort_by{|k,v| k}.each do |key,value| %>
<% if value.is_a?(Hash) -%><% value.each do |subkey,subvalue| -%>
<% Array(subvalue).each do |asubvalue| -%>
<%= key %> <%= subkey %> <%= asubvalue %>;
<% end -%>
<% end -%><% else -%>
<% Array(value).each do |asubvalue| -%>
<%= key %> <%= asubvalue %>;
<% end -%>
<% end -%>
<% end -%><% end -%>
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 comments on commit 77397e1

Please sign in to comment.