Question

I was trying the ruby fog vsphere example:

#!/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

It connects and reports back data, but there are several attributes that do not show the correct data, instead it shows what looks like a Proc object. I am interested in the mac_addresses attribute. How do I get the data out of that object?

{"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"}
Was it helpful?

Solution

You can simply call the proc to retrieve the data:

my_data['mac_addresses'].()

If fog is able to collect the data at this point, it will return the mac address, else it will return nil. Foq uses this technique to lazily evaluate some attributes that might not be available during compile time of hash but which will require additional calls to the vsphere hypervisor.

To quote the comment from the 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.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top