2012-12-26 16:52:53 -08:00
|
|
|
module Google # deviates from other bin stuff to accomodate gem
|
2010-09-18 13:40:48 -04:00
|
|
|
class << self
|
|
|
|
|
2010-12-06 19:49:58 -08:00
|
|
|
def class_for(key)
|
|
|
|
case key
|
2012-12-04 12:15:53 -08:00
|
|
|
when :compute
|
|
|
|
Fog::Compute::Google
|
2010-12-06 19:49:58 -08:00
|
|
|
when :storage
|
2011-06-15 14:26:43 -07:00
|
|
|
Fog::Storage::Google
|
2010-12-06 19:49:58 -08:00
|
|
|
else
|
|
|
|
raise ArgumentError, "Unsupported #{self} service: #{key}"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-24 13:12:01 -07:00
|
|
|
def [](service)
|
|
|
|
@@connections ||= Hash.new do |hash, key|
|
2011-01-14 10:41:12 -08:00
|
|
|
hash[key] = case key
|
2011-01-12 17:55:24 -08:00
|
|
|
when :storage
|
2011-10-19 14:49:34 -05:00
|
|
|
Fog::Logger.warning("Google[:storage] is not recommended, use Storage[:google] for portability")
|
2011-01-12 17:55:24 -08:00
|
|
|
Fog::Storage.new(:provider => 'Google')
|
2012-12-04 12:15:53 -08:00
|
|
|
when :compute
|
|
|
|
Fog::Logger.warning("Google[:compute] is not recommended, use Compute[:google] for portability")
|
|
|
|
Fog::Compute.new(:provider => 'Google')
|
2011-01-12 17:55:24 -08:00
|
|
|
else
|
2011-02-04 11:17:30 -05:00
|
|
|
raise ArgumentError, "Unrecognized service: #{key.inspect}"
|
2011-01-12 17:55:24 -08:00
|
|
|
end
|
2010-09-18 13:40:48 -04:00
|
|
|
end
|
2010-09-24 13:12:01 -07:00
|
|
|
@@connections[service]
|
|
|
|
end
|
2010-09-18 13:40:48 -04:00
|
|
|
|
2012-12-04 12:15:53 -08:00
|
|
|
def account
|
|
|
|
@@connections[:compute].account
|
|
|
|
end
|
|
|
|
|
2010-09-24 13:12:01 -07:00
|
|
|
def services
|
2011-02-04 11:17:30 -05:00
|
|
|
Fog::Google.services
|
2010-09-18 13:40:48 -04:00
|
|
|
end
|
2010-09-24 13:12:01 -07:00
|
|
|
|
2012-12-26 16:52:53 -08:00
|
|
|
# based off of virtual_box.rb
|
|
|
|
def available?
|
2013-04-16 20:09:16 -07:00
|
|
|
# Make sure the gem we use is enabled.
|
2012-12-26 16:52:53 -08:00
|
|
|
availability = if Gem::Specification.respond_to?(:find_all_by_name)
|
2013-04-16 20:09:16 -07:00
|
|
|
!Gem::Specification.find_all_by_name('google-api-client').empty? # newest rubygems
|
2012-12-26 16:52:53 -08:00
|
|
|
else
|
2013-04-16 20:09:16 -07:00
|
|
|
!Gem.source_index.find_name('google-api-client').empty? # legacy
|
2012-12-26 16:52:53 -08:00
|
|
|
end
|
2013-04-16 20:09:16 -07:00
|
|
|
|
2013-05-01 17:40:20 -07:00
|
|
|
# Then make sure we have all of the requirements
|
|
|
|
for service in services
|
|
|
|
begin
|
|
|
|
service = self.class_for(service)
|
|
|
|
availability &&= service.requirements.all? { |requirement| Fog.credentials.include?(requirement) }
|
|
|
|
rescue ArgumentError => e
|
|
|
|
Fog::Logger.warning(e.message)
|
|
|
|
availability = false
|
|
|
|
rescue => e
|
|
|
|
availability = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-12-26 16:52:53 -08:00
|
|
|
if availability
|
|
|
|
for service in services
|
|
|
|
for collection in self.class_for(service).collections
|
|
|
|
unless self.respond_to?(collection)
|
|
|
|
self.class_eval <<-EOS, __FILE__, __LINE__
|
|
|
|
def self.#{collection}
|
|
|
|
self[:#{service}].#{collection}
|
|
|
|
end
|
|
|
|
EOS
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
availability
|
|
|
|
end
|
2010-09-18 13:40:48 -04:00
|
|
|
end
|
|
|
|
end
|