Commit 076398d8 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch '34758-fix-code-reuse-clusters-applications_controller' into 'master'

Fix code reuse issue in Projects::Clusters::ApplicationController#index

See merge request gitlab-org/gitlab-ce!22182
parents 9bbad400 504cbb27
...@@ -2,31 +2,20 @@ ...@@ -2,31 +2,20 @@
class Projects::Clusters::ApplicationsController < Projects::ApplicationController class Projects::Clusters::ApplicationsController < Projects::ApplicationController
before_action :cluster before_action :cluster
before_action :application_class, only: [:create]
before_action :authorize_read_cluster! before_action :authorize_read_cluster!
before_action :authorize_create_cluster!, only: [:create] before_action :authorize_create_cluster!, only: [:create]
# rubocop: disable CodeReuse/ActiveRecord
def create def create
application = @application_class.find_or_initialize_by(cluster: @cluster) Clusters::Applications::CreateService
.new(@cluster, current_user, create_cluster_application_params)
if application.has_attribute?(:hostname) .execute(request)
application.hostname = params[:hostname]
end
if application.respond_to?(:oauth_application)
application.oauth_application = create_oauth_application(application)
end
application.save!
Clusters::Applications::ScheduleInstallationService.new(project, current_user).execute(application)
head :no_content head :no_content
rescue Clusters::Applications::CreateService::InvalidApplicationError
render_404
rescue StandardError rescue StandardError
head :bad_request head :bad_request
end end
# rubocop: enable CodeReuse/ActiveRecord
private private
...@@ -34,18 +23,7 @@ class Projects::Clusters::ApplicationsController < Projects::ApplicationControll ...@@ -34,18 +23,7 @@ class Projects::Clusters::ApplicationsController < Projects::ApplicationControll
@cluster ||= project.clusters.find(params[:id]) || render_404 @cluster ||= project.clusters.find(params[:id]) || render_404
end end
def application_class def create_cluster_application_params
@application_class ||= Clusters::Cluster::APPLICATIONS[params[:application]] || render_404 params.permit(:application, :hostname)
end
def create_oauth_application(application)
oauth_application_params = {
name: params[:application],
redirect_uri: application.callback_url,
scopes: 'api read_user openid',
owner: current_user
}
Applications::CreateService.new(current_user, oauth_application_params).execute(request)
end end
end end
...@@ -9,6 +9,7 @@ module Applications ...@@ -9,6 +9,7 @@ module Applications
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
# EE would override and use `request` arg
def execute(request) def execute(request)
Doorkeeper::Application.create(@params) Doorkeeper::Application.create(@params)
end end
......
# frozen_string_literal: true
module Clusters
module Applications
class CreateService
InvalidApplicationError = Class.new(StandardError)
attr_reader :cluster, :current_user, :params
def initialize(cluster, user, params = {})
@cluster = cluster
@current_user = user
@params = params.dup
end
def execute(request)
create_application.tap do |application|
if application.has_attribute?(:hostname)
application.hostname = params[:hostname]
end
if application.respond_to?(:oauth_application)
application.oauth_application = create_oauth_application(application, request)
end
application.save!
Clusters::Applications::ScheduleInstallationService.new(application).execute
end
end
private
def create_application
builder.call(@cluster)
end
def builder
builders[application_name] || raise(InvalidApplicationError, "invalid application: #{application_name}")
end
def builders
{
"helm" => -> (cluster) { cluster.application_helm || cluster.build_application_helm },
"ingress" => -> (cluster) { cluster.application_ingress || cluster.build_application_ingress },
"prometheus" => -> (cluster) { cluster.application_prometheus || cluster.build_application_prometheus },
"runner" => -> (cluster) { cluster.application_runner || cluster.build_application_runner },
"jupyter" => -> (cluster) { cluster.application_jupyter || cluster.build_application_jupyter }
}
end
def application_name
params[:application]
end
def create_oauth_application(application, request)
oauth_application_params = {
name: params[:application],
redirect_uri: application.callback_url,
scopes: 'api read_user openid',
owner: current_user
}
::Applications::CreateService.new(current_user, oauth_application_params).execute(request)
end
end
end
end
...@@ -2,8 +2,14 @@ ...@@ -2,8 +2,14 @@
module Clusters module Clusters
module Applications module Applications
class ScheduleInstallationService < ::BaseService class ScheduleInstallationService
def execute(application) attr_reader :application
def initialize(application)
@application = application
end
def execute
application.make_scheduled! application.make_scheduled!
ClusterInstallAppWorker.perform_async(application.name, application.id) ClusterInstallAppWorker.perform_async(application.name, application.id)
......
# frozen_string_literal: true
require "spec_helper" require "spec_helper"
describe ::Applications::CreateService do describe ::Applications::CreateService do
include TestRequestHelpers
let(:user) { create(:user) } let(:user) { create(:user) }
let(:params) { attributes_for(:application) } let(:params) { attributes_for(:application) }
let(:request) do
if Gitlab.rails5?
ActionController::TestRequest.new({ remote_ip: "127.0.0.1" }, ActionController::TestSession.new)
else
ActionController::TestRequest.new(remote_ip: "127.0.0.1")
end
end
subject { described_class.new(user, params) } subject { described_class.new(user, params) }
it { expect { subject.execute(request) }.to change { Doorkeeper::Application.count }.by(1) } it { expect { subject.execute(test_request) }.to change { Doorkeeper::Application.count }.by(1) }
end end
# frozen_string_literal: true
require 'spec_helper'
describe Clusters::Applications::CreateService do
include TestRequestHelpers
let(:cluster) { create(:cluster, :project, :provided_by_gcp) }
let(:user) { create(:user) }
let(:params) { { application: 'helm' } }
let(:service) { described_class.new(cluster, user, params) }
describe '#execute' do
before do
allow(ClusterInstallAppWorker).to receive(:perform_async)
end
subject { service.execute(test_request) }
it 'creates an application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_helm)
end
it 'schedules an install via worker' do
expect(ClusterInstallAppWorker).to receive(:perform_async).with('helm', anything).once
subject
end
context 'jupyter application' do
let(:params) do
{
application: 'jupyter',
hostname: 'example.com'
}
end
before do
allow_any_instance_of(Clusters::Applications::ScheduleInstallationService).to receive(:execute)
end
it 'creates the application' do
expect do
subject
cluster.reload
end.to change(cluster, :application_jupyter)
end
it 'sets the hostname' do
expect(subject.hostname).to eq('example.com')
end
it 'sets the oauth_application' do
expect(subject.oauth_application).to be_present
end
end
context 'invalid application' do
let(:params) { { application: 'non-existent' } }
it 'raises an error' do
expect { subject }.to raise_error(Clusters::Applications::CreateService::InvalidApplicationError)
end
end
end
end
...@@ -10,14 +10,13 @@ describe Clusters::Applications::ScheduleInstallationService do ...@@ -10,14 +10,13 @@ describe Clusters::Applications::ScheduleInstallationService do
expect(ClusterInstallAppWorker).not_to receive(:perform_async) expect(ClusterInstallAppWorker).not_to receive(:perform_async)
count_before = count_scheduled count_before = count_scheduled
expect { service.execute(application) }.to raise_error(StandardError) expect { service.execute }.to raise_error(StandardError)
expect(count_scheduled).to eq(count_before) expect(count_scheduled).to eq(count_before)
end end
end end
describe '#execute' do describe '#execute' do
let(:project) { double(:project) } let(:service) { described_class.new(application) }
let(:service) { described_class.new(project, nil) }
context 'when application is installable' do context 'when application is installable' do
let(:application) { create(:clusters_applications_helm, :installable) } let(:application) { create(:clusters_applications_helm, :installable) }
...@@ -25,7 +24,7 @@ describe Clusters::Applications::ScheduleInstallationService do ...@@ -25,7 +24,7 @@ describe Clusters::Applications::ScheduleInstallationService do
it 'make the application scheduled' do it 'make the application scheduled' do
expect(ClusterInstallAppWorker).to receive(:perform_async).with(application.name, kind_of(Numeric)).once expect(ClusterInstallAppWorker).to receive(:perform_async).with(application.name, kind_of(Numeric)).once
expect { service.execute(application) }.to change { application.class.with_status(:scheduled).count }.by(1) expect { service.execute }.to change { application.class.with_status(:scheduled).count }.by(1)
end end
end end
......
# frozen_string_literal: true
module TestRequestHelpers
def test_request(remote_ip: '127.0.0.1')
if Gitlab.rails5?
ActionController::TestRequest.new({ remote_ip: remote_ip }, ActionController::TestSession.new)
else
ActionController::TestRequest.new(remote_ip: remote_ip)
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