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


6
7
8
# File 'lib/fog/google/shared.rb', line 6

def api_url
  @api_url
end

#api_versionObject (readonly)

Returns the value of attribute api_version


6
7
8
# File 'lib/fog/google/shared.rb', line 6

def api_version
  @api_version
end

#projectObject (readonly)

Returns the value of attribute project


6
7
8
# File 'lib/fog/google/shared.rb', line 6

def project
  @project
end

Instance Method Details

#apply_client_options(service, google_client_options: nil, **_ignored) ⇒ void

This method returns an undefined value.

Applies given options to the client instance

Parameters:

  • service (Google::Apis::Core::BaseService)

    API service client instance

  • google_client_options (Hash) (defaults to: nil)

    Service client options to apply

  • _ignored (Hash)

    Rest of the options (for convenience, ignored)


93
94
95
96
97
98
# File 'lib/fog/google/shared.rb', line 93

def apply_client_options(service, google_client_options: nil, **_ignored)
  return if google_client_options.nil? || google_client_options.empty?
  (service.client_options.members & google_client_options.keys).each do |option|
    service.client_options[option] = google_client_options[option]
  end
end

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

Builds an Excon response

Parameters:

  • Response (Hash)

    body

  • Response (Integer)

    status

Returns:

  • (Excon::Response)

    Excon response


129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
# File 'lib/fog/google/shared.rb', line 129

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

    if body["error"].is_a?(Hash)
      response.status = body["error"]["code"]
      if body["error"].key?("errors")
        msg = body["error"]["errors"].map { |error| error["message"] }.join(", ")
      elsif body["error"].key?("message")
        msg = body["error"]["message"]
      end
    elsif body["error"].is_a?(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_application_default (Bool)

    Explicitly use application default credentials

  • :google_auth (Google::Auth|Signet)

    Manually created authorization to use

  • :google_json_key_location (String)

    The location of a JSON key file

  • :google_json_key_string (String)

    The content of the JSON 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_options (Hash)

    A hash to send additional options to Google API Client

Returns:

  • (Google::APIClient)

    Google API Client


38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
# File 'lib/fog/google/shared.rb', line 38

def initialize_google_client(options)
  # NOTE: loaded here to avoid requiring this as a core Fog dependency
  begin
    # TODO: google-api-client is in gemspec now, re-assess if this initialization logic is still needed
    require "google/apis/monitoring_#{Fog::Google::Monitoring::GOOGLE_MONITORING_API_VERSION}"
    require "google/apis/compute_#{Fog::Compute::Google::GOOGLE_COMPUTE_API_VERSION}"
    require "google/apis/dns_#{Fog::DNS::Google::GOOGLE_DNS_API_VERSION}"
    require "google/apis/pubsub_#{Fog::Google::Pubsub::GOOGLE_PUBSUB_API_VERSION}"
    require "google/apis/sqladmin_#{Fog::Google::SQL::GOOGLE_SQL_API_VERSION}"
    require "google/apis/storage_#{Fog::Storage::GoogleJSON::GOOGLE_STORAGE_JSON_API_VERSION}"
    require "google/apis/iamcredentials_#{Fog::Storage::GoogleJSON::GOOGLE_STORAGE_JSON_IAM_API_VERSION}"
    require "googleauth"
  rescue LoadError => error
    Fog::Errors::Error.new("Please install the google-api-client (>= 0.9) gem before using this provider")
    raise error
  end

  validate_client_options(options)

  application_name = "fog"
  unless options[:app_name].nil?
    application_name = "#{options[:app_name]}/#{options[:app_version] || '0.0.0'} fog"
  end

  ::Google::Apis::ClientOptions.default.application_name = application_name
  ::Google::Apis::ClientOptions.default.application_version = Fog::Google::VERSION

  if ENV["DEBUG"]
    ::Google::Apis.logger = ::Logger.new(::STDERR)
    ::Google::Apis.logger.level = ::Logger::DEBUG
  end

  auth = nil

  if options[:google_json_key_location] || options[:google_json_key_string]
    auth = process_key_auth(options)
  elsif options[:google_auth]
    auth = options[:google_auth]
  elsif options[:google_application_default]
    auth = process_application_default_auth(options)
  else
    auth = process_fallback_auth(options)
  end

  ::Google::Apis::RequestOptions.default.authorization = auth
  auth
end

#request(api_method, parameters, body_object = nil, media = 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


107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/fog/google/shared.rb', line 107

def request(api_method, parameters, body_object = nil, media = nil)
  client_parms = {
    :api_method => api_method,
    :parameters => parameters
  }
  # The Google API complains when given null values for enums, so just don't pass it any null fields
  # XXX It may still balk if we have a nested object, e.g.:
  #   {:a_field => "string", :a_nested_field => { :an_empty_nested_field => nil } }
  client_parms[:body_object] = body_object.reject { |_k, v| v.nil? } if body_object
  client_parms[:media] = media if media

  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


15
16
17
18
19
20
21
22
# File 'lib/fog/google/shared.rb', line 15

def shared_initialize(project, api_version, base_url)
  @project = project
  @api_version = api_version
  @api_url = base_url + api_version + "/projects/"
  # google-cloud-env allows us to figure out which GCP runtime we're running in and query metadata
  # e.g. whether we're running in GCE/GKE/AppEngine or what region the instance is running in
  @google_cloud_env = ::Google::Cloud::Env.get
end