summaryrefslogtreecommitdiff
path: root/app/models/clusters/cluster.rb
diff options
context:
space:
mode:
authorThong Kuah <tkuah@gitlab.com>2018-10-15 09:42:29 +1300
committerThong Kuah <tkuah@gitlab.com>2018-10-29 16:21:31 +1300
commit0e15eec86d83cbdfefe17966bf5c02e4d419a34d (patch)
treef2928be797556746f44e2689181d3cd5ba4dab79 /app/models/clusters/cluster.rb
parentb868b02c626dc4f9986eb93e54cf593055121972 (diff)
downloadgitlab-ce-0e15eec86d83cbdfefe17966bf5c02e4d419a34d.tar.gz
Associate clusters model to groups34758-create-group-clusters
Even though we currently only should have one group for a cluster, we allow the flexibility to associate to other groups in the future. This also matches the runner <=> groups association. - Adds Cluster#first_group, aliased to Cluster#group. For the conceivable future, a cluster will have at most one group. - Prevent mixing of group and project clusters. If project type clusters, it should only have projects assigned. Similarly with groups. - Default cluster_type to :project_type. As it's very small table we can set default and null: false in one release.
Diffstat (limited to 'app/models/clusters/cluster.rb')
-rw-r--r--app/models/clusters/cluster.rb28
1 files changed, 28 insertions, 0 deletions
diff --git a/app/models/clusters/cluster.rb b/app/models/clusters/cluster.rb
index 95efecfc41d..222e4217e67 100644
--- a/app/models/clusters/cluster.rb
+++ b/app/models/clusters/cluster.rb
@@ -20,6 +20,12 @@ module Clusters
has_many :cluster_projects, class_name: 'Clusters::Project'
has_many :projects, through: :cluster_projects, class_name: '::Project'
+ has_many :cluster_groups, class_name: 'Clusters::Group'
+ has_many :groups, through: :cluster_groups, class_name: '::Group'
+
+ has_one :cluster_group, -> { order(id: :desc) }, class_name: 'Clusters::Group'
+ has_one :group, through: :cluster_group, class_name: '::Group'
+
# we force autosave to happen when we save `Cluster` model
has_one :provider_gcp, class_name: 'Clusters::Providers::Gcp', autosave: true
@@ -38,8 +44,12 @@ module Clusters
accepts_nested_attributes_for :platform_kubernetes, update_only: true
validates :name, cluster_name: true
+ validates :cluster_type, presence: true
validate :restrict_modification, on: :update
+ validate :no_groups, unless: :group_type?
+ validate :no_projects, unless: :project_type?
+
delegate :status, to: :provider, allow_nil: true
delegate :status_reason, to: :provider, allow_nil: true
delegate :on_creation?, to: :provider, allow_nil: true
@@ -50,6 +60,12 @@ module Clusters
delegate :available?, to: :application_ingress, prefix: true, allow_nil: true
delegate :available?, to: :application_prometheus, prefix: true, allow_nil: true
+ enum cluster_type: {
+ instance_type: 1,
+ group_type: 2,
+ project_type: 3
+ }
+
enum platform_type: {
kubernetes: 1
}
@@ -122,5 +138,17 @@ module Clusters
true
end
+
+ def no_groups
+ if groups.any?
+ errors.add(:cluster, 'cannot have groups assigned')
+ end
+ end
+
+ def no_projects
+ if projects.any?
+ errors.add(:cluster, 'cannot have projects assigned')
+ end
+ end
end
end