2021-11-24 10:14:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class Projects::GoogleCloud::ServiceAccountsController < Projects::GoogleCloud::BaseController
|
|
|
|
before_action :validate_gcp_token!
|
|
|
|
|
|
|
|
def index
|
|
|
|
@google_cloud_path = project_google_cloud_index_path(project)
|
|
|
|
google_api_client = GoogleApi::CloudPlatform::Client.new(token_in_session, nil)
|
|
|
|
gcp_projects = google_api_client.list_projects
|
|
|
|
|
|
|
|
if gcp_projects.empty?
|
2021-12-02 22:14:42 -05:00
|
|
|
@js_data = { screen: 'no_gcp_projects' }.to_json
|
2022-03-08 16:20:24 -05:00
|
|
|
track_event('service_accounts#index', 'form_error', 'no_gcp_projects')
|
2021-11-24 10:14:19 -05:00
|
|
|
render status: :unauthorized, template: 'projects/google_cloud/errors/no_gcp_projects'
|
|
|
|
else
|
2022-03-02 10:16:07 -05:00
|
|
|
params = { per_page: 50 }
|
|
|
|
branches = BranchesFinder.new(project.repository, params).execute(gitaly_pagination: true)
|
|
|
|
tags = TagsFinder.new(project.repository, params).execute(gitaly_pagination: true)
|
|
|
|
refs = (branches + tags).map(&:name)
|
2022-03-15 08:07:44 -04:00
|
|
|
js_data = {
|
2021-12-02 22:14:42 -05:00
|
|
|
screen: 'service_accounts_form',
|
2021-11-24 10:14:19 -05:00
|
|
|
gcpProjects: gcp_projects,
|
2022-03-02 10:16:07 -05:00
|
|
|
refs: refs,
|
2021-11-24 10:14:19 -05:00
|
|
|
cancelPath: project_google_cloud_index_path(project)
|
2022-03-15 08:07:44 -04:00
|
|
|
}
|
|
|
|
@js_data = js_data.to_json
|
2022-03-08 16:20:24 -05:00
|
|
|
|
2022-03-15 08:07:44 -04:00
|
|
|
track_event('service_accounts#index', 'form_success', js_data)
|
2021-11-24 10:14:19 -05:00
|
|
|
end
|
|
|
|
rescue Google::Apis::ClientError => error
|
2022-03-08 16:20:24 -05:00
|
|
|
handle_gcp_error('service_accounts#index', error)
|
2021-11-24 10:14:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2022-03-02 10:16:07 -05:00
|
|
|
permitted_params = params.permit(:gcp_project, :ref)
|
|
|
|
|
2022-01-18 10:14:54 -05:00
|
|
|
response = GoogleCloud::CreateServiceAccountsService.new(
|
|
|
|
project,
|
|
|
|
current_user,
|
|
|
|
google_oauth2_token: token_in_session,
|
2022-03-02 10:16:07 -05:00
|
|
|
gcp_project_id: permitted_params[:gcp_project],
|
|
|
|
environment_name: permitted_params[:ref]
|
2022-01-18 10:14:54 -05:00
|
|
|
).execute
|
2021-11-24 10:14:19 -05:00
|
|
|
|
2022-03-08 16:20:24 -05:00
|
|
|
track_event('service_accounts#create', 'form_submit', response)
|
2022-01-18 10:14:54 -05:00
|
|
|
redirect_to project_google_cloud_index_path(project), notice: response.message
|
2021-11-24 10:14:19 -05:00
|
|
|
rescue Google::Apis::ClientError, Google::Apis::ServerError, Google::Apis::AuthorizationError => error
|
2022-03-08 16:20:24 -05:00
|
|
|
handle_gcp_error('service_accounts#create', error)
|
2021-11-24 10:14:19 -05:00
|
|
|
end
|
|
|
|
end
|