vmware / vmware/pyvmomi

Unable to retreive the full Path of my environment object hierarchy when Listing Cluster.

Open
#884 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
Python
Stars
2.3k
Forks
763
PR merge metrics
No merged PRs in 30d

Description

Issue:
When i try to execute the below code, i always get only the hostname , but not the full path

Script Below

===============================================

#!/usr/bin/env python
"""
List Of Hosts in a Cluster
"""
from pyVmomi import vim
from pyVim.connect import SmartConnect, Disconnect
import atexit
import argparse
import getpass
import ssl
import requests

def get_args():
""" Get arguments from CLI """
parser = argparse.ArgumentParser(
description='Arguments for talking to vCenter')

parser.add_argument('-s', '--host',
                    required=True,
                    action='store',
                    help='vSpehre service to connect to')

parser.add_argument('-o', '--port',
                    type=int,
                    default=443,
                    action='store',
                    help='Port to connect on')

parser.add_argument('-u', '--user',
                    required=True,
                    action='store',
                    help='Username to use')

parser.add_argument('-p', '--password',
                    required=False,
                    action='store',
                    help='Password to use')

args = parser.parse_args()

if not args.password:
    args.password = getpass.getpass(
        prompt='Enter password')

return args

def get_obj(content, vimtype, name = None):
return [item for item in content.viewManager.CreateContainerView(
content.rootFolder, [vimtype], recursive=True).view]

def get_all_objs(content, vimtype):
obj = {}
container = content.viewManager.CreateContainerView(content.rootFolder, vimtype, True)
for managed_object_ref in container.view:
obj.update({managed_object_ref: managed_object_ref.name})
return obj

def main():
args = get_args()
# Disabling urllib3 ssl warnings
requests.packages.urllib3.disable_warnings()

# Disabling SSL certificate verification
context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
context.verify_mode = ssl.CERT_NONE

# connect this thing
si = SmartConnect(
    host=args.host,
    user=args.user,
    pwd=args.password,
    port=args.port,
    sslContext=context)

# disconnect this thing
atexit.register(Disconnect, si)

content = si.RetrieveContent()


hosts=get_all_objs(content, [vim.HostSystem])
for host_obj in hosts: #get_obj(content, [vim.HostSystem]):
    print "Host Name:", host_obj.name

if name == "main":
main()

===============================================

Output:

cluster1
cluster2
cluster3

Expected Output:

/Folder/DCNAME/clustername

What should i do to get the full path.

For example when we use govc ls

It displays the full path as mentioned in the Expected Output. Help please.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with the supplied script's get_all_objs function and the host iteration in main. Trace the pyVmomi object hierarchy from each returned object and determine how to assemble its containing folder, datacenter, and cluster names. Done means the listing prints the full hierarchy path rather than only the object name; no repository file or test is mentioned.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
infrastructure
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.