module Fog::OpenStack::Core

Attributes

auth_token[RW]
auth_token_expiration[R]
current_tenant[R]
current_user[R]
current_user_id[R]
openstack_cache_ttl[R]
openstack_domain_id[R]
openstack_domain_name[R]
openstack_identity_prefix[R]
openstack_project_domain[R]
openstack_project_domain_id[R]
openstack_project_id[R]
openstack_user_domain[R]
openstack_user_domain_id[R]
unscoped_token[R]

Public Class Methods

not_found_class() click to toggle source

fallback

# File lib/fog/openstack/core.rb, line 21
def self.not_found_class
  Fog::Compute::OpenStack::NotFound
end

Public Instance Methods

credentials() click to toggle source
# File lib/fog/openstack/core.rb, line 58
def credentials
  options = {
    :provider                    => 'openstack',
    :openstack_auth_url          => @openstack_auth_uri.to_s,
    :openstack_auth_token        => @auth_token,
    :openstack_identity_endpoint => @openstack_identity_public_endpoint,
    :current_user                => @current_user,
    :current_user_id             => @current_user_id,
    :current_tenant              => @current_tenant,
    :unscoped_token              => @unscoped_token
  }
  openstack_options.merge options
end
initialize_identity(options) click to toggle source
# File lib/fog/openstack/core.rb, line 25
def initialize_identity(options)
  # Create @openstack_* instance variables from all :openstack_* options
  options.select { |x| x.to_s.start_with? 'openstack' }.each do |openstack_param, value|
    instance_variable_set "@#{openstack_param}".to_sym, value
  end

  @auth_token ||= options[:openstack_auth_token]
  @openstack_identity_public_endpoint = options[:openstack_identity_endpoint]

  @openstack_auth_uri = URI.parse(options[:openstack_auth_url])
  @openstack_must_reauthenticate = false
  @openstack_endpoint_type = options[:openstack_endpoint_type] || 'publicURL'

  @openstack_cache_ttl = options[:openstack_cache_ttl] || 0

  if @auth_token
    @openstack_can_reauthenticate = false
  else
    missing_credentials = []

    missing_credentials << :openstack_api_key unless @openstack_api_key
    unless @openstack_username || @openstack_userid
      missing_credentials << 'openstack_username or openstack_userid'
    end
    raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
    @openstack_can_reauthenticate = true
  end

  @current_user = options[:current_user]
  @current_user_id = options[:current_user_id]
  @current_tenant = options[:current_tenant]
end
reload() click to toggle source
# File lib/fog/openstack/core.rb, line 72
def reload
  @connection.reset
end

Private Instance Methods

authenticate() click to toggle source
# File lib/fog/openstack/core.rb, line 132
def authenticate
  if !@openstack_management_url || @openstack_must_reauthenticate

    options = openstack_options

    options[:openstack_auth_token] = @openstack_must_reauthenticate ? nil : @openstack_auth_token

    credentials = Fog::OpenStack.authenticate(options, @connection_options)

    @current_user = credentials[:user]
    @current_user_id = credentials[:current_user_id]
    @current_tenant = credentials[:tenant]

    @openstack_must_reauthenticate = false
    @auth_token = credentials[:token]
    @openstack_management_url = credentials[:server_management_url]
    @unscoped_token = credentials[:unscoped_token]
  else
    @auth_token = @openstack_auth_token
  end
  @openstack_management_uri = URI.parse(@openstack_management_url)

  @host   = @openstack_management_uri.host
  @path   = @openstack_management_uri.path
  @path.sub!(%r{/$}, '')
  @port   = @openstack_management_uri.port
  @scheme = @openstack_management_uri.scheme

  # Not all implementations have identity service in the catalog
  if @openstack_identity_public_endpoint || @openstack_management_url
    @identity_connection = Fog::Core::Connection.new(
      @openstack_identity_public_endpoint || @openstack_management_url,
      false, @connection_options
    )
  end

  true
end
openstack_options() click to toggle source
# File lib/fog/openstack/core.rb, line 122
def openstack_options
  options = {}
  # Create a hash of (:openstack_*, value) of all the @openstack_* instance variables
  instance_variables.select { |x| x.to_s.start_with? '@openstack' }.each do |openstack_param|
    option_name = openstack_param.to_s[1..-1]
    options[option_name.to_sym] = instance_variable_get openstack_param
  end
  options
end
request(params, parse_json = true) click to toggle source
# File lib/fog/openstack/core.rb, line 78
def request(params, parse_json = true)
  retried = false
  begin
    response = @connection.request(params.merge(
                                     :headers => {
                                       'Content-Type' => 'application/json',
                                       'Accept'       => 'application/json',
                                       'X-Auth-Token' => @auth_token
                                     }.merge!(params[:headers] || {}),
                                     :path    => "#{@path}/#{params[:path]}"
    ))
  rescue Excon::Errors::Unauthorized => error
    # token expiration and token renewal possible
    if error.response.body != 'Bad username or password' && @openstack_can_reauthenticate && !retried
      @openstack_must_reauthenticate = true
      authenticate
      set_api_path
      retried = true
      retry
    # bad credentials or token renewal not possible
    else
      raise error
    end
  rescue Excon::Errors::HTTPStatusError => error
    raise case error
          when Excon::Errors::NotFound
            self.class.not_found_class.slurp(error)
          else
            error
          end
  end

  if !response.body.empty? && response.get_header('Content-Type').match('application/json')
    # TODO: remove parse_json in favor of :raw_body
    response.body = Fog::JSON.decode(response.body) if parse_json && !params[:raw_body]
  end

  response
end
set_api_path() click to toggle source
# File lib/fog/openstack/core.rb, line 118
def set_api_path
  # if the service supports multiple versions, do the selection here
end