Permalink
Browse files

Changing the MongoDB connection creation to use Mongo::Connection.fro…

…m_uri method and wrapping the connection in a proxy with a no-arg db method

Change-Id: I7f7ea8e0aeb5bc1f5b567907f207500461a67c0a
  • Loading branch information...
1 parent 3e92935 commit 57a80e501d47d59f63d0671d760da72b4e3efafe @trisberg trisberg committed Jan 23, 2012
@@ -97,8 +97,7 @@ def load_mysql
end
def load_mongo
- conn = CFRuntime::MongoClient.create_from_svc('test-cfruntime-svc-test-mongo')
- db = CFRuntime::MongoClient.db_from_svc('test-cfruntime-svc-test-mongo',conn)
+ db = CFRuntime::MongoClient.create_from_svc('test-cfruntime-svc-test-mongo').db
coll = db['data_values']
end
@@ -97,8 +97,7 @@ def load_mysql
end
def load_mongo
- conn = CFRuntime::MongoClient.create
- db = CFRuntime::MongoClient.db(conn)
+ db = CFRuntime::MongoClient.create.db
coll = db['data_values']
end
@@ -122,4 +121,4 @@ def read_from_rabbit(key, client)
msg = q.pop(:ack => true)
q.ack
msg
-end
+end
@@ -5,6 +5,8 @@ class MongoClient
# Creates and returns a Mongo +Connection+ to a single mongodb service.
# Passes optional Hash of non-connection-related options to +Mongo::Connection.new+.
+ # The connection is wrapped in a proxy that adds a no-argument db method to gain access
+ # to the database created by CloudFoundry without having to specify the name.
# Raises +ArgumentError+ If zero or multiple mongodb services are found.
def self.create(options={})
service_names = CloudApp.service_names_of_type('mongodb')
@@ -19,40 +21,66 @@ def self.create(options={})
# Creates and returns a Mongo +Connection+ to a mongodb service with the
# specified name.
# Passes optional Hash of non-connection-related options to +Mongo::Connection.new+.
+ # The connection is wrapped in a proxy that adds a no-argument db method to gain access
+ # to the database created by CloudFoundry without having to specify the name.
# Raises +ArgumentError+ If specified mongodb service is not found.
def self.create_from_svc(service_name,options={})
service_props = CFRuntime::CloudApp.service_props(service_name)
if service_props.nil?
raise ArgumentError.new("Service with name #{service_name} not found")
end
- Mongo::Connection.new(service_props[:host], service_props[:port],options)
+ uri = "mongodb://#{service_props[:username]}:#{service_props[:password]}@#{service_props[:host]}:#{service_props[:port]}/#{service_props[:db]}"
+ conn = Mongo::Connection.from_uri(uri, options)
+ MongoConnection.new(conn, service_props[:db])
end
- # Creates and returns an authenticated Mongo +DB+ instance connected to a single mongodb service
+ # Returns the db_name for a single mongodb service.
# Raises +ArgumentError+ If zero or multiple mongodb services are found.
- def self.db(connection)
+ def self.db_name()
service_names = CloudApp.service_names_of_type('mongodb')
if service_names.length != 1
raise ArgumentError.new("Expected 1 service of mongodb type, " +
"but found #{service_names.length}. " +
- "Consider using db_from_svc(service_name) instead.")
+ "Consider using db_name_from_svc(service_name) instead.")
end
- db_from_svc(service_names[0],connection)
+ db_name_from_svc(service_names[0])
end
- # Creates and returns an authenticated Mongo +DB+ instance connected to a mongodb service with the
- # specified name.
+ # Returns the db_name for the mongodb service with the specified name.
# Raises +ArgumentError+ If specified mongodb service is not found.
- def self.db_from_svc(service_name,connection)
+ def self.db_name_from_svc(service_name)
service_props = CFRuntime::CloudApp.service_props(service_name)
if service_props.nil?
raise ArgumentError.new("Service with name #{service_name} not found")
end
- db = connection.db(service_props[:db])
- db.authenticate(service_props[:username],service_props[:password])
- db
+ service_props[:db]
+ end
+
+ end
+
+ class MongoConnection
+
+ instance_methods.each { |m| undef_method m unless m =~ /^__|instance_eval|object_id/ }
+
+ def initialize(connection, db_name)
+ @target = connection
+ @dbname = db_name
end
+ def db(db_name=@dbname, opts={})
+ @target.send('db', db_name, opts)
+ end
+
+ def target()
+ @target
+ end
+
+ protected
+
+ def method_missing(method, *args, &block)
+ @target.send(method, *args, &block)
+ end
end
+
end
@@ -18,8 +18,7 @@ def authenticate(username,password)
it 'creates a client with a Mongo service by type and additional options' do
svcs = {"mongodb-#{mongo_version}"=>[create_mongo_service('mongo-test')]}
with_vcap_services(svcs) do
- conn = CFRuntime::MongoClient.create(:connect=>false)
- db = CFRuntime::MongoClient.db(conn)
+ db = CFRuntime::MongoClient.create(:connect=>false).db
db.name.should == "db"
db.connection.host_to_try.should == [SOME_SERVER,SOME_SERVICE_PORT]
end
@@ -28,8 +27,29 @@ def authenticate(username,password)
it 'creates a client with a Mongo service by name and additional options' do
svcs = {"mongodb-#{mongo_version}"=>[create_mongo_service('mongo-test')]}
with_vcap_services(svcs) do
- conn = CFRuntime::MongoClient.create_from_svc('mongo-test',:connect=>false)
- db = CFRuntime::MongoClient.db_from_svc('mongo-test',conn)
+ db = CFRuntime::MongoClient.create_from_svc('mongo-test',:connect=>false).db
+ db.name.should == "db"
+ db.connection.host_to_try.should == [SOME_SERVER,SOME_SERVICE_PORT]
+ end
+ end
+
+ it 'creates a client with a Mongo service by type and gives access to native connection object and db_name' do
+ svcs = {"mongodb-#{mongo_version}"=>[create_mongo_service('mongo-test')]}
+ with_vcap_services(svcs) do
+ conn = CFRuntime::MongoClient.create(:connect=>false).target
+ db_name = CFRuntime::MongoClient.db_name()
+ db = conn.db(db_name)
+ db.name.should == "db"
+ db.connection.host_to_try.should == [SOME_SERVER,SOME_SERVICE_PORT]
+ end
+ end
+
+ it 'creates a client with a Mongo service by name and gives access to native connection object and db_name' do
+ svcs = {"mongodb-#{mongo_version}"=>[create_mongo_service('mongo-test')]}
+ with_vcap_services(svcs) do
+ conn = CFRuntime::MongoClient.create_from_svc('mongo-test',:connect=>false).target
+ db_name = CFRuntime::MongoClient.db_name_from_svc('mongo-test')
+ db = conn.db(db_name)
db.name.should == "db"
db.connection.host_to_try.should == [SOME_SERVER,SOME_SERVICE_PORT]
end
@@ -57,22 +77,22 @@ def authenticate(username,password)
end
it 'raises an ArgumentError on db if no service of Mongo type found' do
ENV['VCAP_SERVICES'] = nil
- expect{CFRuntime::MongoClient.db(mock("connection"))}.to raise_error(ArgumentError,
- 'Expected 1 service of mongodb type, but found 0. Consider using db_from_svc(service_name) instead.')
+ expect{CFRuntime::MongoClient.db_name}.to raise_error(ArgumentError,
+ 'Expected 1 service of mongodb type, but found 0. Consider using db_name_from_svc(service_name) instead.')
end
it 'raises an ArgumentError on db if multiple services of Mongo type found' do
svcs = {"mongodb-#{mongo_version}"=>[create_mongo_service('mongo-test'),
create_mongo_service('mongo-test2')]}
with_vcap_services(svcs) do
- expect{CFRuntime::MongoClient.db(mock("connection"))}.to raise_error(ArgumentError,
- 'Expected 1 service of mongodb type, but found 2. Consider using db_from_svc(service_name) instead.')
+ expect{CFRuntime::MongoClient.db_name}.to raise_error(ArgumentError,
+ 'Expected 1 service of mongodb type, but found 2. Consider using db_name_from_svc(service_name) instead.')
end
end
it 'raises an ArgumentError on db if Mongo service of specified name is not found' do
ENV['VCAP_SERVICES'] = nil
- expect{CFRuntime::MongoClient.db_from_svc('non-existent-mongo',mock("connection"))}.to raise_error(ArgumentError,
+ expect{CFRuntime::MongoClient.db_name_from_svc('non-existent-mongo')}.to raise_error(ArgumentError,
'Service with name non-existent-mongo not found')
end
end

0 comments on commit 57a80e5

Please sign in to comment.