2018-03-16 12:50:48 -04:00
|
|
|
require 'google/apis/compute_v1'
|
2017-09-26 07:34:49 -04:00
|
|
|
require 'google/apis/container_v1'
|
2017-12-15 17:54:50 -05:00
|
|
|
require 'google/apis/cloudbilling_v1'
|
2017-12-15 13:39:15 -05:00
|
|
|
require 'google/apis/cloudresourcemanager_v1'
|
2017-09-26 07:34:49 -04:00
|
|
|
|
2017-09-25 03:10:25 -04:00
|
|
|
module GoogleApi
|
|
|
|
module CloudPlatform
|
2017-09-30 11:54:22 -04:00
|
|
|
class Client < GoogleApi::Auth
|
2017-10-03 10:44:06 -04:00
|
|
|
SCOPE = 'https://www.googleapis.com/auth/cloud-platform'.freeze
|
2017-10-06 05:23:53 -04:00
|
|
|
LEAST_TOKEN_LIFE_TIME = 10.minutes
|
2017-10-01 04:48:21 -04:00
|
|
|
|
2017-09-25 13:11:26 -04:00
|
|
|
class << self
|
2017-09-30 11:54:22 -04:00
|
|
|
def session_key_for_token
|
2017-09-25 13:11:26 -04:00
|
|
|
:cloud_platform_access_token
|
|
|
|
end
|
2017-10-02 04:13:46 -04:00
|
|
|
|
|
|
|
def session_key_for_expires_at
|
|
|
|
:cloud_platform_expires_at
|
|
|
|
end
|
2017-10-06 08:28:40 -04:00
|
|
|
|
2017-10-06 10:14:14 -04:00
|
|
|
def new_session_key_for_redirect_uri
|
|
|
|
SecureRandom.hex.tap do |state|
|
|
|
|
yield session_key_for_redirect_uri(state)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def session_key_for_redirect_uri(state)
|
|
|
|
"cloud_platform_second_redirect_uri_#{state}"
|
2017-10-06 08:28:40 -04:00
|
|
|
end
|
2017-09-25 13:11:26 -04:00
|
|
|
end
|
|
|
|
|
2017-09-25 03:10:25 -04:00
|
|
|
def scope
|
2017-10-03 10:44:06 -04:00
|
|
|
SCOPE
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
|
2017-10-02 04:13:46 -04:00
|
|
|
def validate_token(expires_at)
|
|
|
|
return false unless access_token
|
|
|
|
return false unless expires_at
|
|
|
|
|
|
|
|
# Making sure that the token will have been still alive during the cluster creation.
|
2017-10-06 05:23:53 -04:00
|
|
|
return false if token_life_time(expires_at) < LEAST_TOKEN_LIFE_TIME
|
2017-10-02 04:13:46 -04:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
2017-09-26 10:05:12 -04:00
|
|
|
def projects_zones_clusters_get(project_id, zone, cluster_id)
|
2017-09-26 07:34:49 -04:00
|
|
|
service = Google::Apis::ContainerV1::ContainerService.new
|
|
|
|
service.authorization = access_token
|
|
|
|
|
2017-12-03 20:19:02 -05:00
|
|
|
service.get_zone_cluster(project_id, zone, cluster_id, options: user_agent_header)
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
|
2018-09-07 00:23:45 -04:00
|
|
|
def projects_zones_clusters_create(project_id, zone, cluster_name, cluster_size, machine_type:, legacy_abac:)
|
2017-09-26 07:34:49 -04:00
|
|
|
service = Google::Apis::ContainerV1::ContainerService.new
|
|
|
|
service.authorization = access_token
|
|
|
|
|
|
|
|
request_body = Google::Apis::ContainerV1::CreateClusterRequest.new(
|
2017-10-04 03:04:45 -04:00
|
|
|
{
|
|
|
|
"cluster": {
|
|
|
|
"name": cluster_name,
|
|
|
|
"initial_node_count": cluster_size,
|
|
|
|
"node_config": {
|
|
|
|
"machine_type": machine_type
|
2018-02-22 16:13:30 -05:00
|
|
|
},
|
|
|
|
"legacy_abac": {
|
2018-09-07 00:23:45 -04:00
|
|
|
"enabled": legacy_abac
|
2017-09-26 07:34:49 -04:00
|
|
|
}
|
|
|
|
}
|
2018-02-22 16:13:30 -05:00
|
|
|
}
|
|
|
|
)
|
2017-09-26 07:34:49 -04:00
|
|
|
|
2017-12-03 20:19:02 -05:00
|
|
|
service.create_cluster(project_id, zone, request_body, options: user_agent_header)
|
2017-09-26 07:34:49 -04:00
|
|
|
end
|
2017-09-25 13:11:26 -04:00
|
|
|
|
2017-09-26 10:05:12 -04:00
|
|
|
def projects_zones_operations(project_id, zone, operation_id)
|
|
|
|
service = Google::Apis::ContainerV1::ContainerService.new
|
|
|
|
service.authorization = access_token
|
|
|
|
|
2017-12-03 20:19:02 -05:00
|
|
|
service.get_zone_operation(project_id, zone, operation_id, options: user_agent_header)
|
2017-09-26 10:05:12 -04:00
|
|
|
end
|
|
|
|
|
2017-09-30 11:54:22 -04:00
|
|
|
def parse_operation_id(self_link)
|
2017-10-05 05:17:21 -04:00
|
|
|
m = self_link.match(%r{projects/.*/zones/.*/operations/(.*)})
|
|
|
|
m[1] if m
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
2017-10-06 05:23:53 -04:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def token_life_time(expires_at)
|
|
|
|
DateTime.strptime(expires_at, '%s').to_time.utc - Time.now.utc
|
|
|
|
end
|
2017-12-03 18:53:04 -05:00
|
|
|
|
|
|
|
def user_agent_header
|
2017-12-04 07:43:45 -05:00
|
|
|
Google::Apis::RequestOptions.new.tap do |options|
|
|
|
|
options.header = { 'User-Agent': "GitLab/#{Gitlab::VERSION.match('(\d+\.\d+)').captures.first} (GPN:GitLab;)" }
|
|
|
|
end
|
2017-12-03 18:53:04 -05:00
|
|
|
end
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|