2018-12-05 23:21:45 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
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]
|
|
|
|
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: disable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
def create
|
2018-11-08 19:23:39 +05:30
|
|
|
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!
|
|
|
|
|
|
|
|
Clusters::Applications::ScheduleInstallationService.new(project, current_user).execute(application)
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
head :no_content
|
|
|
|
rescue StandardError
|
|
|
|
head :bad_request
|
|
|
|
end
|
2018-12-05 23:21:45 +05:30
|
|
|
# rubocop: enable CodeReuse/ActiveRecord
|
2018-03-17 18:26:18 +05:30
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def cluster
|
|
|
|
@cluster ||= project.clusters.find(params[:id]) || render_404
|
|
|
|
end
|
|
|
|
|
|
|
|
def application_class
|
|
|
|
@application_class ||= Clusters::Cluster::APPLICATIONS[params[:application]] || render_404
|
|
|
|
end
|
2018-11-08 19:23:39 +05:30
|
|
|
|
|
|
|
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
|
2018-03-17 18:26:18 +05:30
|
|
|
end
|