Class: OpenStack::AuthV20

Inherits:
Object
  • Object
show all
Defined in:
lib/openstack/connection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(connection) ⇒ AuthV20

Returns a new instance of AuthV20.



273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
# File 'lib/openstack/connection.rb', line 273

def initialize(connection)

  tries = connection.retries
  time = 3

  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
    puts "Can't connect to the server: #{tries} tries  to reconnect" if connection.is_debug
    sleep time += 1
    retry unless (tries -= 1) <= 0
    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 =~ /^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|
      service["endpoints"].each do |endpoint|
        connection.regions_list[endpoint["region"]] ||= []
        connection.regions_list[endpoint["region"]] << {:service=>service["type"], :versionId => endpoint["versionId"]}
      end
      if connection.service_name
        check_service_name = connection.service_name
      else
        check_service_name = service['name']
      end
      if service['type'] == connection.service_type and service['name'] == check_service_name
        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
          if @version #already got one version of endpoints
            current_version = get_version_from_response(service)
            if @version.to_f > current_version.to_f
              next
            end
          end
          #grab version to check next time round for multi-version deployments
          @version = get_version_from_response(service)
          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

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



271
272
273
# File 'lib/openstack/connection.rb', line 271

def uri
  @uri
end

#versionObject (readonly)

Returns the value of attribute version.



272
273
274
# File 'lib/openstack/connection.rb', line 272

def version
  @version
end

Instance Method Details

#get_version_from_response(service) ⇒ Object



359
360
361
# File 'lib/openstack/connection.rb', line 359

def get_version_from_response(service)
  service["endpoints"].first["versionId"] || parse_version_from_endpoint(service["endpoints"].first["publicURL"])
end

#parse_version_from_endpoint(endpoint) ⇒ Object



365
366
367
# File 'lib/openstack/connection.rb', line 365

def parse_version_from_endpoint(endpoint)
  endpoint.match(/\/v(\d).(\d)/).to_s.sub("/v", "")
end