This uses the REST api exposed by conductor to send updates
that the connector receives from the factory.
---
.../console/lib/image_factory/base_handler.rb | 4 +-
.../image_factory/image_factory_connector/Rakefile | 1 +
.../factory_rest_handler.rb | 49 +++++++++++++++--
.../image_factory_connector.rb | 4 +-
.../image_factory_connector/spec/connector_spec.rb | 5 ++
.../spec/factory_rest_handler_spec.rb | 58 ++++++++++++++++++++
6 files changed, 112 insertions(+), 9 deletions(-)
create mode 100644
services/image_factory/image_factory_connector/spec/factory_rest_handler_spec.rb
diff --git a/services/image_factory/console/lib/image_factory/base_handler.rb
b/services/image_factory/console/lib/image_factory/base_handler.rb
index 7dcead8..17756ec 100644
--- a/services/image_factory/console/lib/image_factory/base_handler.rb
+++ b/services/image_factory/console/lib/image_factory/base_handler.rb
@@ -27,8 +27,8 @@ class BaseHandler
def handle(data)
logger.debug "====== Type of event: #{data.event}"
- #puts "should be calling the logger now..."
if data.event == 'STATUS'
+ logger.debug "calling handle status in base handler"
handle_status(data)
elsif data.event == 'FAILURE'
handle_failed(data)
@@ -43,7 +43,7 @@ class BaseHandler
logger.error "#{data.to_s}"
end
- private
+ protected
def logger(logger=nil)
@logger ||= logger
unless @logger
diff --git a/services/image_factory/image_factory_connector/Rakefile
b/services/image_factory/image_factory_connector/Rakefile
index 17fda70..9df396a 100644
--- a/services/image_factory/image_factory_connector/Rakefile
+++ b/services/image_factory/image_factory_connector/Rakefile
@@ -43,6 +43,7 @@ spec = Gem::Specification.new do |s|
s.add_dependency('image_factory_console', '>=0.2.0')
s.add_dependency('builder', '~>2.1.2')
s.add_dependency('sinatra', '~>1.0')
+ s.add_dependency('typhoeus', '~>0.1.31')
s.add_development_dependency('rspec', '~>1.3.0')
diff --git
a/services/image_factory/image_factory_connector/lib/image_factory_connector/factory_rest_handler.rb
b/services/image_factory/image_factory_connector/lib/image_factory_connector/factory_rest_handler.rb
index 0e73e60..af7baf2 100644
---
a/services/image_factory/image_factory_connector/lib/image_factory_connector/factory_rest_handler.rb
+++
b/services/image_factory/image_factory_connector/lib/image_factory_connector/factory_rest_handler.rb
@@ -18,14 +18,51 @@
# also available at
http://www.gnu.org/copyleft/gpl.html.
require 'base_handler'
+require 'typhoeus'
class FactoryRestHandler < BaseHandler
- def handle(data)
- super
- logger.debug "====THIS WAS CALLED FROM REST HANDLER===="
- # TODO: implement rest calls back to conductor, mainly status
- # update. This will be passed back to:
- # /image-factory/{images or provider_images}/status-update/{uuid}
+ class EventData
+ attr_accessor :event, :value, :obj, :uuid
+ def initialize(args={})
+ self.event = args[:event]
+ self.value = args[:value].downcase
+ self.obj = args[:obj]
+ self.uuid = args[:uuid]
end
+ end
+
+ def handle(data)
+ super
+ end
+
+ def handle_status(data)
+ # Steps:
+ # 1. Take the data passed in, and split the addr into
+ # object type and uuid
+ e= _process_event(data)
+
+ # 2. Call the conductor api with uuid and status
+ hydra = Typhoeus::Hydra.new
+ request =
Typhoeus::Request.new("http://localhost:3000/image_factory/builds/update_status",
+ :method => :post,
+ :timeout => 2000, # in milliseconds
+ :headers => {:Accepts =>
"application/xml"},
+ :params => {:uuid => e.uuid, :status =>
e.value})
+ hydra.queue(request)
+ request.on_complete do |response|
+ # 3. Log errors
+ logger.debug "Return code is: #{response.code}"
+ logger.debug "Return body is: #{response.body}"
+ end
+ hydra.run
+ end
+
+ def _process_event(data)
+ addr = data["addr"]["_object_name"].split(':')
+ e= EventData.new({:event=>data.event, :value=>data.new_status,
+ :obj=>addr[1], :uuid=>addr[2]})
+ logger.debug "Data: #{e.inspect}"
+ e
+ end
end
diff --git
a/services/image_factory/image_factory_connector/lib/image_factory_connector/image_factory_connector.rb
b/services/image_factory/image_factory_connector/lib/image_factory_connector/image_factory_connector.rb
index 94486f5..ce7446e 100644
---
a/services/image_factory/image_factory_connector/lib/image_factory_connector/image_factory_connector.rb
+++
b/services/image_factory/image_factory_connector/lib/image_factory_connector/image_factory_connector.rb
@@ -29,7 +29,9 @@ class ImageFactoryConnector < Sinatra::Base
set :port, 2003
set :app_file, __FILE__
set :views, File.dirname(__FILE__) + '/views'
- @console = ImageFactoryConsole.new({:handler=>FactoryRestHandler.new})
+ l = Logger.new(STDOUT)
+ l.level = Logger::DEBUG
+ @console = ImageFactoryConsole.new({:handler=>FactoryRestHandler.new(l)})
@console.start
set :console, @console
end
diff --git a/services/image_factory/image_factory_connector/spec/connector_spec.rb
b/services/image_factory/image_factory_connector/spec/connector_spec.rb
index 613147d..bbf10c9 100644
--- a/services/image_factory/image_factory_connector/spec/connector_spec.rb
+++ b/services/image_factory/image_factory_connector/spec/connector_spec.rb
@@ -41,6 +41,11 @@ describe 'image_factory_connector app' do
last_response.body.should == app.console.q.to_s
end
+ it "sends status updates to the conductor" do
+ post 'build', {:template => '<template></template>',
:target => 'mock'}
+ #app.console.handler.should_receive(:handle_status).at_least(1).times
+ end
+
# TODO: clean up these xml checks so they are in some fixture (or similar) and not
repeated
it 'calls the console build_image method and returns xml response with uuid'
do
app.console.stub!(:build_image).and_return(@b)
diff --git
a/services/image_factory/image_factory_connector/spec/factory_rest_handler_spec.rb
b/services/image_factory/image_factory_connector/spec/factory_rest_handler_spec.rb
new file mode 100644
index 0000000..463e7a1
--- /dev/null
+++ b/services/image_factory/image_factory_connector/spec/factory_rest_handler_spec.rb
@@ -0,0 +1,58 @@
+#
+# Copyright (C) 2011 Red Hat, Inc.
+# Written by Jason Guiditta <jguiditt(a)redhat.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; version 2 of the License.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
+# MA 02110-1301, USA. A copy of the GNU General Public License is
+# also available at
http://www.gnu.org/copyleft/gpl.html.
+
+require 'spec_helper'
+require 'typhoeus'
+
+class FakeData < Hash
+ attr_accessor :event, :new_status
+ def initialize(args={})
+ self.event = args[:event]
+ self.new_status = args[:new_status].downcase
+ end
+end
+
+describe "factory_rest_handler" do
+ before(:each) do
+ @l = Logger.new(STDOUT)
+ @l.level = Logger::DEBUG
+ @handler=FactoryRestHandler.new(@l)
+ @image_id ="71c852f5-de8f-467a-81db-eedb72c5ec8b"
+ @fd=FakeData.new({:event=>"STATUS", :new_status=>"FUBAR"})
+
@fd["addr"]={"_object_name"=>"build_adaptor:image:#{@image_id}"}
+ end
+
+ it "parses the event properly" do
+ e=@handler._process_event((a)fd)
+ e.event.should == @fd.event
+ e.value.should == @fd.new_status
+ e.uuid.should == @image_id
+ e.obj.should == "image"
+ end
+
+ it "sends status updates to the conductor" do
+ # TODO: properly stub this out
+ #@handler.handle_status((a)fd)
+ end
+
+ it "handles failure when updating the conductor" do
+
@fd["addr"]={"_object_name"=>"build_adaptor:image:some-bogus-uuid"}
+ @handler.handle_status(@fd)
+ end
+end
\ No newline at end of file
--
1.7.4