gpt4 book ai didi

ruby fog vsphere 输出列表 proc 对象

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:58:17 26 4
gpt4 key购买 nike

我正在尝试 ruby​​ fog 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:0x00000004922d50@/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:0x00000004922fa8@/opt/rh/ruby193/root/usr/local/share/gems/gems/fog-1.22.0/lib/fog/vsphere/compute.rb:148>,
"cluster"=>
#<Proc:0x00000004922e68@/opt/rh/ruby193/root/usr/local/share/gems/gems/fog-1.22.0/lib/fog/vsphere/compute.rb:149>,
"resource_pool"=>
#<Proc:0x00000004922c60@/opt/rh/ruby193/root/usr/local/share/gems/gems/fog-1.22.0/lib/fog/vsphere/compute.rb:151>,
"mac_addresses"=>
#<Proc:0x00000004922b48@/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"}

最佳答案

您可以简单地调用 proc 来检索数据:

my_data['mac_addresses'].()

如果此时 fog 能够收集到数据,它将返回 mac 地址,否则将返回 nil。 Foq 使用这种技术延迟评估某些属性,这些属性在哈希的编译期间可能不可用,但需要额外调用 vsphere 管理程序。

引用来自 respective source file 的评论:

Here we create the hash object that this method returns, but first we need to add a few more attributes that require additional calls to the vSphere API. The hypervisor name and mac_addresses attributes may not be available so we need catch any exceptions thrown during lookup and set them to nil.

关于ruby fog vsphere 输出列表 proc 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23437555/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com