2012-12-04 15:01:36 -05:00
|
|
|
require 'fog/google'
|
2012-10-22 20:15:33 -04:00
|
|
|
require 'fog/compute'
|
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
2012-12-04 15:05:11 -05:00
|
|
|
class Google < Fog::Service
|
2012-10-22 20:15:33 -04:00
|
|
|
|
2012-12-04 15:01:36 -05:00
|
|
|
requires :google_project
|
2013-01-25 22:54:44 -05:00
|
|
|
requires :google_client_email
|
|
|
|
requires :google_key_location
|
2012-10-22 20:15:33 -04:00
|
|
|
|
2012-12-04 15:01:36 -05:00
|
|
|
request_path 'fog/google/requests/compute'
|
2012-10-22 20:15:33 -04:00
|
|
|
request :list_servers
|
|
|
|
request :list_disks
|
|
|
|
request :list_firewalls
|
|
|
|
request :list_images
|
|
|
|
request :list_machine_types
|
|
|
|
request :list_networks
|
|
|
|
request :list_zones
|
2013-04-29 15:27:37 -04:00
|
|
|
request :list_global_operations
|
|
|
|
request :list_zone_operations
|
2012-10-22 20:15:33 -04:00
|
|
|
|
|
|
|
request :get_server
|
|
|
|
request :get_disk
|
|
|
|
request :get_firewall
|
|
|
|
request :get_image
|
|
|
|
request :get_machine_type
|
|
|
|
request :get_network
|
|
|
|
request :get_zone
|
|
|
|
|
|
|
|
request :delete_disk
|
|
|
|
request :delete_firewall
|
|
|
|
request :delete_image
|
|
|
|
request :delete_network
|
|
|
|
request :delete_operation
|
|
|
|
request :delete_server
|
|
|
|
|
|
|
|
request :insert_disk
|
|
|
|
request :insert_firewall
|
|
|
|
request :insert_image
|
|
|
|
request :insert_network
|
|
|
|
request :insert_server
|
|
|
|
|
2013-06-18 21:46:09 -04:00
|
|
|
request :set_metadata
|
|
|
|
|
2012-12-04 15:01:36 -05:00
|
|
|
model_path 'fog/google/models/compute'
|
2012-10-22 20:15:33 -04:00
|
|
|
model :server
|
|
|
|
collection :servers
|
|
|
|
|
|
|
|
model :image
|
|
|
|
collection :images
|
|
|
|
|
|
|
|
model :flavor
|
|
|
|
collection :flavors
|
|
|
|
|
2013-07-01 19:20:25 -04:00
|
|
|
model :disk
|
|
|
|
collection :disks
|
|
|
|
|
2012-10-22 20:15:33 -04:00
|
|
|
class Mock
|
|
|
|
include Collections
|
|
|
|
|
|
|
|
def initialize(options)
|
|
|
|
Fog::Mock::not_implemented
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
|
|
|
|
class Real
|
|
|
|
include Collections
|
|
|
|
|
2013-03-12 18:06:19 -04:00
|
|
|
attr_reader :project
|
|
|
|
|
2012-10-22 20:15:33 -04:00
|
|
|
def initialize(options)
|
|
|
|
base_url = 'https://www.googleapis.com/compute/'
|
2013-06-04 17:27:14 -04:00
|
|
|
api_version = 'v1beta15'
|
2012-10-22 20:15:33 -04:00
|
|
|
api_scope_url = 'https://www.googleapis.com/auth/compute'
|
|
|
|
|
2012-12-04 15:01:36 -05:00
|
|
|
@project = options[:google_project]
|
2013-01-25 22:54:44 -05:00
|
|
|
google_client_email = options[:google_client_email]
|
2012-10-22 20:15:33 -04:00
|
|
|
@api_url = base_url + api_version + '/projects/'
|
2013-06-27 03:27:38 -04:00
|
|
|
|
2013-06-04 17:27:14 -04:00
|
|
|
# NOTE: loaded here to avoid requiring this as a core Fog dependency
|
2013-05-04 19:26:24 -04:00
|
|
|
begin
|
|
|
|
require 'google/api_client'
|
|
|
|
rescue LoadError
|
|
|
|
Fog::Logger.warning("Please install the google-api-client gem before using this provider.")
|
|
|
|
end
|
2013-03-12 17:34:20 -04:00
|
|
|
key = ::Google::APIClient::KeyUtils.load_from_pkcs12(File.expand_path(options[:google_key_location]), 'notasecret')
|
2013-01-25 22:54:44 -05:00
|
|
|
|
2013-03-12 19:44:17 -04:00
|
|
|
@client = ::Google::APIClient.new({
|
2013-03-12 18:22:48 -04:00
|
|
|
:application_name => "fog",
|
2013-03-12 19:44:17 -04:00
|
|
|
:application_version => Fog::VERSION,
|
|
|
|
})
|
2013-06-04 17:27:14 -04:00
|
|
|
|
2013-03-12 19:52:21 -04:00
|
|
|
@client.authorization = Signet::OAuth2::Client.new({
|
2013-01-25 22:54:44 -05:00
|
|
|
: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',
|
2013-03-12 19:52:21 -04:00
|
|
|
})
|
2013-01-25 22:54:44 -05:00
|
|
|
|
2013-06-04 17:27:14 -04:00
|
|
|
@client.authorization.fetch_access_token!
|
2012-10-22 20:15:33 -04:00
|
|
|
@compute = @client.discovered_api('compute', api_version)
|
|
|
|
@default_network = 'default'
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_result(api_method, parameters, body_object=nil)
|
|
|
|
if body_object
|
|
|
|
result = @client.execute(
|
|
|
|
:api_method => api_method,
|
|
|
|
:parameters => parameters,
|
|
|
|
:body_object => body_object
|
|
|
|
)
|
|
|
|
else
|
|
|
|
result = @client.execute(
|
|
|
|
:api_method => api_method,
|
|
|
|
:parameters => parameters
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def build_response(result)
|
|
|
|
response = Excon::Response.new
|
|
|
|
response.body = Fog::JSON.decode(result.body)
|
|
|
|
if response.body["error"]
|
|
|
|
response.status = response.body["error"]["code"]
|
2013-06-20 19:29:29 -04:00
|
|
|
|
|
|
|
response.body["error"]["errors"].each do |error|
|
2013-06-27 03:27:38 -04:00
|
|
|
throw Fog::Errors::Error.new(error["message"])
|
2013-06-20 19:29:29 -04:00
|
|
|
end
|
2012-10-22 20:15:33 -04:00
|
|
|
else
|
|
|
|
response.status = 200
|
|
|
|
end
|
|
|
|
response
|
|
|
|
end
|
|
|
|
|
2013-07-11 13:30:54 -04:00
|
|
|
def backoff_if_unfound(&block)
|
|
|
|
retries_remaining = 5
|
|
|
|
begin
|
|
|
|
result = block.call
|
|
|
|
rescue Exception => msg
|
|
|
|
if msg.to_s.include? 'was not found' and retries_remaining > 0
|
|
|
|
retries_remaining -= 1
|
|
|
|
sleep 0.1
|
|
|
|
retry
|
|
|
|
else
|
|
|
|
raise msg
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
|
|
|
end
|
|
|
|
|
2012-10-22 20:15:33 -04:00
|
|
|
end
|
|
|
|
|
2013-06-26 20:53:53 -04:00
|
|
|
RUNNING = 'RUNNING'
|
2012-10-22 20:15:33 -04:00
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|