Merge pull request #4 from jaypipes/working

Adds db_create_with_user library routine
This commit is contained in:
John Dewey
2012-11-16 10:49:12 -08:00
3 changed files with 81 additions and 0 deletions

View File

@@ -79,4 +79,61 @@ module Openstack
end
end
end
# Library routine that uses the database cookbook to create the
# service's database and grant read/write access to the
# given user and password. A privileged "super user" and password
# is supplied, which is needed to create the database and user
# records. Returns the db info from the db() library call.
def db_create_with_user(service, super_user, super_pass, user, pass)
info = db(service)
if info
host = info['host']
port = info['port'].to_s
type = info['db_type']
db_name = info['db_name']
connection_info = {
'host' => host,
'port' => port,
'username' => super_user,
'password' => super_pass
}
prov = case type
when 'postgresql', 'pgsql'
Chef::Provider::Database::Postgresql
when 'mysql'
Chef::Provider::Database::Mysql
else
Chef::Log.error("Unsupported database type #{type}")
end
# create database
database "create #{db_name} database" do
provider prov
connection connection_info
database_name db_name
action :create
end
# create user
database_user username do
provider prov
connection connection_info
password pass
action :create
end
# grant privs to user
database_user username do
provider prov
connection connection_info
password pass
database_name db_name
host '%'
privileges [:all]
action :grant
end
end
info
end
end

View File

@@ -1,7 +1,9 @@
name "openstack-common"
maintainer "Jay Pipes"
maintainer_email "jaypipes@gmail.com"
license "Apache 2.0"
description "Common OpenStack attributes, libraries and recipes."
long_description IO.read(File.join(File.dirname(__FILE__), 'README.md'))
version "0.0.1"
depends "database"
depends "openstack-utils"

View File

@@ -129,4 +129,26 @@ describe ::Openstack do
@subject.db_uri("compute", "user", "pass").should eq expect
end
end
describe "#db_create_with_user" do
it "returns nil when no openstack.db not in node attrs" do
@subject.instance_variable_set(:@node, {})
@subject.db_create_with_user("nonexisting", "superuser", "superpass", "user", "pass").should be_nil
end
it "returns nil when no such service was found" do
@subject.instance_variable_set(:@node, @chef_run.node)
@subject.db_create_with_user("nonexisting", "superuser", "superpass", "user", "pass").should be_nil
end
it "returns db info and creates database with user when service found" do
stub_const("Chef::Provider::Database::Mysql", nil)
Chef::Recipe.any_instance.stub(:database).and_return(Hash.new)
@subject.instance_variable_set(:@node, @chef_run.node)
expect = {
'host' => '127.0.0.1',
'port' => 3306
}
result = @subject.db_create_with_user("compute", "superuser", "superpass", "user", "pass")
result.should eq expect
end
end
end