def initialize(connection)
begin
server = Net::HTTP::Proxy(connection.proxy_host, connection.proxy_port).new(connection.auth_host, connection.auth_port)
if connection.auth_scheme == "https"
server.use_ssl = true
server.verify_mode = OpenSSL::SSL::VERIFY_NONE
end
server.start
rescue
raise OpenStack::Exception::Connection, "Unable to connect to #{server}"
end
@uri = String.new
case connection.auth_method
when "password"
auth_data = JSON.generate({ "auth" => { "passwordCredentials" => { "username" => connection.authuser, "password" => connection.authkey }, connection.authtenant[:type] => connection.authtenant[:value]}})
when "rax-kskey"
auth_data = JSON.generate({"auth" => {"RAX-KSKEY:apiKeyCredentials" => {"username" => connection.authuser, "apiKey" => connection.authkey}}})
when "key"
auth_data = JSON.generate({"auth" => { "apiAccessKeyCredentials" => {"accessKey" => connection.authuser, "secretKey" => connection.authkey}, connection.authtenant[:type] => connection.authtenant[:value]}})
else
raise Exception::InvalidArgument, "Unrecognized auth method #{connection.auth_method}"
end
response = server.post(connection.auth_path.chomp("/")+"/tokens", auth_data, {'Content-Type' => 'application/json'})
if (response.code =~ %r^20./)
resp_data=JSON.parse(response.body)
connection.authtoken = resp_data['access']['token']['id']
implemented_services = resp_data["access"]["serviceCatalog"].inject([]){|res, current| res << current["type"] ;res}
raise OpenStack::Exception::NotImplemented.new("The requested service: \"#{connection.service_type}\" is not present " +
"in the returned service catalogue.", 501, "#{resp_data["access"]["serviceCatalog"]}") unless implemented_services.include?(connection.service_type)
resp_data['access']['serviceCatalog'].each do |service|
if service['type'] == connection.service_type
endpoints = service["endpoints"]
if connection.region
endpoints.each do |ep|
if ep["region"] and ep["region"].upcase == connection.region.upcase
@uri = URI.parse(ep["publicURL"])
end
end
else
@uri = URI.parse(endpoints[0]["publicURL"])
end
if @uri == ""
raise OpenStack::Exception::Authentication, "No API endpoint for region #{connection.region}"
else
connection.service_host = @uri.host
connection.service_path = @uri.path
connection.service_port = @uri.port
connection.service_scheme = @uri.scheme
connection.authok = true
end
end
end
else
connection.authtoken = false
raise OpenStack::Exception::Authentication, "Authentication failed with response code #{response.code}"
end
server.finish if server.started?
end