From: Michal Fojtik <[email protected]>

---
 server/lib/sinatra/rack_matrix_params.rb |   54 ++++++++++++++++++++++++++++++
 server/server.rb                         |    2 +
 server/views/instances/show.xml.haml     |    4 +-
 3 files changed, 58 insertions(+), 2 deletions(-)
 create mode 100644 server/lib/sinatra/rack_matrix_params.rb

diff --git a/server/lib/sinatra/rack_matrix_params.rb 
b/server/lib/sinatra/rack_matrix_params.rb
new file mode 100644
index 0000000..7488c73
--- /dev/null
+++ b/server/lib/sinatra/rack_matrix_params.rb
@@ -0,0 +1,54 @@
+# Copyright (C) 2009, 2010  Red Hat, Inc.
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.  The
+# ASF licenses this file to you under the Apache License, Version 2.0 (the
+# "License"); you may not use this file except in compliance with the
+# License.  You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+# License for the specific language governing permissions and limitations
+# under the License.
+
+# Methods added to this helper will be available to all templates in the 
application.
+
+module Rack
+
+  class MatrixParams
+    def initialize(app)
+      @app = app
+    end
+
+    # This will allow to use 'matrix' params in POST requests, like:
+    #
+    # POST http://127.0.0.1:9393/test/test;person_id=i-123123;test=1
+    # 
+    # => {"instance_id"=>"i-123123", "haha"=>"1", "test"=>"1"}
+    #
+    # (where 'haha' is regular POST param sent from HTML form
+    #
+    def call(env)
+      uri = env['REQUEST_URI']
+      if env['REQUEST_METHOD'] == 'POST' and uri =~ /\;(\w+)\=/
+        params_arr = uri.split(/;(\w+)=/)
+        matrix_params, value = {}, nil
+        while param=params_arr.pop do
+          if value
+            (matrix_params[param]=value) && (value=nil)
+            next
+          else
+            value = param
+          end
+        end
+        env['rack.request.form_hash'].merge!(matrix_params)
+      end
+      @app.call(env)
+    end
+  end
+
+end
diff --git a/server/server.rb b/server/server.rb
index e146704..96b59fb 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -30,6 +30,7 @@ require 'lib/deltacloud/helpers/blob_stream'
 require 'sinatra/rack_driver_select'
 require 'sinatra/rack_runtime'
 require 'sinatra/rack_etag'
+require 'sinatra/rack_matrix_params'
 
 set :version, '0.2.0'
 
@@ -39,6 +40,7 @@ set :drivers, Proc.new { driver_config }
 use Rack::DriverSelect
 use Rack::ETag
 use Rack::Runtime
+use Rack::MatrixParams
 
 configure do
   set :raise_errors => false
diff --git a/server/views/instances/show.xml.haml 
b/server/views/instances/show.xml.haml
index 5e1b974..7e9f4fa 100644
--- a/server/views/instances/show.xml.haml
+++ b/server/views/instances/show.xml.haml
@@ -22,11 +22,11 @@
       - @instance.actions.compact.each do |instance_action|
         %link{:rel => instance_action, :method => 
instance_action_method(instance_action), :href => 
self.send("#{instance_action}_instance_url", @instance.id)}
       - if driver.respond_to?(:run_on_instance)
-        %link{:rel => 'run', :method => :post, :href => 
run_instance_url(@instance.id)}
+        %link{:rel => 'run', :method => :post, :href => 
"#{run_instance_url(@instance.id)};id=#{@instance.id}"}
           - generate_action_params(:instances, :run) do |param|
             = param.call(:id => @instance.id, :password => @instance.password)
       - if @instance.can_create_image?
-        %link{:rel => 'create_image', :method => :post, :href => 
create_image_url}
+        %link{:rel => 'create_image', :method => :post, :href => 
"#{create_image_url};instance_id=#{@instance.id}"}
           - generate_action_params(:images, :create) do |param|
             = param.call(:instance_id => @instance.id)
   - if @instance.instance_variables.include?("@launch_time")
-- 
1.7.4

Reply via email to