Module: Fog::Google::Shared

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#api_urlObject (readonly)

Returns the value of attribute api_url.



26
27
28
# File 'lib/fog/google/core.rb', line 26

def api_url
  @api_url
end

#api_versionObject (readonly)

Returns the value of attribute api_version.



26
27
28
# File 'lib/fog/google/core.rb', line 26

def api_version
  @api_version
end

#projectObject (readonly)

Returns the value of attribute project.



26
27
28
# File 'lib/fog/google/core.rb', line 26

def project
  @project
end

Instance Method Details

#build_excon_response(body, status = 200) ⇒ Excon::Response

Builds an Excon response

Parameters:

  • Response (Hash)

    body

  • Response (Integer)

    status

Returns:

  • (Excon::Response)

    Excon response



152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
# File 'lib/fog/google/core.rb', line 152

def build_excon_response(body, status = 200)
  response = Excon::Response.new(:body => body, :status => status)
  if body && body.has_key?('error')
    msg = 'Google Cloud did not return an error message'

    if body['error'].kind_of?(Hash)
      response.status = body['error']['code']
      if body['error'].has_key?('errors')
        msg = body['error']['errors'].map{ |error| error['message'] }.join(', ')
      elsif body['error'].has_key?('message')
        msg = body['error']['message']
      end
    elsif body['error'].kind_of?(Array)
      msg = body['error'].map{ |error| error['code'] }.join(', ')
    end

    case response.status
      when 404
        raise Fog::Errors::NotFound.new(msg)
      else
        raise Fog::Errors::Error.new(msg)
    end
  end

  response
end

#initialize_google_client(options) ⇒ Google::APIClient

Initializes the Google API Client

Parameters:

  • options (Hash)

    Google API options

Options Hash (options):

  • :google_client_email (String)

    A @developer.gserviceaccount.com email address to use

  • :google_key_location (String)

    The location of a pkcs12 key file

  • :google_key_string (String)

    The content of the pkcs12 key file

  • :google_api_scope_url (String)

    The access scope URLs

  • :app_name (String)

    The app name to set in the user agent

  • :app_version (String)

    The app version to set in the user agent

  • :google_client (Google::APIClient)

    Existing Google API Client

Returns:

  • (Google::APIClient)

    Google API Client



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'lib/fog/google/core.rb', line 54

def initialize_google_client(options)
  # NOTE: loaded here to avoid requiring this as a core Fog dependency
  begin
    require 'google/api_client'
  rescue LoadError => error
    Fog::Logger.warning('Please install the google-api-client gem before using this provider')
    raise error
  end

  # User can provide an existing Google API Client
  client = options[:google_client]
  return client unless client.nil?

  # Validate required arguments
  unless options[:google_client_email]
    raise ArgumentError.new('Missing required arguments: google_client_email')
  end

  if options[:google_key_location]
    google_key = File.expand_path(options[:google_key_location])
  elsif options[:google_key_string]
    google_key = options[:google_key_string]
  else
    raise ArgumentError.new('Missing required arguments: google_key_location or google_key_string')
  end

  unless options[:google_api_scope_url]
    raise ArgumentError.new('Missing required arguments: google_api_scope_url')
  end

  # Create a new Google API Client
  self.new_pk12_google_client(
    options[:google_client_email],
    google_key,
    options[:google_api_scope_url],
    options[:app_name],
    options[:app_version]
  )
end

#new_pk12_google_client(google_client_email, google_key, google_api_scope_url, app_name = nil, app_version = nil) ⇒ Google::APIClient

Create a Google API Client with a user email and a pkcs12 key

Parameters:

  • google_client_email (String)

    A @developer.gserviceaccount.com email address to use

  • google_key (String)

    An absolute location to a pkcs12 key file or the content of the file itself

  • google_api_scope_url (String)

    Access scope URLs

  • app_name (String) (defaults to: nil)

    The app name to set in the user agent

  • app_version (String) (defaults to: nil)

    The app version to set in the user agent

Returns:

  • (Google::APIClient)

    Google API Client



103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/fog/google/core.rb', line 103

def new_pk12_google_client(google_client_email, google_key, google_api_scope_url, app_name = nil, app_version = nil)
  application_name = app_name.nil? ? 'fog' : "#{app_name}/#{app_version || '0.0.0'} fog"
  api_client_options = {
    :application_name => application_name,
    :application_version => Fog::VERSION,
  }
  client = ::Google::APIClient.new(api_client_options)

  client.authorization = Signet::OAuth2::Client.new(
    {
      :audience => 'https://accounts.google.com/o/oauth2/token',
      :auth_provider_x509_cert_url => 'https://www.googleapis.com/oauth2/v1/certs',
      :client_x509_cert_url => "https://www.googleapis.com/robot/v1/metadata/x509/#{google_client_email}",
      :issuer => google_client_email,
      :scope => google_api_scope_url,
      :signing_key => ::Google::APIClient::KeyUtils.load_from_pkcs12(google_key, 'notasecret'),
      :token_credential_uri => 'https://accounts.google.com/o/oauth2/token',
    }
  )
  client.authorization.fetch_access_token!

  client
end

#request(api_method, parameters, body_object = nil) ⇒ Excon::Response

Executes a request and wraps it in a result object

Parameters:

  • api_method (Google::APIClient::Method)

    The method object or the RPC name of the method being executed

  • parameters (Hash)

    The parameters to send to the method

  • body_object (Hash) (defaults to: nil)

    The body object of the request

Returns:

  • (Excon::Response)

    The result from the API



134
135
136
137
138
139
140
141
142
143
144
# File 'lib/fog/google/core.rb', line 134

def request(api_method, parameters, body_object = nil)
  client_parms = {
    :api_method => api_method,
    :parameters => parameters,
  }
  client_parms[:body_object] = body_object if body_object

  result = @client.execute(client_parms)

  build_excon_response(result.body.nil? || result.body.empty? ? nil : Fog::JSON.decode(result.body), result.status)
end

#shared_initialize(project, api_version, base_url) ⇒ void

This method returns an undefined value.

Initializes shared attributes

Parameters:

  • project (String)

    Google Cloud Project

  • api_version (String)

    Google API version

  • base_url (String)

    Google API base url



35
36
37
38
39
# File 'lib/fog/google/core.rb', line 35

def shared_initialize(project, api_version, base_url)
  @project = project
  @api_version = api_version
  @api_url = base_url + api_version + '/projects/'
end