Method: CloudQueues::Client#authenticate!

Defined in:
lib/cloud-queues/client.rb

#authenticate!Object



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
# File 'lib/cloud-queues/client.rb', line 42

def authenticate!
  @client = Excon.new("https://identity.api.rackspacecloud.com")
  @base_path = nil

  if @token.nil?
    request = {auth: {"RAX-KSKEY:apiKeyCredentials" => {username: @username, apiKey: @api_key}}}
    response = request(method: :post, path: "/v2.0/tokens", body: request)
    @token = response.body["access"]["token"]["id"]
  else
    # try the current token
    request = {auth: {tenantId: @tenant, token: {id: @token}}}
    response = request(method: :post, path: "/v2.0/tokens", body: request)
  end

  @default_region = response.body["access"]["user"]["RAX-AUTH:defaultRegion"]

  url_type = @internal ? "internalURL" : "publicURL"
  queues = response.body["access"]["serviceCatalog"].select{|service| service["name"] == "cloudQueues" }
  endpoints = queues[0]["endpoints"]

  # default to the account's preferred region
  unless @region
    @region = @default_region
  end

  endpoint = endpoints.select { |endpoint| endpoint["region"] == @region.to_s.upcase }
  raise ArgumentError.new "Region #{@region.to_s.upcase} does not exist!" if endpoint.count == 0
  url = endpoint[0][url_type].split('/')

  @api_host = url[0..2].join('/')
  @base_path = "/" + url[3..-1].join('/')
  @tenant = url[-1]
  
  @client = Excon.new(@api_host, tcp_nodelay: true)
end