2018-07-16 12:31:01 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
module Clusters
|
|
|
|
module Gcp
|
|
|
|
class ProvisionService
|
2019-09-22 20:06:29 -04:00
|
|
|
CLOUD_RUN_ADDONS = %i[http_load_balancing istio_config cloud_run_config].freeze
|
|
|
|
|
2017-10-13 13:21:23 -04:00
|
|
|
attr_reader :provider
|
|
|
|
|
|
|
|
def execute(provider)
|
|
|
|
@provider = provider
|
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
get_operation_id do |operation_id|
|
|
|
|
if provider.make_creating(operation_id)
|
|
|
|
WaitForClusterCreationWorker.perform_in(
|
|
|
|
Clusters::Gcp::VerifyProvisionStatusService::INITIAL_INTERVAL,
|
2017-11-01 12:10:41 -04:00
|
|
|
provider.cluster_id)
|
2017-10-23 04:36:35 -04:00
|
|
|
else
|
|
|
|
provider.make_errored!("Failed to update provider record; #{provider.errors}")
|
|
|
|
end
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
2017-10-23 04:36:35 -04:00
|
|
|
end
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
private
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
def get_operation_id
|
2019-09-22 20:06:29 -04:00
|
|
|
enable_addons = provider.cloud_run? ? CLOUD_RUN_ADDONS : []
|
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
operation = provider.api_client.projects_zones_clusters_create(
|
|
|
|
provider.gcp_project_id,
|
|
|
|
provider.zone,
|
|
|
|
provider.cluster.name,
|
|
|
|
provider.num_nodes,
|
2018-09-07 00:23:45 -04:00
|
|
|
machine_type: provider.machine_type,
|
2019-09-22 20:06:29 -04:00
|
|
|
legacy_abac: provider.legacy_abac,
|
|
|
|
enable_addons: enable_addons
|
2018-09-07 00:23:45 -04:00
|
|
|
)
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
unless operation.status == 'PENDING' || operation.status == 'RUNNING'
|
|
|
|
return provider.make_errored!("Operation status is unexpected; #{operation.status_message}")
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
operation_id = provider.api_client.parse_operation_id(operation.self_link)
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
unless operation_id
|
|
|
|
return provider.make_errored!('Can not find operation_id from self_link')
|
|
|
|
end
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
yield(operation_id)
|
2017-10-13 13:21:23 -04:00
|
|
|
|
2017-10-23 04:36:35 -04:00
|
|
|
rescue Google::Apis::ServerError, Google::Apis::ClientError, Google::Apis::AuthorizationError => e
|
|
|
|
provider.make_errored!("Failed to request to CloudPlatform; #{e.message}")
|
2017-10-13 13:21:23 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|