Class: ImageSearchController

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

Instance Method Summary collapse

Instance Method Details

#action_permissionObject



70
71
72
73
74
75
76
77
# File 'app/controllers/image_search_controller.rb', line 70

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



9
10
11
12
13
14
15
16
17
18
19
# File 'app/controllers/image_search_controller.rb', line 9

def auto_complete_image_tag
  # 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

#auto_complete_repository_nameObject



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

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

#find_resourceObject



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

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



40
41
42
# File 'app/controllers/image_search_controller.rb', line 40

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

#hub_image_exists?(terms) ⇒ Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/controllers/image_search_controller.rb', line 36

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

#hub_search_image(terms) ⇒ Object



44
45
46
# File 'app/controllers/image_search_controller.rb', line 44

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

#registry_auto_complete_image_tags(terms) ⇒ Object



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

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)


48
49
50
51
52
53
54
55
# File 'app/controllers/image_search_controller.rb', line 48

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



63
64
65
66
67
68
# File 'app/controllers/image_search_controller.rb', line 63

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



21
22
23
24
25
26
27
28
29
30
# File 'app/controllers/image_search_controller.rb', line 21

def search_repository
  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 }
    end
  end
end

#use_hub?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/controllers/image_search_controller.rb', line 32

def use_hub?
  @registry.nil?
end