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 2010/08/27 14:55:33 UTC
[PATCH core 5/5] Added 'disable_if_not' method into Rabbit DSL.
---
server/lib/sinatra/rabbit.rb | 34 ++++++++++++++++++++++++++++++----
server/server.rb | 10 +++++++++-
2 files changed, 39 insertions(+), 5 deletions(-)
diff --git a/server/lib/sinatra/rabbit.rb b/server/lib/sinatra/rabbit.rb
index 91d48da..4d52f5d 100644
--- a/server/lib/sinatra/rabbit.rb
+++ b/server/lib/sinatra/rabbit.rb
@@ -25,13 +25,14 @@ module Sinatra
def initialize(coll, name, opts, &block)
@name = name.to_sym
+ @disabled = false
opts = STANDARD[@name].merge(opts) if standard?
@collection = coll
raise "No method for operation #{name}" unless opts[:method]
@method = opts[:method].to_sym
@member = opts[:member]
@description = ""
- instance_eval(&block) if block_given?
+ instance_eval(&block) if block_given? and not disabled?
generate_documentation
end
@@ -39,6 +40,10 @@ module Sinatra
STANDARD.keys.include?(name)
end
+ def disabled?
+ @disabled
+ end
+
def description(text="")
return @description if text.blank?
@description = text
@@ -63,6 +68,17 @@ module Sinatra
end
end
+ # Exclude this operation HTTP method from HEAD if current driver
+ # is not supporting given method
+ #
+ # Examples:
+ # disable_if_not :create_instance
+ # HEAD /api/instances => Allow: GET, DELETE
+
+ def disable_if_not(method)
+ @disabled = true unless driver.respond_to?(method.to_s)
+ end
+
def prefix
# FIXME: Make the /api prefix configurable
"/api"
@@ -85,6 +101,7 @@ module Sinatra
::Sinatra::Application.send(@method, path, {}, &@control)
# Set up some Rails-like URL helpers
if name == :index
+
gen_route "#{@collection.name}_url"
elsif name == :show
gen_route "#{@collection.name.to_s.singularize}_url"
@@ -117,6 +134,7 @@ module Sinatra
@name = name
@description = ""
@operations = {}
+ @methods = []
instance_eval(&block) if block_given?
generate_documentation
end
@@ -157,17 +175,25 @@ module Sinatra
# the URL to this operation (in request context)
def operation(name, opts = {}, &block)
raise DuplicateOperationException if @operations[name]
- @operations[name] = Operation.new(self, name, opts, &block)
+ o = Operation.new(self, name, opts, &block)
+ # Add operation HTTP method to collection
+ @methods << o.method.to_s.upcase unless o.disabled?
+ @operations[name] = o
end
def generate
+ collname = name # Work around Ruby's weird scoping/capture
+ methods = @methods
+ # Generate HEAD route and supported HTTP methods for current
+ # collection
+ ::Sinatra::Application.send(:head, "/api/#{collname}", {}) do
+ headers 'Allow' => 'HEAD,'+methods.uniq.join(',')
+ end
operations.values.each { |op| op.generate }
app = ::Sinatra::Application
- collname = name # Work around Ruby's weird scoping/capture
app.send(:define_method, "#{name.to_s.singularize}_url") do |id|
url_for "/api/#{collname}/#{id}", :full
end
-
if index_op = operations[:index]
app.send(:define_method, "#{name}_url") do
url_for index_op.path.gsub(/\/\?$/,''), :full
diff --git a/server/server.rb b/server/server.rb
index 130ecb1..6007959 100644
--- a/server/server.rb
+++ b/server/server.rb
@@ -2,8 +2,8 @@ require 'sinatra'
require 'deltacloud'
require 'drivers'
require 'json'
-require 'sinatra/respond_to'
require 'sinatra/static_assets'
+require 'sinatra/respond_to'
require 'sinatra/rabbit'
require 'sinatra/lazy_auth'
require 'erb'
@@ -353,6 +353,12 @@ collection :keys do
end
+get '/api/ip_addresses/new' do
+ respond_to do |format|
+ format.html { haml :"ip_addresses/new" }
+ end
+end
+
collection :ip_addresses do
description "Manage IP addresses"
@@ -374,6 +380,7 @@ collection :ip_addresses do
operation :create do
description "Create a new IP address"
param :ip, :string, :optional
+ disable_if_not :create_ip_addresss
control do
@ip_address = driver.create_ip_address(credentials, params)
show :ip_address
@@ -383,6 +390,7 @@ collection :ip_addresses do
operation :destroy do
description "Destroy IP address"
param :id, :string, :required
+ disable_if_not :destroy_ip_addresss
control do
@ip_address = driver.destroy_ip_address(credentials, params)
redirect ip_addresses_url
--
1.7.2.2