Skip to content
Snippets Groups Projects
Commit 22868f60 authored by Thong Kuah's avatar Thong Kuah :speech_balloon:
Browse files

Validate against mixing of group and project clusters

If project type clusters, it should only have projects assigned.
Similarly with groups.
parent 679fcb55
No related branches found
No related tags found
No related merge requests found
Loading
Loading
@@ -40,6 +40,9 @@ module Clusters
validates :name, cluster_name: 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
Loading
Loading
@@ -50,6 +53,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
}
Loading
Loading
@@ -129,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
Loading
Loading
@@ -42,6 +42,7 @@ module DeploymentPlatform
{
name: 'kubernetes-template',
projects: [self],
cluster_type: :project_type,
provider_type: :user,
platform_type: :kubernetes,
platform_kubernetes_attributes: platform_kubernetes_attributes_from_service_template
Loading
Loading
Loading
Loading
@@ -16,9 +16,9 @@ module Clusters
end
 
if project
cluster_params = params.merge(user: current_user, projects: [project])
cluster_params = params.merge(user: current_user, cluster_type: :project_type, projects: [project])
elsif group
cluster_params = params.merge(user: current_user, groups: [group])
cluster_params = params.merge(user: current_user, cluster_type: :group_type, groups: [group])
end
 
cluster_params[:provider_gcp_attributes].try do |provider|
Loading
Loading
# frozen_string_literal: true
class AddClusterTypeToClusters < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def change
add_column :clusters, :cluster_type, :smallint
end
end
Loading
Loading
@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
 
ActiveRecord::Schema.define(version: 20181014203236) do
ActiveRecord::Schema.define(version: 20181017001059) do
 
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Loading
Loading
@@ -637,6 +637,7 @@ ActiveRecord::Schema.define(version: 20181014203236) do
t.boolean "enabled", default: true
t.string "name", null: false
t.string "environment_scope", default: "*", null: false
t.integer "cluster_type", limit: 2
end
 
add_index "clusters", ["enabled"], name: "index_clusters_on_enabled", using: :btree
Loading
Loading
Loading
Loading
@@ -2,14 +2,23 @@ FactoryBot.define do
factory :cluster, class: Clusters::Cluster do
user
name 'test-cluster'
cluster_type :project_type
trait :instance do
cluster_type :instance_type
end
 
trait :project do
cluster_type :project_type
before(:create) do |cluster, evaluator|
cluster.projects << create(:project, :repository)
end
end
 
trait :group do
cluster_type :group_type
before(:create) do |cluster, evalutor|
cluster.groups << create(:group)
end
Loading
Loading
Loading
Loading
@@ -177,6 +177,46 @@ describe Clusters::Cluster do
it { expect(cluster.update(enabled: false)).to be_truthy }
end
end
describe 'cluster_type validations' do
let(:instance_cluster) { create(:cluster, :instance) }
let(:group_cluster) { create(:cluster, :group) }
let(:project_cluster) { create(:cluster, :project) }
context 'project_type cluster' do
it 'does not allow setting group' do
project_cluster.groups << build(:group)
expect(project_cluster).not_to be_valid
expect(project_cluster.errors.full_messages).to include('Cluster cannot have groups assigned')
end
end
context 'group_type cluster' do
it 'does not allow setting project' do
group_cluster.projects << build(:project)
expect(group_cluster).not_to be_valid
expect(group_cluster.errors.full_messages).to include('Cluster cannot have projects assigned')
end
end
context 'instance_type cluster' do
it 'does not allow setting group' do
instance_cluster.groups << build(:group)
expect(instance_cluster).not_to be_valid
expect(instance_cluster.errors.full_messages).to include('Cluster cannot have groups assigned')
end
it 'does not allow setting project' do
instance_cluster.projects << build(:project)
expect(instance_cluster).not_to be_valid
expect(instance_cluster.errors.full_messages).to include('Cluster cannot have projects assigned')
end
end
end
end
 
describe '#provider' do
Loading
Loading
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment