From 7b46964770e338528c3b508a317e1fdee0e06620 Mon Sep 17 00:00:00 2001 From: Nelvin Driz Date: Fri, 20 Apr 2012 22:25:00 +0800 Subject: [PATCH] [openstack] Make current_user and current_token accessible to services - This is for users with no permission to access keystone admin API Signed-off-by: Nelvin Driz --- lib/fog/openstack.rb | 11 +++++++---- lib/fog/openstack/compute.rb | 15 +++++++++++++-- lib/fog/openstack/identity.rb | 15 +++++++++++++-- lib/fog/openstack/image.rb | 15 +++++++++++++-- lib/fog/openstack/volume.rb | 17 ++++++++++++++--- 5 files changed, 60 insertions(+), 13 deletions(-) diff --git a/lib/fog/openstack.rb b/lib/fog/openstack.rb index a5cd5b70b..decc40c30 100644 --- a/lib/fog/openstack.rb +++ b/lib/fog/openstack.rb @@ -103,8 +103,6 @@ module Fog svc = body['access']['serviceCatalog']. detect{|x| @service_name.include?(x['type']) } - identity_svc = body['access']['serviceCatalog']. - detect{|x| @identity_service_name.include?(x['type']) } if @identity_service_name unless svc unless @openstack_tenant @@ -124,15 +122,20 @@ module Fog body = retrieve_tokens_v2(connection, req_body, uri) svc = body['access']['serviceCatalog']. detect{|x| @service_name.include?(x['type']) } - identity_svc = body['access']['serviceCatalog']. - detect{|x| @identity_service_name.include?(x['type']) } if @identity_service_name end + identity_svc = body['access']['serviceCatalog']. + detect{|x| @identity_service_name.include?(x['type']) } if @identity_service_name + tenant = body['access']['token']['tenant'] + user = body['access']['user'] + mgmt_url = svc['endpoints'].detect{|x| x[@endpoint_type]}[@endpoint_type] identity_url = identity_svc['endpoints'].detect{|x| x['publicURL']}['publicURL'] if identity_svc token = body['access']['token']['id'] { + :user => user, + :tenant => tenant, :token => token, :server_management_url => mgmt_url, :identity_public_endpoint => identity_url, diff --git a/lib/fog/openstack/compute.rb b/lib/fog/openstack/compute.rb index b1e43e581..f2c398cfd 100644 --- a/lib/fog/openstack/compute.rb +++ b/lib/fog/openstack/compute.rb @@ -9,7 +9,8 @@ module Fog requires :openstack_auth_url recognizes :openstack_auth_token, :openstack_management_url, :persistent, :openstack_service_name, :openstack_tenant, - :openstack_api_key, :openstack_username, :openstack_identity_endpoint + :openstack_api_key, :openstack_username, :openstack_identity_endpoint, + :current_user, :current_tenant ## MODELS # @@ -208,6 +209,8 @@ module Fog class Real attr_reader :auth_token + attr_reader :current_user + attr_reader :current_tenant def initialize(options={}) @openstack_auth_token = options[:openstack_auth_token] @@ -232,6 +235,9 @@ module Fog @connection_options = options[:connection_options] || {} + @current_user = options[:current_user] + @current_tenant = options[:current_tenant] + authenticate @persistent = options[:persistent] || false @@ -243,7 +249,9 @@ module Fog :openstack_auth_url => @openstack_auth_uri.to_s, :openstack_auth_token => @auth_token, :openstack_management_url => @openstack_management_url, - :openstack_identity_endpoint => @openstack_identity_public_endpoint } + :openstack_identity_endpoint => @openstack_identity_public_endpoint, + :current_user => @current_user, + :current_tenant => @current_tenant } end def reload @@ -305,6 +313,9 @@ module Fog credentials = Fog::OpenStack.authenticate_v1(options, @connection_options) end + @current_user = credentials[:user] + @current_tenant = credentials[:tenant] + @openstack_must_reauthenticate = false @auth_token = credentials[:token] @openstack_management_url = credentials[:server_management_url] diff --git a/lib/fog/openstack/identity.rb b/lib/fog/openstack/identity.rb index 62e1c55a2..c643008ac 100644 --- a/lib/fog/openstack/identity.rb +++ b/lib/fog/openstack/identity.rb @@ -8,7 +8,8 @@ module Fog requires :openstack_auth_url recognizes :openstack_auth_token, :openstack_management_url, :persistent, :openstack_service_name, :openstack_tenant, - :openstack_api_key, :openstack_username, :openstack_current_user_id + :openstack_api_key, :openstack_username, :openstack_current_user_id, + :current_user, :current_tenant model_path 'fog/openstack/models/identity' model :tenant @@ -102,6 +103,8 @@ module Fog end class Real + attr_reader :current_user + attr_reader :current_tenant def initialize(options={}) require 'multi_json' @@ -128,6 +131,9 @@ module Fog @openstack_current_user_id = options[:openstack_current_user_id] + @current_user = options[:current_user] + @current_tenant = options[:current_tenant] + authenticate @persistent = options[:persistent] || false @@ -139,7 +145,9 @@ module Fog :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} + :openstack_current_user_id => @openstack_current_user_id, + :current_user => @current_user, + :current_tenant => @current_tenant } end def reload @@ -196,6 +204,9 @@ module Fog credentials = Fog::OpenStack.authenticate_v2(options, @connection_options) + @current_user = credentials[:user] + @current_tenant = credentials[:tenant] + @openstack_must_reauthenticate = false @auth_token = credentials[:token] @openstack_management_url = credentials[:server_management_url] diff --git a/lib/fog/openstack/image.rb b/lib/fog/openstack/image.rb index d525f2830..2d6d7c098 100644 --- a/lib/fog/openstack/image.rb +++ b/lib/fog/openstack/image.rb @@ -8,7 +8,8 @@ module Fog requires :openstack_auth_url recognizes :openstack_auth_token, :openstack_management_url, :persistent, :openstack_service_name, :openstack_tenant, - :openstack_api_key, :openstack_username + :openstack_api_key, :openstack_username, + :current_user, :current_tenant model_path 'fog/openstack/models/image' @@ -79,6 +80,8 @@ module Fog end class Real + attr_reader :current_user + attr_reader :current_tenant def initialize(options={}) require 'multi_json' @@ -103,6 +106,9 @@ module Fog @connection_options = options[:connection_options] || {} + @current_user = options[:current_user] + @current_tenant = options[:current_tenant] + authenticate @persistent = options[:persistent] || false @@ -113,7 +119,9 @@ module Fog { :provider => 'openstack', :openstack_auth_url => @openstack_auth_uri.to_s, :openstack_auth_token => @auth_token, - :openstack_management_url => @openstack_management_url } + :openstack_management_url => @openstack_management_url, + :current_user => @current_user, + :current_tenant => @current_tenant } end def reload @@ -170,6 +178,9 @@ module Fog credentials = Fog::OpenStack.authenticate_v2(options, @connection_options) + @current_user = credentials[:user] + @current_tenant = credentials[:tenant] + @openstack_must_reauthenticate = false @auth_token = credentials[:token] @openstack_management_url = credentials[:server_management_url] diff --git a/lib/fog/openstack/volume.rb b/lib/fog/openstack/volume.rb index a6925dba6..65b8cd0b1 100644 --- a/lib/fog/openstack/volume.rb +++ b/lib/fog/openstack/volume.rb @@ -2,13 +2,14 @@ require File.expand_path(File.join(File.dirname(__FILE__), '..', 'openstack')) require 'fog/openstack' module Fog - module Volume + module Volume class OpenStack < Fog::Service requires :openstack_auth_url recognizes :openstack_auth_token, :openstack_management_url, :persistent, :openstack_service_name, :openstack_tenant, - :openstack_api_key, :openstack_username + :openstack_api_key, :openstack_username, + :current_user, :current_tenant #model_path 'fog/openstack/models/volume' @@ -79,6 +80,8 @@ module Fog end class Real + attr_reader :current_user + attr_reader :current_tenant def initialize(options={}) require 'multi_json' @@ -103,6 +106,9 @@ module Fog @connection_options = options[:connection_options] || {} + @current_user = options[:current_user] + @current_tenant = options[:current_tenant] + authenticate @persistent = options[:persistent] || false @@ -113,7 +119,9 @@ module Fog { :provider => 'openstack', :openstack_auth_url => @openstack_auth_uri.to_s, :openstack_auth_token => @auth_token, - :openstack_management_url => @openstack_management_url } + :openstack_management_url => @openstack_management_url, + :current_user => @current_user, + :current_tenant => @current_tenant } end def reload @@ -170,6 +178,9 @@ module Fog credentials = Fog::OpenStack.authenticate_v2(options, @connection_options) + @current_user = credentials[:user] + @current_tenant = credentials[:tenant] + @openstack_must_reauthenticate = false @auth_token = credentials[:token] @openstack_management_url = credentials[:server_management_url]