debian-mirror-gitlab/app/services/google_cloud/create_service_accounts_service.rb

57 lines
1.7 KiB
Ruby
Raw Normal View History

2022-03-02 08:16:31 +05:30
# frozen_string_literal: true
module GoogleCloud
class CreateServiceAccountsService < :: BaseService
def execute
service_account = google_api_client.create_service_account(gcp_project_id, service_account_name, service_account_desc)
service_account_key = google_api_client.create_service_account_key(gcp_project_id, service_account.unique_id)
2022-04-04 11:22:00 +05:30
google_api_client.grant_service_account_roles(gcp_project_id, service_account.email)
2022-03-02 08:16:31 +05:30
service_accounts_service.add_for_project(
environment_name,
service_account.project_id,
service_account.to_json,
service_account_key.to_json,
2022-05-07 20:08:51 +05:30
ProtectedBranch.protected?(project, environment_name) || ProtectedTag.protected?(project, environment_name)
2022-03-02 08:16:31 +05:30
)
ServiceResponse.success(message: _('Service account generated successfully'), payload: {
service_account: service_account,
service_account_key: service_account_key
})
end
private
def google_oauth2_token
@params[:google_oauth2_token]
end
def gcp_project_id
@params[:gcp_project_id]
end
def environment_name
@params[:environment_name]
end
def google_api_client
2022-04-04 11:22:00 +05:30
@google_api_client_instance ||= GoogleApi::CloudPlatform::Client.new(google_oauth2_token, nil)
2022-03-02 08:16:31 +05:30
end
def service_accounts_service
GoogleCloud::ServiceAccountsService.new(project)
end
def service_account_name
"GitLab :: #{project.name} :: #{environment_name}"
end
def service_account_desc
"GitLab generated service account for project '#{project.name}' and environment '#{environment_name}'"
end
end
end
GoogleCloud::CreateServiceAccountsService.prepend_mod