diff options
4 files changed, 137 insertions, 27 deletions
diff --git a/app/controllers/projects/clusters/applications_controller.rb b/app/controllers/projects/clusters/applications_controller.rb index c356f8d2987..a3fc4f4741b 100644 --- a/app/controllers/projects/clusters/applications_controller.rb +++ b/app/controllers/projects/clusters/applications_controller.rb @@ -2,31 +2,22 @@ class Projects::Clusters::ApplicationsController < Projects::ApplicationController before_action :cluster - before_action :application_class, only: [:create] before_action :authorize_read_cluster! before_action :authorize_create_cluster!, only: [:create] - # rubocop: disable CodeReuse/ActiveRecord def create - application = @application_class.find_or_initialize_by(cluster: @cluster) - - if application.has_attribute?(:hostname) - application.hostname = params[:hostname] - end - - if application.respond_to?(:oauth_application) - application.oauth_application = create_oauth_application(application) - end - - application.save! + application = Clusters::Applications::CreateService + .new(@cluster, current_user, create_cluster_application_params) + .execute(request) Clusters::Applications::ScheduleInstallationService.new(project, current_user).execute(application) head :no_content + rescue Clusters::Applications::CreateService::InvalidApplicationError + render_404 rescue StandardError head :bad_request end - # rubocop: enable CodeReuse/ActiveRecord private @@ -34,18 +25,7 @@ class Projects::Clusters::ApplicationsController < Projects::ApplicationControll @cluster ||= project.clusters.find(params[:id]) || render_404 end - def application_class - @application_class ||= Clusters::Cluster::APPLICATIONS[params[:application]] || render_404 - 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) + def create_cluster_application_params + params.permit(:application, :hostname) end end diff --git a/app/services/applications/create_service.rb b/app/services/applications/create_service.rb index 3d88c4f064e..b6c30da4d3a 100644 --- a/app/services/applications/create_service.rb +++ b/app/services/applications/create_service.rb @@ -9,6 +9,7 @@ module Applications end # rubocop: enable CodeReuse/ActiveRecord + # EE would override and use `request` arg def execute(request) Doorkeeper::Application.create(@params) end diff --git a/app/services/clusters/applications/create_service.rb b/app/services/clusters/applications/create_service.rb new file mode 100644 index 00000000000..d69e8612eed --- /dev/null +++ b/app/services/clusters/applications/create_service.rb @@ -0,0 +1,66 @@ +# 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! + 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 diff --git a/spec/services/clusters/applications/create_service_spec.rb b/spec/services/clusters/applications/create_service_spec.rb new file mode 100644 index 00000000000..58d9682602a --- /dev/null +++ b/spec/services/clusters/applications/create_service_spec.rb @@ -0,0 +1,63 @@ +# frozen_string_literal: true + +require 'spec_helper' + +describe Clusters::Applications::CreateService do + let(:cluster) { create(:cluster) } + let(:user) { create(:user) } + let(:params) { { application: 'helm' } } + let(:service) { described_class.new(cluster, user, params) } + + 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 + + describe '#execute' do + subject { service.execute(request) } + + it 'creates an application' do + expect do + subject + + cluster.reload + end.to change(cluster, :application_helm) + end + + context 'jupyter application' do + let(:params) do + { + application: 'jupyter', + hostname: 'example.com' + } + 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 |