1
0
Fork 0
mirror of https://github.com/fog/fog.git synced 2022-11-09 13:51:43 -05:00

subclass to use the new Collection class

This commit is contained in:
Rodrigo Estebanez 2013-07-09 14:19:21 +02:00
parent 24d8ff9c38
commit 50139e8621
3 changed files with 34 additions and 55 deletions

View file

@ -142,6 +142,34 @@ module Fog
out
end
end
class Collection < Fog::Collection
def all(lazy_load=true)
lazy_load ? index : get_everyone
end
def get(item_id)
item = get_by_id(item_id)
return nil unless item
new(item)
end
def get_by_name(item_name)
item_found = item_list.detect{|item| item[:name] == item_name }
return nil unless item_found
get(item_found[:id])
end
def index
load(item_list)
end
def get_everyone
item_ids = item_list.map {|item| item[:id] }
items = item_ids.map{ |item_id| get_by_id(item_id)}
load(items)
end
end
class Real
include Fog::Compute::Helper

View file

@ -5,47 +5,22 @@ module Fog
module Compute
class Vcloudng
class Catalogs < Fog::Collection
class Catalogs < Collection
model Fog::Compute::Vcloudng::Catalog
attribute :organization
def all(lazy_load=true)
lazy_load ? index : get_everyone
end
def get(catalog_id)
catalog = get_by_id(catalog_id)
return nil unless catalog
new(catalog)
end
def get_by_name(catalog_name)
catalog = catalog_links.detect{|catalog_link| catalog_link[:name] == catalog_name }
return nil unless catalog
get(catalog[:id])
end
def index
load(catalog_links)
end
def get_everyone
catalog_ids = catalog_links.map {|catalog| catalog[:id] }
catalogs = catalog_ids.map{ |catalog_id| get_by_id(catalog_id)}
load(catalogs)
end
private
def get_by_id(catalog_id)
catalog = service.get_catalog(catalog_id).body
def get_by_id(item_id)
catalog = service.get_catalog(item_id).body
%w(:CatalogItems :Link).each {|key_to_delete| catalog.delete(key_to_delete) }
service.add_id_from_href!(catalog)
catalog
end
def catalog_links
def item_list
data = service.get_organization(organization.id).body
catalogs = data[:Link].select { |link| link[:type] == "application/vnd.vmware.vcloud.catalog+xml" }
catalogs.each{|catalog| service.add_id_from_href!(catalog) }

View file

@ -5,33 +5,9 @@ module Fog
module Compute
class Vcloudng
class Organizations < Fog::Collection
class Organizations < Collection
model Fog::Compute::Vcloudng::Organization
def all(lazy_load=true)
lazy_load ? index : get_everyone
end
def get(org_id)
org = get_by_id(org_id)
return nil unless org
new(org)
end
def get_by_name(org_name)
org = org_links.detect{|org| org[:name] == org_name}
return nil unless org
get(org[:id])
end
def index
load(org_links)
end
def get_everyone
orgs = org_links.map{|org| get_by_id(org[:id]) }
load(orgs)
end
private
@ -42,7 +18,7 @@ module Fog
org
end
def org_links
def item_list
data = service.get_organizations.body
org = data[:Org] # there is only a single Org
service.add_id_from_href!(org)