2017-09-25 07:10:25 +00:00
|
|
|
class Projects::ClustersController < Projects::ApplicationController
|
2017-09-28 09:11:17 +00:00
|
|
|
before_action :cluster, except: [:login, :index, :new, :create]
|
2017-09-27 12:01:08 +00:00
|
|
|
before_action :authorize_google_api, except: [:login]
|
2017-09-25 07:10:25 +00:00
|
|
|
# before_action :authorize_admin_clusters! # TODO: Authentication
|
|
|
|
|
2017-09-27 12:01:08 +00:00
|
|
|
def login
|
|
|
|
begin
|
|
|
|
@authorize_url = api_client.authorize_url
|
|
|
|
rescue GoogleApi::Authentication::ConfigMissingError
|
2017-09-28 15:08:11 +00:00
|
|
|
# Show an alert message that gitlab.yml is not configured properly
|
2017-09-27 12:01:08 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-25 07:10:25 +00:00
|
|
|
def index
|
2017-09-28 09:11:17 +00:00
|
|
|
if project.clusters.any?
|
2017-09-28 15:08:11 +00:00
|
|
|
redirect_to edit_project_cluster_path(project, project.clusters.last.id)
|
2017-09-25 07:10:25 +00:00
|
|
|
else
|
2017-09-28 15:08:11 +00:00
|
|
|
redirect_to new_project_cluster_path(project)
|
2017-09-25 07:10:25 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def new
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2017-09-28 09:11:17 +00:00
|
|
|
begin
|
|
|
|
Ci::CreateClusterService.new(project, current_user, params)
|
|
|
|
.create_cluster_on_gke(api_client)
|
|
|
|
rescue Ci::CreateClusterService::UnexpectedOperationError => e
|
|
|
|
# TODO: error
|
2017-09-28 15:08:11 +00:00
|
|
|
puts "#{self.class.name} - #{__callee__}: e: #{e}"
|
2017-09-27 12:01:08 +00:00
|
|
|
end
|
2017-09-26 14:05:12 +00:00
|
|
|
|
2017-09-28 15:08:11 +00:00
|
|
|
redirect_to project_clusters_path(project)
|
2017-09-28 09:11:17 +00:00
|
|
|
end
|
2017-09-27 12:01:08 +00:00
|
|
|
|
2017-09-28 09:11:17 +00:00
|
|
|
##
|
|
|
|
# Return
|
|
|
|
# @status: The current status of the operation.
|
|
|
|
# @status_message: If an error has occurred, a textual description of the error.
|
|
|
|
def creation_status
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: cluster.creation_status(session[GoogleApi::CloudPlatform::Client.token_in_session])
|
2017-09-27 12:53:50 +00:00
|
|
|
end
|
|
|
|
end
|
2017-09-25 07:10:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
2017-09-28 15:08:11 +00:00
|
|
|
Ci::Cluster.transaction do
|
|
|
|
if params['enabled'] == 'true'
|
|
|
|
|
|
|
|
cluster.service.attributes = {
|
|
|
|
active: true,
|
|
|
|
api_url: cluster.endpoint,
|
|
|
|
ca_pem: cluster.ca_cert,
|
|
|
|
namespace: cluster.project_namespace,
|
|
|
|
token: cluster.token
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster.service.save!
|
|
|
|
else
|
|
|
|
cluster.service.update(active: false)
|
|
|
|
end
|
|
|
|
|
|
|
|
cluster.update(enabled: params['enabled'])
|
|
|
|
end
|
|
|
|
|
2017-09-25 07:10:25 +00:00
|
|
|
render :edit
|
|
|
|
end
|
|
|
|
|
2017-09-27 13:17:41 +00:00
|
|
|
def destroy
|
2017-09-28 15:08:11 +00:00
|
|
|
if cluster.destroy
|
|
|
|
redirect_to project_clusters_path(project), status: 302
|
|
|
|
else
|
|
|
|
redirect_to project_clusters_path(project),
|
|
|
|
status: :forbidden,
|
|
|
|
alert: _("Failed to remove the cluster")
|
|
|
|
end
|
2017-09-27 13:17:41 +00:00
|
|
|
end
|
|
|
|
|
2017-09-25 07:10:25 +00:00
|
|
|
private
|
|
|
|
|
|
|
|
def cluster
|
2017-09-28 09:11:17 +00:00
|
|
|
@cluster ||= project.clusters.find(params[:id])
|
2017-09-25 07:10:25 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
def api_client
|
|
|
|
@api_client ||=
|
|
|
|
GoogleApi::CloudPlatform::Client.new(
|
2017-09-25 17:11:26 +00:00
|
|
|
session[GoogleApi::CloudPlatform::Client.token_in_session],
|
2017-09-25 07:10:25 +00:00
|
|
|
callback_google_api_authorizations_url,
|
|
|
|
state: namespace_project_clusters_url.to_s
|
|
|
|
)
|
|
|
|
end
|
2017-09-26 08:46:09 +00:00
|
|
|
|
2017-09-27 12:01:08 +00:00
|
|
|
def authorize_google_api
|
|
|
|
unless session[GoogleApi::CloudPlatform::Client.token_in_session]
|
|
|
|
redirect_to action: 'login'
|
|
|
|
end
|
|
|
|
end
|
2017-09-25 07:10:25 +00:00
|
|
|
end
|