2017-09-26 07:34:49 -04:00
|
|
|
require 'google/apis/container_v1'
|
|
|
|
|
2017-09-25 03:10:25 -04:00
|
|
|
module GoogleApi
|
|
|
|
module CloudPlatform
|
|
|
|
class Client < GoogleApi::Authentication
|
2017-09-25 13:11:26 -04:00
|
|
|
class << self
|
|
|
|
def token_in_session
|
|
|
|
:cloud_platform_access_token
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-09-25 03:10:25 -04:00
|
|
|
def scope
|
|
|
|
'https://www.googleapis.com/auth/cloud-platform'
|
|
|
|
end
|
|
|
|
|
2017-09-28 11:08:11 -04:00
|
|
|
##
|
|
|
|
# Exception
|
|
|
|
# Google::Apis::ClientError:
|
|
|
|
# Google::Apis::AuthorizationError:
|
|
|
|
##
|
|
|
|
|
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-09-28 11:08:11 -04:00
|
|
|
begin
|
|
|
|
cluster = service.get_zone_cluster(project_id, zone, cluster_id)
|
|
|
|
rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => e
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2017-09-26 10:05:12 -04:00
|
|
|
puts "#{self.class.name} - #{__callee__}: cluster: #{cluster.inspect}"
|
|
|
|
cluster
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
|
2017-09-26 07:34:49 -04:00
|
|
|
# Responce exmaple
|
2017-09-26 10:05:12 -04:00
|
|
|
# TODO: machine_type : Defailt 3.75 GB
|
|
|
|
def projects_zones_clusters_create(project_id, zone, cluster_name, cluster_size:, machine_type:)
|
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(
|
|
|
|
{
|
|
|
|
"cluster": {
|
|
|
|
"name": cluster_name,
|
|
|
|
"initial_node_count": cluster_size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
2017-09-26 10:05:12 -04:00
|
|
|
begin
|
|
|
|
operation = service.create_cluster(project_id, zone, request_body)
|
|
|
|
rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => e
|
2017-09-28 11:08:11 -04:00
|
|
|
return nil
|
2017-09-26 10:05:12 -04:00
|
|
|
end
|
2017-09-28 11:08:11 -04:00
|
|
|
|
2017-09-26 10:05:12 -04:00
|
|
|
puts "#{self.class.name} - #{__callee__}: operation: #{operation.inspect}"
|
|
|
|
operation
|
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-09-28 11:08:11 -04:00
|
|
|
begin
|
|
|
|
operation = service.get_zone_operation(project_id, zone, operation_id)
|
|
|
|
rescue Google::Apis::ClientError, Google::Apis::AuthorizationError => e
|
|
|
|
return nil
|
|
|
|
end
|
|
|
|
|
2017-09-28 05:11:17 -04:00
|
|
|
puts "#{self.class.name} - #{__callee__}: operation: #{operation.inspect}"
|
2017-09-26 10:05:12 -04:00
|
|
|
operation
|
|
|
|
end
|
|
|
|
|
2017-09-28 05:11:17 -04:00
|
|
|
def parse_self_link(self_link)
|
2017-09-26 10:05:12 -04:00
|
|
|
ret = self_link.match(/projects\/(.*)\/zones\/(.*)\/operations\/(.*)/)
|
|
|
|
|
2017-09-28 05:11:17 -04:00
|
|
|
return ret[1], ret[2], ret[3] # project_id, zone, operation_id
|
2017-09-25 03:10:25 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|