1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00
fog--fog/lib/fog/google/models/compute/images.rb
Akshay Moghe b42d86360c Query global projects when get/list'ing compute images.
This patch refactors some of the code that would query global
projects when get'ing images. It makes the list of global projects
a constant on the Images collection, so that both 'get' and 'list'
can use the same list of projects to query.

Also, when bootstrapping/create'ing a server, the validation of
the specified image name is done by trying to 'get' the image
instead of 'save'ing it.
2013-08-07 11:17:45 -07:00

61 lines
1.6 KiB
Ruby

require 'fog/core/collection'
require 'fog/google/models/compute/image'
module Fog
module Compute
class Google
class Images < Fog::Collection
model Fog::Compute::Google::Image
GLOBAL_PROJECTS = [ 'google',
'debian-cloud',
'centos-cloud',
]
def all
data = []
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
end
load(data)
end
def get(identity)
# 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
new(data)
end
end
end
end
end