Project

General

Profile

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

foreman_docker / app / controllers / image_search_controller.rb @ 64f6a1f2

1
class ImageSearchController < ::ApplicationController
2
  before_filter :find_resource
3

    
4
  def auto_complete_repository_name
5
    render :text => (use_hub? ? hub_image_exists?(params[:search]) :
6
        registry_image_exists?(params[:search])).to_s
7
  end
8

    
9
  def auto_complete_image_tag
10
    # This is the format jQuery UI autocomplete expects
11
    tags = use_hub? ? hub_auto_complete_image_tags(params[:search]) :
12
        registry_auto_complete_image_tags(params[:search])
13
    respond_to do |format|
14
      format.js do
15
        tags.map! { |tag| { :label => CGI.escapeHTML(tag), :value => CGI.escapeHTML(tag) } }
16
        render :json => tags
17
      end
18
    end
19
  end
20

    
21
  def search_repository
22
    repositories = use_hub? ? hub_search_image(params[:search]) :
23
                              registry_search_image(params[:search])
24
    respond_to do |format|
25
      format.js do
26
        render :partial => 'repository_search_results',
27
               :locals  => { :repositories => repositories }
28
      end
29
    end
30
  end
31

    
32
  def use_hub?
33
    @registry.nil?
34
  end
35

    
36
  def hub_image_exists?(terms)
37
    @compute_resource.exist?(terms)
38
  end
39

    
40
  def hub_auto_complete_image_tags(terms)
41
    @compute_resource.tags(terms)
42
  end
43

    
44
  def hub_search_image(terms)
45
    @compute_resource.search(terms)
46
  end
47

    
48
  def registry_image_exists?(term)
49
    result = ::Service::RegistryApi.new(:url => @registry.url,
50
                                        :user => @registry.username,
51
                                        :password => @registry.password).search(term)
52
    registry_name = term.split('/').size > 1 ? term :
53
        'library/' + term
54
    result['results'].any? { |r| r['name'] == registry_name }
55
  end
56

    
57
  def registry_auto_complete_image_tags(terms)
58
    ::Service::RegistryApi.new(:url => @registry.url,
59
                               :user => @registry.username,
60
                               :password => @registry.password).list_repository_tags(terms).keys
61
  end
62

    
63
  def registry_search_image(terms)
64
    r = ::Service::RegistryApi.new(:url => @registry.url,
65
                                   :user => @registry.username,
66
                                   :password => @registry.password).search(terms)
67
    r['results']
68
  end
69

    
70
  def action_permission
71
    case params[:action]
72
    when 'auto_complete_repository_name', 'auto_complete_image_tag', 'search_repository'
73
      :search_repository
74
    else
75
      super
76
    end
77
  end
78

    
79
  def find_resource
80
    if params[:registry_id].present?
81
      @registry = DockerRegistry.authorized(:view_registries).find(params[:registry_id])
82
    else
83
      @compute_resource = ComputeResource.authorized(:view_compute_resources).find(params[:id])
84
    end
85
  rescue ActiveRecord::RecordNotFound
86
    not_found
87
  end
88
end