Class: HTTPHandler

Inherits:
Object show all
Defined in:
lib/base/http_handler.rb

Constant Summary collapse

HTTP_UNAUTHENTICATED_CODE =
401

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ HTTPHandler

Returns a new instance of HTTPHandler.



7
8
9
10
11
# File 'lib/base/http_handler.rb', line 7

def initialize(options)
  @logger = options[:logger]
  @options = options
  @cld_ctrl_uri = options[:cloud_controller_uri]
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



5
6
7
# File 'lib/base/http_handler.rb', line 5

def logger
  @logger
end

Instance Method Details

#cc_http_request(args, &block) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/base/http_handler.rb', line 13

def cc_http_request(args, &block)
  args[:uri] = "#{@cld_ctrl_uri}#{args[:uri]}"
  args[:head] = cc_req_hdrs

  max_attempts = args[:max_attempts] || 2
  attempts = 0
  while true
    attempts += 1
    logger.debug("#{args[:method].upcase} - #{args[:uri]}")
    http = make_http_request(args)
    if attempts < max_attempts && http.response_header.status == HTTP_UNAUTHENTICATED_CODE
      args[:head] = refresh_client_auth_token
    else
      block.call(http)
      return http
    end
  end
end

#cc_req_hdrsObject



55
56
57
# File 'lib/base/http_handler.rb', line 55

def cc_req_hdrs
  @cc_req_hdrs || refresh_client_auth_token
end

#generate_cc_advertise_offering_request(service, active = true) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/base/http_handler.rb', line 59

def generate_cc_advertise_offering_request(service, active = true)
  svc = service.to_hash

  # NOTE: In CCNG, multiple versions is expected to be supported via multiple plans
  # The gateway will have to maintain a mapping of plan-name to version so that
  # the correct version will be provisioned
  plans = {}
  svc.delete('plans').each do |p|
    plans[p[:name]] = {
      "unique_id" => p[:unique_id],
      "name" => p[:name],
      "description" => p[:description],
      "free" => p[:free]
    }
  end

  [svc, plans]
end

#refresh_client_auth_tokenObject



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/base/http_handler.rb', line 32

def refresh_client_auth_token
  # Load the auth token to be sent out in Authorization header when making CCNG-v2 requests
  credentials = @options.fetch(:uaa_client_auth_credentials)
  client_id = @options.fetch(:uaa_client_id)

  if ENV["AUTHORIZATION_TOKEN"]
    uaa_client_auth_token = ENV["AUTHORIZATION_TOKEN"]
  else
    ti = CF::UAA::TokenIssuer.new(@options.fetch(:uaa_endpoint), client_id)
    token = ti.implicit_grant_with_creds(credentials).info
    uaa_client_auth_token = "#{token["token_type"]} #{token["access_token"]}"
    expire_time = token["expires_in"].to_i
    logger.info("Successfully refresh auth token for:\
                #{credentials[:username]}, token expires in \
                #{expire_time} seconds.")
  end

  @cc_req_hdrs = {
    'Content-Type' => 'application/json',
    'Authorization' => uaa_client_auth_token
  }
end