add list_volumes

This commit is contained in:
Anthony Shaw 2017-04-29 15:54:59 +10:00
parent ec95304153
commit 3f8827f6b4
No known key found for this signature in database
GPG Key ID: AB4A19AE1CE85744
2 changed files with 69 additions and 3 deletions

View File

@ -243,6 +243,42 @@ def destroy_node(node_id, profile, **libcloud_kwargs):
return conn.destroy_node(matches[0], **libcloud_kwargs)
def list_volumes(profile, **libcloud_kwargs):
'''
Return a list of storage volumes for this cloud
:param profile: The profile key
:type profile: ``str``
:param libcloud_kwargs: Extra arguments for the driver's list_volumes method
:type libcloud_kwargs: ``dict``
CLI Example:
.. code-block:: bash
salt myminion libcloud_compute.list_volumes profile1
'''
conn = _get_driver(profile=profile)
libcloud_kwargs = clean_kwargs(**libcloud_kwargs)
volumes = conn.list_volumes(**libcloud_kwargs)
ret = []
for volume in volumes:
ret.append(_simple_volume(volume))
return ret
def _simple_volume(volume):
return {
'id': volume.id,
'name': volume.name,
'size': volume.size,
'state': volume.state,
'extra': volume.extra
}
def _simple_location(location):
return {
'id': location.id,

View File

@ -17,7 +17,9 @@ from tests.support.mock import (
)
import salt.modules.libcloud_compute as libcloud_compute
from libcloud.compute.base import BaseDriver, Node, NodeSize, NodeState, NodeLocation
from libcloud.compute.base import (BaseDriver, Node,
NodeSize, NodeState, NodeLocation,
StorageVolume, StorageVolumeState)
class MockComputeDriver(BaseDriver):
@ -31,8 +33,21 @@ class MockComputeDriver(BaseDriver):
size=self._TEST_SIZE, extra={
'ex_key': 'ex_value'
})
self._TEST_LOCATION = NodeLocation(id='test_location',
name='location1', country='Australia', driver=self)
self._TEST_LOCATION = NodeLocation(
id='test_location',
name='location1',
country='Australia',
driver=self)
self._TEST_VOLUME = StorageVolume(
id='vol1',
name='vol_name',
size=40960,
driver=self,
state=StorageVolumeState.AVAILABLE,
extra={
'ex_key': 'ex_value'
}
)
def list_nodes(self):
return [self._TEST_NODE]
@ -53,6 +68,9 @@ class MockComputeDriver(BaseDriver):
assert node.id == 'test_id'
return True
def list_volumes(self):
return [self._TEST_VOLUME]
@skipIf(NO_MOCK, NO_MOCK_REASON)
@patch('salt.modules.libcloud_compute._get_driver',
@ -101,6 +119,13 @@ class LibcloudComputeModuleTestCase(TestCase, LoaderModuleMockMixin):
self.assertEqual(location['name'], 'location1')
self.assertEqual(location['country'], 'Australia')
def _validate_volume(self, volume):
self.assertEqual(volume['id'], 'vol1')
self.assertEqual(volume['name'], 'vol_name')
self.assertEqual(volume['size'], 40960)
self.assertEqual(volume['state'], 'available')
self.assertEqual(volume['extra'], {'ex_key': 'ex_value'})
def test_list_nodes(self):
nodes = libcloud_compute.list_nodes('test')
self.assertEqual(len(nodes), 1)
@ -136,3 +161,8 @@ class LibcloudComputeModuleTestCase(TestCase, LoaderModuleMockMixin):
def test_destroy_node_invalid(self):
with self.assertRaises(ValueError):
libcloud_compute.destroy_node('foo_node', 'test')
def test_list_volumes(self):
volumes = libcloud_compute.list_volumes('test')
self.assertEqual(len(volumes), 1)
self._validate_volume(volumes[0])