fog / fog-vsphere

Fog for vSphere
MIT License
36 stars 63 forks source link

compute.rb: suggest to store the id or the object itself instead of name for RbVmomi object in props_to_attr_hash #181

Open manchiu opened 6 years ago

manchiu commented 6 years ago

In the current implementation, it stores the name of the RbVmomi object in props_to_attr_hash method. However, in my vCenter, some RbVmomi objects have the same name like datacenter, cluster, resource pool etc as they are stored in different folders. It will be hard to distinguish for those RbVmomi objects with the same name. Therefore, I suggest to store the ID or the object itself for differentiation. Below is my code suggestion:

attrs['datacenter'] = proc {
  begin
    if has_permission_to_access_host
      parent_attribute(host.path, :datacenter)[0] # or parent_attribute(host.path, :datacenter)[0]._ref
    else
      parent_attribute(vm_mob_ref.path, :datacenter)[0] # or parent_attribute(vm_mob_ref.path, :datacenter)[0]._ref
    end
  rescue
    nil
  end
}
attrs['cluster'] = proc {
  begin
    parent_attribute(host.path, :cluster)[0] # or parent_attribute(host.path, :cluster)[0]._ref
  rescue
    nil
  end
}
attrs['hypervisor'] = proc {
  begin
    host._ref
  rescue
    nil
  end
}
attrs['resource_pool'] = proc {
  begin
    (vm_mob_ref.resourcePool || host.resourcePool)._ref
  rescue
    nil
  end
}