From 15f397c65f39a0b0526f4d237d9d94a6826c5854 Mon Sep 17 00:00:00 2001 From: Ralf Bosz Date: Wed, 18 Jan 2017 08:56:45 +0100 Subject: [PATCH] Add the option to use an array for extra_config_section The option "extra_config_section" can be defined as an array, this adds the option to add an option on multiple lines instead of one big line. This commit addresses issue #31 --- .rubocop.yml | 2 +- README.md | 30 +++++++++++++++++-- manifests/extra_config_section.pp | 10 +++++-- spec/defines/extra_config_section_spec.rb | 21 +++++++++++++ spec/spec_helper.rb | 4 +-- templates/squid.conf.extra_config_section.erb | 10 +++++++ 6 files changed, 69 insertions(+), 8 deletions(-) diff --git a/.rubocop.yml b/.rubocop.yml index 0e28667..f2136f3 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -302,7 +302,7 @@ Style/EmptyLiteral: Metrics/LineLength: Enabled: False -Style/MethodCallParentheses: +Style/MethodCallWithoutArgsParentheses: Enabled: True Style/MethodDefParentheses: diff --git a/README.md b/README.md index 4043253..92a0ad9 100644 --- a/README.md +++ b/README.md @@ -79,9 +79,9 @@ class{'::squid': 'http://example.com/anotherpath'], }, }, - http_access => { 'our_networks hosts' => { action => 'allow', }, - http_ports => { '10000' => { options => 'accel vhost'} }, - snmp_ports => { '1000' => { process_number => 3 }, + http_access => { 'our_networks hosts' => { action => 'allow', }}, + http_ports => { '10000' => { options => 'accel vhost', }}, + snmp_ports => { '1000' => { process_number => 3 }}, cache_dirs => { '/data/' => { type => 'ufs', options => '15000 32 256 min-size=32769', process_number => 2 }}, } ``` @@ -337,6 +337,30 @@ mail_from squid@example.com mail_program mail ``` +And using an array: + +```puppet +squid::extra_config_section { 'refresh patterns': + order => '60', + config_entries => [{ + 'refresh_pattern' => ['^ftp: 1440 20% 10080', + '^gopher: 1440 0% 1440', + '-i (/cgi-bin/|\?) 0 0% 0', + '. 0 20% 4320'], + }], +} +``` + +Results in a squid configuration of + +``` +# refresh_patterns +refresh_pattern ^ftp: 1440 20% 10080 +refresh_pattern ^gopher: 1440 0% 1440 +refresh_pattern -i (/cgi-bin/|\?) 0 0% 0 +refresh_pattern . 0 20% 4320 +``` + #### Parameters for Type squid::extra\_config\_section * `comment` defaults to the namevar and is used as a section comment in `squid.conf`. * `config_entries` A hash of configuration entries to create in this section. The hash key is the name of the configuration directive. The value is either a string, or an array of strings to use as the configuration directive options. diff --git a/manifests/extra_config_section.pp b/manifests/extra_config_section.pp index 85f2055..80f9e0d 100644 --- a/manifests/extra_config_section.pp +++ b/manifests/extra_config_section.pp @@ -5,12 +5,18 @@ ) { validate_string($comment) - validate_hash($config_entries) + + if is_array($config_entries) { + each($config_entries) |$single_entry| { + validate_hash($single_entry) + } + } else { + validate_hash($config_entries) + } concat::fragment{"squid_extra_config_section_${comment}": target => $::squid::config, content => template('squid/squid.conf.extra_config_section.erb'), order => "${order}-${comment}", } - } diff --git a/spec/defines/extra_config_section_spec.rb b/spec/defines/extra_config_section_spec.rb index 8b3ba30..8ae39ba 100644 --- a/spec/defines/extra_config_section_spec.rb +++ b/spec/defines/extra_config_section_spec.rb @@ -19,6 +19,12 @@ expected_config_section += %(sslcrtd_children 8 startup=1 idle=1\n) expected_config_section += %(\n) + expected_config_section2 = %(# my config section\n) + expected_config_section2 += %(refresh_pattern ^ftp: 1440 20% 10080\n) + expected_config_section2 += %(refresh_pattern ^gopher: 1440 0% 1440\n) + expected_config_section2 += %(refresh_pattern . 0 20% 4320\n) + expected_config_section2 += %(\n) + let(:title) { 'my config section' } context 'when config entry parameters are strings' do let(:params) do @@ -52,6 +58,21 @@ expect(content).to match(expected_config_section) end end + context 'when config entry is an array' do + let(:params) do + { + config_entries: [{ + 'refresh_pattern' => ['^ftp: 1440 20% 10080', + '^gopher: 1440 0% 1440', + '. 0 20% 4320'] + }] + } + end + it 'config section' do + content = catalogue.resource('concat_fragment', 'squid_extra_config_section_my config section').send(:parameters)[:content] + expect(content).to match(expected_config_section2) + end + end end end end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 2aa9da7..a069fc7 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -24,8 +24,8 @@ puppetversion: Puppet.version, facterversion: Facter.version } - default_facts.merge!(YAML.load(File.read(File.expand_path('../default_facts.yml', __FILE__)))) if File.exist?(File.expand_path('../default_facts.yml', __FILE__)) - default_facts.merge!(YAML.load(File.read(File.expand_path('../default_module_facts.yml', __FILE__)))) if File.exist?(File.expand_path('../default_module_facts.yml', __FILE__)) + default_facts.merge!(YAML.safe_load(File.read(File.expand_path('../default_facts.yml', __FILE__)))) if File.exist?(File.expand_path('../default_facts.yml', __FILE__)) + default_facts.merge!(YAML.safe_load(File.read(File.expand_path('../default_module_facts.yml', __FILE__)))) if File.exist?(File.expand_path('../default_module_facts.yml', __FILE__)) c.default_facts = default_facts end diff --git a/templates/squid.conf.extra_config_section.erb b/templates/squid.conf.extra_config_section.erb index 994e044..f9a4535 100644 --- a/templates/squid.conf.extra_config_section.erb +++ b/templates/squid.conf.extra_config_section.erb @@ -1,5 +1,15 @@ # <%= @comment %> +<% if @config_entries.is_a?(Array) -%> +<% @config_entries.each do |i| -%> +<% i.each do |k, v| -%> +<% v.each do |v2| -%> +<%= k %> <%= v2 %> +<% end -%> +<% end -%> +<% end -%> +<% else -%> <% @config_entries.each do |k,v| -%> <%= k %> <%= v.is_a?(Array) ? v.join(' ') : v %> <% end -%> +<% end -%>