2014-05-02 23 views
1

我試圖紅寶石霧的vSphere例如:紅寶石霧的vSphere輸出上市進程內對象

#!/usr/bin/env ruby 

require 'rubygems' 
require 'pp' 
require 'fog' 
require 'highline/import' 

def get_password(prompt="Enter password:") 
    ask(prompt) {|q| q.echo = false} 
end 

#user = gets.chomp 
pass = get_password() 

credentials = { 
    :provider => "vsphere", 
    :vsphere_username => "user.name", 
    :vsphere_password => pass, 
    :vsphere_server => "my_vcserver", 
    :vsphere_ssl => true, 
    :vsphere_expected_pubkey_hash => "my_hash", 
    :vsphere_rev => "4.0" 
} 
connection = Fog::Compute.new(credentials) 

# MUST BE Ruby v 1.9 to use this hash style 
vms = connection.list_virtual_machines(datacenter: 'my_dc', folder: 'my_folder') 
pp vms 

它連接並報告數據,但也有幾個屬性,不顯示正確的數據,相反,它說明了什麼看起來像一個Proc對象。我對mac_addresses屬性感興趣。如何從該對象中獲取數據?

{"id"=>"52e9592f-4da9-c5b4-a78e-92d39705d900", 
    "name"=>"a41", 
    "uuid"=>"784d4e21-e4a7-e059-cdef-4ff1453f093d", 
    "template"=>false, 
    "parent"=>Folder("group-v16163"), 
    "hostname"=>nil, 
    "operatingsystem"=>nil, 
    "ipaddress"=>nil, 
    "power_state"=>"poweredOn", 
    "connection_state"=>"connected", 
    "hypervisor"=> 
    #<Proc:[email protected]/opt/rh/ruby193/root/usr/local/share/gems/gems/fog-1.22.0/lib/fog/vsphere/compute.rb:150>, 
    "tools_state"=>"toolsNotInstalled", 
    "tools_version"=>"guestToolsNotInstalled", 
    "memory_mb"=>8192, 
    "cpus"=>2, 
    "corespersocket"=>2, 
    "overall_status"=>"green", 
    "guest_id"=>"centos64Guest", 
    "mo_ref"=>"vm-16217", 
    "datacenter"=> 
    #<Proc:[email protected]/opt/rh/ruby193/root/usr/local/share/gems/gems/fog-1.22.0/lib/fog/vsphere/compute.rb:148>, 
    "cluster"=> 
    #<Proc:[email protected]/opt/rh/ruby193/root/usr/local/share/gems/gems/fog-1.22.0/lib/fog/vsphere/compute.rb:149>, 
    "resource_pool"=> 
    #<Proc:[email protected]/opt/rh/ruby193/root/usr/local/share/gems/gems/fog-1.22.0/lib/fog/vsphere/compute.rb:151>, 
    "mac_addresses"=> 
    #<Proc:[email protected]/opt/rh/ruby193/root/usr/local/share/gems/gems/fog-1.22.0/lib/fog/vsphere/compute.rb:155>, 
    "path"=>"/Datacenters/DEV/vm", 
    "relative_path"=>"DEV"} 

回答

1

您可以直接撥打PROC檢索數據:

my_data['mac_addresses'].() 

如果霧是能夠在這一點上收集數據,它將返回的MAC地址,否則將返回nil。 Foq使用這種技術來懶散地評估一些散列編譯時可能無法使用的屬性,但這需要對vSphere虛擬機監控程序進行額外的調用。

要從respective source file引用註釋:

這裏我們創建哈希對象,這種方法回報,但首先我們需要 添加需要到vSphere API額外調用了更多的屬性。管理程序名稱和mac_addresses屬性可能不可用 因此,我們需要捕獲在查找過程中拋出的任何異常,並將它們設置爲零。