Project

General

Profile

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

foreman_docker / app / models / docker_container_wizard_states / image.rb @ fb471374

1
module DockerContainerWizardStates
2
  class Image < ApplicationRecord
3
    self.table_name_prefix = 'docker_container_wizard_states_'
4
    belongs_to :wizard_state, :class_name => DockerContainerWizardState,
5
                              :foreign_key => :docker_container_wizard_state_id
6
    delegate :compute_resource_id, :to => :wizard_state
7
    delegate :compute_resource, :to => :wizard_state
8

    
9
    validates :tag,             :presence => true
10
    validates :repository_name, :presence => true
11
    validate :image_exists
12

    
13
    def name
14
      "#{repository_name}:#{tag}"
15
    end
16

    
17
    def registry_api
18
      if registry_id
19
        DockerRegistry.find(registry_id).api
20
      else
21
        Service::RegistryApi.docker_hub
22
      end
23
    end
24

    
25
    def sources
26
      [compute_resource, registry_api]
27
    end
28

    
29
    def image_search_service
30
      ForemanDocker::ImageSearch.new(*sources)
31
    end
32

    
33
    def image_exists
34
      return true if image_search_service.available?(name)
35
      error_msg = _("Container image %{image_name} is not available.") % {
36
        image_name: "#{name}",
37
      }
38
      errors.add(:image, error_msg)
39
    end
40
  end
41
end