utils.boto: add convenience partials

This commit is contained in:
Matthew Williams 2015-04-13 15:42:32 +00:00
parent 4976f863f9
commit b3ec570499
2 changed files with 37 additions and 0 deletions

View File

@ -14,6 +14,7 @@ from __future__ import absolute_import
import hashlib
import logging
from distutils.version import LooseVersion as _LooseVersion # pylint: disable=import-error,no-name-in-module
from functools import partial
# Import salt libs
import salt.ext.six as six
@ -129,6 +130,19 @@ def cache_id(service, name, sub_resource=None, resource_id=None,
return __context__.get(cxkey)
def cache_id_func(service):
'''
Returns a partial `cache_id` function for the provided service.
... code-block:: python
cache_id = __utils__['boto.cache_id_func']('ec2')
cache_id('myinstance', 'i-a1b2c3')
instance_id = cache_id('myinstance')
'''
return partial(cache_id, service)
def get_connection(service, module=None, region=None, key=None, keyid=None,
profile=None):
'''
@ -163,6 +177,18 @@ def get_connection(service, module=None, region=None, key=None, keyid=None,
return conn
def get_connection_func(service):
'''
Returns a partial `get_connection` function for the provided service.
... code-block:: python
get_conn = __utils__['boto.get_connection_func']('ec2')
conn = get_conn()
'''
return partial(get_connection, service)
def get_exception(e):
'''
Extract the message from a boto exception and return a

View File

@ -131,6 +131,11 @@ class BotoUtilsCacheIdTestCase(BotoUtilsTestCaseBase):
salt.utils.boto.cache_id(service, resource_name, resource_id=resource_id, invalidate=True)
self.assertEqual(salt.utils.boto.cache_id(service, resource_name), None)
def test_partial(self):
cache_id = salt.utils.boto.cache_id_func(service)
cache_id(resource_name, resource_id=resource_id)
self.assertEqual(cache_id(resource_name), resource_id)
@skipIf(NO_MOCK, NO_MOCK_REASON)
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@ -164,6 +169,12 @@ class BotoUtilsGetConnTestCase(BotoUtilsTestCaseBase):
with self.assertRaises(CommandExecutionError):
salt.utils.boto.get_connection(service)
@mock_ec2
def test_partial(self):
get_conn = salt.utils.boto.get_connection_func(service)
conn = get_conn(**conn_parameters)
self.assertTrue(conn in salt.utils.boto.__context__.values())
@skipIf(HAS_BOTO is False, 'The boto module must be installed.')
@skipIf(_has_required_boto() is False, 'The boto module must be greater than'