2012-02-17 04:29:44 -05:00
|
|
|
require 'fog/openstack'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Identity
|
|
|
|
class OpenStack < Fog::Service
|
|
|
|
|
2012-03-04 02:42:20 -05:00
|
|
|
requires :openstack_auth_url
|
2012-02-22 01:46:56 -05:00
|
|
|
recognizes :openstack_auth_token, :openstack_management_url, :persistent,
|
2012-12-10 07:55:54 -05:00
|
|
|
:openstack_service_type, :openstack_service_name, :openstack_tenant,
|
2012-04-20 10:25:00 -04:00
|
|
|
:openstack_api_key, :openstack_username, :openstack_current_user_id,
|
2013-01-24 05:38:45 -05:00
|
|
|
:current_user, :current_tenant,
|
|
|
|
:openstack_endpoint_type
|
2012-02-17 04:29:44 -05:00
|
|
|
|
2012-02-22 01:46:56 -05:00
|
|
|
model_path 'fog/openstack/models/identity'
|
2012-02-22 09:40:32 -05:00
|
|
|
model :tenant
|
|
|
|
collection :tenants
|
|
|
|
model :user
|
|
|
|
collection :users
|
|
|
|
model :role
|
|
|
|
collection :roles
|
2012-10-29 17:48:08 -04:00
|
|
|
model :ec2_credential
|
|
|
|
collection :ec2_credentials
|
2012-02-17 04:29:44 -05:00
|
|
|
|
|
|
|
request_path 'fog/openstack/requests/identity'
|
2012-02-22 01:46:56 -05:00
|
|
|
|
2012-02-17 04:29:44 -05:00
|
|
|
request :check_token
|
2012-02-22 01:46:56 -05:00
|
|
|
request :validate_token
|
|
|
|
|
2012-02-22 09:40:32 -05:00
|
|
|
request :list_tenants
|
2012-02-26 03:09:22 -05:00
|
|
|
request :create_tenant
|
2012-02-22 09:40:32 -05:00
|
|
|
request :get_tenant
|
2012-02-17 04:29:44 -05:00
|
|
|
request :get_tenants_by_id
|
|
|
|
request :get_tenants_by_name
|
2012-02-26 03:09:22 -05:00
|
|
|
request :update_tenant
|
|
|
|
request :delete_tenant
|
2012-02-22 01:46:56 -05:00
|
|
|
|
2012-02-22 09:40:32 -05:00
|
|
|
request :list_users
|
2012-02-26 21:43:42 -05:00
|
|
|
request :create_user
|
|
|
|
request :update_user
|
|
|
|
request :delete_user
|
2012-02-17 04:29:44 -05:00
|
|
|
request :get_user_by_id
|
|
|
|
request :get_user_by_name
|
2012-03-04 08:24:28 -05:00
|
|
|
request :add_user_to_tenant
|
2012-04-02 03:26:45 -04:00
|
|
|
request :remove_user_from_tenant
|
2012-02-22 01:46:56 -05:00
|
|
|
|
2012-02-17 04:29:44 -05:00
|
|
|
request :list_endpoints_for_token
|
|
|
|
request :list_roles_for_user_on_tenant
|
|
|
|
request :list_user_global_roles
|
|
|
|
|
2012-02-24 06:38:05 -05:00
|
|
|
request :create_role
|
|
|
|
request :delete_role
|
|
|
|
request :delete_user_role
|
|
|
|
request :create_user_role
|
|
|
|
request :get_role
|
|
|
|
request :list_roles
|
|
|
|
|
2012-11-05 02:58:55 -05:00
|
|
|
request :set_tenant
|
|
|
|
|
2012-10-29 17:47:14 -04:00
|
|
|
request :create_ec2_credential
|
|
|
|
request :delete_ec2_credential
|
|
|
|
request :get_ec2_credential
|
|
|
|
request :list_ec2_credentials
|
2012-02-24 06:38:05 -05:00
|
|
|
|
2012-02-17 04:29:44 -05:00
|
|
|
class Mock
|
2012-08-14 04:32:23 -04:00
|
|
|
attr_reader :auth_token
|
2012-11-15 09:29:57 -05:00
|
|
|
attr_reader :unscoped_token
|
2012-08-14 04:32:23 -04:00
|
|
|
attr_reader :auth_token_expiration
|
|
|
|
attr_reader :current_user
|
|
|
|
attr_reader :current_tenant
|
2012-11-15 09:29:57 -05:00
|
|
|
attr_reader :unscoped_token
|
2012-08-14 04:32:23 -04:00
|
|
|
|
2012-02-22 09:40:32 -05:00
|
|
|
def self.data
|
2012-10-29 17:47:14 -04:00
|
|
|
@users ||= {}
|
|
|
|
@roles ||= {}
|
|
|
|
@tenants ||= {}
|
|
|
|
@ec2_credentials ||= Hash.new { |hash, key| hash[key] = {} }
|
2012-12-06 01:42:25 -05:00
|
|
|
@user_tenant_membership ||= {}
|
2012-09-17 04:10:36 -04:00
|
|
|
|
2012-02-22 09:40:32 -05:00
|
|
|
@data ||= Hash.new do |hash, key|
|
|
|
|
hash[key] = {
|
2012-10-29 17:47:14 -04:00
|
|
|
:users => @users,
|
|
|
|
:roles => @roles,
|
|
|
|
:tenants => @tenants,
|
|
|
|
:ec2_credentials => @ec2_credentials,
|
2012-12-06 01:42:25 -05:00
|
|
|
:user_tenant_membership => @user_tenant_membership
|
2012-02-22 09:40:32 -05:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2012-02-17 04:29:44 -05:00
|
|
|
|
2012-09-17 04:10:36 -04:00
|
|
|
def self.reset!
|
2012-10-29 17:47:14 -04:00
|
|
|
@data = nil
|
|
|
|
@users = nil
|
|
|
|
@roles = nil
|
|
|
|
@tenants = nil
|
|
|
|
@ec2_credentials = nil
|
2012-02-22 09:40:32 -05:00
|
|
|
end
|
2012-02-17 04:29:44 -05:00
|
|
|
|
2012-02-22 09:40:32 -05:00
|
|
|
def initialize(options={})
|
2012-09-17 04:10:36 -04:00
|
|
|
require 'multi_json'
|
|
|
|
@openstack_username = options[:openstack_username] || 'admin'
|
|
|
|
@openstack_tenant = options[:openstack_tenant] || 'admin'
|
2012-12-04 03:06:37 -05:00
|
|
|
@openstack_auth_uri = URI.parse(options[:openstack_auth_url])
|
2012-08-14 04:32:23 -04:00
|
|
|
@openstack_management_url = @openstack_auth_uri.to_s
|
|
|
|
|
|
|
|
@auth_token = Fog::Mock.random_base64(64)
|
|
|
|
@auth_token_expiration = (Time.now.utc + 86400).iso8601
|
2012-09-16 22:20:09 -04:00
|
|
|
|
|
|
|
@admin_tenant = self.data[:tenants].values.find do |u|
|
|
|
|
u['name'] == 'admin'
|
|
|
|
end
|
|
|
|
|
2012-09-17 04:10:36 -04:00
|
|
|
if @openstack_tenant
|
2012-09-16 22:20:09 -04:00
|
|
|
@current_tenant = self.data[:tenants].values.find do |u|
|
2012-09-17 04:10:36 -04:00
|
|
|
u['name'] == @openstack_tenant
|
2012-09-16 22:20:09 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
unless @current_tenant
|
|
|
|
@current_tenant_id = Fog::Mock.random_hex(32)
|
|
|
|
@current_tenant = self.data[:tenants][@current_tenant_id] = {
|
|
|
|
'id' => @current_tenant_id,
|
2012-09-17 04:10:36 -04:00
|
|
|
'name' => @openstack_tenant
|
2012-09-16 22:20:09 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
@current_tenant_id = @current_tenant['id']
|
|
|
|
end
|
|
|
|
else
|
|
|
|
@current_tenant = @admin_tenant
|
|
|
|
end
|
|
|
|
|
|
|
|
@current_user = self.data[:users].values.find do |u|
|
|
|
|
u['name'] == @openstack_username
|
|
|
|
end
|
2012-11-15 09:29:57 -05:00
|
|
|
@current_tenant_id = Fog::Mock.random_hex(32)
|
2012-09-16 22:20:09 -04:00
|
|
|
|
|
|
|
unless @current_user
|
|
|
|
@current_user_id = Fog::Mock.random_hex(32)
|
|
|
|
@current_user = self.data[:users][@current_user_id] = {
|
|
|
|
'id' => @current_user_id,
|
2012-09-17 04:10:36 -04:00
|
|
|
'name' => @openstack_username,
|
|
|
|
'email' => "#{@openstack_username}@mock.com",
|
2012-03-19 01:52:58 -04:00
|
|
|
'tenantId' => Fog::Mock.random_numbers(6).to_s,
|
|
|
|
'enabled' => true
|
|
|
|
}
|
2012-09-16 22:20:09 -04:00
|
|
|
else
|
|
|
|
@current_user_id = @current_user['id']
|
2012-03-19 01:52:58 -04:00
|
|
|
end
|
2012-02-22 09:40:32 -05:00
|
|
|
end
|
2012-02-17 04:29:44 -05:00
|
|
|
|
2012-02-22 09:40:32 -05:00
|
|
|
def data
|
|
|
|
self.class.data[@openstack_username]
|
|
|
|
end
|
|
|
|
|
|
|
|
def reset_data
|
|
|
|
self.class.data.delete(@openstack_username)
|
|
|
|
end
|
2012-03-19 01:52:58 -04:00
|
|
|
|
|
|
|
def credentials
|
2012-08-14 04:32:23 -04:00
|
|
|
{ :provider => 'openstack',
|
|
|
|
:openstack_auth_url => @openstack_auth_uri.to_s,
|
|
|
|
:openstack_auth_token => @auth_token,
|
|
|
|
:openstack_management_url => @openstack_management_url,
|
|
|
|
:openstack_current_user_id => @openstack_current_user_id,
|
|
|
|
:current_user => @current_user,
|
|
|
|
:current_tenant => @current_tenant}
|
2012-03-19 01:52:58 -04:00
|
|
|
end
|
2012-02-17 04:29:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
2012-04-20 10:25:00 -04:00
|
|
|
attr_reader :current_user
|
|
|
|
attr_reader :current_tenant
|
2012-11-14 21:04:53 -05:00
|
|
|
attr_reader :unscoped_token
|
2012-02-17 04:29:44 -05:00
|
|
|
|
|
|
|
def initialize(options={})
|
2012-03-04 02:42:20 -05:00
|
|
|
@openstack_auth_token = options[:openstack_auth_token]
|
|
|
|
|
|
|
|
unless @openstack_auth_token
|
|
|
|
missing_credentials = Array.new
|
|
|
|
@openstack_api_key = options[:openstack_api_key]
|
|
|
|
@openstack_username = options[:openstack_username]
|
|
|
|
|
|
|
|
missing_credentials << :openstack_api_key unless @openstack_api_key
|
|
|
|
missing_credentials << :openstack_username unless @openstack_username
|
|
|
|
raise ArgumentError, "Missing required arguments: #{missing_credentials.join(', ')}" unless missing_credentials.empty?
|
|
|
|
end
|
|
|
|
|
2012-02-22 01:46:56 -05:00
|
|
|
@openstack_tenant = options[:openstack_tenant]
|
2012-10-30 02:03:31 -04:00
|
|
|
@openstack_auth_uri = URI.parse(options[:openstack_auth_url])
|
2012-02-22 01:46:56 -05:00
|
|
|
@openstack_management_url = options[:openstack_management_url]
|
|
|
|
@openstack_must_reauthenticate = false
|
2012-12-10 07:55:54 -05:00
|
|
|
@openstack_service_type = options[:openstack_service_type] || ['identity']
|
|
|
|
@openstack_service_name = options[:openstack_service_name]
|
2012-02-22 01:46:56 -05:00
|
|
|
|
2012-02-17 04:29:44 -05:00
|
|
|
@connection_options = options[:connection_options] || {}
|
2012-02-22 01:46:56 -05:00
|
|
|
|
2012-03-04 07:55:17 -05:00
|
|
|
@openstack_current_user_id = options[:openstack_current_user_id]
|
2013-01-24 05:38:45 -05:00
|
|
|
|
|
|
|
@openstack_endpoint_type = options[:openstack_endpoint_type] || 'adminURL'
|
2012-03-04 07:55:17 -05:00
|
|
|
|
2012-04-20 10:25:00 -04:00
|
|
|
@current_user = options[:current_user]
|
|
|
|
@current_tenant = options[:current_tenant]
|
|
|
|
|
2012-02-17 04:29:44 -05:00
|
|
|
authenticate
|
2012-02-22 01:46:56 -05:00
|
|
|
|
2012-02-17 04:29:44 -05:00
|
|
|
@persistent = options[:persistent] || false
|
|
|
|
@connection = Fog::Connection.new("#{@scheme}://#{@host}:#{@port}", @persistent, @connection_options)
|
|
|
|
end
|
|
|
|
|
2012-03-04 02:55:11 -05:00
|
|
|
def credentials
|
|
|
|
{ :provider => 'openstack',
|
|
|
|
:openstack_auth_url => @openstack_auth_uri.to_s,
|
2012-03-04 03:17:14 -05:00
|
|
|
:openstack_auth_token => @auth_token,
|
2012-03-04 07:55:17 -05:00
|
|
|
:openstack_management_url => @openstack_management_url,
|
2012-04-20 10:25:00 -04:00
|
|
|
:openstack_current_user_id => @openstack_current_user_id,
|
|
|
|
:current_user => @current_user,
|
|
|
|
:current_tenant => @current_tenant }
|
2012-03-04 02:55:11 -05:00
|
|
|
end
|
|
|
|
|
2012-02-17 04:29:44 -05:00
|
|
|
def reload
|
|
|
|
@connection.reset
|
|
|
|
end
|
|
|
|
|
|
|
|
def request(params)
|
2012-10-29 17:48:08 -04:00
|
|
|
retried = false
|
2012-02-17 04:29:44 -05:00
|
|
|
begin
|
|
|
|
response = @connection.request(params.merge({
|
|
|
|
:headers => {
|
|
|
|
'Content-Type' => 'application/json',
|
2012-09-11 18:45:56 -04:00
|
|
|
'Accept' => 'application/json',
|
2012-02-17 04:29:44 -05:00
|
|
|
'X-Auth-Token' => @auth_token
|
|
|
|
}.merge!(params[:headers] || {}),
|
|
|
|
:host => @host,
|
2012-02-22 09:40:32 -05:00
|
|
|
:path => "#{@path}/#{params[:path]}"#,
|
|
|
|
# Causes errors for some requests like tenants?limit=1
|
|
|
|
# :query => ('ignore_awful_caching' << Time.now.to_i.to_s)
|
2012-02-17 04:29:44 -05:00
|
|
|
}))
|
|
|
|
rescue Excon::Errors::Unauthorized => error
|
2012-10-29 17:48:08 -04:00
|
|
|
raise if retried
|
|
|
|
retried = true
|
|
|
|
|
|
|
|
@openstack_must_reauthenticate = true
|
|
|
|
authenticate
|
|
|
|
retry
|
2012-02-17 04:29:44 -05:00
|
|
|
rescue Excon::Errors::HTTPStatusError => error
|
|
|
|
raise case error
|
|
|
|
when Excon::Errors::NotFound
|
2012-02-26 03:09:22 -05:00
|
|
|
Fog::Identity::OpenStack::NotFound.slurp(error)
|
2012-02-17 04:29:44 -05:00
|
|
|
else
|
|
|
|
error
|
|
|
|
end
|
|
|
|
end
|
|
|
|
unless response.body.empty?
|
|
|
|
response.body = MultiJson.decode(response.body)
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def authenticate
|
2012-11-14 21:04:53 -05:00
|
|
|
if !@openstack_management_url || @openstack_must_reauthenticate
|
2012-02-17 04:29:44 -05:00
|
|
|
options = {
|
|
|
|
:openstack_api_key => @openstack_api_key,
|
|
|
|
:openstack_username => @openstack_username,
|
2012-03-04 09:01:55 -05:00
|
|
|
:openstack_auth_token => @openstack_auth_token,
|
2012-02-22 01:46:56 -05:00
|
|
|
:openstack_auth_uri => @openstack_auth_uri,
|
|
|
|
:openstack_tenant => @openstack_tenant,
|
2012-12-10 07:55:54 -05:00
|
|
|
:openstack_service_type => @openstack_service_type,
|
2012-03-03 08:32:05 -05:00
|
|
|
:openstack_service_name => @openstack_service_name,
|
2013-01-24 05:38:45 -05:00
|
|
|
:openstack_endpoint_type => @openstack_endpoint_type
|
2012-02-17 04:29:44 -05:00
|
|
|
}
|
2012-02-22 01:46:56 -05:00
|
|
|
|
2012-02-17 04:29:44 -05:00
|
|
|
credentials = Fog::OpenStack.authenticate_v2(options, @connection_options)
|
2012-02-22 01:46:56 -05:00
|
|
|
|
2012-04-20 10:25:00 -04:00
|
|
|
@current_user = credentials[:user]
|
|
|
|
@current_tenant = credentials[:tenant]
|
|
|
|
|
2012-02-22 01:46:56 -05:00
|
|
|
@openstack_must_reauthenticate = false
|
2012-02-17 04:29:44 -05:00
|
|
|
@auth_token = credentials[:token]
|
2012-02-22 01:46:56 -05:00
|
|
|
@openstack_management_url = credentials[:server_management_url]
|
2012-03-04 07:29:27 -05:00
|
|
|
@openstack_current_user_id = credentials[:current_user_id]
|
2012-11-14 21:04:53 -05:00
|
|
|
@unscoped_token = credentials[:unscoped_token]
|
2012-02-22 01:46:56 -05:00
|
|
|
uri = URI.parse(@openstack_management_url)
|
2012-02-17 04:29:44 -05:00
|
|
|
else
|
|
|
|
@auth_token = @openstack_auth_token
|
|
|
|
uri = URI.parse(@openstack_management_url)
|
|
|
|
end
|
2012-02-22 01:46:56 -05:00
|
|
|
|
2012-02-17 04:29:44 -05:00
|
|
|
@host = uri.host
|
|
|
|
@path = uri.path
|
|
|
|
@path.sub!(/\/$/, '')
|
|
|
|
@port = uri.port
|
|
|
|
@scheme = uri.scheme
|
2012-02-22 01:46:56 -05:00
|
|
|
true
|
2012-02-17 04:29:44 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|