Commit cc1ccbf8 authored by Thong Kuah's avatar Thong Kuah

Move non-controller code into dedicated service

This should help with code re-use when we create applications for group
level cluster next.

Change `find_or_initialize_by` to explicitly find or build the right
association based on the application name. The benefit here is that we
use the associations on @cluster rather than querying from the other
side of the association.
parent da4f7795
...@@ -2,31 +2,22 @@ ...@@ -2,31 +2,22 @@
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) application = 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) 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 +25,7 @@ class Projects::Clusters::ApplicationsController < Projects::ApplicationControll ...@@ -34,18 +25,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!
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
# 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
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