Read about our upcoming Code of Conduct on this issue

cluster.rb 1.19 KB
Newer Older
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
47
48
49
50
51
52
53
54
55
56
module Clusters
  class Cluster < ActiveRecord::Base
    include Presentable

    belongs_to :user
    belongs_to :service

    enum :platform_type {
      kubernetes: 1
    }

    enum :provider_type {
      user: 0,
      gcp: 1
    }

    has_many :cluster_projects
    has_many :projects, through: :cluster_projects

    has_one :gcp_provider
    has_one :kubernetes_platform

    accepts_nested_attributes_for :gcp_provider
    accepts_nested_attributes_for :kubernetes_platform

    validates :kubernetes_platform, presence: true, if: :kubernetes?
    validates :gcp_provider, presence: true, if: :gcp?
    validate :restrict_modification, on: :update

    delegate :status, to: :provider, allow_nil: true
    delegate :status_reason, to: :provider, allow_nil: true

    def restrict_modification
      if provider&.on_creation?
        errors.add(:base, "cannot modify during creation")
        return false
      end

      true
    end

    def provider
      return gcp_provider if gcp?
    end

    def platform
      return kubernetes_platform if kubernetes?
    end

    def first_project
      return @first_project if defined?(@first_project)

      @first_project = projects.first
    end
  end
end