From 54a798f0723dfee3a7fa6a7faeea0984b46a9f42 Mon Sep 17 00:00:00 2001 From: Lennart Betz Date: Fri, 22 Dec 2023 09:02:16 +0100 Subject: [PATCH] Replace icinga2::db::connect function with icinga::db::connect from module icinga --- REFERENCE.md | 51 -------------------- functions/db/connect.pp | 89 ----------------------------------- manifests/feature/idomysql.pp | 2 +- manifests/feature/idopgsql.pp | 2 +- spec/classes/idomysql_spec.rb | 6 +-- spec/classes/idopgsql_spec.rb | 2 +- 6 files changed, 6 insertions(+), 146 deletions(-) delete mode 100644 functions/db/connect.pp diff --git a/REFERENCE.md b/REFERENCE.md index 251b12a9..7673b9b2 100644 --- a/REFERENCE.md +++ b/REFERENCE.md @@ -75,8 +75,6 @@ start on boot and will be restarted if stopped. ### Functions * [`icinga2::cert`](#icinga2--cert): Choose the path of tls key, cert and ca file. -* [`icinga2::db::connect`](#icinga2--db--connect): This function returns a string to connect databases -with or without TLS information. * [`icinga2::icinga2_attributes`](#icinga2--icinga2_attributes): Calls the simple parser to decide what to quote. For more information, see lib/puppet_x/icinga2/utils.rb. * [`icinga2::icinga2_ticket_id`](#icinga2--icinga2_ticket_id): Summarise what the function does here @@ -5587,55 +5585,6 @@ Data type: `Optional[String]` -### `icinga2::db::connect` - -Type: Puppet Language - -This function returns a string to connect databases -with or without TLS information. - -#### `icinga2::db::connect(Struct[{ - type => Enum['pgsql','mysql','mariadb'], - host => Stdlib::Host, - port => Optional[Stdlib::Port], - database => String, - username => String, - password => Optional[Variant[String, Sensitive[String]]], - }] $db, Hash[String, Any] $tls, Optional[Boolean] $use_tls = undef)` - -The icinga2::db::connect function. - -Returns: `String` Connection string to connect database. - -##### `db` - -Data type: - -```puppet -Struct[{ - type => Enum['pgsql','mysql','mariadb'], - host => Stdlib::Host, - port => Optional[Stdlib::Port], - database => String, - username => String, - password => Optional[Variant[String, Sensitive[String]]], - }] -``` - - - -##### `tls` - -Data type: `Hash[String, Any]` - - - -##### `use_tls` - -Data type: `Optional[Boolean]` - - - ### `icinga2::icinga2_attributes` Type: Ruby 4.x API diff --git a/functions/db/connect.pp b/functions/db/connect.pp deleted file mode 100644 index 9be99007..00000000 --- a/functions/db/connect.pp +++ /dev/null @@ -1,89 +0,0 @@ -# @summary -# This function returns a string to connect databases -# with or without TLS information. -# -# @return -# Connection string to connect database. -# -function icinga2::db::connect( - Struct[{ - type => Enum['pgsql','mysql','mariadb'], - host => Stdlib::Host, - port => Optional[Stdlib::Port], - database => String, - username => String, - password => Optional[Variant[String, Sensitive[String]]], - }] $db, - Hash[String, Any] $tls, - Optional[Boolean] $use_tls = undef, -) >> String { - # @param db - # Data hash with database information. - # - # @param tls - # Data hash with TLS connection information. - # - # @param use_tls - # Wether or not to use TLS encryption. - # - if $use_tls { - case $db['type'] { - 'pgsql': { - $tls_options = regsubst(join(any2array(delete_undef_values({ - 'sslmode=' => if $tls['noverify'] { 'require' } else { 'verify-full' }, - 'sslcert=' => $tls['cert_file'], - 'sslkey=' => $tls['key_file'], - 'sslrootcert=' => $tls['cacert_file'], - })), ' '), '= ', '=', 'G') - } - 'mariadb': { - $tls_options = join(any2array(delete_undef_values({ - '--ssl' => '', - '--ssl-ca' => $tls['cacert_file'], - '--ssl-cert' => $tls['cert_file'], - '--ssl-key' => $tls['key_file'], - '--ssl-capath' => $tls['capath'], - '--ssl-cipher' => $tls['cipher'], - })), ' ') - } - 'mysql': { - $tls_options = join(any2array(delete_undef_values({ - '--ssl-mode' => 'required', - '--ssl-ca' => $tls['cacert_file'], - '--ssl-cert' => $tls['cert_file'], - '--ssl-key' => $tls['key_file'], - '--ssl-capath' => $tls['capath'], - '--ssl-cipher' => $tls['cipher'], - })), ' ') - } - default: { - fail('The database type you provided is not supported.') - } - } - } else { - $tls_options = '' - } - - if $db['type'] == 'pgsql' { - $options = regsubst(join(any2array(delete_undef_values({ - 'host=' => $db['host'], - 'user=' => $db['username'], - 'port=' => $db['port'], - 'dbname=' => $db['database'], - })), ' '), '= ', '=', 'G') - } else { - $_password = unwrap($db['password']) - $options = join(any2array(delete_undef_values({ - '-h' => $db['host'] ? { - /localhost/ => undef, - default => $db['host'], - }, - '-P' => $db['port'], - '-u' => $db['username'], - "-p'${_password}'" => '', - '-D' => $db['database'], - })), ' ') - } - - "${options} ${tls_options}" -} diff --git a/manifests/feature/idomysql.pp b/manifests/feature/idomysql.pp index d85ab303..116a6a54 100644 --- a/manifests/feature/idomysql.pp +++ b/manifests/feature/idomysql.pp @@ -225,7 +225,7 @@ Package[$ido_mysql_package_name] -> Exec['idomysql-import-schema'] } - $db_cli_options = icinga2::db::connect({ + $db_cli_options = icinga::db::connect({ 'type' => $type, 'host' => $host, 'port' => $port, diff --git a/manifests/feature/idopgsql.pp b/manifests/feature/idopgsql.pp index 7c3041ac..7fd1723f 100644 --- a/manifests/feature/idopgsql.pp +++ b/manifests/feature/idopgsql.pp @@ -206,7 +206,7 @@ Package[$ido_pgsql_package_name] -> Exec['idopgsql-import-schema'] } - $db_cli_options = icinga2::db::connect({ + $db_cli_options = icinga::db::connect({ 'type' => 'pgsql', 'host' => $host, 'port' => $port, diff --git a/spec/classes/idomysql_spec.rb b/spec/classes/idomysql_spec.rb index 2009cbc8..616f136e 100644 --- a/spec/classes/idomysql_spec.rb +++ b/spec/classes/idomysql_spec.rb @@ -115,7 +115,7 @@ is_expected.to contain_exec('idomysql-import-schema').with( { user: 'root', - command: "mysql -u icinga -p'foo' -D icinga < \"#{ido_mysql_schema_dir}/mysql.sql\"", + command: "mysql -u icinga -p'foo' -D icinga < \"#{ido_mysql_schema_dir}/mysql.sql\"", }, ) } @@ -186,7 +186,7 @@ is_expected.to contain_exec('idomysql-import-schema').with( { 'user' => 'root', - 'command' => "mysql -h 127.0.0.1 -P 3306 -u icinga -p'foo' -D icinga --ssl --ssl-ca #{icinga2_pki_dir}/IdoMysqlConnection_ido-mysql_ca.crt" \ + 'command' => "mysql -h 127.0.0.1 -P 3306 -u icinga -p'foo' -D icinga --ssl --ssl-ca #{icinga2_pki_dir}/IdoMysqlConnection_ido-mysql_ca.crt" \ " --ssl-cert #{icinga2_pki_dir}/IdoMysqlConnection_ido-mysql.crt --ssl-key #{icinga2_pki_dir}/IdoMysqlConnection_ido-mysql.key < \"#{ido_mysql_schema_dir}/mysql.sql\"", }, ) @@ -227,7 +227,7 @@ is_expected.to contain_exec('idomysql-import-schema').with( { 'user' => 'root', - 'command' => "mysql -u icinga -p'foo' -D icinga --ssl --ssl-ca #{icinga2_pki_dir}/IdoMysqlConnection_ido-mysql_ca.crt" \ + 'command' => "mysql -u icinga -p'foo' -D icinga --ssl --ssl-ca #{icinga2_pki_dir}/IdoMysqlConnection_ido-mysql_ca.crt" \ " --ssl-cert #{icinga2_pki_dir}/IdoMysqlConnection_ido-mysql.crt --ssl-key #{icinga2_pki_dir}/IdoMysqlConnection_ido-mysql.key < \"#{ido_mysql_schema_dir}/mysql.sql\"", }, ) diff --git a/spec/classes/idopgsql_spec.rb b/spec/classes/idopgsql_spec.rb index e7b822b7..7096422e 100644 --- a/spec/classes/idopgsql_spec.rb +++ b/spec/classes/idopgsql_spec.rb @@ -99,7 +99,7 @@ { 'user' => 'root', 'environment' => ['PGPASSWORD=foo'], - 'command' => "psql 'host=localhost user=icinga dbname=icinga ' -w -f '#{ido_pgsql_schema_dir}/pgsql.sql'", + 'command' => "psql 'host=localhost user=icinga dbname=icinga' -w -f '#{ido_pgsql_schema_dir}/pgsql.sql'", }, ) }