Repository is archived
No commit activity in last 3 years
No release in over 3 years
A library for managing multiple Aviator sessions
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
~> 1.8.0
~> 0.0.0
~> 0.9.0
>= 0
~> 0.9.0
~> 2.3.0
~> 0.3.0
~> 0.8.0

Runtime

>= 0.0.6
>= 3.0.4
 Project Readme

Aviator Session Pool

Experimental library for managing Aviator sessions.

Build Status Coverage Status

require 'aviator'
require 'aviator/session_pool'

Aviator::SessionPool.configure(
  config_file: 'path/to/aviator.yml',
  environment: :production,
  log_file:    'path/to/aviator.log',
  redis_host:  'localhost', 
  redis_port:   6785
)


#==================
# LOGIN CONTROLLER
#==================

# Create an unscoped session
Aviator::SessionPool.get_or_create(session[:session_id]) do |creds|
  creds.username = username
  creds.password = password
end


# Moments pass...

# Now the user is requesting access to a specific project/tenant

#===============================
# IN A CONTROLLER BEFORE_FILTER
#===============================

# Attempt to get the unscoped session which is an indicator that
# the user has been previously authenticated.
#
# When getting a session from the pool, SessionPool calls the
# object's validate method. If that method returns false, then
# SessionPool will return nil. If there is no session with the
# given key, SessionPool will also return nil.
unless unscoped = Aviator::SessionPool.get(session[:session_id])
  # This means the user is not yet authenticated or
  # her session with OpenStack has expired. Do the ff:
  #   - Log out user
  #   - Redirect user to login page
end


#=====================================
# IN ANOTHER CONTROLLER BEFORE_FILTER
#=====================================

# Since user is asking for resources for a specific tenant, let's
# get a session scoped to that tenant.
Aviator::SessionPool.get_or_create(session[:session_id] + tenant_name.underscore) do |creds|
  creds.token_id = unscoped[:auth_info][:access][:token][:id]
  creds.tenant_name = tenant_name
end

# scoped will have to be shared between the controller and
# whichever model or object will need to use it. 
Aviator::SessionPool.set_current(session[:session_id] + tenant_name.underscore)


#=========================
# IN SOME MODEL OR OBJECT
#=========================

# Use current session like any other Aviator session. If set_current was not
# called prior to this, get_current will raise a CurrentSessionNotDefinedError
#
# WARNING: Since get_current uses a class instance variable, it will contain
# a value between http requests whether set_current was called or not for as long
# as it was called at least once.
Aviator::SessionPool.get_current.compute_service.request(:list_servers)



# Maintaining an admin session

# Authentication will use credentials in the config file since
# a block is not provided in this call.
admin = Aviator::SessionPool.get_or_create('admin')

#=========================
# IN SOME MODEL OR OBJECT
#=========================

# Use the admin session
Aviator::SessionPool.get_or_create('admin').identity_service.request(:list_tenants, endpoint_type: :admin)