Permalink
Browse files

Parse Mongo URL

Change-Id: I29082f4934b875a44da9842fbacf408a6ca64ef6
  • Loading branch information...
1 parent 34fd211 commit 981f33bb395735643daf7c3bdbe43d992204cdbd Jennifer Hickey committed Sep 20, 2012
@@ -2,17 +2,30 @@ module CFRuntime
class MongodbParser
def self.parse(svc)
serviceopts = {}
- { :username => :username,
- :password => :password,
- :hostname => :host,
- :port => :port,
- :db => :db
- }.each do |from, to|
- serviceopts[to] = svc["credentials"][from.to_s]
+ cred = svc["credentials"]
+ if cred["url"]
+ uri=URI.parse(cred["url"])
+ user=URI.unescape(uri.user) if uri.user
+ passwd=URI.unescape(uri.password) if uri.password
+ host=uri.host
+ port=uri.port
+ if uri.path =~ %r{^/(.*)}
+ raise ArgumentError.new("multiple segments in path of mongo URI: #{uri}") if $1.index('/')
+ db = URI.unescape($1)
+ end
+ url = cred["url"]
+ else
+ # The old credentials format with no URL
+ user,passwd,host,port, db = %w(username password hostname port db).map {|key|
+ cred[key]}
+ url = "mongodb://#{user}:#{passwd}@#{host}:#{port}/#{db}"
end
- serviceopts[:url] = svc["credentials"]["url"] ||
- "mongodb://#{serviceopts[:username]}:#{serviceopts[:password]}@" +
- "#{serviceopts[:host]}:#{serviceopts[:port]}/#{serviceopts[:db]}"
+ serviceopts[:username] = user
+ serviceopts[:password] = passwd
+ serviceopts[:host] = host
+ serviceopts[:port] = port
+ serviceopts[:url] = url
+ serviceopts[:db] = db
serviceopts
end
end
@@ -2,7 +2,7 @@
require 'cfruntime/properties'
describe 'CFRuntime::MongoParser' do
- it 'parses a mongo service' do
+ it 'parses a mongo service without a URL (old format)' do
svcs = {
"mongodb-#{mongo_version}" => [create_mongo_service('mongo-test')]
}
@@ -20,4 +20,23 @@
CFRuntime::CloudApp.service_props('mongodb').should == expected
end
end
+
+ it 'parses a mongo service with a URL (new format)' do
+ svcs = {
+ "mongodb-#{mongo_version}" => [create_mongo_service('mongo-test', true)]
+ }
+ with_vcap_services(svcs) do
+ expected = { :label => "mongodb",
+ :version => "#{mongo_version}",
+ :name => "mongo-test",
+ :username => "testuser",
+ :password => "testpw",
+ :host => SOME_SERVER,
+ :port => SOME_SERVICE_PORT,
+ :db => "db",
+ :url => "mongodb://testuser:testpw@#{SOME_SERVER}:#{SOME_SERVICE_PORT}/db"
+ }
+ CFRuntime::CloudApp.service_props('mongodb').should == expected
+ end
+ end
end
@@ -61,20 +61,26 @@ def postgres_version
"0.11.0"
end
- def create_mongo_service(name)
- create_service(name, "mongodb", mongo_version,"db")
+ def create_mongo_service(name, url=false)
+ svc = "{\"name\":\"#{name}\",\"label\":\"mongodb-#{mongo_version}\",\"plan\":\"free\"," +
+ "\"tags\":[\"mongodb\",\"mongodb-#{mongo_version}\"],\"credentials\":{\"hostname\":\"#{SOME_SERVER}\"," +
+ "\"host\":\"#{SOME_SERVER}\",\"port\":#{SOME_SERVICE_PORT},\"username\":\"testuser\",\"password\":\"testpw\"," +
+ "\"db\":\"db\""
+ svc = svc + ", \"url\":\"mongodb://testuser:testpw@#{SOME_SERVER}:#{SOME_SERVICE_PORT}/db\"" if url
+ svc = svc + "}}"
+ svc
end
def create_redis_service(name)
- create_service(name, "redis", redis_version, nil, "redisdata")
+ create_service(name, "redis", redis_version, "redisdata")
end
def create_mysql_service(name)
- create_service(name, "mysql", mysql_version,nil,"mysqldatabase")
+ create_service(name, "mysql", mysql_version,"mysqldatabase")
end
def create_postgres_service(name)
- create_service(name, "postgresql", postgres_version,nil,"pgdatabase")
+ create_service(name, "postgresql", postgres_version,"pgdatabase")
end
def create_rabbit_service(name, vhost=nil)
@@ -98,14 +104,10 @@ def create_rabbit_srs_service(name, vhost=nil)
"\"credentials\":{\"url\":\"#{url}\"}}"
end
- def create_service(name, type, version, db=nil, cred_name=nil)
+ def create_service(name, type, version, cred_name)
svc = "{\"name\":\"#{name}\",\"label\":\"#{type}-#{version}\",\"plan\":\"free\"," +
"\"tags\":[\"#{type}\",\"#{type}-#{version}\"],\"credentials\":{\"hostname\":\"#{SOME_SERVER}\"," +
"\"host\":\"#{SOME_SERVER}\",\"port\":#{SOME_SERVICE_PORT},\"username\":\"testuser\",\"password\":\"testpw\"," +
- "\"name\":\"#{cred_name}\""
- if db
- svc = svc + ", \"db\":\"#{db}\""
- end
- svc = svc + '}}'
+ "\"name\":\"#{cred_name}\"}}"
end
end

0 comments on commit 981f33b

Please sign in to comment.