Skip to content

Commit

Permalink
Manage whole ini sections
Browse files Browse the repository at this point in the history
closes #113
closes #114
closes #117
  • Loading branch information
bobapple committed Jun 6, 2017
1 parent 056fc53 commit 26932f5
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 40 deletions.
22 changes: 13 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,9 @@ Manage settings in INI configuration files.
Set to present creates the ini section, absent removes it. Defaults to present. Singhe settings may be set to 'absent'
in the $settings parameter.

##### `section_name`
Name of the target section. Settings are set under `[$section_name]`

##### `target`
Absolute path to the configuration file.

Expand Down Expand Up @@ -246,31 +249,31 @@ multiple hosts separated by a space.
Port number to use.

##### `db_type`
Supported DB types are `mysql` and `pgsql`.
Supported DB types are `mysql` and `pgsql`. Only valid when `type` is `db`.

##### `db_name`
The database to use.
The database to use. Only valid if `type` is `db`.

##### `db_username`
The username to use when connecting to the server.
The username to use when connecting to the server. Only valid if `type` is `db`.

##### `db_password`
The password to use when connecting to the server.
The password to use when connecting to the server. Only valid if `type` is `db`.

##### `db_charset`
The character set to use for the database connection.
The character set to use for the database connection. Only valid if `type` is `db`.

##### `ldap_root_dn`
Root object of the tree, e.g. `ou=people,dc=icinga,dc=com`
Root object of the tree, e.g. `ou=people,dc=icinga,dc=com`. Only valid if `type` is `ldap`.

##### `ldap_bind_dn`
The user to use when connecting to the server.
The user to use when connecting to the server. Only valid if `type` is `ldap`.

##### `ldap_bind_pw`
The password to use when connecting to the server.
The password to use when connecting to the server. Only valid if `type` is `ldap`.

##### `ldap_encryption`
Type of encryption to use: `none` (default), `starttls`, `ldaps`.
Type of encryption to use: `none` (default), `starttls`, `ldaps`. Only valid if `type` is `ldap`.

### Private Defined Types

Expand Down Expand Up @@ -303,6 +306,7 @@ See also [CHANGELOG.md]
[darin/zypprepo]: https://forge.puppet.com/darin/zypprepo
[puppetlabs/stdlib]: https://github.com/puppetlabs/puppetlabs-stdlib
[puppetlabs/inifile]: https://forge.puppet.com/puppetlabs/inifile
[puppetlabs/vcsrepo]: https://forge.puppet.com/puppetlabs/vcsrepo
[packages.icinga.com]: https://packages.icinga.com

[CHANGELOG.md]: CHANGELOG.md
Expand Down
32 changes: 20 additions & 12 deletions manifests/config.pp
Original file line number Diff line number Diff line change
Expand Up @@ -33,29 +33,37 @@
group => $conf_group
}

$config_ini = {
logging => {
icingaweb2::inisection {'logging':
target => "${conf_dir}/config.ini",
settings => {
'log' => $logging,
'file' => $logging_file,
'level' => $logging_level,
'level' => $logging_level
},
preferences => {
'type' => $preferences_type,
}

icingaweb2::inisection {'preferences':
target => "${conf_dir}/config.ini",
settings => {
'type' => $preferences_type
},
global => {
}

icingaweb2::inisection {'global':
target => "${conf_dir}/config.ini",
settings => {
'show_stacktraces' => $show_stacktraces,
'module_path' => $module_path,
'config_backend' => $config_backend,
},
themes => {
'default' => $theme,
'disabled' => $theme_disabled,
}
}

icingaweb2::inisection {'config.ini':
icingaweb2::inisection {'themes':
target => "${conf_dir}/config.ini",
settings => $config_ini,
settings => {
'default' => $theme,
'disabled' => $theme_disabled,
},
}

file {
Expand Down
29 changes: 13 additions & 16 deletions manifests/config/resource.pp
Original file line number Diff line number Diff line change
Expand Up @@ -25,28 +25,28 @@
# Supported DB types are `mysql` and `pgsql`.
#
# [*db_name*]
# The database to use.
# The database to use. Only valid if `type` is `db`.
#
# [*db_username*]
# The username to use when connecting to the server.
# The username to use when connecting to the server. Only valid if `type` is `db`.
#
# [*db_password*]
# The password to use when connecting to the server.
# The password to use when connecting to the server. Only valid if `type` is `db`.
#
# [*db_charset*]
# The character set to use for the database connection.
# The character set to use for the database connection. Only valid if `type` is `db`.
#
# [*ldap_root_dn*]
# Root object of the tree, e.g. 'ou=people,dc=icinga,dc=com'
# Root object of the tree, e.g. 'ou=people,dc=icinga,dc=com'. Only valid if `type` is `ldap`.
#
# [*ldap_bind_dn*]
# The user to use when connecting to the server.
# The user to use when connecting to the server. Only valid if `type` is `ldap`.
#
# [*ldap_bind_pw*]
# The password to use when connecting to the server.
# The password to use when connecting to the server. Only valid if `type` is `ldap`.
#
# [*ldap_encryption*]
# Type of encryption to use: none (default), starttls, ldaps.
# Type of encryption to use: none (default), starttls, ldaps. Only valid if `type` is `ldap`.
#
# === Examples
#
Expand Down Expand Up @@ -100,13 +100,14 @@
validate_string($db_name)
if $db_charset { validate_string($db_charset) }

$type_settings = {
$settings = {
'type' => $type,
'db' => $db_type,
'host' => $host,
'port' => $port,
'dbname' => $db_name,
'username' => $db_username,
'password' => $db_password,
'dbname' => $db_name,
'charset' => $db_charset,
}
}
Expand All @@ -117,7 +118,7 @@
validate_re($ldap_encryption, [ 'none', 'starttls', 'ldaps' ],
"${ldap_encryption} isn't supported. Valid values are 'none', 'starttls' and 'ldaps'.")

$type_settings = {
$settings = {
'type' => $type,
'hostname' => $host,
'port' => $port,
Expand All @@ -132,13 +133,9 @@
}
}

$settings = {
$resource_name => delete_undef_values($type_settings)
}

icingaweb2::inisection { $resource_name:
ensure => $ensure,
target => "${conf_dir}/resources.ini",
settings => $settings,
settings => delete_undef_values($settings),
}
}
12 changes: 9 additions & 3 deletions manifests/inisection.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
# Set to present creates the ini section, absent removes it. Defaults to present.
# Single settings may be set to 'absent' in the $settings parameter.
#
# [*section_name*]
# Name of the target section. Settings are set under [$section_name]
#
# [*target*]
# Absolute path to the configuration file.
#
Expand All @@ -31,15 +34,17 @@
#
define icingaweb2::inisection(
$target,
$ensure = present,
$settings = {},
$ensure = present,
$section_name = $title,
$settings = {},
){

$conf_user = $::icingaweb2::params::conf_user
$conf_group = $::icingaweb2::params::conf_group

validate_re($ensure, [ '^present$', '^absent$' ],
"${ensure} isn't supported. Valid values are 'present' and 'absent'.")
validate_string($section_name)
validate_absolute_path($target)
validate_hash($settings)

Expand All @@ -54,7 +59,8 @@
'path' => $target,
}

manage_ini_settings($settings, $defaults)
$section = { "${section_name}" => $settings }
manage_ini_settings($section, $defaults)

File <| |> -> Ini_setting <| |>
}
31 changes: 31 additions & 0 deletions spec/defines/inisection_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'spec_helper'

describe('icingaweb2::inisection', :type => :define) do
let(:title) { 'foo' }
let(:pre_condition) { [
"class { 'icingaweb2': }"
] }

on_supported_os.each do |os, facts|
let :facts do
facts
end

context "#{os} with valid params" do
let(:params) { {:target => '/foo/bar', :section_name => 'global', :settings => {'setting1' => 'value1', 'setting2' => 'value2'} } }

it { is_expected.to contain_file('/foo/bar') }

it { is_expected.to contain_ini_setting('present /foo/bar [global] setting1')
.with_path('/foo/bar')
.with_setting('setting1')
.with_value('value1')
}

it { is_expected.to contain_ini_setting('present /foo/bar [global] setting2')
.with_path('/foo/bar')
.with_setting('setting2')
.with_value('value2') }
end
end
end
114 changes: 114 additions & 0 deletions spec/defines/resource_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
require 'spec_helper'

describe('icingaweb2::config::resource', :type => :define) do
let(:title) { 'myresource' }
let(:pre_condition) { [
"class { 'icingaweb2': }"
] }

on_supported_os.each do |os, facts|
let :facts do
facts
end

context "#{os} with type db" do
let(:params) { {
:type => 'db',
:host => 'localhost',
:port => '3306',
:db_type => 'mysql',
:db_name => 'foo',
:db_username => 'bar',
:db_password => 'secret' } }

it { is_expected.to contain_file('/etc/icingaweb2/resources.ini') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] type')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('type')
.with_value('db') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] host')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('host')
.with_value('localhost') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] port')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('port')
.with_value('3306') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] db')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('db')
.with_value('mysql') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] dbname')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('dbname')
.with_value('foo') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] username')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('username')
.with_value('bar') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] password')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('password')
.with_value('secret') }
end

context "#{os} with type ldap" do
let(:params) { {
:type => 'ldap',
:host => 'localhost',
:port => '389',
:ldap_root_dn => 'cn=foo,dc=bar',
:ldap_bind_dn => 'cn=root,dc=bar',
:ldap_bind_pw => 'secret' } }

it { is_expected.to contain_file('/etc/icingaweb2/resources.ini') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] type')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('type')
.with_value('ldap') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] hostname')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('hostname')
.with_value('localhost') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] port')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('port')
.with_value('389') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] root_dn')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('root_dn')
.with_value('cn=foo,dc=bar') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] bind_dn')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('bind_dn')
.with_value('cn=root,dc=bar') }

it { is_expected.to contain_ini_setting('present /etc/icingaweb2/resources.ini [myresource] bind_pw')
.with_path('/etc/icingaweb2/resources.ini')
.with_setting('bind_pw')
.with_value('secret') }

end

context "#{os} with invalid type" do
let(:params) { {
:type => 'foobar',
:host => 'localhost',
:port => '3306' } }

it { is_expected.to raise_error(Puppet::Error, /foobar isn't supported/) }
end
end
end

0 comments on commit 26932f5

Please sign in to comment.