gitlab-org--gitlab-foss/app/models/clusters/cluster.rb

85 lines
2.1 KiB
Ruby
Raw Normal View History

module Clusters
class Cluster < ActiveRecord::Base
include Presentable
2017-10-23 08:36:35 +00:00
self.table_name = 'clusters'
2017-11-02 14:10:46 +00:00
APPLICATIONS = {
Clusters::Applications::Helm::NAME => Clusters::Applications::Helm
2017-11-02 16:08:56 +00:00
}.freeze
2017-11-02 14:10:46 +00:00
belongs_to :user
2017-10-23 08:36:35 +00:00
has_many :cluster_projects, class_name: 'Clusters::Project'
has_many :projects, through: :cluster_projects, class_name: '::Project'
# we force autosave to happen when we save `Cluster` model
has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp', autosave: true
# We have to ":destroy" it today to ensure that we clean also the Kubernetes Integration
has_one :platform_kubernetes, class_name: 'Clusters::Platforms::Kubernetes', autosave: true, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
2017-11-02 14:10:46 +00:00
has_one :application_helm, class_name: 'Clusters::Applications::Helm'
accepts_nested_attributes_for :provider_gcp, update_only: true
2017-10-30 12:55:18 +00:00
accepts_nested_attributes_for :platform_kubernetes, update_only: true
2017-10-23 08:36:35 +00:00
validates :name, cluster_name: true
validate :restrict_modification, on: :update
delegate :status_reason, to: :provider, allow_nil: true
2017-10-23 08:36:35 +00:00
delegate :status_name, to: :provider, allow_nil: true
delegate :on_creation?, to: :provider, allow_nil: true
2017-10-29 18:48:45 +00:00
enum platform_type: {
kubernetes: 1
}
enum provider_type: {
user: 0,
gcp: 1
}
scope :enabled, -> { where(enabled: true) }
scope :disabled, -> { where(enabled: false) }
2017-11-02 14:10:46 +00:00
def status_name
if provider
provider.status_name
else
:created
end
end
def provider
2017-10-23 08:36:35 +00:00
return provider_gcp if gcp?
end
def platform
2017-10-23 08:36:35 +00:00
return platform_kubernetes if kubernetes?
end
def first_project
return @first_project if defined?(@first_project)
@first_project = projects.first
end
2017-10-29 18:48:45 +00:00
alias_method :project, :first_project
2017-10-23 08:36:35 +00:00
2017-11-02 14:10:46 +00:00
def kubeclient
platform_kubernetes.kubeclient if kubernetes?
end
2017-10-23 08:36:35 +00:00
private
def restrict_modification
if provider&.on_creation?
errors.add(:base, "cannot modify during creation")
return false
end
true
end
end
end