Skip to content

Commit

Permalink
Fix exception handing for credential validation on raw_connect
Browse files Browse the repository at this point in the history
  • Loading branch information
tumido committed Nov 6, 2017
1 parent c506e6d commit 8751495
Showing 1 changed file with 26 additions and 25 deletions.
51 changes: 26 additions & 25 deletions app/models/manageiq/providers/google/manager_mixin.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,8 @@ module ManageIQ::Providers::Google::ManagerMixin
extend ActiveSupport::Concern

def verify_credentials(auth_type = nil, options = {})
begin
options[:auth_type] = auth_type

connection = connect(options)

# Not all errors will cause Fog to raise an exception,
# for example an error in the google_project id will
# succeed to connect but the first API call will raise
# an exception, so make a simple call to the API to
# confirm everything is working
connection.regions.all
rescue => err
raise MiqException::MiqInvalidCredentialsError, err.message
end
options[:auth_type] = auth_type
connect(options)

true
end
Expand All @@ -32,7 +20,7 @@ def gce
end

module ClassMethods
def raw_connect(google_project, google_json_key, options, proxy_uri = nil)
def raw_connect(google_project, google_json_key, options, proxy_uri = nil, validate = false)
require 'fog/google'

config = {
Expand All @@ -46,17 +34,30 @@ def raw_connect(google_project, google_json_key, options, proxy_uri = nil)
}
}

case options[:service]
# specify Compute as the default
when 'compute', nil
::Fog::Compute.new(config)
when 'pubsub'
::Fog::Google::Pubsub.new(config.except(:provider))
when 'monitoring'
::Fog::Google::Monitoring.new(config.except(:provider))
else
raise ArgumentError, "Unknown service: #{options[:service]}"
begin
case options[:service]
# specify Compute as the default
when 'compute', nil
connection = ::Fog::Compute.new(config)
when 'pubsub'
connection = ::Fog::Google::Pubsub.new(config.except(:provider))
when 'monitoring'
connection = ::Fog::Google::Monitoring.new(config.except(:provider))
else
raise ArgumentError, "Unknown service: #{options[:service]}"
end

# Not all errors will cause Fog to raise an exception,
# for example an error in the google_project id will
# succeed to connect but the first API call will raise
# an exception, so make a simple call to the API to
# confirm everything is working
connection.regions.all if validate
rescue => err
raise MiqException::MiqInvalidCredentialsError, err.message
end

connection
end
end
end

0 comments on commit 8751495

Please sign in to comment.