From b0665e8a42f58b680d5c6f82bf368ba25bbd6b7a Mon Sep 17 00:00:00 2001 From: Alexey Deryugin Date: Fri, 28 Aug 2015 14:40:33 +0300 Subject: [PATCH] MySQL and PostreSQL class implementation Change-Id: Ib7decde6d03047d91bc50e9441c741ba132642ae --- manifests/db/mysql.pp | 58 +++++++++++++++ manifests/db/postgresql.pp | 47 ++++++++++++ spec/classes/murano_db_mysql_spec.rb | 89 +++++++++++++++++++++++ spec/classes/murano_db_postgresql_spec.rb | 58 +++++++++++++++ 4 files changed, 252 insertions(+) create mode 100644 manifests/db/mysql.pp create mode 100644 manifests/db/postgresql.pp create mode 100644 spec/classes/murano_db_mysql_spec.rb create mode 100644 spec/classes/murano_db_postgresql_spec.rb diff --git a/manifests/db/mysql.pp b/manifests/db/mysql.pp new file mode 100644 index 0000000..4f78c34 --- /dev/null +++ b/manifests/db/mysql.pp @@ -0,0 +1,58 @@ +# == Class: murano::db::mysql +# +# The murano::db::mysql class creates a MySQL database for murano. +# It must be used on the MySQL server. +# +# === Parameters +# +# [*password*] +# (Required) Password to connect to the database. +# +# [*dbname*] +# (Optional) Name of the database. +# Defaults to 'murano'. +# +# [*user*] +# (Optional) User to connect to the database. +# Defaults to 'murano'. +# +# [*host*] +# (Optional) The default source host user is allowed to connect from. +# Defaults to '127.0.0.1' +# +# [*allowed_hosts*] +# (Optional) Other hosts the user is allowed to connect from. +# Defaults to 'undef'. +# +# [*charset*] +# (Optional) The database charset. +# Defaults to 'utf8'. +# +# [*collate*] +# (Optional) Charset collate of murano database. +# Defaults to 'utf8_general_ci'. +# +class murano::db::mysql( + $password, + $dbname = 'murano', + $user = 'murano', + $host = '127.0.0.1', + $allowed_hosts = undef, + $charset = 'utf8', + $collate = 'utf8_general_ci', +) { + + validate_string($password) + + ::openstacklib::db::mysql{ 'murano': + user => $user, + password_hash => mysql_password($password), + dbname => $dbname, + host => $host, + charset => $charset, + collate => $collate, + allowed_hosts => $allowed_hosts, + } + + ::Openstacklib::Db::Mysql['murano'] ~> Exec<| title == 'murano-dbmanage' |> +} diff --git a/manifests/db/postgresql.pp b/manifests/db/postgresql.pp new file mode 100644 index 0000000..52bb5ce --- /dev/null +++ b/manifests/db/postgresql.pp @@ -0,0 +1,47 @@ +# == Class: murano::db::postgresql +# +# The murano::db::postgresql creates a PostgreSQL database for murano. +# It must be used on the PostgreSQL server. +# +# === Parameters +# +# [*password*] +# (Required) Password to connect to the database. +# +# [*dbname*] +# (Optional) Name of the database. +# Defaults to 'murano'. +# +# [*user*] +# (Optional) User to connect to the database. +# Defaults to 'murano'. +# +# [*encoding*] +# (Optional) The charset to use for the database. +# Default to undef. +# +# [*privileges*] +# (Optional) Privileges given to the database user. +# Default to 'ALL' +# +class murano::db::postgresql( + $password, + $dbname = 'murano', + $user = 'murano', + $encoding = undef, + $privileges = 'ALL', +) { + + validate_string($password) + + ::openstacklib::db::postgresql { 'murano': + password_hash => postgresql_password($user, $password), + dbname => $dbname, + user => $user, + encoding => $encoding, + privileges => $privileges, + } + + ::Openstacklib::Db::Postgresql['murano'] ~> Exec<| title == 'murano-dbmanage' |> + +} diff --git a/spec/classes/murano_db_mysql_spec.rb b/spec/classes/murano_db_mysql_spec.rb new file mode 100644 index 0000000..2becde5 --- /dev/null +++ b/spec/classes/murano_db_mysql_spec.rb @@ -0,0 +1,89 @@ +require 'spec_helper' + +describe 'murano::db::mysql' do + + let :pre_condition do + ['include mysql::server'] + end + + let :params do + { :dbname => 'murano', + :password => 's3cr3t', + :user => 'murano', + :charset => 'utf8', + :collate => 'utf8_general_ci', + :host => '127.0.0.1', + } + end + + shared_examples_for 'murano mysql database' do + + context 'when omiting the required parameter password' do + before { params.delete(:password) } + it { expect { is_expected.to raise_error(Puppet::Error) } } + end + + it 'creates a mysql database' do + is_expected.to contain_openstacklib__db__mysql('murano').with( + :user => params[:user], + :dbname => params[:dbname], + :password_hash => '*58C036CDA51D8E8BBBBF2F9EA5ABF111ADA444F0', + :host => params[:host], + :charset => params[:charset] + ) + end + + context 'overriding allowed_hosts param to array' do + before :each do + params.merge!( + :allowed_hosts => ['127.0.0.1','%'] + ) + end + + it { + is_expected.to contain_openstacklib__db__mysql('murano').with( + :user => params[:user], + :dbname => params[:dbname], + :password_hash => '*58C036CDA51D8E8BBBBF2F9EA5ABF111ADA444F0', + :host => params[:host], + :charset => params[:charset], + :allowed_hosts => ['127.0.0.1','%'] + )} + end + + context 'overriding allowed_hosts param to string' do + before :each do + params.merge!( + :allowed_hosts => '192.168.1.1' + ) + end + + it { + is_expected.to contain_openstacklib__db__mysql('murano').with( + :user => params[:user], + :dbname => params[:dbname], + :password_hash => '*58C036CDA51D8E8BBBBF2F9EA5ABF111ADA444F0', + :host => params[:host], + :charset => params[:charset], + :allowed_hosts => '192.168.1.1' + )} + end + + end + + context 'on Debian platforms' do + let :facts do + { :osfamily => 'Debian' } + end + + it_configures 'murano mysql database' + end + + context 'on RedHat platforms' do + let :facts do + { :osfamily => 'RedHat' } + end + + it_configures 'murano mysql database' + end +end diff --git a/spec/classes/murano_db_postgresql_spec.rb b/spec/classes/murano_db_postgresql_spec.rb new file mode 100644 index 0000000..6a422b0 --- /dev/null +++ b/spec/classes/murano_db_postgresql_spec.rb @@ -0,0 +1,58 @@ +require 'spec_helper' + +describe 'murano::db::postgresql' do + + let :req_params do + { :password => 'pw' } + end + + let :pre_condition do + 'include postgresql::server' + end + + context 'on a RedHat osfamily' do + let :facts do + { + :osfamily => 'RedHat', + :operatingsystemrelease => '7.0', + :concat_basedir => '/var/lib/puppet/concat' + } + end + + context 'with only required parameters' do + let :params do + req_params + end + + it { is_expected.to contain_postgresql__server__db('murano').with( + :user => 'murano', + :password => 'md5c73cd23dae0b015e51ff2b38714d0bab' + )} + end + + end + + context 'on a Debian osfamily' do + let :facts do + { + :operatingsystemrelease => '7.8', + :operatingsystem => 'Debian', + :osfamily => 'Debian', + :concat_basedir => '/var/lib/puppet/concat' + } + end + + context 'with only required parameters' do + let :params do + req_params + end + + it { is_expected.to contain_postgresql__server__db('murano').with( + :user => 'murano', + :password => 'md5c73cd23dae0b015e51ff2b38714d0bab' + )} + end + + end + +end