diff --git a/salt/modules/vsphere.py b/salt/modules/vsphere.py index 96254e0591ad..24ef97542408 100644 --- a/salt/modules/vsphere.py +++ b/salt/modules/vsphere.py @@ -2011,7 +2011,8 @@ def system_info(host, username, password, protocol=None, port=None): @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_datacenters(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_datacenters(host=None, username=None, password=None, protocol=None, port=None, service_instance=None): ''' Returns a list of datacenters for the the specified host. @@ -2039,17 +2040,19 @@ def list_datacenters(host, username, password, protocol=None, port=None): salt '*' vsphere.list_datacenters 1.2.3.4 root bad-password ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([host, username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) return salt.utils.vmware.list_datacenters(service_instance) @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_clusters(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_clusters(host=None, username=None, password=None, protocol=None, port=None, service_instance=None): ''' Returns a list of clusters for the the specified host. @@ -2077,17 +2080,19 @@ def list_clusters(host, username, password, protocol=None, port=None): salt '*' vsphere.list_clusters 1.2.3.4 root bad-password ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([host, username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) return salt.utils.vmware.list_clusters(service_instance) @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_datastore_clusters(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_datastore_clusters(host=None, username=None, password=None, protocol=None, port=None, service_instance=None): ''' Returns a list of datastore clusters for the the specified host. @@ -2114,17 +2119,19 @@ def list_datastore_clusters(host, username, password, protocol=None, port=None): salt '*' vsphere.list_datastore_clusters 1.2.3.4 root bad-password ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([host, username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) return salt.utils.vmware.list_datastore_clusters(service_instance) @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_datastores(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_datastores(host=None, username=None, password=None, protocol=None, port=None, service_instance=None): ''' Returns a list of datastores for the the specified host. @@ -2151,17 +2158,19 @@ def list_datastores(host, username, password, protocol=None, port=None): salt '*' vsphere.list_datastores 1.2.3.4 root bad-password ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([host, username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) return salt.utils.vmware.list_datastores(service_instance) @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_hosts(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_hosts(host=None, username=None, password=None, protocol=None, port=None, service_instance=None): ''' Returns a list of hosts for the the specified VMware environment. @@ -2188,17 +2197,19 @@ def list_hosts(host, username, password, protocol=None, port=None): salt '*' vsphere.list_hosts 1.2.3.4 root bad-password ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([host, username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) return salt.utils.vmware.list_hosts(service_instance) @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_resourcepools(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_resourcepools(host=None, username=None, password=None, protocol=None, port=None, service_instance=None): ''' Returns a list of resource pools for the the specified host. @@ -2225,17 +2236,19 @@ def list_resourcepools(host, username, password, protocol=None, port=None): salt '*' vsphere.list_resourcepools 1.2.3.4 root bad-password ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([host, username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) return salt.utils.vmware.list_resourcepools(service_instance) @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_networks(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_networks(host=None, username=None, password=None, protocol=None, port=None, service_instance=None): ''' Returns a list of networks for the the specified host. @@ -2262,17 +2275,19 @@ def list_networks(host, username, password, protocol=None, port=None): salt '*' vsphere.list_networks 1.2.3.4 root bad-password ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([host, username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) return salt.utils.vmware.list_networks(service_instance) @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_vms(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_vms(host=None, username=None, password=None, protocol=None, port=None, service_instance=None): ''' Returns a list of VMs for the the specified host. @@ -2299,17 +2314,19 @@ def list_vms(host, username, password, protocol=None, port=None): salt '*' vsphere.list_vms 1.2.3.4 root bad-password ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([host, username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) return salt.utils.vmware.list_vms(service_instance) @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_folders(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_folders(host, username, password, protocol=None, port=None, service_instance=None): ''' Returns a list of folders for the the specified host. @@ -2336,17 +2353,19 @@ def list_folders(host, username, password, protocol=None, port=None): salt '*' vsphere.list_folders 1.2.3.4 root bad-password ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([host, username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) return salt.utils.vmware.list_folders(service_instance) @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_dvs(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_dvs(host, username, password, protocol=None, port=None, service_instance=None): ''' Returns a list of distributed virtual switches for the the specified host. @@ -2383,7 +2402,8 @@ def list_dvs(host, username, password, protocol=None, port=None): @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_vapps(host, username, password, protocol=None, port=None): +@gets_service_instance_via_proxy +def list_vapps(host=None, username=None, password=None, protocol=None, port=None, service_instance=None): ''' Returns a list of vApps for the the specified host. @@ -2411,17 +2431,19 @@ def list_vapps(host, username, password, protocol=None, port=None): # List vapps from all minions salt '*' vsphere.list_vapps 1.2.3.4 root bad-password ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([host, username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) return salt.utils.vmware.list_vapps(service_instance) @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_ssds(host, username, password, protocol=None, port=None, host_names=None): +@gets_service_instance_via_proxy +def list_ssds(host, username=None, password=None, protocol=None, port=None, host_names=None, service_instance=None): ''' Returns a list of SSDs for the given host or list of host_names. @@ -2462,11 +2484,12 @@ def list_ssds(host, username, password, protocol=None, port=None, host_names=Non salt '*' vsphere.list_ssds my.vcenter.location root bad-password \ host_names='[esxi-1.host.com, esxi-2.host.com]' ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) host_names = _check_hosts(service_instance, host, host_names) ret = {} names = [] @@ -2482,7 +2505,8 @@ def list_ssds(host, username, password, protocol=None, port=None, host_names=Non @depends(HAS_PYVMOMI) @ignores_kwargs('credstore') -def list_non_ssds(host, username, password, protocol=None, port=None, host_names=None): +@gets_service_instance_via_proxy +def list_non_ssds(host, username=None, password=None, protocol=None, port=None, host_names=None, service_instance=None): ''' Returns a list of Non-SSD disks for the given host or list of host_names. @@ -2530,11 +2554,12 @@ def list_non_ssds(host, username, password, protocol=None, port=None, host_names salt '*' vsphere.list_non_ssds my.vcenter.location root bad-password \ host_names='[esxi-1.host.com, esxi-2.host.com]' ''' - service_instance = salt.utils.vmware.get_service_instance(host=host, - username=username, - password=password, - protocol=protocol, - port=port) + if any([username, password, protocol, port]): + service_instance = salt.utils.vmware.get_service_instance(host=host, + username=username, + password=password, + protocol=protocol, + port=port) host_names = _check_hosts(service_instance, host, host_names) ret = {} names = [] diff --git a/salt/proxy/vcenter.py b/salt/proxy/vcenter.py index 2adcba6adec0..f6b51e2bcc58 100644 --- a/salt/proxy/vcenter.py +++ b/salt/proxy/vcenter.py @@ -277,7 +277,7 @@ def init(opts): log.info('Retrieving credentials and testing vCenter connection for ' 'mehchanism \'userpass\'') try: - username, password = find_credentials() + username, password = find_credentials(proxy_conf) DETAILS['password'] = password except salt.exceptions.SaltSystemExit as err: log.critical('Error: %s', err) @@ -306,7 +306,7 @@ def shutdown(): log.debug('VCenter proxy shutdown() called...') -def find_credentials(): +def find_credentials(proxy_conf): ''' Cycle through all the possible credentials and return the first one that works. @@ -317,7 +317,7 @@ def find_credentials(): if 'username' in DETAILS and 'password' in DETAILS: return DETAILS['username'], DETAILS['password'] - passwords = __pillar__['proxy']['passwords'] + passwords = proxy_conf['passwords'] for password in passwords: DETAILS['password'] = password if not __salt__['vsphere.test_vcenter_connection'](): diff --git a/salt/utils/vmware.py b/salt/utils/vmware.py index faeb9cab80f2..ad61d8d3e34f 100644 --- a/salt/utils/vmware.py +++ b/salt/utils/vmware.py @@ -76,11 +76,14 @@ # Import Python Libs from __future__ import absolute_import, print_function, unicode_literals import atexit +import json import errno import logging import time import sys import ssl +import yaml + # Import Salt Libs import salt.exceptions @@ -97,6 +100,7 @@ from pyVim.connect import GetSi, SmartConnect, Disconnect, GetStub, \ SoapStubAdapter from pyVmomi import vim, vmodl, VmomiSupport + from pyVmomi.VmomiSupport import VmomiJSONEncoder HAS_PYVMOMI = True except ImportError: HAS_PYVMOMI = False @@ -3224,7 +3228,9 @@ def get_vm_by_property(service_instance, name, datacenter=None, vm_properties=No elif len(vm_formatted) > 1: raise salt.exceptions.VMwareMultipleObjectsError('Multiple virtual machines were found with the ' 'same name, please specify a container.') - return vm_formatted[0] + log.debug(vm_formatted[0]) + data = yaml.load(json.dumps(vm_formatted[0], cls=VmomiJSONEncoder)) + return data def get_folder(service_instance, datacenter, placement, base_vm_name=None):