mirror of
https://github.com/ansible-collections/community.general.git
synced 2024-09-14 20:13:21 +02:00
Update vmware_inventory (#26308)
Fix adds * Exception handling * Unit tests Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
parent
4e01397817
commit
f7c8e7bdab
2 changed files with 39 additions and 25 deletions
|
@ -39,14 +39,12 @@ from six.moves import configparser
|
||||||
from time import time
|
from time import time
|
||||||
from jinja2 import Environment
|
from jinja2 import Environment
|
||||||
|
|
||||||
HAS_PYVMOMI = False
|
|
||||||
try:
|
|
||||||
from pyVmomi import vim
|
|
||||||
from pyVim.connect import SmartConnect, Disconnect
|
|
||||||
|
|
||||||
HAS_PYVMOMI = True
|
try:
|
||||||
|
from pyVmomi import vim, vmodl
|
||||||
|
from pyVim.connect import SmartConnect, Disconnect
|
||||||
except ImportError:
|
except ImportError:
|
||||||
pass
|
sys.exit("ERROR: This inventory script required 'pyVmomi' Python module, it was not able to load it")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import json
|
import json
|
||||||
|
@ -119,9 +117,7 @@ class VMWareInventory(object):
|
||||||
env.filters['regex_match'] = regex_match
|
env.filters['regex_match'] = regex_match
|
||||||
|
|
||||||
# translation table for attributes to fetch for known vim types
|
# translation table for attributes to fetch for known vim types
|
||||||
if not HAS_PYVMOMI:
|
|
||||||
vimTable = {}
|
|
||||||
else:
|
|
||||||
vimTable = {
|
vimTable = {
|
||||||
vim.Datastore: ['_moId', 'name'],
|
vim.Datastore: ['_moId', 'name'],
|
||||||
vim.ResourcePool: ['_moId', 'name'],
|
vim.ResourcePool: ['_moId', 'name'],
|
||||||
|
@ -347,13 +343,21 @@ class VMWareInventory(object):
|
||||||
''' Make API calls '''
|
''' Make API calls '''
|
||||||
|
|
||||||
instances = []
|
instances = []
|
||||||
|
try:
|
||||||
si = SmartConnect(**inkwargs)
|
si = SmartConnect(**inkwargs)
|
||||||
|
except ssl.SSLError as connection_error:
|
||||||
|
if '[SSL: CERTIFICATE_VERIFY_FAILED]' in str(connection_error) and self.validate_certs:
|
||||||
|
sys.exit("Unable to connect to ESXi server due to %s, "
|
||||||
|
"please specify validate_certs=False and try again" % connection_error)
|
||||||
|
|
||||||
|
except Exception as exc:
|
||||||
|
self.debugl("Unable to connect to ESXi server due to %s" % exc)
|
||||||
|
sys.exit("Unable to connect to ESXi server due to %s" % exc)
|
||||||
|
|
||||||
self.debugl('retrieving all instances')
|
self.debugl('retrieving all instances')
|
||||||
if not si:
|
if not si:
|
||||||
print("Could not connect to the specified host using specified "
|
sys.exit("Could not connect to the specified host using specified "
|
||||||
"username and password")
|
"username and password")
|
||||||
return -1
|
|
||||||
atexit.register(Disconnect, si)
|
atexit.register(Disconnect, si)
|
||||||
content = si.RetrieveContent()
|
content = si.RetrieveContent()
|
||||||
|
|
||||||
|
@ -384,12 +388,16 @@ class VMWareInventory(object):
|
||||||
instance_tuples.append((instance, ifacts))
|
instance_tuples.append((instance, ifacts))
|
||||||
self.debugl('facts collected for all instances')
|
self.debugl('facts collected for all instances')
|
||||||
|
|
||||||
|
try:
|
||||||
cfm = content.customFieldsManager
|
cfm = content.customFieldsManager
|
||||||
if cfm is not None and cfm.field:
|
if cfm is not None and cfm.field:
|
||||||
for f in cfm.field:
|
for f in cfm.field:
|
||||||
if f.managedObjectType == vim.VirtualMachine:
|
if f.managedObjectType == vim.VirtualMachine:
|
||||||
self.custom_fields[f.key] = f.name
|
self.custom_fields[f.key] = f.name
|
||||||
self.debugl('%d custom fieds collected' % len(self.custom_fields))
|
self.debugl('%d custom fields collected' % len(self.custom_fields))
|
||||||
|
except vmodl.RuntimeFault as exc:
|
||||||
|
self.debugl("Unable to gather custom fields due to %s" % exc.msg)
|
||||||
|
|
||||||
return instance_tuples
|
return instance_tuples
|
||||||
|
|
||||||
def instances_to_inventory(self, instances):
|
def instances_to_inventory(self, instances):
|
||||||
|
|
|
@ -3,11 +3,17 @@ import os
|
||||||
import pickle
|
import pickle
|
||||||
import unittest
|
import unittest
|
||||||
import sys
|
import sys
|
||||||
|
from nose.plugins.skip import SkipTest
|
||||||
|
|
||||||
|
|
||||||
|
try:
|
||||||
|
from pyVmomi import vim, vmodl
|
||||||
|
except ImportError:
|
||||||
|
raise SkipTest("test_vmware_inventory.py requires the python module 'pyVmomi'")
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from vmware_inventory import VMWareInventory
|
from vmware_inventory import VMWareInventory
|
||||||
except ImportError:
|
except ImportError:
|
||||||
from nose.plugins.skip import SkipTest
|
|
||||||
raise SkipTest("test_vmware_inventory.py requires the python module 'vmware_inventory'")
|
raise SkipTest("test_vmware_inventory.py requires the python module 'vmware_inventory'")
|
||||||
|
|
||||||
# contrib's dirstruct doesn't contain __init__.py files
|
# contrib's dirstruct doesn't contain __init__.py files
|
||||||
|
|
Loading…
Reference in a new issue