Class: ForemanRhCloud::CloudRequestForwarder
- Inherits:
-
Object
- Object
- ForemanRhCloud::CloudRequestForwarder
- Includes:
- CloudRequest
- Defined in:
- app/services/foreman_rh_cloud/cloud_request_forwarder.rb
Instance Method Summary collapse
- #connection_test_request? ⇒ Boolean
- #core_app_name ⇒ Object
- #core_app_version ⇒ Object
- #forward_request(original_request, controller_name, branch_id, certs) ⇒ Object
- #http_user_agent(original_request) ⇒ Object
- #logger ⇒ Object
- #original_headers(original_request) ⇒ Object
- #path_params(request_path, certs) ⇒ Object
- #platform_request? ⇒ Boolean
- #prepare_forward_cloud_url(base_url, request_path) ⇒ Object
- #prepare_forward_params(original_request, branch_id) ⇒ Object
- #prepare_forward_payload(original_request, controller_name) ⇒ Object
- #prepare_request_opts(original_request, forward_payload, forward_params, certs) ⇒ Object
Methods included from CloudRequest
Instance Method Details
#connection_test_request? ⇒ Boolean
99 100 101 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 99 def connection_test_request? ->(request_path) { request_path =~ /redhat_access\/r\/insights\/?$/ } end |
#core_app_name ⇒ Object
110 111 112 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 110 def core_app_name BranchInfo.new.core_app_name end |
#core_app_version ⇒ Object
114 115 116 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 114 def core_app_version BranchInfo.new.core_app_version end |
#forward_request(original_request, controller_name, branch_id, certs) ⇒ Object
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 7 def forward_request(original_request, controller_name, branch_id, certs) forward_params = prepare_forward_params(original_request, branch_id) logger.debug("Request parameters for telemetry request: #{forward_params}") forward_payload = prepare_forward_payload(original_request, controller_name) logger.debug("User agent for telemetry is: #{http_user_agent original_request}") request_opts = prepare_request_opts(original_request, forward_payload, forward_params, certs) logger.debug("Sending request to: #{request_opts[:url]}") execute_cloud_request(request_opts) rescue RestClient::Exception => error_response error_response.response end |
#http_user_agent(original_request) ⇒ Object
118 119 120 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 118 def http_user_agent(original_request) "#{core_app_name}/#{core_app_version};#{ForemanRhCloud::Engine.engine_name}/#{ForemanRhCloud::VERSION};#{original_request.env['HTTP_USER_AGENT']}" end |
#logger ⇒ Object
122 123 124 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 122 def logger Foreman::Logging.logger('app') end |
#original_headers(original_request) ⇒ Object
85 86 87 88 89 90 91 92 93 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 85 def original_headers(original_request) headers = { if_none_match: original_request.if_none_match, if_modified_since: original_request.if_modified_since, }.compact logger.debug("Sending headers: #{headers}") headers end |
#path_params(request_path, certs) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 61 def path_params(request_path, certs) case request_path when platform_request? { url: ForemanRhCloud.cert_base_url + request_path.sub('/redhat_access/r/insights/platform', '/api'), ssl_client_cert: OpenSSL::X509::Certificate.new(certs[:cert]), ssl_client_key: OpenSSL::PKey.read(certs[:key]), } when connection_test_request? { url: ForemanRhCloud.cert_base_url + '/api/apicast-tests/ping', ssl_client_cert: OpenSSL::X509::Certificate.new(certs[:cert]), ssl_client_key: OpenSSL::PKey.read(certs[:key]), } else # Legacy insights API { url: ForemanRhCloud.legacy_insights_url + request_path.sub('/redhat_access/r/insights', '/r/insights'), ssl_client_cert: OpenSSL::X509::Certificate.new(certs[:cert]), ssl_client_key: OpenSSL::PKey.read(certs[:key]), ssl_ca_file: ForemanRhCloud.legacy_insights_ca, } end end |
#platform_request? ⇒ Boolean
95 96 97 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 95 def platform_request? ->(request_path) { request_path.include? '/platform' } end |
#prepare_forward_cloud_url(base_url, request_path) ⇒ Object
103 104 105 106 107 108 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 103 def prepare_forward_cloud_url(base_url, request_path) cloud_path = request_path.sub('/redhat_access/r/insights/platform/', '') .sub('/redhat_access/r/insights/', '') "#{base_url}/api/#{cloud_path}" end |
#prepare_forward_params(original_request, branch_id) ⇒ Object
52 53 54 55 56 57 58 59 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 52 def prepare_forward_params(original_request, branch_id) forward_params = original_request.query_parameters if original_request.user_agent && !original_request.user_agent.include?('redhat_access_cfme') forward_params = forward_params.merge(:branch_id => branch_id) end forward_params end |
#prepare_forward_payload(original_request, controller_name) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 39 def prepare_forward_payload(original_request, controller_name) forward_payload = original_request.request_parameters[controller_name] forward_payload = original_request.raw_post.clone if (original_request.post? || original_request.patch?) && original_request.raw_post forward_payload = original_request.body.read if original_request.put? forward_payload = original_request.params.slice(:file, :metadata) if original_request.params[:file] # fix rails behaviour for http PATCH: forward_payload = forward_payload.to_json if original_request.format.json? && original_request.patch? && forward_payload && !forward_payload.is_a?(String) forward_payload end |
#prepare_request_opts(original_request, forward_payload, forward_params, certs) ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 24 def prepare_request_opts(original_request, forward_payload, forward_params, certs) base_params = { method: original_request.method, payload: forward_payload, headers: original_headers(original_request).merge( { params: forward_params, user_agent: http_user_agent(original_request), content_type: original_request.media_type.presence || original_request.format.to_s, } ), } base_params.merge(path_params(original_request.path, certs)) end |