lib/fog/rackspace/block_storage.rb in fog-maestrodev-1.8.0.20130114204828 vs lib/fog/rackspace/block_storage.rb in fog-maestrodev-1.14.0.20130806165225

- old
+ new

@@ -1,10 +1,11 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'rackspace')) module Fog module Rackspace class BlockStorage < Fog::Service + include Fog::Rackspace::Errors class IdentifierTaken < Fog::Errors::Error; end class ServiceError < Fog::Rackspace::Errors::ServiceError; end class InternalServerError < Fog::Rackspace::Errors::InternalServerError; end class BadRequest < Fog::Rackspace::Errors::BadRequest; end @@ -14,10 +15,12 @@ ORD_ENDPOINT = 'https://ord.blockstorage.api.rackspacecloud.com/v1' requires :rackspace_api_key, :rackspace_username recognizes :rackspace_auth_url recognizes :rackspace_endpoint + recognizes :rackspace_region + recognizes :rackspace_block_storage_url model_path 'fog/rackspace/models/block_storage' model :volume collection :volumes @@ -41,75 +44,135 @@ request :create_snapshot request :delete_snapshot request :get_snapshot request :list_snapshots - class Mock + class Mock < Fog::Rackspace::Service + include Fog::Rackspace::MockData + + def initialize(options = {}) + @rackspace_api_key = options[:rackspace_api_key] + end + def request(params) Fog::Mock.not_implemented end + + def response(params={}) + body = params[:body] || {} + status = params[:status] || 200 + headers = params[:headers] || {} + + response = Excon::Response.new(:body => body, :headers => headers, :status => status) + if params.has_key?(:expects) && ![*params[:expects]].include?(response.status) + raise(Excon::Errors.status_error(params, response)) + else response + end + end end - class Real + class Real < Fog::Rackspace::Service def initialize(options = {}) @rackspace_api_key = options[:rackspace_api_key] @rackspace_username = options[:rackspace_username] @rackspace_auth_url = options[:rackspace_auth_url] @rackspace_must_reauthenticate = false @connection_options = options[:connection_options] || {} + setup_custom_endpoint(options) - endpoint = options[:rackspace_endpoint] || DFW_ENDPOINT - uri = URI.parse(endpoint) + authenticate - @host = uri.host + deprecation_warnings(options) + @persistent = options[:persistent] || false - @path = uri.path - @port = uri.port - @scheme = uri.scheme + @connection = Fog::Connection.new(endpoint_uri.to_s, @persistent, @connection_options) + end - authenticate + def request(params, parse_json = true, &block) + super(params, parse_json, &block) + rescue Excon::Errors::NotFound => error + raise NotFound.slurp(error, self) + rescue Excon::Errors::BadRequest => error + raise BadRequest.slurp(error, self) + rescue Excon::Errors::InternalServerError => error + raise InternalServerError.slurp(error, self) + rescue Excon::Errors::HTTPStatusError => error + raise ServiceError.slurp(error, self) + end - @connection = Fog::Connection.new(uri.to_s, @persistent, @connection_options) + def authenticate(options={}) + super({ + :rackspace_api_key => @rackspace_api_key, + :rackspace_username => @rackspace_username, + :rackspace_auth_url => @rackspace_auth_url, + :connection_options => @connection_options + }) end - def request(params) - begin - response = @connection.request(params.merge!({ - :headers => { - 'Content-Type' => 'application/json', - 'X-Auth-Token' => @auth_token - }.merge!(params[:headers] || {}), - :host => @host, - :path => "#{@path}/#{params[:path]}" - })) - rescue Excon::Errors::NotFound => error - raise NotFound.slurp error - rescue Excon::Errors::BadRequest => error - raise BadRequest.slurp error - rescue Excon::Errors::InternalServerError => error - raise InternalServerError.slurp error - rescue Excon::Errors::HTTPStatusError => error - raise ServiceError.slurp error + def service_name + :cloudBlockStorage + end + + def region + @rackspace_region + end + + def request_id_header + "X-Compute-Request-Id" + end + + def endpoint_uri(service_endpoint_url=nil) + @uri = super(@rackspace_endpoint || service_endpoint_url, :rackspace_block_storage_url) + end + + private + + def setup_custom_endpoint(options) + @rackspace_endpoint = Fog::Rackspace.normalize_url(options[:rackspace_block_storage_url] || options[:rackspace_endpoint]) + + if v2_authentication? + case @rackspace_endpoint + when DFW_ENDPOINT + @rackspace_endpoint = nil + @rackspace_region = :dfw + when ORD_ENDPOINT + @rackspace_endpoint = nil + @rackspace_region = :ord + when LON_ENDPOINT + @rackspace_endpoint = nil + @rackspace_region = :lon + else + @rackspace_region = options[:rackspace_region] || :dfw + end + else + #if we are using auth1 and the endpoint is not set, default to DFW_ENDPOINT for historical reasons + @rackspace_endpoint ||= DFW_ENDPOINT end - unless response.body.empty? - response.body = Fog::JSON.decode(response.body) + end + + def deprecation_warnings(options) + Fog::Logger.deprecation("The :rackspace_endpoint option is deprecated. Please use :rackspace_block_storage_url for custom endpoints") if options[:rackspace_endpoint] + + if [DFW_ENDPOINT, ORD_ENDPOINT, LON_ENDPOINT].include?(@rackspace_endpoint) && v2_authentication? + regions = @identity_service.service_catalog.display_service_regions(service_name) + Fog::Logger.deprecation("Please specify region using :rackspace_region rather than :rackspace_endpoint. Valid region for :rackspace_region are #{regions}.") end - response end - private + def append_tenant_v1(credentials) + account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1] - def authenticate - options = { - :rackspace_api_key => @rackspace_api_key, - :rackspace_username => @rackspace_username, - :rackspace_auth_url => @rackspace_auth_url - } + endpoint = @rackspace_endpoint || credentials['X-Server-Management-Url'] || DFW_ENDPOINT + @uri = URI.parse(endpoint) + @uri.path = "#{@uri.path}/#{account_id}" + end + + def authenticate_v1(options) credentials = Fog::Rackspace.authenticate(options, @connection_options) + append_tenant_v1 credentials @auth_token = credentials['X-Auth-Token'] - account_id = credentials['X-Server-Management-Url'].match(/.*\/([\d]+)$/)[1] - @path = "#{@path}/#{account_id}" end + end end end end