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