Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
path: root/app
diff options
context:
space:
mode:
authorThong Kuah <tkuah@gitlab.com>2018-10-10 06:47:27 +0300
committerThong Kuah <tkuah@gitlab.com>2018-10-15 01:07:47 +0300
commitcc1ccbf83a4a20f367011a6aec3d158214cfeb34 (patch)
treee593c381c5df19daf43481011e0ad69ad0ceefa5 /app
parentda4f77957ccff7cdf22110ef56a4286bbc5a7749 (diff)
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.
Diffstat (limited to 'app')
-rw-r--r--app/controllers/projects/clusters/applications_controller.rb34
-rw-r--r--app/services/applications/create_service.rb1
-rw-r--r--app/services/clusters/applications/create_service.rb66
3 files changed, 74 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