Return-Path: X-Original-To: apmail-deltacloud-dev-archive@www.apache.org Delivered-To: apmail-deltacloud-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id F2156C84D for ; Mon, 21 May 2012 10:07:51 +0000 (UTC) Received: (qmail 80372 invoked by uid 500); 21 May 2012 10:07:51 -0000 Delivered-To: apmail-deltacloud-dev-archive@deltacloud.apache.org Received: (qmail 80311 invoked by uid 500); 21 May 2012 10:07:51 -0000 Mailing-List: contact dev-help@deltacloud.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@deltacloud.apache.org Delivered-To: mailing list dev@deltacloud.apache.org Received: (qmail 80294 invoked by uid 99); 21 May 2012 10:07:51 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 May 2012 10:07:51 +0000 X-ASF-Spam-Status: No, hits=-5.0 required=5.0 tests=RCVD_IN_DNSWL_HI,SPF_HELO_PASS,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: domain of mfojtik@redhat.com designates 209.132.183.28 as permitted sender) Received: from [209.132.183.28] (HELO mx1.redhat.com) (209.132.183.28) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 21 May 2012 10:07:44 +0000 Received: from int-mx09.intmail.prod.int.phx2.redhat.com (int-mx09.intmail.prod.int.phx2.redhat.com [10.5.11.22]) by mx1.redhat.com (8.14.4/8.14.4) with ESMTP id q4LA7MYR007783 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Mon, 21 May 2012 06:07:22 -0400 Received: from dhcp-29-121.brq.redhat.com (dhcp-29-121.brq.redhat.com [10.34.29.121]) by int-mx09.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id q4LA6sK7020488 for ; Mon, 21 May 2012 06:07:22 -0400 From: mfojtik@redhat.com To: dev@deltacloud.apache.org Subject: [PATCH core 27/51] Core: Removed API_ROOT_URL and API_VERSION constants with proper configuration Date: Mon, 21 May 2012 12:07:08 +0200 Message-Id: <1337594852-42550-28-git-send-email-mfojtik@redhat.com> In-Reply-To: <1337594852-42550-1-git-send-email-mfojtik@redhat.com> References: <1337594852-42550-1-git-send-email-mfojtik@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.22 From: Michal Fojtik Signed-off-by: Michal fojtik --- server/lib/cimi/collections/cloud_entry_point.rb | 2 +- server/lib/cimi/helpers.rb | 6 +- server/lib/cimi/server.rb | 2 +- server/lib/deltacloud/helpers.rb | 4 +- server/lib/deltacloud/helpers/deltacloud_helper.rb | 2 +- server/lib/deltacloud/helpers/url_helper.rb | 4 +- server/lib/deltacloud_rack.rb | 68 ++++++++++++++++++++ server/tests/drivers/mock/api_test.rb | 48 +++++++------- server/tests/drivers/mock/buckets_test.rb | 2 +- server/tests/drivers/mock/common.rb | 24 ++----- server/tests/drivers/mock/drivers_test.rb | 2 +- .../tests/drivers/mock/hardware_profiles_test.rb | 2 +- server/tests/drivers/mock/images_test.rb | 2 +- server/tests/drivers/mock/instances_test.rb | 2 +- server/tests/drivers/mock/keys_test.rb | 2 +- server/tests/drivers/mock/realms_test.rb | 2 +- .../tests/drivers/mock/storage_snapshots_test.rb | 2 +- server/tests/drivers/mock/storage_volumes_test.rb | 2 +- 18 files changed, 118 insertions(+), 60 deletions(-) create mode 100644 server/lib/deltacloud_rack.rb diff --git a/server/lib/cimi/collections/cloud_entry_point.rb b/server/lib/cimi/collections/cloud_entry_point.rb index 79454f3..8232e61 100644 --- a/server/lib/cimi/collections/cloud_entry_point.rb +++ b/server/lib/cimi/collections/cloud_entry_point.rb @@ -23,7 +23,7 @@ module CIMI::Collections operation :index do description "list all resources of the cloud" control do - redirect API_ROOT_URL + redirect Deltacloud[:root_url] end end end diff --git a/server/lib/cimi/helpers.rb b/server/lib/cimi/helpers.rb index 4535c39..b0fc9e3 100644 --- a/server/lib/cimi/helpers.rb +++ b/server/lib/cimi/helpers.rb @@ -54,8 +54,8 @@ module CIMI::Collections enable :show_errors disable :show_exceptions - set :root_url, API_ROOT_URL - set :version, API_VERSION + set :root_url, Deltacloud[:root_url] + set :version, Deltacloud[:version] set :root, File.join(File.dirname(__FILE__), '..', '..') set :views, root + '/views/cimi' set :public_folder, root + '/public' @@ -74,7 +74,7 @@ module CIMI::Collections end after do - headers 'X-CIMI-Specification-Version' => API_VERSION + headers 'X-CIMI-Specification-Version' => Deltacloud[:version] end def self.new_route_for(route, &block) diff --git a/server/lib/cimi/server.rb b/server/lib/cimi/server.rb index a1c7ef9..c34dba9 100644 --- a/server/lib/cimi/server.rb +++ b/server/lib/cimi/server.rb @@ -45,7 +45,7 @@ module CIMI include CIMI::Collections include CIMI::Model - get API_ROOT_URL do + get Deltacloud[:root_url] do if params[:force_auth] return [401, 'Authentication failed'] unless driver.valid_credentials?(credentials) end diff --git a/server/lib/deltacloud/helpers.rb b/server/lib/deltacloud/helpers.rb index 8e265fc..b4f859f 100644 --- a/server/lib/deltacloud/helpers.rb +++ b/server/lib/deltacloud/helpers.rb @@ -45,8 +45,8 @@ module Deltacloud::Collections enable :method_override disable :show_exceptions - set :root_url, API_ROOT_URL - set :version, API_VERSION + set :root_url, Deltacloud[:root_url] + set :version, Deltacloud[:version] set :root, File.join(File.dirname(__FILE__), '..', '..') set :views, root + '/views' set :public_folder, root + '/public' diff --git a/server/lib/deltacloud/helpers/deltacloud_helper.rb b/server/lib/deltacloud/helpers/deltacloud_helper.rb index 2fc2a59..ed3e57d 100644 --- a/server/lib/deltacloud/helpers/deltacloud_helper.rb +++ b/server/lib/deltacloud/helpers/deltacloud_helper.rb @@ -20,7 +20,7 @@ module Deltacloud::Helpers def self.included(klass) klass.class_eval do - set :root_url, API_ROOT_URL + set :root_url, Deltacloud[:root_url] include Sinatra::Rabbit Sinatra::Rabbit.set :root_path, root_url+'/' end diff --git a/server/lib/deltacloud/helpers/url_helper.rb b/server/lib/deltacloud/helpers/url_helper.rb index b35c714..fd4d9bc 100644 --- a/server/lib/deltacloud/helpers/url_helper.rb +++ b/server/lib/deltacloud/helpers/url_helper.rb @@ -79,7 +79,7 @@ module Sinatra def url_for url_fragment, mode=:path_only case mode when :path_only - base = request.script_name.empty? ? API_ROOT_URL : request.script_name + base = request.script_name.empty? ? Deltacloud[:root_url] : request.script_name when :full scheme = request.scheme port = request.port @@ -96,7 +96,7 @@ module Sinatra else port = ":#{port}" end - base = "#{scheme}://#{request_host}#{port}#{request.script_name.empty? ? API_ROOT_URL : request.script_name}" + base = "#{scheme}://#{request_host}#{port}#{request.script_name.empty? ? Deltacloud[:root_url] : request.script_name}" else raise TypeError, "Unknown url_for mode #{mode}" end diff --git a/server/lib/deltacloud_rack.rb b/server/lib/deltacloud_rack.rb new file mode 100644 index 0000000..873ef29 --- /dev/null +++ b/server/lib/deltacloud_rack.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 + + def self.config(conf=nil) + @config ||= conf + end + + def self.configure(&block) + config(Server.new(&block)) + self + end + + def self.[](item) + config.send(item) + end + + def self.require_frontend! + ENV['API_FRONTEND'] ||= 'deltacloud' + require File.join(File.dirname(__FILE__), ENV['API_FRONTEND'], 'server.rb') + config.klass eval(self[:klass]) + end + + class Server + + attr_reader :root_url + attr_reader :version + attr_reader :klass + + def initialize(opts={}, &block) + @root_url = opts[:root_url] + @version = opts[:version] + @klass = opts[:klass] + instance_eval(&block) + end + + def root_url(url=nil) + return @root_url if url.nil? + raise '[Core] The server URL must start with /' unless url =~ /^\// + @root_url = url + end + + def version(version=nil) + return @version if version.nil? + @version = version + end + + def klass(k=nil) + return @klass if k.nil? + @klass = k + end + + end + +end diff --git a/server/tests/drivers/mock/api_test.rb b/server/tests/drivers/mock/api_test.rb index 3308365..cafe0be 100644 --- a/server/tests/drivers/mock/api_test.rb +++ b/server/tests/drivers/mock/api_test.rb @@ -2,37 +2,37 @@ describe 'Deltacloud API' do include Deltacloud::Test it 'return HTTP_OK when accessing API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] last_response.status.must_equal 200 end it 'advertise the current driver in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] xml_response.root[:driver].must_equal ENV['API_DRIVER'] end it 'advertise the current API version in API entrypoint' do - get API_ROOT_URL - xml_response.root[:version].must_equal API_VERSION + get Deltacloud[:root_url] + xml_response.root[:version].must_equal Deltacloud[:version] end it 'advertise the current API version in HTTP headers' do - get API_ROOT_URL - last_response.headers['Server'].must_equal "Apache-Deltacloud/#{API_VERSION}" + get Deltacloud[:root_url] + last_response.headers['Server'].must_equal "Apache-Deltacloud/#{Deltacloud[:version]}" end it 'must include the ETag in HTTP headers' do - get API_ROOT_URL + get Deltacloud[:root_url] last_response.headers['ETag'].wont_be_nil end it 'advertise collections in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link').wont_be_empty end it 'include the :href and :rel attribute for each collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link').each do |collection| collection[:href].wont_be_nil collection[:rel].wont_be_nil @@ -40,19 +40,19 @@ describe 'Deltacloud API' do end it 'uses the absolute URI in the :href attribute for each collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link').each do |collection| collection[:href].must_match /^http/ end end it 'advertise features for some collections in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link/feature').wont_be_empty end it 'advertise the name of the feature for some collections in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link/feature').each do |f| f[:name].wont_be_nil end @@ -60,55 +60,55 @@ describe 'Deltacloud API' do it 'must change the media type from XML to JSON using Accept headers' do header 'Accept', 'application/json' - get API_ROOT_URL + get Deltacloud[:root_url] last_response.headers['Content-Type'].must_equal 'application/json' end it 'must change the media type to JSON using the "?format" parameter in URL' do - get API_ROOT_URL, { :format => 'json' } + get Deltacloud[:root_url], { :format => 'json' } last_response.headers['Content-Type'].must_equal 'application/json' end it 'must change the driver when using X-Deltacloud-Driver HTTP header' do header 'X-Deltacloud-Driver', 'ec2' - get API_ROOT_URL + get Deltacloud[:root_url] xml_response.root[:driver].must_equal 'ec2' header 'X-Deltacloud-Driver', 'mock' - get API_ROOT_URL + get Deltacloud[:root_url] xml_response.root[:driver].must_equal 'mock' end it 'must change the features when driver is swapped using HTTP headers' do header 'X-Deltacloud-Driver', 'ec2' - get API_ROOT_URL + get Deltacloud[:root_url] # The 'user_name' feature is not supported currently for the EC2 driver (xml_response/'api/link/feature').map { |f| f[:name] }.wont_include 'user_name' header 'X-Deltacloud-Driver', 'mock' - get API_ROOT_URL + get Deltacloud[:root_url] # But it's supported in Mock driver (xml_response/'api/link/feature').map { |f| f[:name] }.must_include 'user_name' end it 'must re-validate the driver credentials when using "?force_auth" parameter in URL' do - get API_ROOT_URL, { :force_auth => '1' } + get Deltacloud[:root_url], { :force_auth => '1' } last_response.status.must_equal 401 auth_as_mock - get API_ROOT_URL, { :force_auth => '1' } + get Deltacloud[:root_url], { :force_auth => '1' } last_response.status.must_equal 200 end it 'must change the API PROVIDER using the /api;provider matrix parameter in URI' do - get API_ROOT_URL + ';provider=test1' + get Deltacloud[:root_url] + ';provider=test1' xml_response.root[:provider].wont_be_nil xml_response.root[:provider].must_equal 'test1' - get API_ROOT_URL + ';provider=test2' + get Deltacloud[:root_url] + ';provider=test2' xml_response.root[:provider].must_equal 'test2' end it 'must change the API DRIVER using the /api;driver matrix parameter in URI' do - get API_ROOT_URL + ';driver=ec2' + get Deltacloud[:root_url] + ';driver=ec2' xml_response.root[:driver].must_equal 'ec2' - get API_ROOT_URL + ';driver=mock' + get Deltacloud[:root_url] + ';driver=mock' xml_response.root[:driver].must_equal 'mock' end diff --git a/server/tests/drivers/mock/buckets_test.rb b/server/tests/drivers/mock/buckets_test.rb index f98ad24..91970ff 100644 --- a/server/tests/drivers/mock/buckets_test.rb +++ b/server/tests/drivers/mock/buckets_test.rb @@ -2,7 +2,7 @@ describe 'Deltacloud API buckets' do include Deltacloud::Test it 'must advertise have the buckets collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link[@rel=buckets]').wont_be_empty end diff --git a/server/tests/drivers/mock/common.rb b/server/tests/drivers/mock/common.rb index 08650a3..2b96711 100644 --- a/server/tests/drivers/mock/common.rb +++ b/server/tests/drivers/mock/common.rb @@ -1,25 +1,15 @@ -unless Kernel.respond_to?(:require_relative) - module Kernel - def require_relative(path) - require File.join(File.dirname(caller[0]), path.to_str) - end - end -end +load File.join(File.dirname(__FILE__), '..', '..', '..', 'lib', 'deltacloud_rack.rb') -API_ROOT_URL = "/api" unless defined?(API_ROOT_URL) -API_VERSION = "1.0.0" unless defined?(API_VERSION) -ENV['API_DRIVER'] ||= 'mock' - -ENV['API_USERNAME'] ||= 'mockuser' -ENV['API_PASSWORD'] ||= 'mockpassword' - -require_relative '../../../lib/deltacloud/server.rb' +Deltacloud::configure do |server| + server.root_url '/api' + server.version '0.5.0' + server.klass 'Deltacloud::API' +end.require_frontend! require 'minitest/autorun' require 'rack/test' require 'nokogiri' require 'json' - require 'pp' module Deltacloud @@ -43,7 +33,7 @@ module Deltacloud end def collection_url(collection) - [API_ROOT_URL, collection.to_s].join('/') + [Deltacloud[:root_url], collection.to_s].join('/') end def app diff --git a/server/tests/drivers/mock/drivers_test.rb b/server/tests/drivers/mock/drivers_test.rb index 41c2e66..9e5bc71 100644 --- a/server/tests/drivers/mock/drivers_test.rb +++ b/server/tests/drivers/mock/drivers_test.rb @@ -2,7 +2,7 @@ describe 'Deltacloud API drivers' do include Deltacloud::Test it 'must advertise have the drivers collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link[@rel=drivers]').wont_be_empty end diff --git a/server/tests/drivers/mock/hardware_profiles_test.rb b/server/tests/drivers/mock/hardware_profiles_test.rb index 3dad5a8..aed2a04 100644 --- a/server/tests/drivers/mock/hardware_profiles_test.rb +++ b/server/tests/drivers/mock/hardware_profiles_test.rb @@ -2,7 +2,7 @@ describe 'Deltacloud API Hardware Profiles' do include Deltacloud::Test it 'must advertise have the hardware_profiles collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link[@rel=hardware_profiles]').wont_be_empty end diff --git a/server/tests/drivers/mock/images_test.rb b/server/tests/drivers/mock/images_test.rb index 3faf752..909fdf1 100644 --- a/server/tests/drivers/mock/images_test.rb +++ b/server/tests/drivers/mock/images_test.rb @@ -2,7 +2,7 @@ describe 'Deltacloud API Images' do include Deltacloud::Test it 'must advertise have the images collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link[@rel=images]').wont_be_empty end diff --git a/server/tests/drivers/mock/instances_test.rb b/server/tests/drivers/mock/instances_test.rb index c601a6f..878589c 100644 --- a/server/tests/drivers/mock/instances_test.rb +++ b/server/tests/drivers/mock/instances_test.rb @@ -2,7 +2,7 @@ describe 'Deltacloud API instances' do include Deltacloud::Test it 'must advertise have the instances collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link[@rel=instances]').wont_be_empty end diff --git a/server/tests/drivers/mock/keys_test.rb b/server/tests/drivers/mock/keys_test.rb index 9267b5a..2738a50 100644 --- a/server/tests/drivers/mock/keys_test.rb +++ b/server/tests/drivers/mock/keys_test.rb @@ -2,7 +2,7 @@ describe 'Deltacloud API Keys' do include Deltacloud::Test it 'must advertise have the keys collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link[@rel=keys]').wont_be_empty end diff --git a/server/tests/drivers/mock/realms_test.rb b/server/tests/drivers/mock/realms_test.rb index 6bc9101..5496bc4 100644 --- a/server/tests/drivers/mock/realms_test.rb +++ b/server/tests/drivers/mock/realms_test.rb @@ -2,7 +2,7 @@ describe 'Deltacloud API Realms' do include Deltacloud::Test it 'must advertise have the realms collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link[@rel=realms]').wont_be_empty end diff --git a/server/tests/drivers/mock/storage_snapshots_test.rb b/server/tests/drivers/mock/storage_snapshots_test.rb index 52ea847..70ac2ba 100644 --- a/server/tests/drivers/mock/storage_snapshots_test.rb +++ b/server/tests/drivers/mock/storage_snapshots_test.rb @@ -2,7 +2,7 @@ describe 'Deltacloud API storage_snapshots' do include Deltacloud::Test it 'must advertise have the storage_snapshots collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link[@rel=storage_snapshots]').wont_be_empty end diff --git a/server/tests/drivers/mock/storage_volumes_test.rb b/server/tests/drivers/mock/storage_volumes_test.rb index cbafd5d..efceaee 100644 --- a/server/tests/drivers/mock/storage_volumes_test.rb +++ b/server/tests/drivers/mock/storage_volumes_test.rb @@ -2,7 +2,7 @@ describe 'Deltacloud API storage_volumes' do include Deltacloud::Test it 'must advertise have the storage_volumes collection in API entrypoint' do - get API_ROOT_URL + get Deltacloud[:root_url] (xml_response/'api/link[@rel=storage_volumes]').wont_be_empty end -- 1.7.10.1