-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_check.rb
More file actions
44 lines (39 loc) · 1.37 KB
/
google_check.rb
File metadata and controls
44 lines (39 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
require "google/cloud/storage"
class Check
def initialize
# So google sdk newer versions use GOOGLE_CLOUD_PROJECT instead of GOOGLE_PROJECT
# Found out between google-cloud-storage-1.35.0 and google-cloud-storage-1.28.0
# Though it seems like an library underneath that with the change.
# Keeping backwards compatibility to not create breakage users who already have GOOGLE_PROJECT
# But then setting GOOGLE_CLOUD_PROJECT so it works with the SDK.
# For users, who set GOOGLE_CLOUD_PROJECT that will work also.
ENV['GOOGLE_CLOUD_PROJECT'] ||= ENV['GOOGLE_PROJECT']
end
def call
# Make an authenticated API request
puts "Listing gcs buckets as a test"
if storage.buckets.empty?
puts "There are no GCS buckets in this project. But the Googgle SDK API successful."
else
storage.buckets.each do |bucket|
puts bucket.name
end
end
puts "Successfully connected to Google API with your GOOGLE_APPLICATION_CREDENTIALS"
end
def storage
@storage ||= Google::Cloud::Storage.new
end
def creds
return @creds if @creds
creds_path = ENV['GOOGLE_APPLICATION_CREDENTIALS']
unless File.exist?(creds_path)
puts "ERROR: #{creds_path} does not exist. Double check GOOGLE_APPLICATION_CREDENTIALS"
exit 1
end
@creds = JSON.load(IO.read(creds_path))
end
end
if __FILE__ == $0
Check.new.call
end