Skip to content

Commit

Permalink
Add the option to use an array for extra_config_section
Browse files Browse the repository at this point in the history
The option "extra_config_section" can be defined as an array,
this adds the option to create multiple lines instead of one
big line with all the parameters.

This commit addresses issue voxpupuli#31
  • Loading branch information
ralfbosz committed Jan 20, 2017
1 parent 192c272 commit 4d38dc3
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 8 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Expand Up @@ -302,7 +302,7 @@ Style/EmptyLiteral:
Metrics/LineLength:
Enabled: False

Style/MethodCallParentheses:
Style/MethodCallWithoutArgsParentheses:
Enabled: True

Style/MethodDefParentheses:
Expand Down
30 changes: 27 additions & 3 deletions README.md
Expand Up @@ -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 }},
}
```
Expand Down Expand Up @@ -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.
Expand Down
10 changes: 8 additions & 2 deletions manifests/extra_config_section.pp
Expand Up @@ -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}",
}

}
21 changes: 21 additions & 0 deletions spec/defines/extra_config_section_spec.rb
Expand Up @@ -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
Expand Down Expand Up @@ -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
4 changes: 2 additions & 2 deletions spec/spec_helper.rb
Expand Up @@ -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

Expand Down
10 changes: 10 additions & 0 deletions 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 -%>

0 comments on commit 4d38dc3

Please sign in to comment.