You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@deltacloud.apache.org by mf...@redhat.com on 2012/09/12 11:01:27 UTC
Improve url_for helpers generation
Hi,
These patches will improve the way how we generate *_url helpers.
Now, all helpers are generated using 'method_missing' which is not
just hard to debug but also lead to many wrong urls.
This patch use Rabbit to list all collections and operations and
generate url helpers from them.
Note that this patch need 'Code layout fixes' patch applied.
-- Michal
[PATCH core 3/3] Core: Replaced api_url_for with url_for
Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>
* The api_url_for does not add any extra value to original
'url_for' helper. Mixing there two helper in views cause
confusion.
Signed-off-by: Michal fojtik <mf...@redhat.com>
---
server/views/api/show.html.haml | 2 +-
server/views/buckets/index.html.haml | 2 +-
server/views/cimi/cloudEntryPoint/index.xml.haml | 2 +-
server/views/cimi/collection/index.html.haml | 6 +++---
server/views/cimi/collection/response.xml.haml | 2 +-
server/views/cimi/error.html.haml | 2 +-
server/views/cimi/layout.html.haml | 2 +-
server/views/cimi/machine_configurations/show.html.haml | 2 +-
server/views/cimi/machine_configurations/show.xml.haml | 2 +-
server/views/cimi/machine_images/show.html.haml | 2 +-
server/views/cimi/machine_images/show.xml.haml | 2 +-
server/views/cimi/machines/show.html.haml | 2 +-
server/views/cimi/machines/show.xml.haml | 2 +-
server/views/cimi/volumes/show.html.haml | 2 +-
server/views/cimi/volumes/show.xml.haml | 2 +-
server/views/docs/collection.html.haml | 2 +-
server/views/docs/collection.xml.haml | 4 ++--
server/views/docs/index.html.haml | 2 +-
server/views/docs/index.xml.haml | 2 +-
server/views/docs/operation.xml.haml | 2 +-
server/views/error.html.haml | 2 +-
server/views/firewalls/index.html.haml | 2 +-
server/views/firewalls/show.html.haml | 2 +-
server/views/images/show.html.haml | 4 ++--
server/views/instance_states/show.html.haml | 2 +-
server/views/instances/run_command.html.haml | 2 +-
server/views/instances/show.html.haml | 4 ++--
server/views/keys/index.html.haml | 2 +-
server/views/layout.html.haml | 2 +-
server/views/load_balancers/index.html.haml | 2 +-
server/views/load_balancers/show.html.haml | 6 +++---
server/views/storage_snapshots/show.html.haml | 2 +-
server/views/storage_volumes/attach.html.haml | 2 +-
server/views/storage_volumes/index.html.haml | 2 +-
server/views/storage_volumes/show.html.haml | 8 ++++----
35 files changed, 45 insertions(+), 45 deletions(-)
diff --git a/server/views/api/show.html.haml b/server/views/api/show.html.haml
index d315ebb..541c388 100644
--- a/server/views/api/show.html.haml
+++ b/server/views/api/show.html.haml
@@ -5,7 +5,7 @@
%ul{ :'data-role' => :listview, :'data-inset' => 'true'}
- supported_collections do |c|
%li
- %a{ :href => api_url_for(c.collection_name), :'data-icon' => "arrow-r"}=c.collection_name.to_s.gsub('_', ' ').titlecase
+ %a{ :href => url_for(c.collection_name), :'data-icon' => "arrow-r"}=c.collection_name.to_s.gsub('_', ' ').titlecase
- unless Deltacloud::Drivers.driver_config.nil?
diff --git a/server/views/buckets/index.html.haml b/server/views/buckets/index.html.haml
index 1510dbb..48dc699 100644
--- a/server/views/buckets/index.html.haml
+++ b/server/views/buckets/index.html.haml
@@ -1,5 +1,5 @@
=header "Buckets" do
- %a{ :href => api_url_for('buckets/new'), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right'} Create new bucket
+ %a{ :href => url_for('buckets/new'), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right'} Create new bucket
%div{ :'data-role' => :content, :'data-theme' => 'c'}
%ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'a'}
diff --git a/server/views/cimi/cloudEntryPoint/index.xml.haml b/server/views/cimi/cloudEntryPoint/index.xml.haml
index 784b931..cce651b 100644
--- a/server/views/cimi/cloudEntryPoint/index.xml.haml
+++ b/server/views/cimi/cloudEntryPoint/index.xml.haml
@@ -1,6 +1,6 @@
!!!XML
%CloudEntryPoint{ :xmlns => CMWG_NAMESPACE }
- %uri= api_url_for("/cloudEntryPoint")
+ %uri= url_for("/cloudEntryPoint")
%name cloud entry point
%description cloud entry point
%created= Time.new.getutc.to_s
diff --git a/server/views/cimi/collection/index.html.haml b/server/views/cimi/collection/index.html.haml
index 0860f5f..76ea9ba 100644
--- a/server/views/cimi/collection/index.html.haml
+++ b/server/views/cimi/collection/index.html.haml
@@ -2,7 +2,7 @@
- new_name = new_name.gsub(/[A-Z]/, ' \0')
%h1 #{new_name.capitalize}
-%form{ :action => api_url_for("/collection/" + @dmtfitem["uri"]) }
+%form{ :action => url_for("/collection/" + @dmtfitem["uri"]) }
%input{ :name => :id, :type => :hidden, :value => @dmtfitem["uri"] }/
%input{ :name => :xmlRootNode, :type => :hidden, :value => @xml_root_node }/
%p
@@ -28,7 +28,7 @@
- iter += 1
%br
%label
- = operation["rel"].capitalize + ": " + api_url_for(operation["href"])
+ = operation["rel"].capitalize + ": " + url_for(operation["href"])
%input{ :name => "operation_"+iter.to_s, :oper_type => operation["rel"], :type => :hidden, :value => operation["href"] }
%br
%p
@@ -42,4 +42,4 @@
- @dmtf_col_items.map do |template|
%tr
%td
- %a{ :href => template["href"] }= template["name"]
\ No newline at end of file
+ %a{ :href => template["href"] }= template["name"]
diff --git a/server/views/cimi/collection/response.xml.haml b/server/views/cimi/collection/response.xml.haml
index ad892d7..0b8eb7a 100644
--- a/server/views/cimi/collection/response.xml.haml
+++ b/server/views/cimi/collection/response.xml.haml
@@ -1,3 +1,3 @@
!!!XML
%ReturnCode{ :xmlns => CMWG_NAMESPACE }
- %Status= "Success"
\ No newline at end of file
+ %Status= "Success"
diff --git a/server/views/cimi/error.html.haml b/server/views/cimi/error.html.haml
index 5a9bb59..942393c 100644
--- a/server/views/cimi/error.html.haml
+++ b/server/views/cimi/error.html.haml
@@ -28,4 +28,4 @@
#copyright
Copyright 2009-2011
%a{:href => 'http://incubator.apache.org/deltacloud/'} The Apache Software Foundation
- and individual contributors.
\ No newline at end of file
+ and individual contributors.
diff --git a/server/views/cimi/layout.html.haml b/server/views/cimi/layout.html.haml
index 2673453..7c3b754 100644
--- a/server/views/cimi/layout.html.haml
+++ b/server/views/cimi/layout.html.haml
@@ -29,4 +29,4 @@
#copyright
Copyright 2009-2011
%a{:href => 'http://incubator.apache.org/deltacloud/'} The Apache Software Foundation
- and individual contributors.
\ No newline at end of file
+ and individual contributors.
diff --git a/server/views/cimi/machine_configurations/show.html.haml b/server/views/cimi/machine_configurations/show.html.haml
index 38dc867..c59242e 100644
--- a/server/views/cimi/machine_configurations/show.html.haml
+++ b/server/views/cimi/machine_configurations/show.html.haml
@@ -156,4 +156,4 @@
})
cellRightBut.appendChild(er);
- }
\ No newline at end of file
+ }
diff --git a/server/views/cimi/machine_configurations/show.xml.haml b/server/views/cimi/machine_configurations/show.xml.haml
index c337fe1..dbcfe78 100644
--- a/server/views/cimi/machine_configurations/show.xml.haml
+++ b/server/views/cimi/machine_configurations/show.xml.haml
@@ -24,4 +24,4 @@
%capacity{ :quantity => disk["capacity"]["quantity"], :units => disk["capacity"]["units"] }
%guestInterface= disk["guestInterface"]
%operation{ :rel => "edit", :href => machine_configurations_url + "/" + @dmtfitem["uri"] }
- %operation{ :rel => "delete", :href => machine_configurations_url + "/" + @dmtfitem["uri"] }
\ No newline at end of file
+ %operation{ :rel => "delete", :href => machine_configurations_url + "/" + @dmtfitem["uri"] }
diff --git a/server/views/cimi/machine_images/show.html.haml b/server/views/cimi/machine_images/show.html.haml
index 1c3a28e..202291e 100644
--- a/server/views/cimi/machine_images/show.html.haml
+++ b/server/views/cimi/machine_images/show.html.haml
@@ -76,4 +76,4 @@
xmlData += "</" + $(theNode.form).attr("xmlRootNode").value + ">";
return xmlData;
- }
\ No newline at end of file
+ }
diff --git a/server/views/cimi/machine_images/show.xml.haml b/server/views/cimi/machine_images/show.xml.haml
index a00713c..3d55ebf 100644
--- a/server/views/cimi/machine_images/show.xml.haml
+++ b/server/views/cimi/machine_images/show.xml.haml
@@ -14,4 +14,4 @@
%property{ :name => key}=value["content"]
%imageLocation{ :href => @dmtfitem["imageLocation"]}
%operation{ :rel => "edit", :href => machine_images_url + "/" + @dmtfitem["uri"] }
- %operation{ :rel => "delete", :href => machine_images_url + "/" + @dmtfitem["uri"] }
\ No newline at end of file
+ %operation{ :rel => "delete", :href => machine_images_url + "/" + @dmtfitem["uri"] }
diff --git a/server/views/cimi/machines/show.html.haml b/server/views/cimi/machines/show.html.haml
index c29ddff..569caeb 100644
--- a/server/views/cimi/machines/show.html.haml
+++ b/server/views/cimi/machines/show.html.haml
@@ -174,4 +174,4 @@
cellRightBut.appendChild(er);
- }
\ No newline at end of file
+ }
diff --git a/server/views/cimi/machines/show.xml.haml b/server/views/cimi/machines/show.xml.haml
index c699b19..653b51f 100644
--- a/server/views/cimi/machines/show.xml.haml
+++ b/server/views/cimi/machines/show.xml.haml
@@ -25,4 +25,4 @@
%capacity{ :quantity => disk["capacity"]["quantity"], :units => disk["capacity"]["units"] }
%guestInterface= disk["guestInterface"]
%operation{ :rel => "edit", :href => machines_url + "/" + @dmtfitem["uri"] }
- %operation{ :rel => "delete", :href => machines_url + "/" + @dmtfitem["uri"] }
\ No newline at end of file
+ %operation{ :rel => "delete", :href => machines_url + "/" + @dmtfitem["uri"] }
diff --git a/server/views/cimi/volumes/show.html.haml b/server/views/cimi/volumes/show.html.haml
index 0e7b20b..c91c540 100644
--- a/server/views/cimi/volumes/show.html.haml
+++ b/server/views/cimi/volumes/show.html.haml
@@ -65,4 +65,4 @@
xmlData += "</" + $(theNode.form).attr("xmlRootNode").value + ">";
return xmlData;
- }
\ No newline at end of file
+ }
diff --git a/server/views/cimi/volumes/show.xml.haml b/server/views/cimi/volumes/show.xml.haml
index 30ef2c2..32f48af 100644
--- a/server/views/cimi/volumes/show.xml.haml
+++ b/server/views/cimi/volumes/show.xml.haml
@@ -14,4 +14,4 @@
%property{ :name => key}=value["content"]
%capacity{ :quantity => @dmtfitem["capacity"]["quantity"], :units => @dmtfitem["capacity"]["units"]}
%operation{ :rel => "edit", :href => volumes_url + "/" + @dmtfitem["uri"] }
- %operation{ :rel => "delete", :href => volumes_url + "/" + @dmtfitem["uri"] }
\ No newline at end of file
+ %operation{ :rel => "delete", :href => volumes_url + "/" + @dmtfitem["uri"] }
diff --git a/server/views/docs/collection.html.haml b/server/views/docs/collection.html.haml
index 537f52c..7485b60 100644
--- a/server/views/docs/collection.html.haml
+++ b/server/views/docs/collection.html.haml
@@ -13,7 +13,7 @@
%h3 Operations:
- @operations.keys.sort_by { |k| k.to_s }.each do |operation|
%li{ :'data-role' => 'list-divider'}
- %a{:href => api_url_for("docs/#{@collection.name.to_s}/#{operation}"), :'data-ajax' => 'false'} #{operation}
+ %a{:href => url_for("docs/#{@collection.name.to_s}/#{operation}"), :'data-ajax' => 'false'} #{operation}
%li
%br
%p=@operations[operation].description
diff --git a/server/views/docs/collection.xml.haml b/server/views/docs/collection.xml.haml
index 8525f45..4359901 100644
--- a/server/views/docs/collection.xml.haml
+++ b/server/views/docs/collection.xml.haml
@@ -1,9 +1,9 @@
%docs{:status => "unsupported"}
- %collection{:url => api_url_for("docs/#{@collection.name}"), :name => "#{@collection.name}"}
+ %collection{:url => url_for("docs/#{@collection.name}"), :name => "#{@collection.name}"}
%description #{@collection.description}
%operations
- @operations.keys.sort_by { |k| k.to_s }.each do |operation|
- %operation{:url => api_url_for("#{@collection.name.to_s}"), :name => "#{operation}", :href => api_url_for("#{@operations[operation].path}"), :method => "#{@operations[operation].method}"}
+ %operation{:url => url_for("#{@collection.name.to_s}"), :name => "#{operation}", :href => url_for("#{@operations[operation].path}"), :method => "#{@operations[operation].method}"}
%description #{@operations[operation].description}
- @operations[operation].each_param do |param|
%parameter{:name => "#{param.name}", :type => "#{param.type}"}
diff --git a/server/views/docs/index.html.haml b/server/views/docs/index.html.haml
index c3912bd..742e03f 100644
--- a/server/views/docs/index.html.haml
+++ b/server/views/docs/index.html.haml
@@ -5,7 +5,7 @@
%ul{ :'data-role' => :listview, :'data-inset' => 'true'}
- collections.keys.sort_by { |k| k.to_s }.each do |collection|
%li
- %a{:href => api_url_for("docs/#{collection}"), :'data-ajax' => 'false'}
+ %a{:href => url_for("docs/#{collection}"), :'data-ajax' => 'false'}
%h3=collection.to_s.capitalize.tr('_', ' ')
%p=collections[collection].description
diff --git a/server/views/docs/index.xml.haml b/server/views/docs/index.xml.haml
index c953c33..18c9f7b 100644
--- a/server/views/docs/index.xml.haml
+++ b/server/views/docs/index.xml.haml
@@ -1,5 +1,5 @@
%docs{:status => "unsupported"}
- collections.keys.sort_by { |k| k.to_s }.each do |collection|
- %collection{:url => api_url_for("docs/#{collection}")}
+ %collection{:url => url_for("docs/#{collection}")}
%name #{collection}
%description #{collections[collection].description}
diff --git a/server/views/docs/operation.xml.haml b/server/views/docs/operation.xml.haml
index fcc9c59..3faa5e5 100644
--- a/server/views/docs/operation.xml.haml
+++ b/server/views/docs/operation.xml.haml
@@ -1,5 +1,5 @@
%docs{:status => "unsupported"}
- %operation{:url => api_url_for("docs/#{@collection.name.to_s}"), :name => "#{@operation.name.to_s}", :href => url_for("#{@operation.path}"), :method => "#{@operation.method}"}
+ %operation{:url => url_for("docs/#{@collection.name.to_s}"), :name => "#{@operation.name.to_s}", :href => url_for("#{@operation.path}"), :method => "#{@operation.method}"}
%description #{@operation.description}
- @operation.each_param do |param|
%parameter{:name => "#{param.name}", :type => "#{param.type}"}
diff --git a/server/views/error.html.haml b/server/views/error.html.haml
index 92897e1..220a44a 100644
--- a/server/views/error.html.haml
+++ b/server/views/error.html.haml
@@ -26,7 +26,7 @@
%div{ 'data-role' => :navbar}
%ul
%li
- %a{ :'data-icon' => 'home', :href => api_url_for(''), :'data-ajax' => 'false'} Home
+ %a{ :'data-icon' => 'home', :href => url_for(''), :'data-ajax' => 'false'} Home
%li=link_to_format(:xml)
%li=link_to_format(:json)
%div{ 'data-role' => :header, 'data-theme' => 'a' }
diff --git a/server/views/firewalls/index.html.haml b/server/views/firewalls/index.html.haml
index 403f21f..f497b59 100644
--- a/server/views/firewalls/index.html.haml
+++ b/server/views/firewalls/index.html.haml
@@ -1,5 +1,5 @@
=header "Firewalls" do
- %a{ :href => api_url_for('firewalls/new'), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right'} Create new firewall
+ %a{ :href => url_for('firewalls/new'), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right'} Create new firewall
=subheader "#{Thread::current[:provider] || ENV['API_PROVIDER'] || 'default'}"
%div{ :'data-role' => :content, :'data-theme' => 'c'}
diff --git a/server/views/firewalls/show.html.haml b/server/views/firewalls/show.html.haml
index 0fdea19..c2fd5d0 100644
--- a/server/views/firewalls/show.html.haml
+++ b/server/views/firewalls/show.html.haml
@@ -18,7 +18,7 @@
=link_to_action "Destroy", destroy_firewall_url(@firewall.id), :delete
=header "Rules", :back => 'false' do
- %a{ :href => api_url_for("firewalls/#{@firewall.id}/new_rule"), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right', :'data-ajax' => 'false'} Add new rule
+ %a{ :href => url_for("firewalls/#{@firewall.id}/new_rule"), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right', :'data-ajax' => 'false'} Add new rule
%div{ :'data-role' => :content, :'data-theme' => 'c'}
- @firewall.rules.each do |rule|
diff --git a/server/views/images/show.html.haml b/server/views/images/show.html.haml
index c166e87..6030a47 100644
--- a/server/views/images/show.html.haml
+++ b/server/views/images/show.html.haml
@@ -25,9 +25,9 @@
%li
%div{ :'data-role' => 'controlgroup', :'data-type' => "horizontal" }
- @image.hardware_profiles.each do |hwp|
- %a{ :href => api_url_for("hardware_profiles/#{hwp.name}"), :'data-role' => "button", :'data-ajax' => 'false'} #{hwp.name}
+ %a{ :href => url_for("hardware_profiles/#{hwp.name}"), :'data-role' => "button", :'data-ajax' => 'false'} #{hwp.name}
%li{ :'data-role' => 'list-divider'} Actions
%li
%div{ :'data-role' => 'controlgroup', :'data-type' => "horizontal" }
- %a{ :href => api_url_for("instances/new?image_id=#{@image.id}"), :'data-role' => "button", :'data-ajax' => 'false'} Launch
+ %a{ :href => url_for("instances/new?image_id=#{@image.id}"), :'data-role' => "button", :'data-ajax' => 'false'} Launch
= link_to_action 'Destroy', destroy_image_url(@image.id), :delete
diff --git a/server/views/instance_states/show.html.haml b/server/views/instance_states/show.html.haml
index 252f249..2a94801 100644
--- a/server/views/instance_states/show.html.haml
+++ b/server/views/instance_states/show.html.haml
@@ -2,7 +2,7 @@
=subheader "#{driver_symbol}@#{Thread::current[:provider] || ENV['API_PROVIDER'] || 'default'}"
%div{ :'data-role' => :content, :'data-theme' => 'd', :class => 'middle-dialog'}
- %img{ :src => api_url_for('instance_states?format=png') }
+ %img{ :src => url_for('instance_states?format=png') }
%ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-theme' => 'a'}
- @machine.states.each do |state|
diff --git a/server/views/instances/run_command.html.haml b/server/views/instances/run_command.html.haml
index 73a267a..75c7534 100644
--- a/server/views/instances/run_command.html.haml
+++ b/server/views/instances/run_command.html.haml
@@ -2,7 +2,7 @@
=subheader "#{@instance.id}"
%div{ :'data-role' => :content, :'data-theme' => 'c', :class => 'middle-dialog'}
- %form{ :action => api_url_for('/instances/%s/run' % @instance.id), :method => :post }
+ %form{ :action => url_for('/instances/%s/run' % @instance.id), :method => :post }
%p
%label{ :for => :cmd } Desired command:
%input{ :name => :cmd, :value => "", :type => :text}
diff --git a/server/views/instances/show.html.haml b/server/views/instances/show.html.haml
index 9910de4..cc0b92b 100644
--- a/server/views/instances/show.html.haml
+++ b/server/views/instances/show.html.haml
@@ -59,6 +59,6 @@
- @instance.actions.each do |action|
=link_to_action action, self.send(:"#{action}_instance_url", @instance.id), instance_action_method(action)
- if @instance.state=="RUNNING" and driver.respond_to?(:run_on_instance)
- =link_to_action 'run command', api_url_for('instances/%s/run' % @instance.id), :get
+ =link_to_action 'run command', url_for('instances/%s/run' % @instance.id), :get
- if @instance.can_create_image?
- =link_to_action 'Create Image', api_url_for("images/new?instance_id=#{@instance.id}"), :get
+ =link_to_action 'Create Image', url_for("images/new?instance_id=#{@instance.id}"), :get
diff --git a/server/views/keys/index.html.haml b/server/views/keys/index.html.haml
index c7a9f36..248aca7 100644
--- a/server/views/keys/index.html.haml
+++ b/server/views/keys/index.html.haml
@@ -1,5 +1,5 @@
=header "Keys" do
- %a{ :href => api_url_for('keys/new'), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right'} Create new key
+ %a{ :href => url_for('keys/new'), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right'} Create new key
%div{ :'data-role' => :content, :'data-theme' => 'c'}
%ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'a'}
diff --git a/server/views/layout.html.haml b/server/views/layout.html.haml
index 4806d59..4db3b7f 100644
--- a/server/views/layout.html.haml
+++ b/server/views/layout.html.haml
@@ -18,7 +18,7 @@
%div{ 'data-role' => :navbar}
%ul
%li
- %a{ :'data-icon' => 'home', :href => "#{api_url_for('').chomp("/")}", :'data-ajax' => 'false'} Home
+ %a{ :'data-icon' => 'home', :href => "#{url_for('').chomp("/")}", :'data-ajax' => 'false'} Home
%li=link_to_format(:xml)
%li=link_to_format(:json)
%div{ 'data-role' => :header, 'data-theme' => 'a' }
diff --git a/server/views/load_balancers/index.html.haml b/server/views/load_balancers/index.html.haml
index 8b730a1..789cd57 100644
--- a/server/views/load_balancers/index.html.haml
+++ b/server/views/load_balancers/index.html.haml
@@ -1,5 +1,5 @@
=header "Load balancers" do
- %a{ :href => api_url_for('load_balancers/new'), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right'} Create new load balancer
+ %a{ :href => url_for('load_balancers/new'), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right'} Create new load balancer
=subheader "#{Thread::current[:provider] || ENV['API_PROVIDER'] || 'default'}"
%div{ :'data-role' => :content, :'data-theme' => 'c'}
diff --git a/server/views/load_balancers/show.html.haml b/server/views/load_balancers/show.html.haml
index cab8961..a1c754d 100644
--- a/server/views/load_balancers/show.html.haml
+++ b/server/views/load_balancers/show.html.haml
@@ -20,16 +20,16 @@
%li
%a{ :href => instance_url(instance[:id]), :'data-ajax' => 'false'}
%h3=instance[:name]
- =link_to_action "UnRegister", api_url_for("load_balancers/#{@load_balancer.id}/unregister?instance_id=#{instance[:id]}"), :post
+ =link_to_action "UnRegister", url_for("load_balancers/#{@load_balancer.id}/unregister?instance_id=#{instance[:id]}"), :post
%li{ :'data-role' => 'list-divider'} UnRegistered Instances
- @unregistered_instances.each do |instance|
%li
%a{:href => instance_url(instance[:id]), :'data-ajax' => 'false'}
%h3=instance[:name]
- =link_to_action "Register", api_url_for("load_balancers/#{@load_balancer.id}/register?instance_id=#{instance[:id]}"), :post
+ =link_to_action "Register", url_for("load_balancers/#{@load_balancer.id}/register?instance_id=#{instance[:id]}"), :post
%li{ :'data-role' => 'list-divider'} Actions
%li
- =link_to_action "Destroy", api_url_for("load_balancers/#{@load_balancer.id}"), :delete
+ =link_to_action "Destroy", url_for("load_balancers/#{@load_balancer.id}"), :delete
=header "Listeners", :back => 'false'
%ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'd'}
diff --git a/server/views/storage_snapshots/show.html.haml b/server/views/storage_snapshots/show.html.haml
index bc76e3c..069e452 100644
--- a/server/views/storage_snapshots/show.html.haml
+++ b/server/views/storage_snapshots/show.html.haml
@@ -19,4 +19,4 @@
%li
%div{ :'data-role' => 'controlgroup', :'data-type' => "horizontal" }
- if driver.respond_to?(:destroy_storage_snapshot)
- =link_to_action "Delete", api_url_for("storage_snapshots/#{@storage_snapshot.id}"), :delete
+ =link_to_action "Delete", url_for("storage_snapshots/#{@storage_snapshot.id}"), :delete
diff --git a/server/views/storage_volumes/attach.html.haml b/server/views/storage_volumes/attach.html.haml
index 8c20753..073cc2a 100644
--- a/server/views/storage_volumes/attach.html.haml
+++ b/server/views/storage_volumes/attach.html.haml
@@ -1,6 +1,6 @@
%h1 Attach storage volume
-%form{ :action => api_url_for("storage_volumes/#{params[:id]}/attach"), :method => :post }
+%form{ :action => url_for("storage_volumes/#{params[:id]}/attach"), :method => :post }
%p
%label
Instance ID:
diff --git a/server/views/storage_volumes/index.html.haml b/server/views/storage_volumes/index.html.haml
index ba44b65..2cc8794 100644
--- a/server/views/storage_volumes/index.html.haml
+++ b/server/views/storage_volumes/index.html.haml
@@ -1,5 +1,5 @@
=header "Storage volumes" do
- %a{ :href => api_url_for('storage_volumes/new'), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right'} Create new volume
+ %a{ :href => url_for('storage_volumes/new'), :'data-icon' => :plus, :'data-role' => :button, :class => 'ui-btn-right'} Create new volume
%div{ :'data-role' => :content, :'data-theme' => 'c'}
%ul{ :'data-role' => :listview , :'data-inset' => :true, :'data-divider-theme' => 'a'}
diff --git a/server/views/storage_volumes/show.html.haml b/server/views/storage_volumes/show.html.haml
index 9a9da37..eddf5a9 100644
--- a/server/views/storage_volumes/show.html.haml
+++ b/server/views/storage_volumes/show.html.haml
@@ -35,9 +35,9 @@
%li
%div{ :'data-role' => 'controlgroup', :'data-type' => "horizontal" }
- if ["AVAILABLE", "IN-USE"].include?(@storage_volume.state)
- =link_to_action "Snapshot", api_url_for("storage_snapshots/new?volume_id=#{@storage_volume.id}"), :get
+ =link_to_action "Snapshot", url_for("storage_snapshots/new?volume_id=#{@storage_volume.id}"), :get
- unless @storage_volume.instance_id
- =link_to_action "Delete", api_url_for("storage_volumes/#{@storage_volume.id}"), :delete
- =link_to_action "Attach", api_url_for("storage_volumes/#{@storage_volume.id}/attach_instance"), :get
+ =link_to_action "Delete", url_for("storage_volumes/#{@storage_volume.id}"), :delete
+ =link_to_action "Attach", url_for("storage_volumes/#{@storage_volume.id}/attach_instance"), :get
- if @storage_volume.instance_id
- =link_to_action "Detach", api_url_for("storage_volumes/#{@storage_volume.id}/detach"), :post
+ =link_to_action "Detach", url_for("storage_volumes/#{@storage_volume.id}/detach"), :post
--
1.7.12
[PATCH core 1/3] Core: Code layout fixes and avoiding duplication
Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>
* Moved '[CIMI|Deltacloud]::Base' class to collections/base.rb.
Having this Sinatra class in 'helpers.rb' is not very intuitive.
* Move *::Base methods to Deltacloud::ApplicationHelper class.
Same methods defined on different places.
* Replaced obsoleted 'check_capability' with 'set' for MachineConfiguration
collection.
* Renamed 'CIMIHelper' to 'CIMI::Helper'
Signed-off-by: Michal fojtik <mf...@redhat.com>
TrackedAt: http://tracker.deltacloud.org/patch/b5a66123ee803827fe6808858fa3f2bf00e15467
---
server/lib/cimi/collections.rb | 5 +-
server/lib/cimi/collections/base.rb | 64 +++++++++++++++++
.../lib/cimi/collections/machine_configurations.rb | 2 +-
server/lib/cimi/helpers.rb | 83 +---------------------
server/lib/cimi/helpers/cimi_helper.rb | 27 +++----
server/lib/cimi/server.rb | 2 +-
server/lib/deltacloud/collections.rb | 5 +-
server/lib/deltacloud/collections/base.rb | 65 +++++++++++++++++
server/lib/deltacloud/collections/drivers.rb | 2 +-
server/lib/deltacloud/helpers.rb | 67 -----------------
server/lib/deltacloud/helpers/deltacloud_helper.rb | 23 ++++++
server/lib/deltacloud/helpers/driver_helper.rb | 7 ++
12 files changed, 187 insertions(+), 165 deletions(-)
create mode 100644 server/lib/cimi/collections/base.rb
create mode 100644 server/lib/deltacloud/collections/base.rb
diff --git a/server/lib/cimi/collections.rb b/server/lib/cimi/collections.rb
index b675fc4..a4e9ab9 100644
--- a/server/lib/cimi/collections.rb
+++ b/server/lib/cimi/collections.rb
@@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
+require_relative './collections/base'
+
module CIMI
def self.collection_names
@@ -35,8 +37,9 @@ module CIMI
end
Dir[File.join(File::dirname(__FILE__), "collections", "*.rb")].each do |collection|
- require collection
base_collection_name = File.basename(collection).gsub('.rb', '')
+ next if base_collection_name == 'base'
+ require_relative collection
cimi_module_class = CIMI::Collections.const_get(base_collection_name.camelize)
cimi_modules << cimi_module_class
unless cimi_module_class.collections.nil?
diff --git a/server/lib/cimi/collections/base.rb b/server/lib/cimi/collections/base.rb
new file mode 100644
index 0000000..7adb58d
--- /dev/null
+++ b/server/lib/cimi/collections/base.rb
@@ -0,0 +1,64 @@
+# 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.
+
+require_relative '../models'
+
+module CIMI::Collections
+ class Base < Sinatra::Base
+
+ include Sinatra::Rabbit
+ include Sinatra::Rabbit::Features
+ include CIMI::Model
+
+ helpers Deltacloud::Helpers::Drivers
+ helpers Sinatra::AuthHelper
+ helpers Sinatra::UrlForHelper
+ helpers Deltacloud::Helpers::Application
+ helpers CIMI::Helper
+
+ register Rack::RespondTo
+
+ enable :xhtml
+ enable :dump_errors
+ enable :show_errors
+ disable :show_exceptions
+
+ set :config, Deltacloud[:cimi]
+ set :root_url, config.root_url
+ set :root_path, config.root_url
+ set :version, config.version
+ set :root, File.join(File.dirname(__FILE__), '..', '..', '..')
+ set :views, root + '/views/cimi'
+ set :public_folder, root + '/public'
+
+ error do
+ report_error
+ end
+
+ error Deltacloud::ExceptionHandler::ValidationFailure do
+ report_error
+ end
+
+ before do
+ # Respond with 400, If we don't get a http Host header,
+ halt 400, "Unable to find HTTP Host header" if @env['HTTP_HOST'] == nil
+ end
+
+ after do
+ headers 'CIMI-Specification-Version' => Deltacloud[:cimi].version
+ end
+
+ end
+end
diff --git a/server/lib/cimi/collections/machine_configurations.rb b/server/lib/cimi/collections/machine_configurations.rb
index d7840df..e24d1aa 100644
--- a/server/lib/cimi/collections/machine_configurations.rb
+++ b/server/lib/cimi/collections/machine_configurations.rb
@@ -16,7 +16,7 @@
module CIMI::Collections
class MachineConfigurations < Base
- check_capability :for => lambda { |m| driver.respond_to? m }
+ set :capability => lambda { |m| driver.respond_to? m }
collection :machine_configurations do
description 'List all machine configurations'
diff --git a/server/lib/cimi/helpers.rb b/server/lib/cimi/helpers.rb
index 31f8d51..6513ed9 100644
--- a/server/lib/cimi/helpers.rb
+++ b/server/lib/cimi/helpers.rb
@@ -13,23 +13,19 @@
# License for the specific language governing permissions and limitations
# under the License.
-module Deltacloud; end
-module CIMI; end
-
require_relative '../deltacloud/drivers/features'
-# Declare namespace for CIMI models
-#
-
module CIMI
module Model; end
-
class FakeCollection
extend Sinatra::Rabbit::Features
include Deltacloud::Features
end
end
+# Declare namespace for CIMI models
+#
+
require_relative '../deltacloud/drivers'
require_relative '../deltacloud/models'
require_relative '../deltacloud/helpers/driver_helper'
@@ -38,76 +34,3 @@ require_relative '../deltacloud/helpers/url_helper'
require_relative '../deltacloud/helpers/deltacloud_helper'
require_relative '../deltacloud/helpers/rabbit_helper'
require_relative './helpers/cimi_helper'
-require_relative './models'
-
-module CIMI::Collections
- class Base < Sinatra::Base
-
- include Sinatra::Rabbit
- extend Deltacloud::Helpers::Drivers
- include Sinatra::Rabbit::Features
- include CIMI::Model
-
- helpers Deltacloud::Helpers::Drivers
- helpers Sinatra::AuthHelper
- helpers Sinatra::UrlForHelper
- helpers Deltacloud::Helpers::Application
- helpers CIMIHelper
-
- register Rack::RespondTo
-
- enable :xhtml
- enable :dump_errors
- enable :show_errors
- disable :show_exceptions
-
- set :config, Deltacloud[:cimi]
- set :root_url, config.root_url
- set :root_path, config.root_url
- set :version, config.version
- set :root, File.join(File.dirname(__FILE__), '..', '..')
- set :views, root + '/views/cimi'
- set :public_folder, root + '/public'
-
- error do
- report_error
- end
-
- error Deltacloud::ExceptionHandler::ValidationFailure do
- report_error
- end
-
- before do
- # Respond with 400, If we don't get a http Host header,
- halt 400, "Unable to find HTTP Host header" if @env['HTTP_HOST'] == nil
- end
-
- after do
- headers 'CIMI-Specification-Version' => Deltacloud[:cimi].version
- end
-
- def self.new_route_for(route, &block)
- get route_for('/' + route.to_s + '/new') do
- instance_eval(&block) if block_given?
- respond_to do |format|
- format.html do
- haml :"#{route}/new"
- end
- end
- end
- end
-
- def self.check_capability(opts={})
- Sinatra::Rabbit.set :check_capability, opts[:for]
- end
-
- def self.check_features(opts={})
- Sinatra::Rabbit.set :check_features, opts[:for]
- end
-
- def self.route_for(url)
- "#{settings.root_url}#{url}"
- end
-
- end
-end
diff --git a/server/lib/cimi/helpers/cimi_helper.rb b/server/lib/cimi/helpers/cimi_helper.rb
index be64dbd..ea1e89e 100644
--- a/server/lib/cimi/helpers/cimi_helper.rb
+++ b/server/lib/cimi/helpers/cimi_helper.rb
@@ -13,29 +13,31 @@
# License for the specific language governing permissions and limitations
# under the License.
-module CIMIHelper
+module CIMI
+ module Helper
- def no_content_with_status(code=200)
- body ''
- status code
- end
+ def no_content_with_status(code=200)
+ body ''
+ status code
+ end
- def href_id(href, entity)
- split_on = self.send(:"#{entity.to_s}_url")
- href.split("#{split_on}/").last
- end
+ def href_id(href, entity)
+ split_on = self.send(:"#{entity.to_s}_url")
+ href.split("#{split_on}/").last
+ end
- def to_kibibyte(value, unit)
- case unit
+ def to_kibibyte(value, unit)
+ case unit
when "GB"
value*1024*1024
when "MB"
value*1024
else
nil # should probably be exploding something here...
+ end
end
- end
+ end
end
class Array
@@ -61,4 +63,3 @@ class Array
end
end
-
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index 88fc5d0..dc7c1ba 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -37,7 +37,7 @@ module CIMI
use Rack::MatrixParams
use Rack::DriverSelect
- helpers CIMIHelper
+ helpers CIMI::Helper
include Deltacloud::Helpers
include CIMI::Collections
diff --git a/server/lib/deltacloud/collections.rb b/server/lib/deltacloud/collections.rb
index 2363887..7c18e09 100644
--- a/server/lib/deltacloud/collections.rb
+++ b/server/lib/deltacloud/collections.rb
@@ -14,6 +14,8 @@
# License for the specific language governing permissions and limitations
# under the License.
+require_relative './collections/base'
+
module Deltacloud
def self.collection_names
@@ -35,8 +37,9 @@ module Deltacloud
end
Dir[File.join(File::dirname(__FILE__), "collections", "*.rb")].each do |collection|
- require collection
base_collection_name = File.basename(collection).gsub('.rb', '')
+ next if base_collection_name == 'base'
+ require collection
deltacloud_module_class = Deltacloud::Collections.const_get(base_collection_name.camelize)
deltacloud_modules << deltacloud_module_class
deltacloud_module_class.collections.each do |c|
diff --git a/server/lib/deltacloud/collections/base.rb b/server/lib/deltacloud/collections/base.rb
new file mode 100644
index 0000000..d64c265
--- /dev/null
+++ b/server/lib/deltacloud/collections/base.rb
@@ -0,0 +1,65 @@
+# 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.
+
+module Deltacloud::Collections
+ class Base < Sinatra::Base
+
+ include Sinatra::Rabbit
+ include Sinatra::Rabbit::Features
+
+ helpers Deltacloud::Helpers::Drivers
+ helpers Sinatra::AuthHelper
+ helpers Sinatra::UrlForHelper
+ helpers Deltacloud::Helpers::Application
+
+ register Rack::RespondTo
+
+ enable :xhtml
+ enable :dump_errors
+ enable :show_errors
+ enable :method_override
+ disable :show_exceptions
+
+ set :config, Deltacloud[:deltacloud]
+ set :root_url, config.root_url
+ set :root_path, config.root_url
+ set :version, config.version
+ set :root, File.join(File.dirname(__FILE__), '..', '..', '..')
+ set :views, File.join(settings.root, 'views')
+ set :public_folder, root + '/public'
+
+ error do
+ report_error
+ end
+
+ error Deltacloud::ExceptionHandler::ValidationFailure do
+ report_error
+ end
+
+ before do
+ # Respond with 400, If we don't get a http Host header,
+ halt 400, "Unable to find HTTP Host header" if @env['HTTP_HOST'] == nil
+ end
+
+ after do
+ headers 'Server' => 'Apache-Deltacloud/' + settings.version
+ headers 'X-Deltacloud-Driver' => driver_name
+ if provider_name
+ headers 'X-Deltacloud-Provider' => provider_name
+ end
+ end
+
+ end
+end
diff --git a/server/lib/deltacloud/collections/drivers.rb b/server/lib/deltacloud/collections/drivers.rb
index 41f324a..9753c36 100644
--- a/server/lib/deltacloud/collections/drivers.rb
+++ b/server/lib/deltacloud/collections/drivers.rb
@@ -25,7 +25,7 @@ module Deltacloud::Collections
format.xml { haml :"drivers/index" }
format.json { @drivers.to_json }
format.html { haml :"drivers/index" }
- end
+ end
end
end
diff --git a/server/lib/deltacloud/helpers.rb b/server/lib/deltacloud/helpers.rb
index c065e64..a97161c 100644
--- a/server/lib/deltacloud/helpers.rb
+++ b/server/lib/deltacloud/helpers.rb
@@ -19,70 +19,3 @@ require_relative 'helpers/url_helper'
require_relative 'helpers/deltacloud_helper'
require_relative 'helpers/rabbit_helper'
require_relative 'helpers/blob_stream_helper'
-
-module Deltacloud::Collections
- class Base < Sinatra::Base
-
- include Sinatra::Rabbit
- extend Deltacloud::Helpers::Drivers
- include Sinatra::Rabbit::Features
-
- helpers Deltacloud::Helpers::Drivers
- helpers Sinatra::AuthHelper
- helpers Sinatra::UrlForHelper
- helpers Deltacloud::Helpers::Application
-
- register Rack::RespondTo
-
- enable :xhtml
- enable :dump_errors
- enable :show_errors
- enable :method_override
- disable :show_exceptions
-
- set :config, Deltacloud[:deltacloud]
- set :root_url, config.root_url
- set :root_path, config.root_url
- set :version, config.version
- set :root, File.join(File.dirname(__FILE__), '..', '..')
- set :views, root + '/views'
- set :public_folder, root + '/public'
-
- error do
- report_error
- end
-
- error Deltacloud::ExceptionHandler::ValidationFailure do
- report_error
- end
-
- before do
- # Respond with 400, If we don't get a http Host header,
- halt 400, "Unable to find HTTP Host header" if @env['HTTP_HOST'] == nil
- end
-
- after do
- headers 'Server' => 'Apache-Deltacloud/' + settings.version
- headers 'X-Deltacloud-Driver' => driver_name
- if provider_name
- headers 'X-Deltacloud-Provider' => provider_name
- end
- end
-
- def self.new_route_for(route, &block)
- get '/' + route.to_s + '/new' do
- instance_eval(&block) if block_given?
- respond_to do |format|
- format.html do
- haml :"#{route}/new"
- end
- end
- end
- end
-
- def self.check_features(opts={})
- Sinatra::Rabbit.set :check_features, opts[:for]
- end
-
- end
-end
diff --git a/server/lib/deltacloud/helpers/deltacloud_helper.rb b/server/lib/deltacloud/helpers/deltacloud_helper.rb
index c31f89d..33fbe23 100644
--- a/server/lib/deltacloud/helpers/deltacloud_helper.rb
+++ b/server/lib/deltacloud/helpers/deltacloud_helper.rb
@@ -286,6 +286,29 @@ module Deltacloud::Helpers
not features_arr.empty?
end
+ module SinatraHelper
+
+ def new_route_for(route, &block)
+ get '/%s/new' % route.to_s do
+ instance_eval(&block) if block_given?
+ respond_to do |format|
+ format.html do
+ haml :"#{route}/new"
+ end
+ end
+ end
+ end
+
+ def check_features(opts={})
+ Sinatra::Rabbit.set :check_features, opts[:for]
+ end
+
+ end
+
+ def Application.included(klass)
+ klass.extend SinatraHelper
+ end
+
private
def hardware_property_unit(prop)
u = ::Deltacloud::HardwareProfile::unit(prop)
diff --git a/server/lib/deltacloud/helpers/driver_helper.rb b/server/lib/deltacloud/helpers/driver_helper.rb
index 009effd..1c52894 100644
--- a/server/lib/deltacloud/helpers/driver_helper.rb
+++ b/server/lib/deltacloud/helpers/driver_helper.rb
@@ -17,6 +17,13 @@ module Deltacloud::Helpers
module Drivers
+ # This will make the Driver helpers, like 'driver'
+ # accessible on class level
+ #
+ def Drivers.included(klass)
+ klass.extend Deltacloud::Helpers::Drivers
+ end
+
def driver_symbol
driver_name.to_sym
end
--
1.7.12
[PATCH core 2/3] Core: Improved entity_url helper generation
Posted by mf...@redhat.com.
From: Michal Fojtik <mf...@redhat.com>
* The 'entity_url' helpers are now generated from the
Rabbit collections directory, instead of guessing them
using method_missing.
* rake cimi:routes added to list all CIMI routes
* rake routes now print URL helper instead of description
Signed-off-by: Michal fojtik <mf...@redhat.com>
---
server/Rakefile | 52 ++++++-----
server/lib/cimi/collections/base.rb | 2 +-
server/lib/cimi/helpers.rb | 1 -
server/lib/cimi/models/cloud_entry_point.rb | 2 +-
server/lib/cimi/server.rb | 2 +
server/lib/deltacloud/collections.rb | 4 +-
server/lib/deltacloud/collections/base.rb | 3 +-
server/lib/deltacloud/core_ext/hash.rb | 8 ++
server/lib/deltacloud/core_ext/string.rb | 5 ++
server/lib/deltacloud/helpers.rb | 1 -
server/lib/deltacloud/helpers/rabbit_helper.rb | 47 ++++++++++
server/lib/deltacloud/helpers/url_helper.rb | 115 -------------------------
server/lib/deltacloud/server.rb | 2 +
13 files changed, 102 insertions(+), 142 deletions(-)
delete mode 100644 server/lib/deltacloud/helpers/url_helper.rb
diff --git a/server/Rakefile b/server/Rakefile
index 80d45d3..34c126e 100644
--- a/server/Rakefile
+++ b/server/Rakefile
@@ -86,36 +86,48 @@ namespace :mock do
end
desc "List the routes defined by Rabbit"
-task :routes do
- load File.join(File.dirname(__FILE__), 'config.ru')
- Deltacloud.collections.each do |c|
- puts "\033[1;32;m#{c.name}\33[0m"
- c.operations.each do |o|
- puts "\033[1;37m%6s\033[0m :%-10s %-30s (%s)" % [
- o.http_method.to_s.upcase,
- o.operation_name,
- o.full_path,
- o.description[0..100]
- ]
- end
- unless c.collections.empty?
- puts
- c.collections.each do |s|
- puts "\033[1;32;m#{s.name}\33[0m"
- s.operations.each do |o|
- puts "\033[1;37m%6s\033[0m :%-10s %-30s (%s)" % [
+[:cimi, :deltacloud].each do |frontend|
+ namespace frontend do
+ desc "Print all routes defined for #{frontend.to_s.capitalize}"
+ task :routes do
+ ENV['API_FRONTEND'] = frontend.to_s
+ load File.join(File.dirname(__FILE__), 'config.ru')
+ f_class = (frontend == :cimi) ? CIMI : Deltacloud
+ f_class.collections.each do |c|
+ puts "\033[1;32;m#{c.name}\33[0m"
+ c.operations.each do |o|
+ puts "\033[1;37m%6s\033[0m :%-10s %-35s (%s)" % [
o.http_method.to_s.upcase,
o.operation_name,
o.full_path,
- o.description[0..100]
+ Sinatra::Rabbit.generate_url_helper_for(c, o)[1]
]
end
+ unless c.collections.empty?
+ puts
+ c.collections.each do |s|
+ puts "\033[1;32;m#{s.name}\33[0m"
+ s.operations.each do |o|
+ puts "\033[1;37m%6s\033[0m :%-10s %-35s (%s)" % [
+ o.http_method.to_s.upcase,
+ o.operation_name,
+ o.full_path,
+ o.description[0..100]
+ ]
+ end
+ end
+ end
+ puts
end
end
- puts
end
end
+desc 'List Deltacloud routes'
+task :routes do
+ Rake::Task['deltacloud:routes'].invoke
+end
+
DRIVERS = [:mock, :ec2, :rhevm, :google, :gogrid, :openstack]
desc 'Run all tests'
diff --git a/server/lib/cimi/collections/base.rb b/server/lib/cimi/collections/base.rb
index 7adb58d..00620fb 100644
--- a/server/lib/cimi/collections/base.rb
+++ b/server/lib/cimi/collections/base.rb
@@ -24,7 +24,7 @@ module CIMI::Collections
helpers Deltacloud::Helpers::Drivers
helpers Sinatra::AuthHelper
- helpers Sinatra::UrlForHelper
+ helpers Sinatra::Rabbit::URLHelper
helpers Deltacloud::Helpers::Application
helpers CIMI::Helper
diff --git a/server/lib/cimi/helpers.rb b/server/lib/cimi/helpers.rb
index 6513ed9..9d069b1 100644
--- a/server/lib/cimi/helpers.rb
+++ b/server/lib/cimi/helpers.rb
@@ -30,7 +30,6 @@ require_relative '../deltacloud/drivers'
require_relative '../deltacloud/models'
require_relative '../deltacloud/helpers/driver_helper'
require_relative '../deltacloud/helpers/auth_helper'
-require_relative '../deltacloud/helpers/url_helper'
require_relative '../deltacloud/helpers/deltacloud_helper'
require_relative '../deltacloud/helpers/rabbit_helper'
require_relative './helpers/cimi_helper'
diff --git a/server/lib/cimi/models/cloud_entry_point.rb b/server/lib/cimi/models/cloud_entry_point.rb
index 52997e7..f623798 100644
--- a/server/lib/cimi/models/cloud_entry_point.rb
+++ b/server/lib/cimi/models/cloud_entry_point.rb
@@ -26,7 +26,7 @@ class CIMI::Model::CloudEntryPoint < CIMI::Model::Base
:name => context.driver.name,
:description => "Cloud Entry Point for the Deltacloud #{context.driver.name} driver",
:id => context.cloudEntryPoint_url,
- :base_uri => context.root_url,
+ :base_uri => context.base_uri,
:created => Time.now,
:entity_metadata => CIMI::Model::EntityMetadata.all_uri(context)
}))
diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb
index dc7c1ba..86aaf3c 100644
--- a/server/lib/cimi/server.rb
+++ b/server/lib/cimi/server.rb
@@ -43,6 +43,8 @@ module CIMI
include CIMI::Collections
include CIMI::Model
+ helpers Sinatra::Rabbit::URLFor(CIMI.collections)
+
get '/' do
redirect url('/cloudEntryPoint'), 301
end
diff --git a/server/lib/deltacloud/collections.rb b/server/lib/deltacloud/collections.rb
index 7c18e09..df42cf1 100644
--- a/server/lib/deltacloud/collections.rb
+++ b/server/lib/deltacloud/collections.rb
@@ -49,7 +49,9 @@ module Deltacloud
def self.included(klass)
klass.class_eval do
- Deltacloud::Collections.deltacloud_modules.each { |c| use c }
+ Deltacloud::Collections.deltacloud_modules.each do |collection_class|
+ use collection_class
+ end
end
end
diff --git a/server/lib/deltacloud/collections/base.rb b/server/lib/deltacloud/collections/base.rb
index d64c265..1cbeb70 100644
--- a/server/lib/deltacloud/collections/base.rb
+++ b/server/lib/deltacloud/collections/base.rb
@@ -21,9 +21,8 @@ module Deltacloud::Collections
helpers Deltacloud::Helpers::Drivers
helpers Sinatra::AuthHelper
- helpers Sinatra::UrlForHelper
helpers Deltacloud::Helpers::Application
-
+ helpers Sinatra::Rabbit::URLHelper
register Rack::RespondTo
enable :xhtml
diff --git a/server/lib/deltacloud/core_ext/hash.rb b/server/lib/deltacloud/core_ext/hash.rb
index 52c6e89..c3f1a2f 100644
--- a/server/lib/deltacloud/core_ext/hash.rb
+++ b/server/lib/deltacloud/core_ext/hash.rb
@@ -35,4 +35,12 @@ class Hash
self
end
+ def to_query_params
+ return '' if empty?
+ '?' + self.map do |k,v|
+ next if k.nil?
+ "#{k}=#{URI.escape(v.to_s.strip, /[^#{URI::PATTERN::UNRESERVED}]/)}"
+ end.compact.join('&')
+ end
+
end
diff --git a/server/lib/deltacloud/core_ext/string.rb b/server/lib/deltacloud/core_ext/string.rb
index 483fe33..6838d6d 100644
--- a/server/lib/deltacloud/core_ext/string.rb
+++ b/server/lib/deltacloud/core_ext/string.rb
@@ -77,6 +77,11 @@ class String
self.gsub(/;([^\/]*)/, '').gsub(/\?(.*)$/, '')
end
+ def convert_query_params(params={})
+ gsub(/:(\w+)/) { |p| params.delete(p[1..-1].to_sym) } +
+ params.to_query_params
+ end
+
unless "".respond_to? :each
alias :each :each_line
end
diff --git a/server/lib/deltacloud/helpers.rb b/server/lib/deltacloud/helpers.rb
index a97161c..d7680c1 100644
--- a/server/lib/deltacloud/helpers.rb
+++ b/server/lib/deltacloud/helpers.rb
@@ -15,7 +15,6 @@
require_relative 'helpers/driver_helper'
require_relative 'helpers/auth_helper'
-require_relative 'helpers/url_helper'
require_relative 'helpers/deltacloud_helper'
require_relative 'helpers/rabbit_helper'
require_relative 'helpers/blob_stream_helper'
diff --git a/server/lib/deltacloud/helpers/rabbit_helper.rb b/server/lib/deltacloud/helpers/rabbit_helper.rb
index d6a472a..ef8080f 100644
--- a/server/lib/deltacloud/helpers/rabbit_helper.rb
+++ b/server/lib/deltacloud/helpers/rabbit_helper.rb
@@ -32,3 +32,50 @@ Sinatra::Rabbit::Collection.class_eval do
end
+module Sinatra::Rabbit
+
+ module URLHelper
+ def url_for(path); url(path); end
+ def root_url; settings.root_url; end
+ def base_uri; url_for('/').gsub(/\/$/,''); end
+ end
+
+ def self.URLFor(collections)
+ collections.each do |c|
+ c.operations.each do |operation|
+ URLHelper.instance_eval(&generate_url_helper_for(c, operation)[0])
+ end
+ end
+ URLHelper
+ end
+
+ def self.generate_url_helper_for(collection, operation)
+ operation_name = operation.operation_name.to_s
+ collection_name = collection.collection_name.to_s
+
+ # Construct OPERATION_COLLECTION_URL helper
+ # The :index and :create operation does not get any prefix
+ #
+ helper_method_name = case operation_name
+ when 'index' then collection_name
+ when 'show' then collection_name.singularize
+ else operation_name + '_' + collection_name.singularize
+ end
+
+ helper_method_name += '_url'
+ [Proc.new do
+ define_method helper_method_name do |*args|
+ if (opts = args.first).kind_of? Hash
+ path = operation.full_path.convert_query_params(opts)
+ elsif !args.empty? and (obj_id = args.first)
+ path = operation.full_path.convert_query_params(:id => obj_id)
+ else
+ path = operation.full_path
+ end
+ path.slice!(root_url)
+ url(path)
+ end unless respond_to?(helper_method_name)
+ end, helper_method_name]
+ end
+
+end
diff --git a/server/lib/deltacloud/helpers/url_helper.rb b/server/lib/deltacloud/helpers/url_helper.rb
deleted file mode 100644
index 1b084d4..0000000
--- a/server/lib/deltacloud/helpers/url_helper.rb
+++ /dev/null
@@ -1,115 +0,0 @@
-#
-# Based on https://github.com/emk/sinatra-url-for/
-# Commit 1df339284203f8f6ed8d
-#
-# Original license:
-# Copyright (C) 2009 Eric Kidd
-#
-# Permission is hereby granted, free of charge, to any person obtaining a
-# copy of this software and associated documentation files (the
-# "Software"), to deal in the Software without restriction, including
-# without limitation the rights to use, copy, modify, merge, publish,
-# distribute, sublicense, and/or sell copies of the Software, and to permit
-# persons to whom the Software is furnished to do so, subject to the
-# following conditions:
-#
-# The above copyright notice and this permission notice shall be included
-# in all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
-# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
-# MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
-# NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
-# DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
-# OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
-# USE OR OTHER DEALINGS IN THE SOFTWARE.
-
-module Sinatra
- module UrlForHelper
-
- require 'uri'
-
- def method_missing(name, *args)
- if name.to_s =~ /^([\w\_]+)_url$/
- if args.size > 0
- t = $1
- if t.match(/^(stop|reboot|start|attach|detach)_/)
- action = $1
- api_url_for(t.pluralize.split('_').last + '/' + args.first.to_s + '/' + action, :full)
- elsif t.match(/^(destroy|update)_/)
- api_url_for(t.pluralize.split('_').last + '/' + args.first.to_s, :full)
- else
- api_url_for(t.pluralize, :full) + '/' + "#{args.first}"
- end
- else
- api_url_for($1, :full)
- end
- else
- super
- end
- end
-
- def api_url_for(url_fragment, mode=:path_only)
- matrix_params = ''
- if request.params['api']
- matrix_params += ";provider=%s" % request.params['api']['provider'] if request.params['api']['provider']
- matrix_params += ";driver=%s" % request.params['api']['driver'] if request.params['api']['driver']
- end
- url_fragment = "/#{url_fragment}" unless url_fragment =~ /^\// # There is no need to prefix URI with '/'
- if mode == :path_only
- url_for "#{settings.root_url}#{matrix_params}#{url_fragment}", mode
- else
- url_for "#{matrix_params}#{url_fragment}", :full
- end
- end
-
- # Construct a link to +url_fragment+, which should be given relative to
- # the base of this Sinatra app. The mode should be either
- # <code>:path_only</code>, which will generate an absolute path within
- # the current domain (the default), or <code>:full</code>, which will
- # include the site name and port number. (The latter is typically
- # necessary for links in RSS feeds.) Example usage:
- #
- # url_for "/" # Returns "/myapp/"
- # url_for "/foo" # Returns "/myapp/foo"
- # url_for "/foo", :full # Returns "http://example.com/myapp/foo"
- #--
- # See README.rdoc for a list of some of the people who helped me clean
- # up earlier versions of this code.
- def url_for url_fragment, mode=:path_only
- case mode
- when :path_only
- base = request.script_name.empty? ? Deltacloud.default_frontend.root_url : request.script_name
- when :full
- scheme = request.scheme
- port = request.port
- request_host = request.host
- if request.env['HTTP_X_FORWARDED_FOR']
- scheme = request.env['HTTP_X_FORWARDED_SCHEME'] || scheme
- port = request.env['HTTP_X_FORWARDED_PORT']
- request_host = request.env['HTTP_X_FORWARDED_HOST']
- end
- if (port.nil? || port == "" ||
- (scheme == 'http' && port.to_s == '80') ||
- (scheme == 'https' && port.to_s == '443'))
- port = ""
- else
- port = ":#{port}"
- end
- base = "#{scheme}://#{request_host}#{port}#{request.script_name.empty? ? settings.config.root_url : request.script_name}"
- else
- raise TypeError, "Unknown url_for mode #{mode}"
- end
- uri_parser = URI.const_defined?(:Parser) ? URI::Parser.new : URI
- url_escape = uri_parser.escape(url_fragment)
- # Don't add the base fragment if url_for gets called more than once
- # per url or the url_fragment passed in is an absolute url
- if url_escape.match(/^#{base}/) or url_escape.match(/^http/)
- url_escape
- else
- "#{base}#{url_escape}"
- end
- end
- end
-
-end
diff --git a/server/lib/deltacloud/server.rb b/server/lib/deltacloud/server.rb
index c7c6c41..089fdfe 100644
--- a/server/lib/deltacloud/server.rb
+++ b/server/lib/deltacloud/server.rb
@@ -41,6 +41,8 @@ module Deltacloud
include Deltacloud::Helpers
include Deltacloud::Collections
+ helpers Sinatra::Rabbit::URLFor(Deltacloud.collections)
+
set :config, Deltacloud[:deltacloud]
get '/' do
--
1.7.12