Module: Fog::Google::Shared
- Included in:
- Compute::Google::Mock, Compute::Google::Real, DNS::Google::Mock, DNS::Google::Real, Monitoring::Mock, Monitoring::Real, Fog::Google::SQL::Mock, Fog::Google::SQL::Real
- Defined in:
- lib/fog/google.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
-
#build_excon_response(body, status = 200) ⇒ Excon::Response
Builds an Excon response.
-
#create_signing_key(options) ⇒ Object
Creates a Google signing key.
-
#initialize_google_client(options) ⇒ Google::APIClient
Initializes the Google API Client.
-
#new_pk12_google_client(google_client_email, signing_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.
-
#request(api_method, parameters, body_object = 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.
43 44 45 |
# File 'lib/fog/google.rb', line 43 def api_url @api_url end |
#api_version ⇒ Object (readonly)
Returns the value of attribute api_version.
43 44 45 |
# File 'lib/fog/google.rb', line 43 def api_version @api_version end |
#project ⇒ Object (readonly)
Returns the value of attribute project.
43 44 45 |
# File 'lib/fog/google.rb', line 43 def project @project end |
Instance Method Details
#build_excon_response(body, status = 200) ⇒ Excon::Response
Builds an Excon response
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 |
# File 'lib/fog/google.rb', line 202 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 |
#create_signing_key(options) ⇒ Object
Creates a Google signing key
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
# File 'lib/fog/google.rb', line 111 def create_signing_key() 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') { |file| file.read } else json_key = [:google_json_key_string] end json_key_hash = Fog::JSON.decode(json_key) unless json_key_hash.has_key?('client_email') || json_key_hash.has_key?('private_key') raise ArgumentError.new('Invalid Google JSON key') end [:google_client_email] = json_key_hash['client_email'] ::Google::APIClient::KeyUtils.load_from_pem(json_key_hash['private_key'], 'notasecret') elsif [:google_key_location] || [:google_key_string] if [:google_key_location] google_key = File.([:google_key_location]) else google_key = [:google_key_string] end ::Google::APIClient::KeyUtils.load_from_pkcs12(google_key, 'notasecret') else raise ArgumentError.new('Missing required arguments: google_key_location, google_key_string, ' \ 'google_json_key_location or google_json_key_string') end end |
#initialize_google_client(options) ⇒ Google::APIClient
Initializes the Google API Client
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 |
# File 'lib/fog/google.rb', line 73 def initialize_google_client() # 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 = [:google_client] return client unless client.nil? # Create a signing key signing_key = create_signing_key() # Validate required arguments unless [:google_client_email] raise ArgumentError.new('Missing required arguments: google_client_email') end unless [: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( [:google_client_email], signing_key, [:google_api_scope_url], [:app_name], [:app_version] ) end |
#new_pk12_google_client(google_client_email, signing_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
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 |
# File 'lib/fog/google.rb', line 150 def new_pk12_google_client(google_client_email, signing_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" = { :application_name => application_name, :application_version => Fog::Google::VERSION, } client = ::Google::APIClient.new() client. = 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 => signing_key, :token_credential_uri => 'https://accounts.google.com/o/oauth2/token', } ) client..fetch_access_token! client end |
#request(api_method, parameters, body_object = nil) ⇒ Excon::Response
Executes a request and wraps it in a result object
181 182 183 184 185 186 187 188 189 190 191 192 193 194 |
# File 'lib/fog/google.rb', line 181 def request(api_method, parameters, body_object = 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 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
52 53 54 55 56 |
# File 'lib/fog/google.rb', line 52 def shared_initialize(project, api_version, base_url) @project = project @api_version = api_version @api_url = base_url + api_version + '/projects/' end |