class Api::V2::Compliance::ArfReportsController

def action_permission

def action_permission
  case params[:action]
  when 'download', 'download_html'
    :view
  else
    super
  end
end

def create

def create
  arf_report = ForemanOpenscap::ArfReport.create_arf(@asset, @smart_proxy, params.to_unsafe_h)
  @asset.host.refresh_statuses([HostStatus.find_status_by_humanized_name("compliance")])
  respond_for_report arf_report
end

def destroy

def destroy
  process_response @arf_report.destroy
end

def download

def download
  response = @arf_report.to_bzip
  send_data response, :filename => "#{format_filename}.xml.bz2"
rescue => e
  handle_download_error e
end

def download_html

def download_html
  response = @arf_report.to_html
  send_data response, :filename => "#{format_filename}.html"
rescue => e
  handle_download_error e
end

def find_resource

def find_resource
  not_found && return if params[:id].blank?
  instance_variable_set("@arf_report", resource_scope.find(params[:id]))
end

def find_resources_before_create

def find_resources_before_create
  policy_id = params[:policy_id].to_i
  unless ForemanOpenscap::Policy.where(:id => policy_id).any?
    upload_fail(_("Policy with id %s not found.") % policy_id)
    return
  end
  @asset = ForemanOpenscap::Helper::get_asset(params[:cname], policy_id)
  unless @asset
    upload_fail(_('Could not find host identified by: %s') % params[:cname])
    return
  end
  if !params[:openscap_proxy_url] && !params[:openscap_proxy_name] && !@asset.host.openscap_proxy
    msg = _('Failed to upload Arf Report, OpenSCAP proxy name or url not found in params when uploading for %s and host is missing openscap_proxy') % @asset.host.name
    upload_fail(msg)
    return
  elsif !params[:openscap_proxy_url] && !params[:openscap_proxy_name] && @asset.host.openscap_proxy
    logger.debug 'No proxy params found when uploading arf report, falling back to asset.host.openscap_proxy'
    @smart_proxy = @asset.host.openscap_proxy
  else
    @smart_proxy = SmartProxy.unscoped.find_by :name => params[:openscap_proxy_name]
    @smart_proxy ||= SmartProxy.unscoped.find_by :url => params[:openscap_proxy_url]
  end
  unless @smart_proxy
    msg = _('No proxy found for %{name} or %{url}') % { :name => params[:openscap_proxy_name], :url => params[:openscap_proxy_url] }
    upload_fail(msg)
    return
  end
end

def get_resource(message = 'no resource loaded')

def get_resource(message = 'no resource loaded')
  instance_variable_get(:"@arf_report") || raise(message)
end

def handle_download_error(error)

def handle_download_error(error)
  render_error :custom_error, :status => :unprocessable_entity,
               :locals => { :message => _("Downloading the report failed: #{error.message}") }
end

def index

def index
  @arf_reports = resource_scope_for_index(:permission => :view_arf_reports).includes(:openscap_proxy, :policy, :host)
end

def resource_name(resource = '::ForemanOpenscap::ArfReport')

def resource_name(resource = '::ForemanOpenscap::ArfReport')
  super resource
end

def respond_for_report(arf_report)

def respond_for_report(arf_report)
  if arf_report.new_record?
    upload_fail arf_report.errors.full_messages.to_sentence
  else
    render :json => { :result => :ok, :id => arf_report.id.to_s }
  end
end

def show

def show
end

def upload_fail(msg)

def upload_fail(msg)
  logger.error msg
  render :json => { :result => :fail, :errors => msg }, :status => :unprocessable_entity
end