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
Nat Welch 5f7886e08d [google|compute] Change projects we search for images in.
This removes Google (which has no valid v1 images) and adds rhel-cloud
and suse-cloud. It also wraps the lookups in a catch block for not-found
exceptions because not everyone has access to everything.
2014-03-17 07:46:25 -07:00

71 lines
1.9 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
# NOTE: Not everyone has access to these projects because of the
# licenses needed to use some of them.
# https://developers.google.com/compute/docs/premium-operating-systems
GLOBAL_PROJECTS = [
'debian-cloud',
'centos-cloud',
'rhel-cloud',
'suse-cloud'
]
def all
data = []
all_projects = GLOBAL_PROJECTS + [ self.service.project ]
all_projects.each do |project|
begin
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
rescue Fog::Errors::NotFound
# Not everyone has access to every Global Project. Requests
# return 404 if you don't have access.
next
end
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::NotFound
next
else
break
end
end
# If it wasn't found in any project, raise
if data.nil?
raise Fog::Errors::NotFound.new(
"Unable to find the image #{identity} in the following projects: #{all_projects.join(', ')}")
end
new(data)
end
end
end
end
end