vmware / vsphere-automation-sdk-python

Python samples, language bindings, and API reference documentation for vSphere, VMC, and NSX-T using the VMware REST API
MIT License
745 stars 313 forks source link

Sample code doesn't return VM.List() #381

Closed giriatgit closed 1 year ago

giriatgit commented 1 year ago

Describe the bug

The sample given at https://github.com/vmware/vsphere-automation-sdk-python/tree/v8.0.0.1

import requests import urllib3 from vmware.vapi.vsphere.client import create_vsphere_client session = requests.session()

Disable cert verification for demo purpose.

This is not recommended in a production environment.

session.verify = False

Disable the secure connection warning for demo purpose.

This is not recommended in a production environment.

urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

Connect to a vCenter Server using username and password

vsphere_client = create_vsphere_client(server='', username='', password='', session=session)

List all VMs inside the vCenter Server

vsphere_client.vcenter.VM.list()

This code doesn't return the list of VMs even hough I have 2 vms created in vCenter/ESXi.

The output is just blank..

(vCenterDemo) (base) $ python vCClient.py (vCenterDemo) (base) $

Reproduction steps

1. 2. 3. ...

Expected behavior

List of VMs in the vCenter

Additional context

No response

giriatgit commented 1 year ago

How to enable logs in this code so that we understand whats happening behind the REST APIs? I tired following but still no logs or output.

import logging

logging.getLogger("requests").setLevel(logging.DEBUG) logging.getLogger('urllib3').setLevel(logging.DEBUG)

aagrawal3 commented 1 year ago

Please share following details:

Our listVM sample is also available in samples dir and detailed instructions to install SDK and run samples are available in README. Please refer to quick start guide and run SDK samples sections.

girishkumarbk commented 1 year ago

Installation type is Typical.

(vCenterDemo) (base) girish$ pip3 --version pip 23.1.2 from /Users/../lib/python3.7/site-packages/pip (python 3.7) (vCenterDemo) (base) girish$ (vCenterDemo) (base) girish$ (vCenterDemo) (base) girish$ python3 --version Python 3.7.6 (vCenterDemo) (base) girish$ (vCenterDemo) (base) girish$

Steps to reproduce: (1) Update the sample code with vCenter server ip, user credentials (2) Run python3 my sample.py (The code is pasted in earlier message) The sample given at https://github.com/vmware/vsphere-automation-sdk-python/tree/v8.0.0.1

aagrawal3 commented 1 year ago

Please try this (replace values in placeholders for IP, Username and Password):

import requests
import urllib3
from vmware.vapi.vsphere.client import create_vsphere_client
session = requests.session()

# Disable cert verification for demo purpose.
# This is not recommended in a production environment.
session.verify = False

# Disable the secure connection warning for demo purpose.
# This is not recommended in a production environment.
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)

# Connect to a vCenter Server using username and password
vsphere_client = create_vsphere_client(server='<vc_ip>', username='<vc_username>', password='<vc_password>', session=session)

# List all VMs inside the vCenter Server
list_of_vms = vsphere_client.vcenter.VM.list()

print("List Of VMs")
print("----------------------------")
print(list_of_vms)
print("----------------------------")
girishkumarbk commented 1 year ago

Hi. Just tried the above code. but still no Vms.

(vCenterDemo) (base) girish$ python3 vCClient2.py List Of VMs

[]

girishkumarbk commented 1 year ago

(vCenterDemo) (base) girish $ python3 vCClient2.py List Of VMs

[ ]

girishkumarbk commented 1 year ago

Nevermind. Got the output.

Summary(vm='vm-16', name='vm-001', power_state=State(string='POWERED_ON'), cpu_count=2, memory_size_mib=8192)]

girishkumarbk commented 1 year ago

Thanks.