Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Add rabbitmq support to cf-runtime

  • Loading branch information...
commit a87244bd6b8b5c33d98bb7471890d6ee4869a646 1 parent d646c64
Jennifer Hickey authored
View
75 cfruntime/lib/cfruntime/properties.rb
@@ -1,19 +1,20 @@
# Copyright (c) 2009-2011 VMware, Inc.
module CFRuntime
-
- require 'json/pure'
+
+ require 'json/pure'
+ require 'uri'
class CloudApp
-
+
@@app = ENV['VCAP_APPLICATION']
@@host = ENV['VCAP_APP_HOST']
@@port = ENV['VCAP_APP_PORT']
@@svcs = {}
@@svc_names = []
-
+
vcapsvcs = ENV['VCAP_SERVICES']
-
+
if vcapsvcs
svcs = JSON.parse(ENV['VCAP_SERVICES'])
svcs.each do |key,list|
@@ -21,48 +22,70 @@ class CloudApp
label = key[0..key.index('-')-1]
version = key[key.index('-')+1..key.length]
list.each do |svc|
- name = svc["name"]
- dbopts = {}
+ name = svc["name"]
+ serviceopts = {}
+ serviceopts[:label] = label
+ serviceopts[:version] = version
+ serviceopts[:name] = name
cred = svc["credentials"]
- user,passwd,host,port,dbname,db = %w(username password hostname port name db).map {|key|
- cred[key]}
- dbopts[:label] = label
- dbopts[:version] = version
- dbopts[:name] = name
- dbopts[:username] = user
- dbopts[:password] = passwd
- dbopts[:host] = host
- dbopts[:port] = port
- if label == "mongodb"
- dbopts[:db] = db
+ if label =~ /rabbitmq/
+ if cred['url']
+ #The RabbitMQ default vhost
+ vhost = '/'
+ # The new "srs" credentials format
+ 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 amqp URI: #{uri}") if $1.index('/')
+ vhost = URI.unescape($1)
+ end
+ else
+ # The "old" credentials format
+ user,passwd,host,port,vhost = %w(user pass hostname port vhost).map {|key|
+ cred[key]}
+ end
+ serviceopts[:vhost] = vhost
else
- dbopts[:database] = dbname
+ user,passwd,host,port,dbname,db = %w(username password hostname port name db).map {|key|
+ cred[key]}
+ if label == "mongodb"
+ serviceopts[:db] = db
+ else
+ serviceopts[:database] = dbname
+ end
end
- @@svcs[name] = dbopts
+ serviceopts[:username] = user
+ serviceopts[:password] = passwd
+ serviceopts[:host] = host
+ serviceopts[:port] = port
+ @@svcs[name] = serviceopts
@@svc_names << name
if count == 1
- @@svcs[label] = dbopts
+ @@svcs[label] = serviceopts
end
end
end
end
-
+
def self.service_names()
@@svc_names
end
-
+
def self.service_props(name)
@@svcs[name]
end
-
+
def self.host()
@@host
end
-
+
def self.port()
@@port
end
-
+
def self.running_in_cloud?()
if @@app == nil
false
View
46 cfruntime/spec/properties_spec.rb
@@ -19,7 +19,7 @@
CFRuntime::CloudApp.running_in_cloud?.should == true
end
end
-
+
it 'works with a service' do
svcs = {"mongodb-#{mongo_version}"=>[create_mongo_service('mongo-test')]}
with_vcap_services(svcs) do
@@ -50,7 +50,7 @@
it 'works with services of different types' do
svcs = {
- "redis-#{redis_version}"=>[create_redis_service('redis-test')],
+ "redis-#{redis_version}"=>[create_redis_service('redis-test')],
"mongodb-#{mongo_version}"=>[create_mongo_service('mongo-test')]}
with_vcap_services(svcs) do
load 'cfruntime/properties.rb'
@@ -61,4 +61,46 @@
end
end
+ it 'works with rabbitmq service (old format)' do
+ svcs = {"rabbitmq-#{rabbit_version}"=>[create_rabbit_service('rabbit-test','testvhost')]}
+ with_vcap_services(svcs) do
+ load 'cfruntime/properties.rb'
+ CFRuntime::CloudApp.service_props('rabbitmq').should_not == nil
+ CFRuntime::CloudApp.service_props('rabbit-test')[:name].should == "rabbit-test"
+ CFRuntime::CloudApp.service_props('rabbit-test')[:host].should == SOME_SERVER
+ CFRuntime::CloudApp.service_props('rabbit-test')[:port].should == 25046
+ CFRuntime::CloudApp.service_props('rabbit-test')[:username].should_not == nil
+ CFRuntime::CloudApp.service_props('rabbit-test')[:password].should_not == nil
+ CFRuntime::CloudApp.service_props('rabbit-test')[:vhost].should == "testvhost"
+ end
+ end
+
+ it 'works with rabbitmq service (new format)' do
+ svcs = {"rabbitmq-#{rabbit_version}"=>[create_rabbit_srs_service('rabbit-test','testvhost')]}
+ with_vcap_services(svcs) do
+ load 'cfruntime/properties.rb'
+ CFRuntime::CloudApp.service_props('rabbitmq').should_not == nil
+ CFRuntime::CloudApp.service_props('rabbit-test')[:name].should == "rabbit-test"
+ CFRuntime::CloudApp.service_props('rabbit-test')[:host].should == SOME_SERVER
+ CFRuntime::CloudApp.service_props('rabbit-test')[:port].should == 25046
+ CFRuntime::CloudApp.service_props('rabbit-test')[:username].should_not == nil
+ CFRuntime::CloudApp.service_props('rabbit-test')[:password].should_not == nil
+ CFRuntime::CloudApp.service_props('rabbit-test')[:vhost].should == "testvhost"
+ end
+ end
+
+ it 'works with rabbitmq service without vhost (new format)' do
+ svcs = {"rabbitmq-#{rabbit_version}"=>[create_rabbit_srs_service('rabbit-test')]}
+ with_vcap_services(svcs) do
+ load 'cfruntime/properties.rb'
+ CFRuntime::CloudApp.service_props('rabbitmq').should_not == nil
+ CFRuntime::CloudApp.service_props('rabbit-test')[:name].should == "rabbit-test"
+ CFRuntime::CloudApp.service_props('rabbit-test')[:host].should == SOME_SERVER
+ CFRuntime::CloudApp.service_props('rabbit-test')[:port].should == 25046
+ CFRuntime::CloudApp.service_props('rabbit-test')[:username].should_not == nil
+ CFRuntime::CloudApp.service_props('rabbit-test')[:password].should_not == nil
+ CFRuntime::CloudApp.service_props('rabbit-test')[:vhost].should == '/'
+ end
+ end
+
end
View
47 cfruntime/spec/spec_helper.rb
@@ -13,10 +13,10 @@
module CFRuntime
module Test
-
+
SOME_SERVER = '172.30.48.73'
SOME_PORT = 56789
-
+
def with_vcap_application
vcap_app = {"instance_id"=>"#{secure_uuid}",
"instance_index"=>0,
@@ -39,7 +39,7 @@ def with_vcap_services(services)
ENV['VCAP_SERVICES'] = JSON services
yield
end
-
+
# this is in commons.rb - could require this maybe?
def secure_uuid
result = File.open('/dev/urandom') { |x| x.read(16).unpack('H*')[0] }
@@ -52,17 +52,50 @@ def mongo_version
def redis_version
"2.2"
end
-
+
+ def rabbit_version
+ "2.4"
+ end
+
def create_mongo_service(name)
vcap_svc = create_service(name, "mongodb", mongo_version)
vcap_svc["credentials"]["db"] = "db"
vcap_svc
end
-
+
def create_redis_service(name)
create_service(name, "redis", redis_version)
end
-
+
+ def create_rabbit_service(name, vhost=nil)
+ {"name"=>"#{name}",
+ "label"=>"rabbitmq-#{rabbit_version}",
+ "plan"=>"free",
+ "tags"=>["rabbitmq","rabbitmq-#{rabbit_version}"],
+ "credentials"=>{
+ "hostname"=>"#{SOME_SERVER}",
+ "port"=>25046,
+ "user"=>"#{secure_uuid}",
+ "pass"=>"#{secure_uuid}",
+ "vhost"=>"#{vhost}"}
+ }
+ end
+
+ def create_rabbit_srs_service(name, vhost=nil)
+ if vhost
+ url = "amqp://#{secure_uuid}:#{secure_uuid}@#{SOME_SERVER}:25046/#{vhost}"
+ else
+ url = "amqp://#{secure_uuid}:#{secure_uuid}@#{SOME_SERVER}:25046"
+ end
+ {"name"=>"#{name}",
+ "label"=>"rabbitmq-#{rabbit_version}",
+ "plan"=>"free",
+ "tags"=>["rabbitmq","rabbitmq-#{rabbit_version}"],
+ "credentials"=>{
+ "url"=>url}
+ }
+ end
+
def create_service(name, type, version)
{"name"=>"#{name}",
"label"=>"#{type}-#{version}",
@@ -75,7 +108,7 @@ def create_service(name, type, version)
"username"=>"#{secure_uuid}",
"password"=>"#{secure_uuid}",
"name"=>"#{secure_uuid}"}
- }
+ }
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.