Class: ImageSearchController

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

Instance Method Summary collapse

Instance Method Details

#action_permissionObject



88
89
90
91
92
93
94
95
# File 'app/controllers/image_search_controller.rb', line 88

def action_permission
  case params[:action]
  when 'auto_complete_repository_name', 'auto_complete_image_tag', 'search_repository'
    :search_repository
  else
    super
  end
end

#auto_complete_image_tagObject



11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'app/controllers/image_search_controller.rb', line 11

def auto_complete_image_tag
  catch_network_errors do
    # This is the format jQuery UI autocomplete expects
    tags = use_hub? ? hub_auto_complete_image_tags(params[:search]) :
        registry_auto_complete_image_tags(params[:search])
    respond_to do |format|
      format.js do
        tags.map! { |tag| { :label => CGI.escapeHTML(tag), :value => CGI.escapeHTML(tag) } }
        render :json => tags
      end
    end
  end
end

#auto_complete_repository_nameObject



4
5
6
7
8
9
# File 'app/controllers/image_search_controller.rb', line 4

def auto_complete_repository_name
  catch_network_errors do
    render :text => (use_hub? ? hub_image_exists?(params[:search]) :
        registry_image_exists?(params[:search])).to_s
  end
end

#catch_network_errorsObject



39
40
41
42
43
44
45
46
47
48
# File 'app/controllers/image_search_controller.rb', line 39

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

#find_resourceObject



97
98
99
100
101
102
103
104
105
# File 'app/controllers/image_search_controller.rb', line 97

def find_resource
  if params[:registry_id].present?
    @registry = DockerRegistry.authorized(:view_registries).find(params[:registry_id])
  else
    @compute_resource = ComputeResource.authorized(:view_compute_resources).find(params[:id])
  end
rescue ActiveRecord::RecordNotFound
  not_found
end

#hub_auto_complete_image_tags(terms) ⇒ Object



58
59
60
# File 'app/controllers/image_search_controller.rb', line 58

def hub_auto_complete_image_tags(terms)
  @compute_resource.tags(terms)
end

#hub_image_exists?(terms) ⇒ Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/controllers/image_search_controller.rb', line 54

def hub_image_exists?(terms)
  @compute_resource.exist?(terms)
end

#hub_search_image(terms) ⇒ Object



62
63
64
# File 'app/controllers/image_search_controller.rb', line 62

def hub_search_image(terms)
  @compute_resource.search(terms)
end

#registry_auto_complete_image_tags(terms) ⇒ Object



75
76
77
78
79
# File 'app/controllers/image_search_controller.rb', line 75

def registry_auto_complete_image_tags(terms)
  ::Service::RegistryApi.new(:url => @registry.url,
                             :user => @registry.username,
                             :password => @registry.password).list_repository_tags(terms).keys
end

#registry_image_exists?(term) ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
69
70
71
72
73
# File 'app/controllers/image_search_controller.rb', line 66

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

#registry_search_image(terms) ⇒ Object



81
82
83
84
85
86
# File 'app/controllers/image_search_controller.rb', line 81

def registry_search_image(terms)
  r = ::Service::RegistryApi.new(:url => @registry.url,
                                 :user => @registry.username,
                                 :password => @registry.password).search(terms)
  r['results']
end

#search_repositoryObject



25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'app/controllers/image_search_controller.rb', line 25

def search_repository
  catch_network_errors do
    repositories = use_hub? ? hub_search_image(params[:search]) :
                              registry_search_image(params[:search])
    respond_to do |format|
      format.js do
        render :partial => 'repository_search_results',
               :locals  => { :repositories => repositories,
                             :use_hub => use_hub? }
      end
    end
  end
end

#use_hub?Boolean

Returns:

  • (Boolean)


50
51
52
# File 'app/controllers/image_search_controller.rb', line 50

def use_hub?
  @registry.nil?
end