class Fog::Rackspace::Service

Public Instance Methods

authenticate(options={}) click to toggle source
# File lib/fog/rackspace/service.rb, line 28
def authenticate(options={})
   self.send authentication_method, options
end
endpoint_uri(service_endpoint=nil, endpoint_name=nil) click to toggle source
# File lib/fog/rackspace/service.rb, line 12
def endpoint_uri(service_endpoint=nil, endpoint_name=nil)
  return @uri if @uri

  url = service_endpoint

  unless url
    if v1_authentication?
      raise "Service Endpoint must be specified via #{endpoint_name} parameter"
    else
      url = endpoint_uri_v2
    end
  end

  @uri = URI.parse url
end
region() click to toggle source
# File lib/fog/rackspace/service.rb, line 8
def region
  raise Fog::Errors::NotImplemented.new("Please implement the #region method")
end
request(params, parse_json = true) click to toggle source
# File lib/fog/rackspace/service.rb, line 39
def request(params, parse_json = true)
  first_attempt = true
  begin
    response = @connection.request(request_params(params))
  rescue Excon::Errors::Unauthorized => error
    raise error unless first_attempt
    first_attempt = false
    authenticate
    retry
  rescue Excon::Error::Socket => ees
    if ees.message && ees.message.downcase.include?('broken pipe')
      raise ees unless first_attempt
      first_attempt = false
      authenticate
      retry
    end
  end

  process_response(response) if parse_json
  response
end
request_without_retry(params, parse_json = true) click to toggle source
# File lib/fog/rackspace/service.rb, line 32
def request_without_retry(params, parse_json = true)
  response = @connection.request(request_params(params))

  process_response(response) if parse_json
  response
end
service_name() click to toggle source
# File lib/fog/rackspace/service.rb, line 4
def service_name
  raise Fog::Errors::NotImplemented.new("Please implement the #service_name method")
end
service_net?() click to toggle source
# File lib/fog/rackspace/service.rb, line 61
def service_net?
  false
end

Private Instance Methods

auth_token() click to toggle source
# File lib/fog/rackspace/service.rb, line 134
def auth_token
  @auth_token || @identity_service.auth_token
end
authenticate_v1(options) click to toggle source
# File lib/fog/rackspace/service.rb, line 126
def authenticate_v1(options)
  raise Fog::Errors::NotImplemented.new("Authentication of legacy endpoints is not implemented for this service.")
end
authenticate_v2(identity_options) click to toggle source
# File lib/fog/rackspace/service.rb, line 114
def authenticate_v2(identity_options)
  hash = {
        :rackspace_api_key => identity_options[:rackspace_api_key],
        :rackspace_username => identity_options[:rackspace_username],
        :rackspace_auth_url => identity_options[:rackspace_auth_url],
        :connection_options => identity_options[:connection_options] || {}
  }

  @identity_service = Fog::Rackspace::Identity.new(hash)
  @auth_token = @identity_service.auth_token
end
authentication_method() click to toggle source
# File lib/fog/rackspace/service.rb, line 96
def authentication_method
  if v2_authentication?
    :authenticate_v2
  else
    Fog::Logger.deprecation "Authentication using a v1.0/v1.1 endpoint is deprecated. Please specify a v2.0 endpoint using :rackspace_auth_url.\
    For a list of v2.0 endpoints refer to http://docs.rackspace.com/auth/api/v2.0/auth-client-devguide/content/Endpoints-d1e180.html"
   :authenticate_v1
  end
end
endpoint_uri_v2() click to toggle source
# File lib/fog/rackspace/service.rb, line 130
def endpoint_uri_v2
  @uri = @identity_service.service_catalog.get_endpoint(service_name, region, service_net?)
end
headers(options={}) click to toggle source
# File lib/fog/rackspace/service.rb, line 82
def headers(options={})
  { 'Content-Type' => 'application/json',
    'Accept' => 'application/json',
    'X-Auth-Token' => auth_token
  }.merge(options[:headers] || {})
end
process_response(response) click to toggle source
# File lib/fog/rackspace/service.rb, line 67
def process_response(response)
  if response &&
     response.body &&
     response.body.is_a?(String) &&
     !response.body.strip.empty? &&
     Fog::Rackspace.json_response?(response)
    begin
      response.body = Fog::JSON.decode(response.body)
    rescue Fog::JSON::DecodeError => e
      Fog::Logger.warning("Error Parsing response json - #{e}")
      response.body = {}
    end
  end
end
request_params(params) click to toggle source
# File lib/fog/rackspace/service.rb, line 89
def request_params(params)
  params.merge({
    :headers  => headers(params),
    :path     => "#{endpoint_uri.path}/#{params[:path]}"
  })
end
select_options(keys) click to toggle source
# File lib/fog/rackspace/service.rb, line 138
def select_options(keys)
  return nil unless @options && keys
  selected = {}
  keys.each do |k|
    selected[k] = @options[k]
  end

  selected
end
v1_authentication?() click to toggle source
# File lib/fog/rackspace/service.rb, line 106
def v1_authentication?
  !v2_authentication?
end
v2_authentication?() click to toggle source
# File lib/fog/rackspace/service.rb, line 110
def v2_authentication?
  @rackspace_auth_url.nil? || @rackspace_auth_url =~ /v2(\.\d)?[\w\/]*$/
end