summaryrefslogtreecommitdiff
path: root/lib/google_api
diff options
context:
space:
mode:
authorGitLab Bot <gitlab-bot@gitlab.com>2021-11-24 15:14:19 +0000
committerGitLab Bot <gitlab-bot@gitlab.com>2021-11-24 15:14:19 +0000
commitb8d516a6876de74b68a800c5b69af9448b0de140 (patch)
tree3a56c8af8b36d03c20e9de4298f30b08cba0ea5a /lib/google_api
parent77b8390171a55d4593e3730551751d8348992f80 (diff)
downloadgitlab-ce-b8d516a6876de74b68a800c5b69af9448b0de140.tar.gz
Add latest changes from gitlab-org/gitlab@master
Diffstat (limited to 'lib/google_api')
-rw-r--r--lib/google_api/cloud_platform/client.rb47
1 files changed, 47 insertions, 0 deletions
diff --git a/lib/google_api/cloud_platform/client.rb b/lib/google_api/cloud_platform/client.rb
index c917debd3d9..9bd2309d2b7 100644
--- a/lib/google_api/cloud_platform/client.rb
+++ b/lib/google_api/cloud_platform/client.rb
@@ -1,10 +1,12 @@
# frozen_string_literal: true
+require 'securerandom'
require 'google/apis/compute_v1'
require 'google/apis/container_v1'
require 'google/apis/container_v1beta1'
require 'google/apis/cloudbilling_v1'
require 'google/apis/cloudresourcemanager_v1'
+require 'google/apis/iam_v1'
module GoogleApi
module CloudPlatform
@@ -83,6 +85,51 @@ module GoogleApi
m[1] if m
end
+ def list_projects
+ result = []
+
+ service = Google::Apis::CloudresourcemanagerV1::CloudResourceManagerService.new
+ service.authorization = access_token
+
+ response = service.fetch_all(items: :projects) do |token|
+ service.list_projects
+ end
+
+ # Google API results are paged by default, so we need to iterate through
+ response.each do |project|
+ result.append(project)
+ end
+
+ result
+ end
+
+ def create_service_account(gcp_project_id, display_name, description)
+ name = "projects/#{gcp_project_id}"
+
+ # initialize google iam service
+ service = Google::Apis::IamV1::IamService.new
+ service.authorization = access_token
+
+ # generate account id
+ random_account_id = "gitlab-" + SecureRandom.hex(11)
+
+ body_params = { account_id: random_account_id,
+ service_account: { display_name: display_name,
+ description: description } }
+
+ request_body = Google::Apis::IamV1::CreateServiceAccountRequest.new(**body_params)
+ service.create_service_account(name, request_body)
+ end
+
+ def create_service_account_key(gcp_project_id, service_account_id)
+ service = Google::Apis::IamV1::IamService.new
+ service.authorization = access_token
+
+ name = "projects/#{gcp_project_id}/serviceAccounts/#{service_account_id}"
+ request_body = Google::Apis::IamV1::CreateServiceAccountKeyRequest.new
+ service.create_service_account_key(name, request_body)
+ end
+
private
def make_cluster_options(cluster_name, cluster_size, machine_type, legacy_abac, enable_addons)