2012-10-22 20:15:33 -04:00
|
|
|
require 'fog/core/collection'
|
2012-12-04 15:01:36 -05:00
|
|
|
require 'fog/google/models/compute/image'
|
2012-10-22 20:15:33 -04:00
|
|
|
|
|
|
|
module Fog
|
|
|
|
module Compute
|
2012-12-04 15:05:11 -05:00
|
|
|
class Google
|
2012-10-22 20:15:33 -04:00
|
|
|
|
|
|
|
class Images < Fog::Collection
|
|
|
|
|
2012-12-04 15:05:11 -05:00
|
|
|
model Fog::Compute::Google::Image
|
2012-10-22 20:15:33 -04:00
|
|
|
|
2013-08-06 13:44:16 -04:00
|
|
|
GLOBAL_PROJECTS = [ 'google',
|
|
|
|
'debian-cloud',
|
|
|
|
'centos-cloud',
|
|
|
|
]
|
|
|
|
|
2012-10-22 20:15:33 -04:00
|
|
|
def all
|
2013-08-02 05:46:32 -04:00
|
|
|
data = []
|
2013-08-06 13:44:16 -04:00
|
|
|
all_projects = GLOBAL_PROJECTS + [ self.service.project ]
|
|
|
|
|
|
|
|
all_projects.each do |project|
|
|
|
|
images = service.list_images(project).body["items"] || []
|
|
|
|
|
|
|
|
# Keep track of the project in which we found the image(s)
|
|
|
|
images.each { |img| img[:project] = project }
|
|
|
|
data += images
|
2013-08-02 05:46:32 -04:00
|
|
|
end
|
2013-08-06 13:44:16 -04:00
|
|
|
|
2012-10-22 20:15:33 -04:00
|
|
|
load(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
def get(identity)
|
2013-08-06 13:44:16 -04:00
|
|
|
# Search own project before global projects
|
|
|
|
all_projects = [ self.service.project ] + GLOBAL_PROJECTS
|
|
|
|
|
|
|
|
data = nil
|
|
|
|
all_projects.each do |project|
|
|
|
|
begin
|
|
|
|
data = service.get_image(identity, project).body
|
|
|
|
data[:project] = project
|
|
|
|
rescue Fog::Errors::Error
|
|
|
|
next
|
|
|
|
else
|
|
|
|
break
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# If it wasn't found in any project, raise
|
|
|
|
if data.nil?
|
|
|
|
raise Fog::Errors::Error.new('Unable to find the specified image '\
|
|
|
|
'in the following projects: '\
|
|
|
|
"#{all_projects.join(', ')}")
|
|
|
|
end
|
|
|
|
|
2012-10-22 20:15:33 -04:00
|
|
|
new(data)
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|