Project

General

Profile

Download (1.78 KB) Statistics
| Branch: | Tag: | Revision:

foreman-docker / app / controllers / image_search_controller.rb @ 04b6e05e

1
class ImageSearchController < ::ApplicationController
2
  def search_repository
3
    catch_network_errors do
4
      tags_enabled = params[:tags] || 'false'
5
      result = image_search_service.search(term: params[:search], tags: tags_enabled)
6

    
7
      respond_to do |format|
8
        format.js { render json: prepare_for_autocomplete(result) }
9
        format.html do
10
          render partial: 'repository_search_results', locals: { repositories: result }
11
        end
12
      end
13
    end
14
  end
15

    
16
  private
17

    
18
  def catch_network_errors
19
    yield
20
  rescue Docker::Error::NotFoundError => e
21
    # not an error
22
    logger.debug "image not found: #{e.backtrace}"
23
    render :js, :nothing => true
24
  rescue Docker::Error::DockerError, Excon::Errors::Error, SystemCallError => e
25
    render :js => _("An error occured during repository search: '%s'") % e.message,
26
           :status => 500
27
  end
28

    
29
  def action_permission
30
    case params[:action]
31
    when 'search_repository'
32
      :search_repository
33
    else
34
      super
35
    end
36
  end
37

    
38
  # This is the format jQuery UI autocomplete expects
39
  def prepare_for_autocomplete(tags)
40
    tags.map do |tag|
41
      tag = tag.is_a?(Hash) ? tag.fetch('name', tag) : tag
42
      tag = CGI.escapeHTML(tag)
43
      { :label => tag, :value => tag }
44
    end
45
  end
46

    
47
  def image_search_service
48
    @image_search_service ||= ForemanDocker::ImageSearch.new(*sources)
49
  end
50

    
51
  def sources
52
    if params[:registry] == 'hub'
53
      @registry ||= Service::RegistryApi.docker_hub
54
      @compute_resource ||= ComputeResource.authorized(:view_compute_resources).find(params[:id])
55
      [@registry, @compute_resource]
56
    elsif params[:registry] == 'registry' && params[:registry_id].present?
57
      @registry ||= DockerRegistry.authorized(:view_registries)
58
        .find(params[:registry_id]).api
59
      [@registry]
60
    end
61
  end
62
end