From 97762f845d535d613af88b9aab6a77be40d0491b Mon Sep 17 00:00:00 2001 From: Pedro Algarvio Date: Tue, 16 Sep 2014 02:25:55 +0100 Subject: [PATCH] Protect against older libcloud versions --- salt/cloud/clouds/gce.py | 27 ++++++++++++++++----------- salt/cloud/libcloudfuncs.py | 17 ++++++++--------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/salt/cloud/clouds/gce.py b/salt/cloud/clouds/gce.py index 3d476532b5..f40ac6003e 100644 --- a/salt/cloud/clouds/gce.py +++ b/salt/cloud/clouds/gce.py @@ -1873,22 +1873,27 @@ def create(vm_=None, call=None): 'ex_metadata': __get_metadata(vm_), 'external_ip': config.get_cloud_config_value( 'external_ip', vm_, __opts__, default='ephemeral' - ), - 'ex_disk_type': config.get_cloud_config_value( - 'ex_disk_type', vm_, __opts__, default='pd-standard'), - 'ex_disk_auto_delete': config.get_cloud_config_value( - 'ex_disk_auto_delete', vm_, __opts__, default=True) + ) } + if LIBCLOUD_VERSION_INFO > (0, 15, 1): + # This only exists in current trunk of libcloud and should be in next + # release + kwargs.update({ + 'ex_disk_type': config.get_cloud_config_value( + 'ex_disk_type', vm_, __opts__, default='pd-standard'), + 'ex_disk_auto_delete': config.get_cloud_config_value( + 'ex_disk_auto_delete', vm_, __opts__, default=True) + }) + if kwargs.get('ex_disk_type') not in ('pd-standard', 'pd-ssd'): + raise SaltCloudSystemExit( + 'The value of \'ex_disk_type\' needs to be one of: ' + '\'pd-standard\', \'pd-ssd\'' + ) + if 'external_ip' in kwargs and kwargs['external_ip'] == "None": kwargs['external_ip'] = None - if kwargs.get('ex_disk_type') not in ('pd-standard', 'pd-ssd'): - raise SaltCloudSystemExit( - 'The value of \'ex_disk_type\' needs to be one of: ' - '\'pd-standard\', \'pd-ssd\'' - ) - log.info('Creating GCE instance {0} in {1}'.format(vm_['name'], kwargs['location'].name) ) diff --git a/salt/cloud/libcloudfuncs.py b/salt/cloud/libcloudfuncs.py index 36bd61c744..0a17b2901c 100644 --- a/salt/cloud/libcloudfuncs.py +++ b/salt/cloud/libcloudfuncs.py @@ -12,6 +12,7 @@ import logging # pylint: disable=W0611 # Import libcloud try: + import libcloud from libcloud.compute.types import Provider from libcloud.compute.providers import get_driver from libcloud.compute.deployment import ( @@ -20,8 +21,13 @@ try: SSHKeyDeployment ) HAS_LIBCLOUD = True + LIBCLOUD_VERSION_INFO = tuple([ + int(part) for part in libcloud.__version__.replace('-', '.').split()[:3] + ]) + except ImportError: HAS_LIBCLOUD = False + LIBCLOUD_VERSION_INFO = (1000,) # pylint: enable=W0611 @@ -68,7 +74,7 @@ def check_libcloud_version(reqver=LIBCLOUD_MINIMAL_VERSION, why=None): '\'reqver\' needs to passed as a tuple or list, ie, (0, 14, 0)' ) try: - import libcloud + import libcloud # pylint: disable=redefined-outer-name except ImportError: raise ImportError( 'salt-cloud requires >= libcloud {0} which is not installed'.format( @@ -76,14 +82,7 @@ def check_libcloud_version(reqver=LIBCLOUD_MINIMAL_VERSION, why=None): ) ) - ver = libcloud.__version__ - ver = ver.replace('-', '.') - comps = ver.split('.') - version = [] - for number in comps[:3]: - version.append(int(number)) - - if tuple(version) >= reqver: + if LIBCLOUD_VERSION_INFO >= reqver: return libcloud.__version__ errormsg = 'Your version of libcloud is {0}. '.format(libcloud.__version__)