From 6111e43304bd0b152e1c8afa3b1594b621decb4b Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Fri, 25 Jan 2013 18:22:35 -0800 Subject: [PATCH 1/8] remove google-api requirements. --- fog.gemspec | 2 -- 1 file changed, 2 deletions(-) diff --git a/fog.gemspec b/fog.gemspec index 9b002af05..96b0793b7 100644 --- a/fog.gemspec +++ b/fog.gemspec @@ -41,8 +41,6 @@ Gem::Specification.new do |s| ## List your runtime dependencies here. Runtime dependencies are those ## that are needed for an end user to actually USE your code. s.add_dependency('builder') - s.add_dependency('google-api-client') - s.add_dependency('thin') s.add_dependency('excon', '~>0.14') s.add_dependency('formatador', '~>0.2.0') s.add_dependency('multi_json', '~>1.0') From fdee1d7d40b57268ba6843049612fa2776a3ba80 Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Fri, 25 Jan 2013 19:09:05 -0800 Subject: [PATCH 2/8] stripping out thin. --- fog.gemspec | 1 + lib/fog/google/oauth/oauth_util.rb | 31 +++++++----------------------- 2 files changed, 8 insertions(+), 24 deletions(-) diff --git a/fog.gemspec b/fog.gemspec index 96b0793b7..ad20e67cd 100644 --- a/fog.gemspec +++ b/fog.gemspec @@ -41,6 +41,7 @@ Gem::Specification.new do |s| ## List your runtime dependencies here. Runtime dependencies are those ## that are needed for an end user to actually USE your code. s.add_dependency('builder') + s.add_dependency('google-api-client') s.add_dependency('excon', '~>0.14') s.add_dependency('formatador', '~>0.2.0') s.add_dependency('multi_json', '~>1.0') diff --git a/lib/fog/google/oauth/oauth_util.rb b/lib/fog/google/oauth/oauth_util.rb index 8cd730c20..ea7eb4995 100644 --- a/lib/fog/google/oauth/oauth_util.rb +++ b/lib/fog/google/oauth/oauth_util.rb @@ -1,5 +1,3 @@ -require 'thin' -require 'launchy' require 'google/api_client' require 'google/api_client/client_secrets' @@ -10,32 +8,17 @@ class CommandLineOAuthHelper def initialize(scope) credentials = Google::APIClient::ClientSecrets.load @authorization = Signet::OAuth2::Client.new( - :authorization_uri => credentials.authorization_uri, - :token_credential_uri => credentials.token_credential_uri, - :client_id => credentials.client_id, - :client_secret => credentials.client_secret, + :audience => 'https://accounts.google.com/o/oauth2/token', + :issuer => "#{crendentials.client_id}@developer.gserviceaccount.com", :redirect_uri => credentials.redirect_uris.first, - :scope => scope) + :scope => scope, + :signing_key => credentials.signing_key, + :token_credential_uri => credentials.token_credential_uri, + ) + @authorization.fetch_access_token! end - # Request authorization. Opens a browser and waits for response def authorize - auth = @authorization - url = @authorization.authorization_uri().to_s - server = Thin::Server.new('0.0.0.0', 3000) do - run lambda { |env| - # Exchange the auth code & quit - req = Rack::Request.new(env) - auth.code = req['code'] - auth.fetch_access_token! - server.stop() - [200, {'Content-Type' => 'text/plain'}, 'OK'] - } - end - - Launchy.open(url) - server.start() - return @authorization end end From 89292047fe6f3b3269f15256d0e916041b748788 Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Fri, 25 Jan 2013 19:54:44 -0800 Subject: [PATCH 3/8] should move auth into a the main class. --- lib/fog/google/compute.rb | 20 ++++++++++++++++---- lib/fog/google/oauth/oauth_util.rb | 24 ------------------------ 2 files changed, 16 insertions(+), 28 deletions(-) delete mode 100644 lib/fog/google/oauth/oauth_util.rb diff --git a/lib/fog/google/compute.rb b/lib/fog/google/compute.rb index f54db6fd3..faa46aaad 100644 --- a/lib/fog/google/compute.rb +++ b/lib/fog/google/compute.rb @@ -1,6 +1,5 @@ require 'fog/google' require 'fog/compute' -require 'fog/google/oauth/oauth_util' require 'google/api_client' module Fog @@ -8,6 +7,8 @@ module Fog class Google < Fog::Service requires :google_project + requires :google_client_email + requires :google_key_location request_path 'fog/google/requests/compute' request :list_servers @@ -71,13 +72,24 @@ module Fog api_scope_url = 'https://www.googleapis.com/auth/compute' @project = options[:google_project] + google_client_email = options[:google_client_email] @api_url = base_url + api_version + '/projects/' + key = ::Google::APIClient::KeyUtils.load_from_pkcs12(options[:google_key_location], 'notasecret') + @client = ::Google::APIClient.new + @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 => api_scope_url, + :signing_key => key, + :token_credential_uri => 'https://accounts.google.com/o/oauth2/token', + ) + @client.authorization.fetch_access_token! + @compute = @client.discovered_api('compute', api_version) @default_network = 'default' - - auth_util = CommandLineOAuthHelper.new(api_scope_url) - @client.authorization = auth_util.authorize() end def build_result(api_method, parameters, body_object=nil) diff --git a/lib/fog/google/oauth/oauth_util.rb b/lib/fog/google/oauth/oauth_util.rb deleted file mode 100644 index ea7eb4995..000000000 --- a/lib/fog/google/oauth/oauth_util.rb +++ /dev/null @@ -1,24 +0,0 @@ -require 'google/api_client' -require 'google/api_client/client_secrets' - -# Small helper for the sample apps for performing OAuth 2.0 flows from the -# command line. Starts an embedded server to handle redirects. -class CommandLineOAuthHelper - - def initialize(scope) - credentials = Google::APIClient::ClientSecrets.load - @authorization = Signet::OAuth2::Client.new( - :audience => 'https://accounts.google.com/o/oauth2/token', - :issuer => "#{crendentials.client_id}@developer.gserviceaccount.com", - :redirect_uri => credentials.redirect_uris.first, - :scope => scope, - :signing_key => credentials.signing_key, - :token_credential_uri => credentials.token_credential_uri, - ) - @authorization.fetch_access_token! - end - - def authorize - return @authorization - end -end From b57ef63f6c39cf23d78228aeef96e44de600a71f Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Tue, 12 Mar 2013 14:24:48 -0700 Subject: [PATCH 4/8] Hopeing the head of the gapi makes things better... --- fog.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fog.gemspec b/fog.gemspec index ad20e67cd..336575975 100644 --- a/fog.gemspec +++ b/fog.gemspec @@ -41,7 +41,7 @@ Gem::Specification.new do |s| ## List your runtime dependencies here. Runtime dependencies are those ## that are needed for an end user to actually USE your code. s.add_dependency('builder') - s.add_dependency('google-api-client') + s.add_dependency('google-api-client', :git => 'git://github.com/google/google-api-ruby-client.git') s.add_dependency('excon', '~>0.14') s.add_dependency('formatador', '~>0.2.0') s.add_dependency('multi_json', '~>1.0') From afa9b025e9a1c474882f9e6a7a7285debddb4d7e Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Tue, 12 Mar 2013 14:30:28 -0700 Subject: [PATCH 5/8] actually just specify a version. --- fog.gemspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/fog.gemspec b/fog.gemspec index 336575975..c3e78db93 100644 --- a/fog.gemspec +++ b/fog.gemspec @@ -41,7 +41,7 @@ Gem::Specification.new do |s| ## List your runtime dependencies here. Runtime dependencies are those ## that are needed for an end user to actually USE your code. s.add_dependency('builder') - s.add_dependency('google-api-client', :git => 'git://github.com/google/google-api-ruby-client.git') + s.add_dependency('google-api-client', '~>0.6.2') s.add_dependency('excon', '~>0.14') s.add_dependency('formatador', '~>0.2.0') s.add_dependency('multi_json', '~>1.0') From 8944e4c077a8d477cbc2708ae5563d949fcbaccb Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Tue, 12 Mar 2013 14:34:20 -0700 Subject: [PATCH 6/8] load file appropriately. --- lib/fog/google/compute.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fog/google/compute.rb b/lib/fog/google/compute.rb index faa46aaad..5f38b3486 100644 --- a/lib/fog/google/compute.rb +++ b/lib/fog/google/compute.rb @@ -74,7 +74,7 @@ module Fog @project = options[:google_project] google_client_email = options[:google_client_email] @api_url = base_url + api_version + '/projects/' - key = ::Google::APIClient::KeyUtils.load_from_pkcs12(options[:google_key_location], 'notasecret') + key = ::Google::APIClient::KeyUtils.load_from_pkcs12(File.expand_path(options[:google_key_location]), 'notasecret') @client = ::Google::APIClient.new @client.authorization = Signet::OAuth2::Client.new( From a17ccf2dc080f7e62d127818881a6eca469f56a4 Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Tue, 12 Mar 2013 14:35:19 -0700 Subject: [PATCH 7/8] fix deprecation notice. --- lib/fog/google/models/compute/servers.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/fog/google/models/compute/servers.rb b/lib/fog/google/models/compute/servers.rb index 068182737..ef7f54e53 100644 --- a/lib/fog/google/models/compute/servers.rb +++ b/lib/fog/google/models/compute/servers.rb @@ -10,7 +10,7 @@ module Fog model Fog::Compute::Google::Server def all - data = connection.list_servers.body["items"] + data = service.list_servers.body["items"] load(data) end From cf01cbfe45126c5e70c975f775798bb388d8f3a4 Mon Sep 17 00:00:00 2001 From: Nat Welch Date: Tue, 12 Mar 2013 15:06:19 -0700 Subject: [PATCH 8/8] more attempts to get server creation working. --- lib/fog/google/compute.rb | 3 +++ lib/fog/google/models/compute/server.rb | 12 ++++++------ lib/fog/google/models/compute/servers.rb | 16 +++++++++++++++- 3 files changed, 24 insertions(+), 7 deletions(-) diff --git a/lib/fog/google/compute.rb b/lib/fog/google/compute.rb index 5f38b3486..500b2ef29 100644 --- a/lib/fog/google/compute.rb +++ b/lib/fog/google/compute.rb @@ -66,8 +66,11 @@ module Fog class Real include Collections + attr_reader :project + def initialize(options) base_url = 'https://www.googleapis.com/compute/' + # TODO(icco): v1beta13 will probably be deprecated in April. api_version = 'v1beta13' api_scope_url = 'https://www.googleapis.com/auth/compute' diff --git a/lib/fog/google/models/compute/server.rb b/lib/fog/google/models/compute/server.rb index c9858c12b..3c3c398a1 100644 --- a/lib/fog/google/models/compute/server.rb +++ b/lib/fog/google/models/compute/server.rb @@ -6,7 +6,7 @@ module Fog class Server < Fog::Model - attribute :name + attribute :name, :aliases => 'identity' attribute :image_name, :aliases => 'image' attribute :network_interfaces, :aliases => 'networkInterfaces' attribute :state, :aliases => 'status' @@ -15,11 +15,11 @@ module Fog def destroy requires :name - connection.delete_server(name) + service.delete_server(name) end def image - connection.get_image(self.image_name.split('/')[-1]) + service.get_image(self.image_name.split('/')[-1]) end def public_ip_address @@ -31,7 +31,7 @@ module Fog end def zone - connection.get_zone(self.zone_name.split('/')[-1]) + service.get_zone(self.zone_name.split('/')[-1]) end def save @@ -40,9 +40,9 @@ module Fog requires :machine_type requires :zone_name - data = connection.insert_server(name, image_name, zone_name, + data = service.insert_server(name, image_name, zone_name, machine_type) - connection.servers.merge_attributes() + service.servers.merge_attributes() end end diff --git a/lib/fog/google/models/compute/servers.rb b/lib/fog/google/models/compute/servers.rb index ef7f54e53..6086965f4 100644 --- a/lib/fog/google/models/compute/servers.rb +++ b/lib/fog/google/models/compute/servers.rb @@ -10,7 +10,7 @@ module Fog model Fog::Compute::Google::Server def all - data = service.list_servers.body["items"] + data = service.list_servers.body["items"] || [] load(data) end @@ -21,6 +21,20 @@ module Fog nil end + def bootstrap(new_attributes = {}) + + defaults = { + :name => "fog_#{Time.now.to_i}", + :image_name => "", + :machine_type => "", + :zone_name => "", + } + + server = create(defaults.merge(new_attributes)) + server.wait_for { ready? } + server + end + end end