Module: Fog::Google::Shared
- Included in:
- Compute::Google::Mock, Compute::Google::Real, DNS::Google::Mock, DNS::Google::Real, Monitoring::Mock, Monitoring::Real, Pubsub::Mock, Pubsub::Real, Fog::Google::SQL::Mock, Fog::Google::SQL::Real, Storage::GoogleJSON::Mock, Storage::GoogleJSON::Real
- Defined in:
- lib/fog/google/shared.rb
Instance Attribute Summary collapse
-
#api_url ⇒ Object
readonly
Returns the value of attribute api_url.
-
#api_version ⇒ Object
readonly
Returns the value of attribute api_version.
-
#project ⇒ Object
readonly
Returns the value of attribute project.
Instance Method Summary collapse
-
#apply_client_options(service, google_client_options: nil, **_ignored) ⇒ void
Applies given options to the client instance.
-
#build_excon_response(body, status = 200) ⇒ Excon::Response
Builds an Excon response.
-
#initialize_google_client(options) ⇒ Google::APIClient
Initializes the Google API Client.
-
#request(api_method, parameters, body_object = nil, media = nil) ⇒ Excon::Response
Executes a request and wraps it in a result object.
-
#shared_initialize(project, api_version, base_url) ⇒ void
Initializes shared attributes.
Instance Attribute Details
#api_url ⇒ Object (readonly)
Returns the value of attribute api_url.
4 5 6 |
# File 'lib/fog/google/shared.rb', line 4 def api_url @api_url end |
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
4 5 6 |
# File 'lib/fog/google/shared.rb', line 4 def api_version @api_version end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
4 5 6 |
# File 'lib/fog/google/shared.rb', line 4 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
129 130 131 132 133 134 |
# File 'lib/fog/google/shared.rb', line 129 def (service, google_client_options: nil, **_ignored) return if .nil? || .empty? (service..members & .keys).each do |option| service.[option] = [option] end end |
#build_excon_response(body, status = 200) ⇒ Excon::Response
Builds an Excon response
165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 |
# File 'lib/fog/google/shared.rb', line 165 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
36 37 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 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/fog/google/shared.rb', line 36 def initialize_google_client() # NOTE: loaded here to avoid requiring this as a core Fog dependency begin # Because of how Google API gem was rewritten, we get to do all sorts # of funky things, like this nonsense. 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 "googleauth" rescue LoadError => error Fog::Errors::Error.new("Please install the google-api-client (>= 0.9) gem before using this provider") raise error end # Users can no longer provide their own clients due to rewrite of auth # in https://github.com/google/google-api-ruby-client/ version 0.9. if [:google_client] raise ArgumentError.new("Deprecated argument no longer works: google_client") end # They can also no longer use pkcs12 files, because Google's new auth # library doesn't support them either. if [:google_key_location] raise ArgumentError.new("Deprecated argument no longer works: google_key_location") end if [:google_key_string] raise ArgumentError.new("Deprecated argument no longer works: google_key_string") end # Validate required arguments unless [:google_api_scope_url] raise ArgumentError.new("Missing required arguments: google_api_scope_url") end application_name = "fog" unless [:app_name].nil? application_name = "#{[:app_name]}/#{[:app_version] || '0.0.0'} fog" end ::Google::Apis::ClientOptions.default.application_name = application_name ::Google::Apis::ClientOptions.default.application_version = Fog::Google::VERSION auth = nil if [:google_json_key_location] || [:google_json_key_string] if [:google_json_key_location] json_key_location = File.([:google_json_key_location]) json_key = File.open(json_key_location, "r", &:read) else json_key = [:google_json_key_string] end json_key_hash = Fog::JSON.decode(json_key) unless json_key_hash.key?("client_email") || json_key_hash.key?("private_key") raise ArgumentError.new("Invalid Google JSON key") end [:google_client_email] = json_key_hash["client_email"] unless [:google_client_email] raise ArgumentError.new("Missing required arguments: google_client_email") end if ENV["DEBUG"] ::Google::Apis.logger = ::Logger.new(::STDERR) ::Google::Apis.logger.level = ::Logger::DEBUG end auth = ::Google::Auth::ServiceAccountCredentials.make_creds( :json_key_io => StringIO.new(json_key_hash.to_json), :scope => [:google_api_scope_url] ) elsif [:google_auth] auth = [:google_auth] else raise ArgumentError.new( "Missing required arguments: google_json_key_location, "\ "google_json_key_string or google_auth" ) end ::Google::Apis::RequestOptions.default. = auth auth end |
#request(api_method, parameters, body_object = nil, media = nil) ⇒ Excon::Response
Executes a request and wraps it in a result object
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 |
# File 'lib/fog/google/shared.rb', line 143 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
13 14 15 16 17 |
# File 'lib/fog/google/shared.rb', line 13 def shared_initialize(project, api_version, base_url) @project = project @api_version = api_version @api_url = base_url + api_version + "/projects/" end |