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:28 UTC
[PATCH core 1/3] Core: Code layout fixes and avoiding duplication
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