Merge pull request #30941 from rallytime/bp-30879

Back-port #30879 to 2015.8
This commit is contained in:
Mike Place 2016-02-05 14:15:20 -07:00
commit d9785451c0
2 changed files with 40 additions and 4 deletions

View File

@ -2081,12 +2081,17 @@ def network_absent(name, driver=None):
return ret
def volume_present(name, driver=None, driver_opts=None):
def volume_present(name, driver=None, driver_opts=None, force=False):
'''
Ensure that a volume is present.
.. versionadded:: 2015.8.4
.. versionchanged:: 2015.8.6
This function no longer deletes and re-creates a volume if the
existing volume's driver does not match the ``driver``
parameter (unless the ``force`` parameter is set to ``True``).
name
Name of the volume
@ -2100,7 +2105,16 @@ def volume_present(name, driver=None, driver_opts=None):
explicitly name Docker's default driver here.)
driver_opts
Option for tha volume driver
Options for the volume driver
force : False
If the volume already exists but the existing volume's driver
does not match the driver specified by the ``driver``
parameter, this parameter controls whether the function errors
out (if ``False``) or deletes and re-creates the volume (if
``True``).
.. versionadded:: 2015.8.6
Usage Examples:
@ -2152,6 +2166,12 @@ def volume_present(name, driver=None, driver_opts=None):
# volume exits, check if driver is the same.
volume = volumes[0]
if driver is not None and volume['Driver'] != driver:
if not force:
ret['comment'] = "Driver for existing volume '{0}' ('{1}')" \
" does not match specified driver ('{2}')" \
" and force is False".format(
name, volume['Driver'], driver)
return ret
try:
ret['changes']['removed'] = __salt__['dockerng.remove_volume'](name)
except Exception as exc:

View File

@ -572,8 +572,23 @@ class DockerngTestCase(TestCase):
ret)
self.assertEqual(orig_volumes, volumes)
# run it again, except with a different driver
# run it again with a different driver but don't force
ret = dockerng_state.volume_present('volume_foo', driver='local')
self.assertEqual(
{
'name': 'volume_foo',
'comment': ("Driver for existing volume 'volume_foo'"
" ('dummy_default') does not match specified"
" driver ('local') and force is False"),
'changes': {},
'result': False,
},
ret)
self.assertEqual(orig_volumes, volumes)
# run it again with a different driver and force
ret = dockerng_state.volume_present(
'volume_foo', driver='local', force=True)
self.assertEqual(
{
'name': 'volume_foo',
@ -611,7 +626,8 @@ class DockerngTestCase(TestCase):
{'__salt__': __salt__}):
ret = dockerng_state.volume_present(
'volume_foo',
driver='bar'
driver='bar',
force=True,
)
dockerng_remove_volume.assert_called_with('volume_foo')
dockerng_create_volume.assert_called_with('volume_foo',