From bc8690c27dc07b6f715da1028d712ea6346e69ba Mon Sep 17 00:00:00 2001 From: Rodrigo Estebanez Date: Mon, 8 Jul 2013 16:57:40 +0200 Subject: [PATCH] organizations refactored --- lib/fog/vcloudng/compute.rb | 6 ++- .../vcloudng/models/compute/organizations.rb | 49 +++++++++++++++---- 2 files changed, 44 insertions(+), 11 deletions(-) diff --git a/lib/fog/vcloudng/compute.rb b/lib/fog/vcloudng/compute.rb index d644aa44f..0e259a501 100644 --- a/lib/fog/vcloudng/compute.rb +++ b/lib/fog/vcloudng/compute.rb @@ -36,6 +36,7 @@ class VcloudngParser < Fog::Parsers::Base end + module Fog module Compute class Vcloudng < Fog::Service @@ -212,7 +213,10 @@ module Fog raise Errors::Task.new "status: #{task.status}, error: #{task.error}" unless task.success? end - + def add_id_from_href!(data={}) + data[:id] = data[:href].split('/').last + end + end diff --git a/lib/fog/vcloudng/models/compute/organizations.rb b/lib/fog/vcloudng/models/compute/organizations.rb index 1202d9c21..99902b25f 100644 --- a/lib/fog/vcloudng/models/compute/organizations.rb +++ b/lib/fog/vcloudng/models/compute/organizations.rb @@ -8,19 +8,48 @@ module Fog class Organizations < Fog::Collection model Fog::Compute::Vcloudng::Organization - def all - data = service.get_organizations.body - org = data[:Org] - org[:id] = org[:href].split('/').last - load([org]) + def all(details=false) + details ? all_with_details : index + 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 + new(org) end - def get(organization_id) - data = service.get_organization(organization_id).body - data.delete(:Link) - data[:id] = data[:href].split('/').last - new(data) + def index + load(org_links) end + + def all_with_details + orgs = org_links.map{|org| get_by_id(org[:id]) } + load(orgs) + end + +# private + + def get_by_id(org_id) + data = service.get_organization(org_id).body + data.delete(:Link) + service.add_id_from_href!(data) + data + end + + def org_links + data = service.get_organizations.body + org = data[:Org] # there is only a single Org + org[:description]='' + service.add_id_from_href!(org) + [org] + end + end end end