Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# frozen_string_literal: true

class UffizziCore::Api::Cli::V1::Accounts::User::ApplicationController < UffizziCore::Api::Cli::V1::Accounts::ApplicationController
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# frozen_string_literal: true

class UffizziCore::Api::Cli::V1::Accounts::User::Projects::ApplicationController <
UffizziCore::Api::Cli::V1::Accounts::User::ApplicationController
def resource_project
@resource_project ||= current_user.projects.find_by!(slug: params[:project_slug])
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# frozen_string_literal: true

class UffizziCore::Api::Cli::V1::Accounts::User::Projects::ClustersController <
UffizziCore::Api::Cli::V1::Accounts::User::Projects::ApplicationController
before_action :authorize_uffizzi_core_api_cli_v1_accounts_user_projects_clusters

def index
clusters = resource_project.clusters.enabled.deployed_by_user(current_user).ransack(q_param)

respond_with(clusters.result, each_serializer: UffizziCore::Api::Cli::V1::Projects::ShortClusterSerializer)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ def index
end

def create
version = cluster_params[:k8s_version]
kubernetes_distribution = find_kubernetes_distribution(version)
return render_distribution_version_error(version) if kubernetes_distribution.blank?
k8s_version = cluster_params[:k8s_version]
kubernetes_distribution = find_kubernetes_distribution(k8s_version)
return render_distribution_version_error(k8s_version) if kubernetes_distribution.blank?

cluster_form = UffizziCore::Api::Cli::V1::Cluster::CreateForm.new(cluster_params)
cluster_form.project = resource_project
Expand Down
4 changes: 2 additions & 2 deletions core/app/forms/uffizzi_core/api/cli/v1/cluster/create_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
class UffizziCore::Api::Cli::V1::Cluster::CreateForm < UffizziCore::Cluster
include UffizziCore::ApplicationForm

permit :name, :manifest, :creation_source
permit :name, :manifest, :creation_source, :kind

validate :check_manifest, if: -> { manifest.present? }

validates_uniqueness_of :deployed_by_id, conditions: -> { enabled }, scope: [:project_id, :kind], if: Proc.new { |c| c.kind.dev? }
private

def check_manifest
Expand Down
7 changes: 6 additions & 1 deletion core/app/lib/uffizzi_core/concerns/models/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,18 @@ module UffizziCore::Concerns::Models::Cluster

belongs_to :project, class_name: UffizziCore::Project.name
belongs_to :deployed_by, class_name: UffizziCore::User.name, foreign_key: :deployed_by_id, optional: true
belongs_to :kubernetes_distribution, optional: true

validates_uniqueness_of :name, conditions: -> { enabled }, scope: :project_id
validates :name, presence: true, format: { with: /\A[a-zA-Z0-9-]*\z/ }

enumerize :creation_source, in: UffizziCore.cluster_creation_sources, scope: true, predicates: true
attribute :creation_source, :string, default: :manual

enumerize :kind, in: [:basic, :dev], scope: true, predicates: true
attribute :kind, :string, default: :basic

validates :creation_source, presence: true
belongs_to :kubernetes_distribution, optional: true

aasm(:state) do
state :deploying_namespace, initial: true
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class UffizziCore::Api::Cli::V1::Accounts::User::Projects::ClustersPolicy < UffizziCore::ApplicationPolicy
def index?
context.user_access_module.admin_or_developer_access_to_project?(context.user, context.account)
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
class UffizziCore::Api::Cli::V1::Projects::ClusterSerializer < UffizziCore::BaseSerializer
type :cluster

attributes :id, :name, :state, :kubeconfig, :created_at, :host, :k8s_version
attributes :id, :name, :state, :kubeconfig, :created_at, :host, :k8s_version, :kind

def k8s_version
object.kubernetes_distribution&.version || UffizziCore::KubernetesDistribution.default.version
Expand Down
4 changes: 4 additions & 0 deletions core/config/locales/en.activerecord.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ en:
attributes:
email:
invalid: Invalid email address
uffizzi_core/cluster:
attributes:
deployed_by_id:
taken: User already has dev cluster for project
9 changes: 9 additions & 0 deletions core/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,15 @@
scope module: :accounts do
resources :projects, only: ['index', 'create']
resources :clusters, only: ['index']
scope module: :user do
resource :user, only: [] do
scope module: :projects do
resources :projects, only: [], param: :slug do
resources :clusters, only: ['index']
end
end
end
end
resources :credentials, only: ['index', 'create', 'update', 'destroy'], param: :type do
member do
get :check_credential
Expand Down
7 changes: 7 additions & 0 deletions core/db/migrate/20231108152020_add_kind_to_cluster.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddKindToCluster < ActiveRecord::Migration[6.1]
def change
add_column(:uffizzi_core_clusters, :kind, :string)
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

require 'test_helper'

class UffizziCore::Api::Cli::V1::Accounts::User::Projects::ClustersControllerTest < ActionController::TestCase
setup do
@developer = create(:user, :with_organizational_account)
@account = @developer.accounts.organizational.first
sign_in(@developer)
end

test '#index' do
project = create(:project, :with_members, account: @account, members: [@developer])
dev_cluster = create(:cluster, :deployed, :dev, project: project, deployed_by_id: @developer.id)
create(:cluster, :deployed, project: project, deployed_by_id: @developer.id)

q_params = { kind_eq: UffizziCore::Cluster.kind.dev }

get :index, params: { account_id: @account.id, project_slug: project.slug, q: q_params }, format: :json

assert_response(:success)

clusters_data = JSON.parse(response.body)['clusters']

assert_equal(1, clusters_data.count)
assert_equal(dev_cluster.name, clusters_data[0]['name'])
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,65 @@ class UffizziCore::Api::Cli::V1::Projects::ClustersControllerTest < ActionContro
assert_requested(stubbed_get_cluster_request)
end

test '#create dev when dev exists for same user and project' do
sign_in(@admin)
create(:cluster, :deployed, :dev, project: @project, deployed_by: @admin)

params = {
project_slug: @project.slug,
cluster: {
name: 'test',
kind: UffizziCore::Cluster.kind.dev,
},
}

differences = {
-> { UffizziCore::Cluster.count } => 0,
}

assert_difference differences do
post :create, params: params, format: :json
end

assert_response(:unprocessable_entity)
end

test '#create multiple basic clusters for same user and project' do
sign_in(@admin)
create(:cluster, :deployed, project: @project, deployed_by: @admin)
cluster_creation_data = json_fixture('files/controller/cluster_not_ready.json')
params = {
project_slug: @project.slug,
cluster: {
name: cluster_creation_data[:name],
},
}

expected_request = {
name: cluster_creation_data[:name],
manifest: nil,
base_ingress_host: /#{UffizziCore::Cluster::NAMESPACE_PREFIX}\d/,
}
stubbed_create_cluster_request = stub_create_cluster_request_with_expected(cluster_creation_data, expected_request)
stubbed_create_namespace_request = stub_create_namespace_request
cluster_show_data = json_fixture('files/controller/cluster_ready.json')
stubbed_cluster_request = stub_get_cluster_request(cluster_show_data)

differences = {
-> { UffizziCore::Cluster.count } => 1,
}

assert_difference differences do
post :create, params: params, format: :json
end

assert_response(:success)
assert(UffizziCore::Cluster.find_by(name: cluster_creation_data[:name]).creation_source.manual?)
assert_requested(stubbed_create_cluster_request)
assert_requested(stubbed_create_namespace_request)
assert_requested(stubbed_cluster_request)
end

test '#show shows cluster created by the same developer' do
cluster = create(:cluster, project: @project, deployed_by: @developer, name: 'test')
sign_in(@developer)
Expand Down
3 changes: 2 additions & 1 deletion core/test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_10_09_182412) do
ActiveRecord::Schema.define(version: 2023_11_08_152020) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -85,6 +85,7 @@
t.string "host"
t.string "creation_source"
t.integer "kubernetes_distribution_id"
t.string "kind"
t.index ["project_id"], name: "index_cluster_on_project_id"
end

Expand Down
4 changes: 4 additions & 0 deletions core/test/factories/cluster.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@
trait :deployed do
state { :deployed }
end

trait :dev do
kind { UffizziCore::Cluster.kind.dev }
end
end
end
7 changes: 7 additions & 0 deletions db/migrate/20231108152020_add_kind_to_cluster.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

class AddKindToCluster < ActiveRecord::Migration[6.1]
def change
add_column(:uffizzi_core_clusters, :kind, :string)
end
end
4 changes: 3 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2023_10_09_163516) do
ActiveRecord::Schema.define(version: 2023_11_08_152020) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -84,6 +84,8 @@
t.datetime "updated_at", precision: 6, null: false
t.string "host"
t.string "creation_source"
t.integer "kubernetes_distribution_id"
t.string "kind"
t.index ["project_id"], name: "index_cluster_on_project_id"
end

Expand Down