-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Initial import of transport for GCP. #283
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
78f0b78
Initial import of transport for GCP.
368d4fd
Updating named clients to use gcp_client to avoid duplication
0a3a587
Updating to latest berkshelf version to avoid conflicts with google d…
9986fbe
Removing unnecessary JSON import.
0019eef
Pinned google-protobuf version to avoid build issue.
a60564d
Remove unnecessary space.
9cadb9f
Downgrading berkshelf to help with integration testing.
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
# encoding: utf-8 | ||
|
||
require 'train/plugins' | ||
require 'google/apis' | ||
require 'google/apis/cloudresourcemanager_v1' | ||
require 'google/apis/compute_v1' | ||
require 'google/apis/storage_v1' | ||
require 'google/apis/iam_v1' | ||
require 'googleauth' | ||
|
||
module Train::Transports | ||
class Gcp < Train.plugin(1) | ||
name 'gcp' | ||
|
||
# GCP will look automatically for the below env var for service accounts etc. : | ||
option :google_application_credentials, required: false, default: ENV['GOOGLE_APPLICATION_CREDENTIALS'] | ||
# see https://cloud.google.com/docs/authentication/production#providing_credentials_to_your_application | ||
# In the absence of this, the client is expected to have already set up local credentials via: | ||
# $ gcloud auth application-default login | ||
# $ gcloud config set project <project-name> | ||
# GCP projects can have default regions / zones set, see: | ||
# https://cloud.google.com/compute/docs/regions-zones/changing-default-zone-region | ||
# can also specify project via env var: | ||
option :google_cloud_project, required: false, default: ENV['GOOGLE_CLOUD_PROJECT'] | ||
|
||
def connection(_ = nil) | ||
@connection ||= Connection.new(@options) | ||
end | ||
|
||
class Connection < BaseConnection | ||
def initialize(options) | ||
super(options) | ||
|
||
# additional GCP platform metadata | ||
release = Gem.loaded_specs['google_cloud'] | ||
@platform_details = { release: "google-cloud-v#{release}" } | ||
|
||
# Initialize the client object cache | ||
@cache_enabled[:api_call] = true | ||
@cache[:api_call] = {} | ||
|
||
connect | ||
end | ||
|
||
def platform | ||
direct_platform('gcp', @platform_details) | ||
end | ||
|
||
# Instantiate some named classes for ease of use | ||
def gcp_compute_client | ||
gcp_client(Google::Apis::ComputeV1::ComputeService) | ||
end | ||
|
||
def gcp_iam_client | ||
gcp_client(Google::Apis::IamV1::IamService) | ||
end | ||
|
||
def gcp_project_client | ||
gcp_client(Google::Apis::CloudresourcemanagerV1::CloudResourceManagerService) | ||
end | ||
|
||
def gcp_storage_client | ||
gcp_client(Google::Apis::StorageV1::StorageService) | ||
end | ||
|
||
# Let's allow for other clients too | ||
def gcp_client(klass) | ||
return klass.new unless cache_enabled?(:api_call) | ||
@cache[:api_call][klass.to_s.to_sym] ||= klass.new | ||
end | ||
|
||
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 | ||
|
||
def uri | ||
"gcp://#{unique_identifier}" | ||
end | ||
|
||
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 | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
# encoding: utf-8 | ||
|
||
require 'helper' | ||
|
||
describe 'gcp transport' do | ||
|
||
let(:credentials_file) do | ||
require 'tempfile' | ||
file = Tempfile.new('application_default_credentials.json') | ||
info = <<-INFO | ||
{ | ||
"client_id": "asdfasf-asdfasdf.apps.googleusercontent.com", | ||
"client_secret": "d-asdfasdf", | ||
"refresh_token": "1/adsfasdf-lCkju3-yQmjr20xVZonrfkE48L", | ||
"type": "authorized_user" | ||
} | ||
INFO | ||
file.write(info) | ||
file.close | ||
file | ||
end | ||
|
||
let(:credentials_file_override) do | ||
require 'tempfile' | ||
file = Tempfile.new('application_default_credentials.json') | ||
info = <<-INFO | ||
{ | ||
"client_id": "asdfasf-asdfasdf.apps.googleusercontent.com", | ||
"client_secret": "d-asdfasdf", | ||
"refresh_token": "1/adsfasdf-lCkju3-yQmjr20xVZonrfkE48L", | ||
"type": "authorized_user" | ||
} | ||
INFO | ||
file.write(info) | ||
file.close | ||
file | ||
end | ||
|
||
def transport(options = nil) | ||
ENV['GOOGLE_APPLICATION_CREDENTIALS'] = credentials_file.path | ||
ENV['GOOGLE_CLOUD_PROJECT'] = 'test_project' | ||
# need to require this at here as it captures the envs on load | ||
require 'train/transports/gcp' | ||
Train::Transports::Gcp.new(options) | ||
end | ||
|
||
let(:connection) { transport.connection } | ||
let(:options) { connection.instance_variable_get(:@options) } | ||
let(:cache) { connection.instance_variable_get(:@cache) } | ||
|
||
describe 'options' do | ||
it 'defaults to env options' do | ||
options[:google_application_credentials] = credentials_file.path | ||
options[:google_cloud_project].must_equal 'test_project' | ||
end | ||
end | ||
|
||
it 'allows for options override' do | ||
transport = transport(google_application_credentials: credentials_file_override.path, google_cloud_project: "override_project") | ||
options = transport.connection.instance_variable_get(:@options) | ||
options[:google_application_credentials].must_equal credentials_file_override.path | ||
options[:google_cloud_project].must_equal "override_project" | ||
end | ||
|
||
describe 'platform' do | ||
it 'returns platform' do | ||
platform = connection.platform | ||
platform.name.must_equal 'gcp' | ||
platform.family_hierarchy.must_equal ['cloud', 'api'] | ||
end | ||
end | ||
|
||
describe 'gcp_client' do | ||
it 'test gcp_client with caching' do | ||
client = connection.gcp_client(Object) | ||
client.is_a?(Object).must_equal true | ||
cache[:api_call].count.must_equal 1 | ||
end | ||
|
||
it 'test gcp_client without caching' do | ||
connection.disable_cache(:api_call) | ||
client = connection.gcp_client(Object) | ||
client.is_a?(Object).must_equal true | ||
cache[:api_call].count.must_equal 0 | ||
end | ||
end | ||
|
||
|
||
describe 'gcp_compute_client' do | ||
it 'test gcp_compute_client with caching' do | ||
client = connection.gcp_compute_client | ||
client.is_a?(Google::Apis::ComputeV1::ComputeService).must_equal true | ||
cache[:api_call].count.must_equal 1 | ||
end | ||
|
||
it 'test gcp_client without caching' do | ||
connection.disable_cache(:api_call) | ||
client = connection.gcp_compute_client | ||
client.is_a?(Google::Apis::ComputeV1::ComputeService).must_equal true | ||
cache[:api_call].count.must_equal 0 | ||
end | ||
end | ||
|
||
describe 'gcp_iam_client' do | ||
it 'test gcp_iam_client with caching' do | ||
client = connection.gcp_iam_client | ||
client.is_a?(Google::Apis::IamV1::IamService).must_equal true | ||
cache[:api_call].count.must_equal 1 | ||
end | ||
|
||
it 'test gcp_iam_client without caching' do | ||
connection.disable_cache(:api_call) | ||
client = connection.gcp_iam_client | ||
client.is_a?(Google::Apis::IamV1::IamService).must_equal true | ||
cache[:api_call].count.must_equal 0 | ||
end | ||
end | ||
|
||
describe 'gcp_project_client' do | ||
it 'test gcp_project_client with caching' do | ||
client = connection.gcp_project_client | ||
client.is_a?(Google::Apis::CloudresourcemanagerV1::CloudResourceManagerService).must_equal true | ||
cache[:api_call].count.must_equal 1 | ||
end | ||
|
||
it 'test gcp_project_client without caching' do | ||
connection.disable_cache(:api_call) | ||
client = connection.gcp_project_client | ||
client.is_a?(Google::Apis::CloudresourcemanagerV1::CloudResourceManagerService).must_equal true | ||
cache[:api_call].count.must_equal 0 | ||
end | ||
end | ||
|
||
describe 'gcp_storage_client' do | ||
it 'test gcp_storage_client with caching' do | ||
client = connection.gcp_storage_client | ||
client.is_a?(Google::Apis::StorageV1::StorageService).must_equal true | ||
cache[:api_call].count.must_equal 1 | ||
end | ||
|
||
it 'test gcp_storage_client without caching' do | ||
connection.disable_cache(:api_call) | ||
client = connection.gcp_storage_client | ||
client.is_a?(Google::Apis::StorageV1::StorageService).must_equal true | ||
cache[:api_call].count.must_equal 0 | ||
end | ||
end | ||
|
||
# test options override of env vars in connect | ||
describe 'connect' do | ||
let(:creds) do | ||
require 'tempfile' | ||
file = Tempfile.new('creds') | ||
info = <<-INFO | ||
{ | ||
"client_id": "asdfasf-asdfasdf.apps.googleusercontent.com", | ||
"client_secret": "d-asdfasdf", | ||
"refresh_token": "1/adsfasdf-lCkju3-yQmjr20xVZonrfkE48L", | ||
"type": "authorized_user" | ||
} | ||
INFO | ||
file.write(info) | ||
file.close | ||
file | ||
end | ||
it 'validate gcp connection with credentials' do | ||
options[:google_application_credentials] = creds.path | ||
connection.connect | ||
ENV['GOOGLE_APPLICATION_CREDENTIALS'].must_equal creds.path | ||
end | ||
it 'validate gcp connection with project' do | ||
options[:google_cloud_project] = 'project' | ||
connection.connect | ||
ENV['GOOGLE_CLOUD_PROJECT'].must_equal 'project' | ||
end | ||
end | ||
|
||
describe 'unique_identifier' do | ||
it 'test connection unique identifier' do | ||
client = connection | ||
client.unique_identifier.must_equal 'asdfasf-asdfasdf.apps.googleusercontent.com' | ||
end | ||
end | ||
|
||
describe 'uri' do | ||
it 'test uri' do | ||
client = connection | ||
client.uri.must_equal 'gcp://asdfasf-asdfasdf.apps.googleusercontent.com' | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if your going to make helper methods can you use the
gcp_client
for them all? That way the logic is in one spot.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good shout! I'll update that now.