From 42a71bb55b87b6f59732a503e6c03f530c65bf10 Mon Sep 17 00:00:00 2001 From: Marc Grimme Date: Wed, 2 Oct 2013 16:56:35 +0200 Subject: [PATCH] [vSphere] Support datacenters that are located below folders not in root folder --- lib/fog/vsphere/models/compute/datacenter.rb | 13 +++++----- .../requests/compute/get_datacenter.rb | 2 +- .../requests/compute/list_datacenters.rb | 25 ++++++++++++++++--- 3 files changed, 30 insertions(+), 10 deletions(-) diff --git a/lib/fog/vsphere/models/compute/datacenter.rb b/lib/fog/vsphere/models/compute/datacenter.rb index f3d798c77..cc90a6916 100644 --- a/lib/fog/vsphere/models/compute/datacenter.rb +++ b/lib/fog/vsphere/models/compute/datacenter.rb @@ -6,30 +6,31 @@ module Fog identity :id attribute :name + attribute :path attribute :status def clusters filters = { } - service.clusters({ :datacenter => name }.merge(filters)) + service.clusters({ :datacenter => path.join("/") }.merge(filters)) end def networks filters = { } - service.networks({ :datacenter => name }.merge(filters)) + service.networks({ :datacenter => path.join("/") }.merge(filters)) end def datastores filters = { } - service.datastores({ :datacenter => name }.merge(filters)) + service.datastores({ :datacenter => path.join("/") }.merge(filters)) end def vm_folders filters = { } - service.folders({ :datacenter => name, :type => :vm }.merge(filters)) + service.folders({ :datacenter => path.join("/"), :type => :vm }.merge(filters)) end def virtual_machines filters = {} - service.servers({ :datacenter => name }.merge(filters)) + service.servers({ :datacenter => path.join("/") }.merge(filters)) end def customfields filters = {} - service.customfields({ :datacenter => name}.merge(filters)) + service.customfields({ :datacenter => path.join("/")}.merge(filters)) end def to_s diff --git a/lib/fog/vsphere/requests/compute/get_datacenter.rb b/lib/fog/vsphere/requests/compute/get_datacenter.rb index 9d58589ea..7b69f2d30 100644 --- a/lib/fog/vsphere/requests/compute/get_datacenter.rb +++ b/lib/fog/vsphere/requests/compute/get_datacenter.rb @@ -5,7 +5,7 @@ module Fog def get_datacenter name dc = find_raw_datacenter(name) raise(Fog::Compute::Vsphere::NotFound) unless dc - {:name => dc.name, :status => dc.overallStatus} + {:name => dc.name, :status => dc.overallStatus, :path => raw_getpathmo(dc) } end protected diff --git a/lib/fog/vsphere/requests/compute/list_datacenters.rb b/lib/fog/vsphere/requests/compute/list_datacenters.rb index 53a85bd88..e8eeb5156 100644 --- a/lib/fog/vsphere/requests/compute/list_datacenters.rb +++ b/lib/fog/vsphere/requests/compute/list_datacenters.rb @@ -8,6 +8,7 @@ module Fog { :id => managed_obj_id(dc), :name => dc.name, + :path => raw_getpathmo(dc), :status => dc.overallStatus } end @@ -15,9 +16,27 @@ module Fog protected - # RbVmomi::VIM::Datacenter Array - def raw_datacenters - @raw_datacenters ||= @connection.rootFolder.childEntity.grep(RbVmomi::VIM::Datacenter) + def raw_getpathmo mo + if mo.parent == nil or mo.parent.name == @connection.rootFolder.name then + [ mo.name ] + else + [ raw_getpathmo(mo.parent), mo.name ].flatten + end + end + + def raw_datacenters folder=nil + folder ||= @connection.rootFolder + @raw_datacenters ||= get_raw_datacenters_from_folder folder + end + + def get_raw_datacenters_from_folder folder=nil + folder.childEntity.map do | childE | + if childE.is_a? RbVmomi::VIM::Datacenter + childE + elsif childE.is_a? RbVmomi::VIM::Folder + get_raw_datacenters_from_folder childE + end + end.flatten end def find_datacenters name=nil