Skip to content

Commit 64d6861

Browse files
authored
Merge pull request #213 from mavin/allow-without-explicit-service-account
feat: Allow without explicit service account
2 parents a5bf8d0 + c71bf30 commit 64d6861

File tree

5 files changed

+20
-30
lines changed

5 files changed

+20
-30
lines changed

lib/vagrant-google/action/connect_google.rb

+13-5
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,31 @@ def initialize(app, env)
2626
@logger = Log4r::Logger.new("vagrant_google::action::connect_google")
2727
end
2828

29+
# Initialize Fog::Compute and add it to the environment
2930
def call(env)
3031
provider_config = env[:machine].provider_config
3132

3233
# Build fog config
3334
fog_config = {
3435
:provider => :google,
3536
:google_project => provider_config.google_project_id,
36-
:google_client_email => provider_config.google_client_email
3737
}
3838

39-
fog_config[:google_json_key_location] = find_key(provider_config.google_json_key_location, env)
39+
unless provider_config.google_client_email.nil?
40+
fog_config[:google_client_email] = provider_config.google_client_email
41+
end
4042

41-
@logger.info("Connecting to Google...")
42-
env[:google_compute] = Fog::Compute.new(fog_config)
43+
unless provider_config.google_json_key_location.nil?
44+
fog_config[:google_json_key_location] = find_key(provider_config.google_json_key_location, env)
45+
end
46+
47+
if provider_config.google_client_email.nil? and provider_config.google_json_key_location.nil?
48+
fog_config[:google_application_default] = true
49+
end
4350

51+
@logger.info("Creating Google API client and adding to Vagrant environment")
52+
env[:google_compute] = Fog::Compute.new(fog_config)
4453
@app.call(env)
45-
@logger.info("...Connected!")
4654
end
4755

4856
# If the key is not found, try expanding from root location (see #159)

lib/vagrant-google/config.rb

+6-7
Original file line numberDiff line numberDiff line change
@@ -421,13 +421,12 @@ def validate(machine)
421421
# TODO: Check why provider-level settings are validated in the zone config
422422
errors << I18n.t("vagrant_google.config.google_project_id_required") if \
423423
config.google_project_id.nil?
424-
errors << I18n.t("vagrant_google.config.google_client_email_required") if \
425-
config.google_client_email.nil?
426-
errors << I18n.t("vagrant_google.config.google_key_location_required") if \
427-
config.google_json_key_location.nil?
428-
errors << I18n.t("vagrant_google.config.private_key_missing") unless \
429-
File.exist?(File.expand_path(config.google_json_key_location.to_s)) or
430-
File.exist?(File.expand_path(config.google_json_key_location.to_s, machine.env.root_path))
424+
425+
if config.google_json_key_location
426+
errors << I18n.t("vagrant_google.config.private_key_missing") unless \
427+
File.exist?(File.expand_path(config.google_json_key_location.to_s)) or
428+
File.exist?(File.expand_path(config.google_json_key_location.to_s, machine.env.root_path))
429+
end
431430

432431
if config.preemptible
433432
errors << I18n.t("vagrant_google.config.auto_restart_invalid_on_preemptible") if \

locales/en.yml

-6
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,6 @@ en:
5252
# Translations for config validation errors
5353
#-------------------------------------------------------------------------------
5454
config:
55-
google_client_email_required: |-
56-
A Google Service Account client email is required via
57-
"google_client_email".
5855
private_key_missing: |-
5956
Private key for Google could not be found in the specified location.
6057
zone_required: |-
@@ -63,9 +60,6 @@ en:
6360
An instance name must be specified via "name" option.
6461
image_required: |-
6562
An image must be specified via "image" or "image_family" option.
66-
google_key_location_required: |-
67-
A private key pathname is required via:
68-
"google_json_key_location" (for JSON keys)
6963
google_project_id_required: |-
7064
A Google Cloud Project ID is required via "google_project_id".
7165
auto_restart_invalid_on_preemptible: |-

test/unit/common/config_test.rb

-11
Original file line numberDiff line numberDiff line change
@@ -118,17 +118,6 @@
118118
its("google_client_email") { should == "client_id_email" }
119119
its("google_json_key_location") { should == "/path/to/json/key" }
120120
end
121-
122-
context "With none of the Google credential environment variables set" do
123-
before :each do
124-
allow(ENV).to receive(:[]).with("GOOGLE_CLIENT_EMAIL").and_return("client_id_email")
125-
end
126-
127-
it "Should return no key set errors" do
128-
instance.finalize!
129-
expect(instance.validate("foo")["Google Provider"][1]).to include("en.vagrant_google.config.google_key_location_required")
130-
end
131-
end
132121
end
133122

134123
describe "zone config" do

vagrant-google.gemspec

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Gem::Specification.new do |s|
2828
s.required_rubygems_version = ">= 1.3.6"
2929
s.rubyforge_project = "vagrant-google"
3030

31-
s.add_runtime_dependency "fog-google", "~> 1.8.1"
31+
s.add_runtime_dependency "fog-google", "~> 1.9.1"
3232

3333
# This is a restriction to avoid errors on `failure_message_for_should`
3434
# TODO: revise after vagrant_spec goes past >0.0.1 (at master@e623a56)

0 commit comments

Comments
 (0)