Class: Train::Transports::Gcp::Connection

Inherits:
BaseConnection
  • Object
show all
Defined in:
lib/train/transports/gcp.rb

Instance Method Summary collapse

Constructor Details

#initialize(options) ⇒ Connection

Returns a new instance of Connection.



31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/train/transports/gcp.rb', line 31

def initialize(options)
  super(options)

  # additional GCP platform metadata
  release = Gem.loaded_specs['google-api-client'].version
  @platform_details = { release: "google-api-client-v#{release}" }

  # Initialize the client object cache
  @cache_enabled[:api_call] = true
  @cache[:api_call] = {}

  connect
end

Instance Method Details

#connectObject



72
73
74
75
76
77
78
79
80
# File 'lib/train/transports/gcp.rb', line 72

def connect
  ENV['GOOGLE_APPLICATION_CREDENTIALS'] = @options[:google_application_credentials] if @options[:google_application_credentials]
  ENV['GOOGLE_CLOUD_PROJECT'] = @options[:google_cloud_project] if @options[:google_cloud_project]
  # GCP initialization
  scopes = ['https://www.googleapis.com/auth/cloud-platform',
            'https://www.googleapis.com/auth/compute']
  authorization = Google::Auth.get_application_default(scopes)
  Google::Apis::RequestOptions.default.authorization = authorization
end

#gcp_client(klass) ⇒ Object

Let’s allow for other clients too



67
68
69
70
# File 'lib/train/transports/gcp.rb', line 67

def gcp_client(klass)
  return klass.new unless cache_enabled?(:api_call)
  @cache[:api_call][klass.to_s.to_sym] ||= klass.new
end

#gcp_compute_clientObject

Instantiate some named classes for ease of use



50
51
52
# File 'lib/train/transports/gcp.rb', line 50

def gcp_compute_client
  gcp_client(Google::Apis::ComputeV1::ComputeService)
end

#gcp_iam_clientObject



54
55
56
# File 'lib/train/transports/gcp.rb', line 54

def gcp_iam_client
  gcp_client(Google::Apis::IamV1::IamService)
end

#gcp_project_clientObject



58
59
60
# File 'lib/train/transports/gcp.rb', line 58

def gcp_project_client
  gcp_client(Google::Apis::CloudresourcemanagerV1::CloudResourceManagerService)
end

#gcp_storage_clientObject



62
63
64
# File 'lib/train/transports/gcp.rb', line 62

def gcp_storage_client
  gcp_client(Google::Apis::StorageV1::StorageService)
end

#platformObject



45
46
47
# File 'lib/train/transports/gcp.rb', line 45

def platform
  force_platform!('gcp', @platform_details)
end

#unique_identifierObject



86
87
88
89
# File 'lib/train/transports/gcp.rb', line 86

def unique_identifier
  # use auth client_id - same to retrieve for any of the clients but use IAM
  gcp_iam_client.request_options.authorization.client_id
end

#uriObject



82
83
84
# File 'lib/train/transports/gcp.rb', line 82

def uri
  "gcp://#{unique_identifier}"
end