Class: VagrantPlugins::Google::Action::ConnectGoogle

Inherits:
Object
  • Object
show all
Defined in:
lib/vagrant-google/action/connect_google.rb

Overview

This action connects to Google, verifies credentials work, and puts the Google connection object into the ‘:google_compute` key in the environment.

Instance Method Summary collapse

Constructor Details

#initialize(app, env) ⇒ ConnectGoogle

Returns a new instance of ConnectGoogle.



24
25
26
27
# File 'lib/vagrant-google/action/connect_google.rb', line 24

def initialize(app, env)
  @app    = app
  @logger = Log4r::Logger.new("vagrant_google::action::connect_google")
end

Instance Method Details

#call(env) ⇒ Object

Initialize Fog::Compute and add it to the environment



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/vagrant-google/action/connect_google.rb', line 30

def call(env)
  provider_config = env[:machine].provider_config

  # Build fog config
  fog_config = {
    :provider            => :google,
    :google_project      => provider_config.google_project_id,
  }

  unless provider_config.google_json_key_location.nil?
    fog_config[:google_json_key_location] = find_key(provider_config.google_json_key_location, env)
  else
    fog_config[:google_application_default] = true
  end

  @logger.info("Creating Google API client and adding to Vagrant environment")
  env[:google_compute] = Fog::Compute.new(fog_config)
  @app.call(env)
end

#find_key(location, env) ⇒ Object

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



51
52
53
54
55
56
57
# File 'lib/vagrant-google/action/connect_google.rb', line 51

def find_key(location, env)
   if File.file?(File.expand_path(location))
     return File.expand_path(location)
   else
     return File.expand_path(location, env[:root_path])
   end
end