Project

General

Profile

Revision f6a9acbd

Added by Sebastian Gräßl over 5 years ago

Fixes #18518 - Add proper validation for container image

For now the image has only been validate via the result
of the search, but not enforced on the server side.

The model validation brings now proper feedback when
the form has been submitted to the server.

View differences:

app/models/docker_container_wizard_states/image.rb
4 4
    belongs_to :wizard_state, :class_name => DockerContainerWizardState,
5 5
                              :foreign_key => :docker_container_wizard_state_id
6 6
    delegate :compute_resource_id, :to => :wizard_state
7
    delegate :compute_resource, :to => :wizard_state
7 8

  
8 9
    validates :tag,             :presence => true
9 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
10 40
  end
11 41
end

Also available in: Unified diff