Module: Magellan::Gcs::Proxy::GCP
- Extended by:
- Log
- Includes:
- Log
- Defined in:
- lib/magellan/gcs/proxy/gcp.rb
Constant Summary
collapse
- SCOPES =
[
'https://www.googleapis.com/auth/devstorage.full_control',
'https://www.googleapis.com/auth/pubsub',
].freeze
- METADATA_HOST =
'metadata.google.internal'.freeze
- METADATA_PATH_BASE =
'/computeMetadata/v1/'.freeze
{ 'Metadata-Flavor' => 'Google' }.freeze
Constants included
from Log
Log::CLOUD_LOGGING_RESOURCE_KEYS
Class Method Summary
collapse
Methods included from Log
build_cloud_logging_logger, build_logger, build_loggers, logger, loggers, verbose
Class Method Details
.auth ⇒ Object
32
33
34
|
# File 'lib/magellan/gcs/proxy/gcp.rb', line 32
def auth
@auth ||= new_auth
end
|
.logging ⇒ Object
70
71
72
|
# File 'lib/magellan/gcs/proxy/gcp.rb', line 70
def logging
@logging ||= Google::Cloud::Logging.new(project: project_id)
end
|
.new_auth ⇒ Object
36
37
38
39
|
# File 'lib/magellan/gcs/proxy/gcp.rb', line 36
def new_auth
logger.debug("#{self.class.name}#new_auth")
Google::Auth.get_application_default(SCOPES).tap(&:fetch_access_token!)
end
|
.project_id ⇒ Object
19
20
21
|
# File 'lib/magellan/gcs/proxy/gcp.rb', line 19
def project_id
@project_id ||= retrieve_project_id
end
|
.pubsub ⇒ Object
58
59
60
|
# File 'lib/magellan/gcs/proxy/gcp.rb', line 58
def pubsub
@pubsub ||= Google::Apis::PubsubV1::PubsubService.new.tap { |api| api.authorization = auth }
end
|
.reset ⇒ Object
74
75
76
|
# File 'lib/magellan/gcs/proxy/gcp.rb', line 74
def reset
instance_variables.each { |ivar| instance_variable_set(ivar, nil) }
end
|
45
46
47
48
49
50
51
52
|
# File 'lib/magellan/gcs/proxy/gcp.rb', line 45
def retrieve_metadata(key)
http = Net::HTTP.new(METADATA_HOST)
res = http.get(METADATA_PATH_BASE + key, METADATA_HEADER)
case res.code
when /\A2\d{2}\z/ then res.body
else raise "[#{res.code}] #{res.body}"
end
end
|
.retrieve_project_id ⇒ Object
23
24
25
|
# File 'lib/magellan/gcs/proxy/gcp.rb', line 23
def retrieve_project_id
ENV['BLOCKS_BATCH_PROJECT_ID'] || retrieve_metadata('project/project-id')
end
|
.storage ⇒ Object
54
55
56
|
# File 'lib/magellan/gcs/proxy/gcp.rb', line 54
def storage
@storage ||= Google::Cloud::Storage.new(project: project_id)
end
|
.subscription ⇒ Object
62
63
64
65
66
67
68
|
# File 'lib/magellan/gcs/proxy/gcp.rb', line 62
def subscription
unless @subscription
@subscription = PubsubSubscription.new(ENV['BLOCKS_BATCH_PUBSUB_SUBSCRIPTION'] || 'test-subscription')
logger.info("subscription: #{@subscription.inspect}")
end
@subscription
end
|