Module: Kura

Defined in:
lib/kura.rb,
lib/kura/client.rb,
lib/kura/version.rb

Defined Under Namespace

Classes: ApiError, Client, TimeoutError

Constant Summary collapse

VERSION =
"1.0.3"

Class Method Summary collapse

Class Method Details

.client(project_id = nil, email_address = nil, private_key = nil, scope: nil, http_options: {timeout: 60}) ⇒ Object

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)


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# 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) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# 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