Commit a5f8eab9 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch '34758-unique-environment-scope' into 'master'

Unique environment scope for clusters belonging to a group

See merge request gitlab-org/gitlab-ee!8124
parents 47fda363 d4489947
......@@ -5,6 +5,7 @@ module Clusters
prepend EE::Clusters::Cluster
include Presentable
include Gitlab::Utils::StrongMemoize
self.table_name = 'clusters'
......@@ -26,9 +27,6 @@ module Clusters
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
......@@ -121,12 +119,19 @@ module Clusters
end
def first_project
return @first_project if defined?(@first_project)
@first_project = projects.first
strong_memoize(:first_project) do
projects.first
end
end
alias_method :project, :first_project
def first_group
strong_memoize(:first_group) do
groups.first
end
end
alias_method :group, :first_group
def kubeclient
platform_kubernetes.kubeclient if kubernetes?
end
......
......@@ -10,11 +10,16 @@ module EE
end
def unique_environment_scope
if project && project.clusters.where(environment_scope: environment_scope).where.not(id: self.id).exists?
if project && project.clusters.where(environment_scope: environment_scope).where.not(id: id).exists?
errors.add(:base, "cannot add duplicated environment scope")
return false
end
if group && group.clusters.where(environment_scope: environment_scope).where.not(id: id).exists?
errors.add(:base, 'cannot add duplicated environment scope')
return false
end
true
end
end
......
......@@ -7,6 +7,7 @@ describe Clusters::Cluster do
subject { cluster.valid? }
context 'when validates unique_environment_scope' do
context 'for a project cluster' do
let(:project) { create(:project) }
before do
......@@ -32,5 +33,32 @@ describe Clusters::Cluster do
it { is_expected.to be_truthy }
end
end
context 'for a group cluster' do
let(:group) { create(:group) }
before do
create(:cluster, cluster_type: :group_type, groups: [group], environment_scope: 'product/*')
end
context 'when identical environment scope exists in group' do
let(:cluster) { build(:cluster, cluster_type: :group_type, groups: [group], environment_scope: 'product/*') }
it { is_expected.to be_falsey }
end
context 'when identical environment scope does not exist in group' do
let(:cluster) { build(:cluster, cluster_type: :group_type, groups: [group], environment_scope: '*') }
it { is_expected.to be_truthy }
end
context 'when identical environment scope exists in different group' do
let(:cluster) { build(:cluster, :group, environment_scope: 'product/*') }
it { is_expected.to be_truthy }
end
end
end
end
end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment