From: Michal Fojtik <[email protected]> * Moved '[CIMI|Deltacloud]::Base' class to base_collection.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 <[email protected]> --- server/lib/cimi/collections.rb | 5 +- server/lib/cimi/collections/base_collection.rb | 65 +++++++++++++++ .../lib/cimi/collections/machine_configurations.rb | 2 +- server/lib/cimi/helpers.rb | 86 +------------------- server/lib/cimi/helpers/cimi_helper.rb | 27 +++--- server/lib/cimi/server.rb | 2 +- server/lib/deltacloud/collections.rb | 5 +- .../lib/deltacloud/collections/base_collection.rb | 68 ++++++++++++++++ server/lib/deltacloud/helpers.rb | 74 ----------------- server/lib/deltacloud/helpers/deltacloud_helper.rb | 27 ++++++ server/lib/deltacloud/helpers/driver_helper.rb | 7 ++ 11 files changed, 194 insertions(+), 174 deletions(-) create mode 100644 server/lib/cimi/collections/base_collection.rb create mode 100644 server/lib/deltacloud/collections/base_collection.rb diff --git a/server/lib/cimi/collections.rb b/server/lib/cimi/collections.rb index b675fc4..198eb6c 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_collection' + 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_collection' + 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_collection.rb b/server/lib/cimi/collections/base_collection.rb new file mode 100644 index 0000000..1aceb13 --- /dev/null +++ b/server/lib/cimi/collections/base_collection.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 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 Rack::RespondTo::Helpers + 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' + + Sinatra::Rabbit.set :root_path, "#{config.root_url}/" + + 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 2dba3be..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,79 +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 Rack::RespondTo::Helpers - 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' - - Sinatra::Rabbit.set :root_path, "#{config.root_url}/" - - 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 0c78a2a..eb2753a 100644 --- a/server/lib/cimi/server.rb +++ b/server/lib/cimi/server.rb @@ -39,7 +39,7 @@ module CIMI use Rack::Accept use Rack::MediaType - 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..c92d5af 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_collection' + 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_collection' + 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_collection.rb b/server/lib/deltacloud/collections/base_collection.rb new file mode 100644 index 0000000..4f533ad --- /dev/null +++ b/server/lib/deltacloud/collections/base_collection.rb @@ -0,0 +1,68 @@ +# 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 Rack::RespondTo::Helpers + 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' + + Sinatra::Rabbit.set :root_path, "#{config.root_url}/" + + 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/helpers.rb b/server/lib/deltacloud/helpers.rb index 6566c59..a97161c 100644 --- a/server/lib/deltacloud/helpers.rb +++ b/server/lib/deltacloud/helpers.rb @@ -19,77 +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 Rack::RespondTo::Helpers - 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' - - Sinatra::Rabbit.set :root_path, "#{config.root_url}/" - - 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_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_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/deltacloud/helpers/deltacloud_helper.rb b/server/lib/deltacloud/helpers/deltacloud_helper.rb index c31f89d..46c8e2d 100644 --- a/server/lib/deltacloud/helpers/deltacloud_helper.rb +++ b/server/lib/deltacloud/helpers/deltacloud_helper.rb @@ -286,6 +286,33 @@ module Deltacloud::Helpers not features_arr.empty? end + module SinatraHelper + + def 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 route_for(url) + "#{settings.root_url}#{url}" + 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.10.2
