diff --git a/manifests/db.pp b/manifests/db.pp index 350252c..46b1c05 100644 --- a/manifests/db.pp +++ b/manifests/db.pp @@ -1,83 +1,82 @@ # Define: mysql::db # # This module creates database instances, a user, and grants that user # privileges to the database. It can also import SQL from a file in order to, # for example, initialize a database schema. # # Since it requires class mysql::server, we assume to run all commands as the # root mysql user against the local mysql server. # # Parameters: # [*title*] - mysql database name. # [*user*] - username to create and grant access. # [*password*] - user's password. # [*charset*] - database charset. # [*host*] - host for assigning privileges to user. # [*grant*] - array of privileges to grant user. # [*enforce_sql*] - whether to enforce or conditionally run sql on creation. # [*sql*] - sql statement to run. # [*ensure*] - specifies if a database is present or absent. # # Actions: # # Requires: # # class mysql::server # # Sample Usage: # # mysql::db { 'mydb': # user => 'my_user', # password => 'password', # host => $::hostname, # grant => ['all'] # } # define mysql::db ( $user, $password, $charset = 'utf8', $host = 'localhost', $grant = 'all', $sql = '', $enforce_sql = false, $ensure = 'present' ) { validate_re($ensure, '^(present|absent)$', "${ensure} is not supported for ensure. Allowed values are 'present' and 'absent'.") database { $name: ensure => $ensure, charset => $charset, provider => 'mysql', require => Class['mysql::server'], + before => Database_user["${user}@${host}"], } - database_user { "${user}@${host}": - ensure => $ensure, - password_hash => mysql_password($password), - provider => 'mysql', - require => Database[$name], - } + ensure_resource('database_user', "${user}@${host}", { ensure => $ensure, + password_hash => mysql_password($password), + provider => 'mysql' + }) if $ensure == 'present' { database_grant { "${user}@${host}/${name}": privileges => $grant, provider => 'mysql', require => Database_user["${user}@${host}"], } $refresh = ! $enforce_sql if $sql { exec{ "${name}-import": command => "/usr/bin/mysql ${name} < ${sql}", logoutput => true, refreshonly => $refresh, require => Database_grant["${user}@${host}/${name}"], subscribe => Database[$name], } } } } diff --git a/spec/unit/mysql_password_spec.rb b/spec/unit/mysql_password_spec.rb index 4e67ed9..350d766 100644 --- a/spec/unit/mysql_password_spec.rb +++ b/spec/unit/mysql_password_spec.rb @@ -1,30 +1,34 @@ #!/usr/bin/env rspec require 'spec_helper' describe "the mysql_password function" do before :all do Puppet::Parser::Functions.autoloader.loadall end before :each do - @scope = Puppet::Parser::Scope.new + @scope = if Puppet.version =~ /^3/ + Puppet::Parser::Scope.new_for_test_harness('localhost') + else + Puppet::Parser::Scope.new + end end it "should exist" do Puppet::Parser::Functions.function("mysql_password").should == "function_mysql_password" end it "should raise a ParseError if there is less than 1 arguments" do lambda { @scope.function_mysql_password([]) }.should( raise_error(Puppet::ParseError)) end it "should raise a ParseError if there is more than 1 arguments" do lambda { @scope.function_mysql_password(['foo', 'bar']) }.should( raise_error(Puppet::ParseError)) end it "should convert password into a hash" do result = @scope.function_mysql_password(["password"]) result.should(eq('*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19')) end end