From 50139e8621adc5dfc0169c8a39610789632aea01 Mon Sep 17 00:00:00 2001 From: Rodrigo Estebanez Date: Tue, 9 Jul 2013 14:19:21 +0200 Subject: [PATCH] subclass to use the new Collection class --- lib/fog/vcloudng/compute.rb | 28 ++++++++++++++++ lib/fog/vcloudng/models/compute/catalogs.rb | 33 +++---------------- .../vcloudng/models/compute/organizations.rb | 28 ++-------------- 3 files changed, 34 insertions(+), 55 deletions(-) diff --git a/lib/fog/vcloudng/compute.rb b/lib/fog/vcloudng/compute.rb index 26085419e..dbfb5786c 100644 --- a/lib/fog/vcloudng/compute.rb +++ b/lib/fog/vcloudng/compute.rb @@ -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 diff --git a/lib/fog/vcloudng/models/compute/catalogs.rb b/lib/fog/vcloudng/models/compute/catalogs.rb index 029be9185..eb5c6b729 100644 --- a/lib/fog/vcloudng/models/compute/catalogs.rb +++ b/lib/fog/vcloudng/models/compute/catalogs.rb @@ -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) } diff --git a/lib/fog/vcloudng/models/compute/organizations.rb b/lib/fog/vcloudng/models/compute/organizations.rb index 4f99f03de..4eba809fb 100644 --- a/lib/fog/vcloudng/models/compute/organizations.rb +++ b/lib/fog/vcloudng/models/compute/organizations.rb @@ -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)