diff --git a/manifests/resource/location.pp b/manifests/resource/location.pp index f5a906abe..2a4efd654 100644 --- a/manifests/resource/location.pp +++ b/manifests/resource/location.pp @@ -7,8 +7,7 @@ # (present|absent) # [*internal*] - Indicates whether or not this location can be # used for internal requests only. Default: false -# [*server*] - Defines the default server for this location -# entry to include with +# [*server*] - Defines all server for this location entry to include with # [*location*] - Specifies the URI associated with this location # entry # [*location_satisfy*] - Allows access if all (all) or at least one (any) of the auth modules allow access. @@ -123,6 +122,14 @@ # server => 'test2.local', # } # +# Use one location in multiple serrvers +# nginx::resource::location { 'test2.local-bob': +# ensure => present, +# www_root => '/var/www/bob', +# location => '/bob', +# server => ['test1.local','test2.local'], +# } +# # Custom config example to limit location on localhost, # create a hash with any extra custom config you want. # $my_config = { @@ -164,7 +171,7 @@ Enum['present', 'absent'] $ensure = present, Boolean $internal = false, String $location = $name, - String $server = undef, + Variant[String[1],Array[String[1],1]] $server = undef, Optional[String] $www_root = undef, Optional[String] $autoindex = undef, Array $index_files = [ @@ -258,24 +265,15 @@ warning('The $fastcgi_script parameter is deprecated; please use $fastcgi_param instead to define custom fastcgi_params!') } - $server_sanitized = regsubst($server, ' ', '_', 'G') - if $nginx::confd_only { - $server_dir = "${nginx::conf_dir}/conf.d" - } else { - $server_dir = "${nginx::conf_dir}/sites-available" - } - - $config_file = "${server_dir}/${server_sanitized}.conf" - # Only try to manage these files if they're the default one (as you presumably # usually don't want the default template if you're using a custom file. - if ( + if ( $ensure == present and $fastcgi != undef and !defined(File[$fastcgi_params]) and $fastcgi_params == "${nginx::conf_dir}/fastcgi.conf" - ) { + ) { file { $fastcgi_params: ensure => present, mode => '0644', @@ -291,25 +289,35 @@ } } - if $ensure == present { - ## Create stubs for server File Fragment Pattern - $location_md5 = md5($location) - if ($ssl_only != true) { - concat::fragment { "${server_sanitized}-${priority}-${location_md5}": - target => $config_file, - content => template('nginx/server/location.erb'), - order => $priority, - } + any2array($server).each |$s| { + $server_sanitized = regsubst($s, ' ', '_', 'G') + if $nginx::confd_only { + $server_dir = "${nginx::conf_dir}/conf.d" + } else { + $server_dir = "${nginx::conf_dir}/sites-available" } - ## Only create SSL Specific locations if $ssl is true. - if ($ssl == true or $ssl_only == true) { - $ssl_priority = $priority + 300 + $config_file = "${server_dir}/${server_sanitized}.conf" + if $ensure == present { + ## Create stubs for server File Fragment Pattern + $location_md5 = md5($location) + if ($ssl_only != true) { + concat::fragment { "${server_sanitized}-${priority}-${location_md5}": + target => $config_file, + content => template('nginx/server/location.erb'), + order => $priority, + } + } + + ## Only create SSL Specific locations if $ssl is true. + if ($ssl == true or $ssl_only == true) { + $ssl_priority = $priority + 300 - concat::fragment { "${server_sanitized}-${ssl_priority}-${location_md5}-ssl": - target => $config_file, - content => template('nginx/server/location.erb'), - order => $ssl_priority, + concat::fragment { "${server_sanitized}-${ssl_priority}-${location_md5}-ssl": + target => $config_file, + content => template('nginx/server/location.erb'), + order => $ssl_priority, + } } } } diff --git a/spec/defines/resource_location_spec.rb b/spec/defines/resource_location_spec.rb index ff0472ab9..eda47cc73 100644 --- a/spec/defines/resource_location_spec.rb +++ b/spec/defines/resource_location_spec.rb @@ -33,6 +33,23 @@ it { is_expected.not_to contain_file('/etc/nginx/rspec-test_htpasswd') } end + describe 'server/location configuration files' do + context 'when we have one location and one server' do + let(:params) { { location: 'my_location', proxy: 'proxy_value', server: 'server1' } } + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it { is_expected.not_to contain_concat__fragment('server2-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + end + context 'when we have one location and two server' do + let(:params) { { location: 'my_location', proxy: 'proxy_value', server: ['server1', 'server2'] } } + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_concat__fragment('server1-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + it { is_expected.to contain_concat__fragment('server2-500-' + Digest::MD5.hexdigest(params[:location].to_s)) } + end + end + describe 'server/location_header template content' do [ {