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_HEADER =
{ '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

.authObject



32
33
34
# File 'lib/magellan/gcs/proxy/gcp.rb', line 32

def auth
  @auth ||= new_auth
end

.loggingObject



70
71
72
# File 'lib/magellan/gcs/proxy/gcp.rb', line 70

def logging
  @logging ||= Google::Cloud::Logging.new(project: project_id)
end

.new_authObject



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_idObject



19
20
21
# File 'lib/magellan/gcs/proxy/gcp.rb', line 19

def project_id
  @project_id ||= retrieve_project_id
end

.pubsubObject



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

.resetObject



74
75
76
# File 'lib/magellan/gcs/proxy/gcp.rb', line 74

def reset
  instance_variables.each { |ivar| instance_variable_set(ivar, nil) }
end

.retrieve_metadata(key) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/magellan/gcs/proxy/gcp.rb', line 45

def (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_idObject



23
24
25
# File 'lib/magellan/gcs/proxy/gcp.rb', line 23

def retrieve_project_id
  ENV['BLOCKS_BATCH_PROJECT_ID'] || ('project/project-id')
end

.storageObject



54
55
56
# File 'lib/magellan/gcs/proxy/gcp.rb', line 54

def storage
  @storage ||= Google::Cloud::Storage.new(project: project_id)
end

.subscriptionObject



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