-
Notifications
You must be signed in to change notification settings - Fork 69
/
Copy pathmanager_auth_mixin.rb
84 lines (71 loc) · 2.69 KB
/
manager_auth_mixin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
require 'fog/vcloud_director'
module ManageIQ::Providers::Vmware::ManagerAuthMixin
extend ActiveSupport::Concern
def verify_credentials(auth_type = nil, options = {})
auth_type ||= 'default'
raise MiqException::MiqHostError, "No credentials defined" if missing_credentials?(auth_type)
options[:auth_type] = auth_type
self.class.connection_rescue_block do
case auth_type.to_s
when 'default' then
with_provider_connection(options) do |vcd|
self.class.validate_connection(vcd)
end
when 'amqp' then
verify_amqp_credentials(options)
else
raise "Invalid Vmware vCloud Authentication Type: #{auth_type.inspect}"
end
end
true
end
def connect(options = {})
raise "no credentials defined" if missing_credentials?(options[:auth_type])
server = options[:ip] || address
port = options[:port] || self.port
username = options[:user] || authentication_userid(options[:auth_type])
password = options[:pass] || authentication_password(options[:auth_type])
self.class.raw_connect(server, port, username, password)
end
module ClassMethods
def raw_connect(server, port, username, password, validate = false)
params = {
:vcloud_director_username => username,
:vcloud_director_password => MiqPassword.try_decrypt(password),
:vcloud_director_host => server,
:vcloud_director_show_progress => false,
:port => port,
:connection_options => {
:ssl_verify_peer => false # for development
}
}
connect = Fog::Compute::VcloudDirector.new(params)
connection_rescue_block { validate_connection(connect) } if validate
connect
end
def validate_connection(connection)
connection.organizations.all
end
def connection_rescue_block
yield
rescue => err
miq_exception = translate_exception(err)
_log.error("Error Class=#{err.class.name}, Message=#{err.message}")
raise miq_exception
end
def translate_exception(err)
case err
when Fog::Compute::VcloudDirector::Unauthorized
MiqException::MiqInvalidCredentialsError.new "Login failed due to a bad username or password."
when Excon::Errors::Timeout
MiqException::MiqUnreachableError.new "Login attempt timed out"
when Excon::Errors::SocketError
MiqException::MiqHostError.new "Socket error: #{err.message}"
when MiqException::MiqInvalidCredentialsError, MiqException::MiqHostError
err
else
MiqException::MiqHostError.new "Unexpected response returned from system: #{err.message}"
end
end
end
end