Class: ForemanRhCloud::CloudRequestForwarder
- Inherits:
-
Object
- Object
- ForemanRhCloud::CloudRequestForwarder
show all
- Includes:
- CertAuth, CloudRequest
- Defined in:
- app/services/foreman_rh_cloud/cloud_request_forwarder.rb
Instance Method Summary
collapse
Methods included from CertAuth
#cert_auth_available?, #execute_cloud_request, #foreman_certificate
#candlepin_id_cert, #cp_owner_id, #upstream_owner
#execute_cloud_request
Instance Method Details
#connection_test_request? ⇒ Boolean
115
116
117
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 115
def connection_test_request?
->(request_path) { request_path =~ /redhat_access\/r\/insights\/?$/ }
end
|
#core_app_name ⇒ Object
126
127
128
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 126
def core_app_name
BranchInfo.new.core_app_name
end
|
#core_app_version ⇒ Object
130
131
132
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 130
def core_app_version
BranchInfo.new.core_app_version
end
|
#forward_request(original_request, controller_name, branch_id, host) ⇒ Object
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 8
def forward_request(original_request, controller_name, branch_id, host)
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, host)
request_opts[:organization] = host.organization
logger.debug("Sending request to: #{request_opts[:url]}")
execute_cloud_request(request_opts)
end
|
#http_user_agent(original_request) ⇒ Object
134
135
136
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 134
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
|
#lightspeed? ⇒ Boolean
107
108
109
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 107
def lightspeed?
->(request_path) { request_path.include? '/lightspeed' }
end
|
#logger ⇒ Object
138
139
140
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 138
def logger
Foreman::Logging.logger('app')
end
|
93
94
95
96
97
98
99
100
101
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 93
def (original_request)
= {
if_none_match: original_request.if_none_match,
if_modified_since: original_request.if_modified_since,
}.compact
logger.debug("Sending headers: #{headers}")
end
|
#path_params(request_path) ⇒ Object
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 71
def path_params(request_path)
case request_path
when lightspeed?
{
url: ForemanRhCloud.cert_base_url + request_path,
}
when platform_request?
{
url: ForemanRhCloud.cert_base_url + request_path.sub('/redhat_access/r/insights/platform', '/api'),
}
when connection_test_request?
{
url: ForemanRhCloud.cert_base_url + '/api/apicast-tests/ping',
}
else
{
url: ForemanRhCloud.legacy_insights_url + request_path.sub('/redhat_access/r/insights', '/r/insights'),
ssl_ca_file: ForemanRhCloud.legacy_insights_ca,
}
end
end
|
111
112
113
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 111
def platform_request?
->(request_path) { request_path.include? '/platform' }
end
|
#prepare_forward_cloud_url(base_url, request_path) ⇒ Object
119
120
121
122
123
124
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 119
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
60
61
62
63
64
65
66
67
68
69
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 60
def prepare_forward_params(original_request, branch_id)
forward_params = original_request.query_parameters
compliance_request = original_request.path.match?(/compliance\/v2(\/.*)?/)
user_agent = original_request.user_agent.present? && !original_request.user_agent.include?('redhat_access_cfme')
if user_agent && !compliance_request
forward_params = forward_params.merge(:branch_id => branch_id)
end
forward_params
end
|
#prepare_forward_payload(original_request, controller_name) ⇒ Object
47
48
49
50
51
52
53
54
55
56
57
58
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 47
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]
forward_payload = forward_payload.to_json if original_request.format.json? && original_request.patch? && forward_payload && !forward_payload.is_a?(String)
forward_payload
end
|
103
104
105
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 103
def (host)
"for=\"_#{host.subscription_facet.uuid}\""
end
|
#prepare_request_opts(original_request, forward_payload, forward_params, host) ⇒ Object
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
|
# File 'app/services/foreman_rh_cloud/cloud_request_forwarder.rb', line 24
def prepare_request_opts(original_request, forward_payload, forward_params, host)
base_params = {
method: original_request.method,
payload: forward_payload,
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,
Forwarded: (host),
}
),
}
requested_url = original_request.original_fullpath.end_with?('/') ? original_request.path + '/' : original_request.path
params = path_params(requested_url)
if ForemanRhCloud.with_iop_smart_proxy?
params[:ssl_ca_file] = ForemanRhCloud.ca_cert
end
base_params.merge(params)
end
|