Class: InsightsCloud::UIRequestsController

Inherits:
ApplicationController
  • Object
show all
Defined in:
app/controllers/insights_cloud/ui_requests_controller.rb

Instance Method Summary collapse

Instance Method Details

#assign_header(res, cloud_res, header, transform) ⇒ Object



71
72
73
74
75
76
# File 'app/controllers/insights_cloud/ui_requests_controller.rb', line 71

def assign_header(res, cloud_res, header, transform)
  header_content = cloud_res.headers[header]
  return unless header_content
  new_header = transform ? header.to_s.tr('_', '-') : header.to_s
  res.headers[new_header] = header_content
end

#forward_requestObject

The method that “proxies” requests over to Cloud



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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
# File 'app/controllers/insights_cloud/ui_requests_controller.rb', line 8

def forward_request
  begin
    path_match = request.original_fullpath.match(%r{^/insights_cloud/(?<path>[^?]*)})&.[](:path)
    path_to_forward = path_match || params.require(:path)

    @cloud_response = ::ForemanRhCloud::InsightsApiForwarder.new.forward_request(
      request,
      path_to_forward,
      controller_name,
      User.current,
      @organization,
      @location
    )
  rescue RestClient::Exceptions::Timeout => e
    response_obj = e.response.presence || e.exception
    return render json: { message: response_obj.to_s, error: response_obj.to_s }, status: :gateway_timeout
  rescue RestClient::Unauthorized => e
    logger.warn("Forwarding request auth error: #{e}")
    message = 'Authentication to the Insights Service failed.'
    return render json: { message: message, error: message }, status: :unauthorized
  rescue RestClient::NotModified => e
    logger.info("Forwarding request not modified: #{e}")
    message = 'Cloud request not modified'
    return render json: { message: message, error: message }, status: :not_modified
  rescue RestClient::ExceptionWithResponse => e
    response_obj = e.response.presence || e.exception
    code = response_obj.try(:code) || response_obj.try(:http_code) || 500
    message = 'Cloud request failed'

    return render json: {
      :message => message,
      :error => response_obj.to_s,
      :headers => {},
      :response => response_obj,
    }, status: code
  rescue StandardError => e
    # Catch any other exceptions here, such as Errno::ECONNREFUSED
    logger.warn("Cloud request failed with exception: #{e}")
    return render json: { error: e.to_s }, status: :bad_gateway
  end

  # Append redhat-specific headers
  @cloud_response.headers.each do |key, _value|
    assign_header(response, @cloud_response, key, false) if key.to_s.start_with?('x_rh_')
  end

  # Append general headers
  assign_header(response, @cloud_response, :x_resource_count, true)
  headers[Rack::ETAG] = @cloud_response.headers[:etag]

  if @cloud_response.headers[:content_disposition]
    # If there is a Content-Disposition header, it means we are forwarding binary data, send the raw data with proper
    # content type
    send_data @cloud_response, disposition: @cloud_response.headers[:content_disposition], type: @cloud_response.headers[:content_type]
  elsif @cloud_response.headers[:content_type] =~ /zip/
    # If there is no Content-Disposition, but the content type is binary according to Content-Type, send the raw data
    # with proper content type
    send_data @cloud_response, type: @cloud_response.headers[:content_type]
  else
    render json: @cloud_response, status: @cloud_response.code
  end
end

#translate_insights_hostObject



78
79
80
81
82
83
84
85
86
87
# File 'app/controllers/insights_cloud/ui_requests_controller.rb', line 78

def translate_insights_host
  facet = InsightsFacet.find_by(uuid: params[:uuid])
  if facet.present?
    Rails.logger.debug "Found InsightsFacet #{params[:uuid]}"
    redirect_to host_details_page_path(facet.host_id)
  else
    Rails.logger.error "Could not find InsightsFacet for #{params[:uuid]}"
    redirect_to '/page-not-found'
  end
end