module Kura
Constants
- VERSION
Public Class Methods
client(project_id=nil, email_address=nil, private_key=nil, scope: nil, http_options: {timeout: 60})
click to toggle source
Kura.client
¶ ↑
Create Kura::Client
object with GCP credential.
Kura.client(json_key_file) Kura.client(json_key_hash) Kura.client(gcp_project_id, email_address, prm_file) Kura.client(gcp_project_id, email_address, prm_file_contents) Kura.client(gcp_project_id, email_address, private_key_object)
# File lib/kura.rb, line 46 def self.client(project_id=nil, email_address=nil, private_key=nil, scope: nil, http_options: {timeout: 60}) if email_address.nil? and private_key.nil? if project_id.is_a?(String) credential = JSON.parse(File.binread(project_id)) elsif project_id.is_a?(Hash) credential = project_id else begin # get project id from metadata server project_id = URI.open("http://metadata/computeMetadata/v1/project/project-id", "Metadata-Flavor" => "Google") do |f| f.read end return self::Client.new(default_project_id: project_id) rescue raise ArgumentError, "#{self.class.name}.client accept JSON credential file path or decoded Hash object." end end project_id = credential["project_id"] email_address = credential["client_email"] private_key = get_private_key(credential["private_key"]) elsif private_key private_key = get_private_key(private_key) end self::Client.new(default_project_id: project_id, email_address: email_address, private_key: private_key, scope: scope, http_options: http_options) end
get_private_key(private_key)
click to toggle source
# File lib/kura.rb, line 18 def self.get_private_key(private_key) private_key_file = nil private_key_content = nil if private_key.respond_to?(:to_path) private_key_file = private_key.to_path elsif private_key.is_a?(String) and File.readable?(private_key) private_key_file = private_key end if private_key_file private_key_content = File.binread(private_key_file) else private_key_content = private_key end if private_key_content private_key = OpenSSL::PKey.read(private_key_content) end private_key end