1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

Merge branch 'no-google-api' into ziyadm

Conflicts:
	fog.gemspec
This commit is contained in:
Nat Welch 2013-03-12 15:11:58 -07:00
commit 079f23379e
5 changed files with 41 additions and 54 deletions

View file

@ -41,8 +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('thin')
s.add_dependency('google-api-client', '~>0.6.2')
s.add_dependency('excon', '~>0.20')
s.add_dependency('formatador', '~>0.2.0')
s.add_dependency('multi_json', '~>1.0')

View file

@ -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
@ -65,19 +66,33 @@ 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'
@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(File.expand_path(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)

View file

@ -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

View file

@ -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
@ -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

View file

@ -1,41 +0,0 @@
require 'thin'
require 'launchy'
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(
:authorization_uri => credentials.authorization_uri,
:token_credential_uri => credentials.token_credential_uri,
:client_id => credentials.client_id,
:client_secret => credentials.client_secret,
:redirect_uri => credentials.redirect_uris.first,
:scope => scope)
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