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

62 lines
1.6 KiB
Ruby
Raw Normal View History

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
GLOBAL_PROJECTS = [ 'google',
'debian-cloud',
'centos-cloud',
]
2012-10-22 20:15:33 -04:00
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
2012-10-22 20:15:33 -04:00
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
2012-10-22 20:15:33 -04:00
new(data)
end
end
end
end
end