summaryrefslogtreecommitdiff
path: root/lib/gitlab/ci/config/entry/port.rb
blob: c239b1225c5f423394137d84008b5e034d52ed7c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# frozen_string_literal: true

module Gitlab
  module Ci
    class Config
      module Entry
        ##
        # Entry that represents a configuration of an Image Port.
        #
        class Port < ::Gitlab::Config::Entry::Node
          include ::Gitlab::Config::Entry::Validatable

          ALLOWED_KEYS = %i[number protocol name].freeze

          validations do
            validates :config, hash_or_integer: true
            validates :config, allowed_keys: ALLOWED_KEYS

            validates :number, type: Integer, presence: true
            validates :protocol, type: String, inclusion: { in: %w[http https], message: 'should be http or https' }, allow_blank: true
            validates :name, type: String, presence: false, allow_nil: true
          end

          def number
            value[:number]
          end

          def protocol
            value[:protocol]
          end

          def name
            value[:name]
          end

          def value
            return { number: @config } if integer?
            return @config if hash?

            {}
          end
        end
      end
    end
  end
end