diff --git a/salt/modules/vsphere.py b/salt/modules/vsphere.py index d4421ce1de..bc59077b1a 100644 --- a/salt/modules/vsphere.py +++ b/salt/modules/vsphere.py @@ -4700,8 +4700,8 @@ def list_default_vsan_policy(service_instance=None): def_policies = [p for p in policies if p.systemCreatedProfileType == 'VsanDefaultProfile'] if not def_policies: - raise excs.VMwareObjectRetrievalError('Default VSAN policy was not ' - 'retrieved') + raise VMwareObjectRetrievalError('Default VSAN policy was not ' + 'retrieved') return _get_policy_dict(def_policies[0]) @@ -4854,8 +4854,8 @@ def update_storage_policy(policy, policy_dict, service_instance=None): profile_manager = salt.utils.pbm.get_profile_manager(service_instance) policies = salt.utils.pbm.get_storage_policies(profile_manager, [policy]) if not policies: - raise excs.VMwareObjectRetrievalError('Policy \'{0}\' was not found' - ''.format(policy)) + raise VMwareObjectRetrievalError('Policy \'{0}\' was not found' + ''.format(policy)) policy_ref = policies[0] policy_update_spec = pbm.profile.CapabilityBasedProfileUpdateSpec() log.trace('Setting policy values in policy_update_spec') @@ -4893,8 +4893,8 @@ def list_default_storage_policy_of_datastore(datastore, service_instance=None): ds_refs = salt.utils.vmware.get_datastores(service_instance, target_ref, datastore_names=[datastore]) if not ds_refs: - raise excs.VMwareObjectRetrievalError('Datastore \'{0}\' was not ' - 'found'.format(datastore)) + raise VMwareObjectRetrievalError('Datastore \'{0}\' was not ' + 'found'.format(datastore)) profile_manager = salt.utils.pbm.get_profile_manager(service_instance) policy = salt.utils.pbm.get_default_storage_policy_of_datastore( profile_manager, ds_refs[0]) @@ -4927,12 +4927,12 @@ def assign_default_storage_policy_to_datastore(policy, datastore, ''' log.trace('Assigning policy {0} to datastore {1}' ''.format(policy, datastore)) - profile_manager = utils_pbm.get_profile_manager(service_instance) + profile_manager = salt.utils.pbm.get_profile_manager(service_instance) # Find policy - policies = utils_pbm.get_storage_policies(profile_manager, [policy]) + policies = salt.utils.pbm.get_storage_policies(profile_manager, [policy]) if not policies: - raise excs.VMwareObjectRetrievalError('Policy \'{0}\' was not found' - ''.format(policy)) + raise VMwareObjectRetrievalError('Policy \'{0}\' was not found' + ''.format(policy)) policy_ref = policies[0] # Find datastore target_ref = _get_proxy_target(service_instance) @@ -4942,9 +4942,9 @@ def assign_default_storage_policy_to_datastore(policy, datastore, raise excs.VMwareObjectRetrievalError('Datastore \'{0}\' was not ' 'found'.format(datastore)) ds_ref = ds_refs[0] - utils_pbm.assign_default_storage_policy_to_datastore(profile_manager, - policy_ref, ds_ref) - return {'assign_storage_policy_to_datastore': True} + salt.utils.pbm.assign_default_storage_policy_to_datastore( + profile_manager, policy_ref, ds_ref) + return True @depends(HAS_PYVMOMI) diff --git a/salt/proxy/vcenter.py b/salt/proxy/vcenter.py index 7b9c9f95e3..5c5ad797d1 100644 --- a/salt/proxy/vcenter.py +++ b/salt/proxy/vcenter.py @@ -189,7 +189,7 @@ import os # Import Salt Libs import salt.exceptions -from salt.config.schemas.vcenter import VCenterProxySchema +from salt.config.schemas.vcenter import VCenterProxySchema from salt.utils.dictupdate import merge # This must be present or the Salt loader won't load this module. @@ -250,18 +250,18 @@ def init(opts): raise salt.exceptions.InvalidConfigError( 'Mechanism is set to \'userpass\' , but no ' '\'username\' key found in proxy config') - if not 'passwords' in proxy_conf: + if 'passwords' not in proxy_conf: raise salt.exceptions.InvalidConfigError( 'Mechanism is set to \'userpass\' , but no ' '\'passwords\' key found in proxy config') for key in ('username', 'passwords'): DETAILS[key] = proxy_conf[key] else: - if not 'domain' in proxy_conf: + if 'domain' not in proxy_conf: raise salt.exceptions.InvalidConfigError( 'Mechanism is set to \'sspi\' , but no ' '\'domain\' key found in proxy config') - if not 'principal' in proxy_conf: + if 'principal' not in proxy_conf: raise salt.exceptions.InvalidConfigError( 'Mechanism is set to \'sspi\' , but no ' '\'principal\' key found in proxy config') diff --git a/salt/states/pbm.py b/salt/states/pbm.py index e77f16f48b..bf54f620ad 100644 --- a/salt/states/pbm.py +++ b/salt/states/pbm.py @@ -95,32 +95,21 @@ PyVmomi can be installed via pip: # Import Python Libs from __future__ import absolute_import -import sys import logging -import json -import time import copy # Import Salt Libs from salt.exceptions import CommandExecutionError, ArgumentValueError -import salt.modules.vsphere as vsphere -from salt.utils import is_proxy from salt.utils.dictdiffer import recursive_diff from salt.utils.listdiffer import list_diff -# External libraries -try: - import jsonschema - HAS_JSONSCHEMA = True -except ImportError: - HAS_JSONSCHEMA = False - # Get Logging Started log = logging.getLogger(__name__) # TODO change with vcenter ALLOWED_PROXY_TYPES = ['esxcluster', 'vcenter'] LOGIN_DETAILS = {} + def __virtual__(): if HAS_JSONSCHEMA: return True @@ -297,7 +286,7 @@ def storage_policies_configured(name, policies): # All allowed proxies have a vcenter detail vcenter = __salt__['{0}.get_details'.format(proxy_type)]()['vcenter'] log.info('Running state \'{0}\' on vCenter ' - '\'{0}\''.format(name, vcenter)) + '\'{1}\''.format(name, vcenter)) si = __salt__['vsphere.get_service_instance_via_proxy']() current_policies = __salt__['vsphere.list_storage_policies']( policy_names=[policy['name'] for policy in policies], @@ -378,7 +367,7 @@ def storage_policies_configured(name, policies): 'State {0} will update the storage policy \'{1}\'' ' on vCenter \'{2}\':\n{3}' ''.format(name, policy['name'], vcenter, - '\n'.join( str_changes))) + '\n'.join(str_changes))) else: __salt__['vsphere.update_storage_policy']( policy=current_policy['name'], @@ -449,7 +438,7 @@ def default_storage_policy_assigned(name, policy, datastore): datastore Name of datastore ''' - log.info('Running state {0} for policy \'{1}\, datastore \'{2}\'.' + log.info('Running state {0} for policy \'{1}\', datastore \'{2}\'.' ''.format(name, policy, datastore)) changes = {} changes_required = False @@ -470,7 +459,7 @@ def default_storage_policy_assigned(name, policy, datastore): changes = { 'default_storage_policy': {'old': existing_policy['name'], 'new': policy}} - if (__opts__['test']): + if __opts__['test']: comment = ('State {0} will assign storage policy \'{1}\' to ' 'datastore \'{2}\'.').format(name, policy, datastore) diff --git a/salt/utils/pbm.py b/salt/utils/pbm.py index 17b25aceca..c7fa43eaa4 100644 --- a/salt/utils/pbm.py +++ b/salt/utils/pbm.py @@ -171,7 +171,7 @@ def get_policies_by_id(profile_manager, policy_ids): raise VMwareRuntimeError(exc.msg) -def get_storage_policies(profile_manager, policy_names=[], +def get_storage_policies(profile_manager, policy_names=None, get_all_policies=False): ''' Returns a list of the storage policies, filtered by name. @@ -181,6 +181,7 @@ def get_storage_policies(profile_manager, policy_names=[], policy_names List of policy names to filter by. + Default is None. get_all_policies Flag specifying to return all policies, regardless of the specified @@ -207,6 +208,8 @@ def get_storage_policies(profile_manager, policy_names=[], pbm.profile.ResourceTypeEnum.STORAGE] if get_all_policies: return policies + if not policy_names: + policy_names = [] return [p for p in policies if p.name in policy_names] diff --git a/salt/utils/vmware.py b/salt/utils/vmware.py index cbfb741dc0..018bb10417 100644 --- a/salt/utils/vmware.py +++ b/salt/utils/vmware.py @@ -434,7 +434,7 @@ def get_new_service_instance_stub(service_instance, path, ns=None, #connection handshaking rule. We may need turn of the hostname checking #and client side cert verification context = None - if sys.version_info[:3] > (2,7,8): + if sys.version_info[:3] > (2, 7, 8): context = ssl.create_default_context() context.check_hostname = False context.verify_mode = ssl.CERT_NONE diff --git a/tests/unit/modules/test_vsphere.py b/tests/unit/modules/test_vsphere.py index 9ebad77363..ed043f2728 100644 --- a/tests/unit/modules/test_vsphere.py +++ b/tests/unit/modules/test_vsphere.py @@ -648,7 +648,6 @@ class _GetProxyConnectionDetailsTestCase(TestCase, LoaderModuleMockMixin): 'principal': 'fake_principal', 'domain': 'fake_domain'} - def tearDown(self): for attrname in ('esxi_host_details', 'esxi_vcenter_details', 'esxdatacenter_details', 'esxcluster_details'): diff --git a/tests/unit/utils/test_pbm.py b/tests/unit/utils/test_pbm.py index 4e08229e26..aec9a51da5 100644 --- a/tests/unit/utils/test_pbm.py +++ b/tests/unit/utils/test_pbm.py @@ -10,7 +10,6 @@ from __future__ import absolute_import import logging # Import Salt testing libraries -from tests.support.mixins import LoaderModuleMockMixin from tests.support.unit import TestCase, skipIf from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock, \ PropertyMock @@ -18,6 +17,7 @@ from tests.support.mock import NO_MOCK, NO_MOCK_REASON, patch, MagicMock, \ # Import Salt libraries from salt.exceptions import VMwareApiError, VMwareRuntimeError, \ VMwareObjectRetrievalError +from salt.ext.six.moves import range import salt.utils.pbm try: @@ -187,9 +187,9 @@ class GetCapabilityDefinitionsTestCase(TestCase): '''Tests for salt.utils.pbm.get_capability_definitions''' def setUp(self): self.mock_res_type = MagicMock() - self.mock_cap_cats =[MagicMock(capabilityMetadata=['fake_cap_meta1', - 'fake_cap_meta2']), - MagicMock(capabilityMetadata=['fake_cap_meta3'])] + self.mock_cap_cats = [MagicMock(capabilityMetadata=['fake_cap_meta1', + 'fake_cap_meta2']), + MagicMock(capabilityMetadata=['fake_cap_meta3'])] self.mock_prof_mgr = MagicMock( FetchCapabilityMetadata=MagicMock(return_value=self.mock_cap_cats)) patches = ( @@ -312,7 +312,7 @@ class GetStoragePoliciesTestCase(TestCase): self.mock_prof_mgr = MagicMock( QueryProfile=MagicMock(return_value=self.mock_policy_ids)) # Policies - self.mock_policies=[] + self.mock_policies = [] for i in range(4): mock_obj = MagicMock(resourceType=MagicMock( resourceType=pbm.profile.ResourceTypeEnum.STORAGE)) @@ -576,7 +576,7 @@ class GetDefaultStoragePolicyOfDatastoreTestCase(TestCase): def test_no_policy_refs(self): mock_get_policies_by_id = MagicMock() - with path('salt.utils.pbm.get_policies_by_id', + with patch('salt.utils.pbm.get_policies_by_id', MagicMock(return_value=None)): with self.assertRaises(VMwareObjectRetrievalError) as excinfo: salt.utils.pbm.get_default_storage_policy_of_datastore( @@ -585,7 +585,7 @@ class GetDefaultStoragePolicyOfDatastoreTestCase(TestCase): 'Storage policy with id \'fake_policy_id\' was not ' 'found') - def test_no_policy_refs(self): + def test_return_policy_ref(self): mock_get_policies_by_id = MagicMock() ret = salt.utils.pbm.get_default_storage_policy_of_datastore( self.mock_prof_mgr, self.mock_ds)