Commit 6c9a6161 authored by Stavros Sachtouris's avatar Stavros Sachtouris

Implement Cyclades Block Storage client

The Cyclades client overrides some methods of the OS-specific
client, due to API differences: create_volume, create_snapshot

Unit tests for these methods are also implemented
parent 77bb1dc3
......@@ -31,7 +31,8 @@
# interpreted as representing official policies, either expressed
# or implied, of GRNET S.A.
from kamaki.clients.cyclades.rest_api import CycladesComputeRestClient
from kamaki.clients.cyclades.rest_api import (
CycladesComputeRestClient, CycladesBlockStorageRestClient)
from kamaki.clients.network import NetworkClient
from kamaki.clients.utils import path4url
from kamaki.clients import ClientError, Waiter
......@@ -280,3 +281,38 @@ class CycladesNetworkClient(NetworkClient):
path = path4url('floatingips', floating_network_id, 'action')
json_data = dict(reassign=dict(project=project_id))
self.post(path, json=json_data, success=200)
class CycladesBlockStorageClient(CycladesBlockStorageRestClient):
"""Cyclades Block Storage REST API Client"""
def create_volume(
self, size, server_id, display_name,
display_description=None,
snapshot_id=None,
imageRef=None,
volume_type=None,
metadata=None,
project=None):
""":returns: (dict) new volumes' details"""
r = self.volumes_post(
size, server_id, display_name,
display_description=display_description,
snapshot_id=snapshot_id,
imageRef=imageRef,
volume_type=volume_type,
metadata=metadata,
project=project)
return r.json
def reassign_volume(self, volume_id, project):
self.volumes_action_post(volume_id, {"reassign": {"project": project}})
def create_snapshot(
self, volume_id, display_name,
force=None, display_description=None):
return super(CycladesBlockStorageClient, self).create_snapshot(
volume_id,
display_name=display_name,
force=force,
display_description=display_description)
......@@ -286,6 +286,54 @@ class CycladesBlockStorageRestClient(TestCase):
json=project_id, success=success, **kwargs))
bsrest_pkg = 'kamaki.clients.cyclades.CycladesBlockStorageRestClient'
class CycladesBlockStorageClient(TestCase):
def setUp(self):
self.url = 'http://volumes.example.com'
self.token = 'v01um3s70k3n'
self.client = cyclades.CycladesBlockStorageClient(self.url, self.token)
@patch('%s.volumes_post' % bsrest_pkg, return_value=FR())
def test_create_volume(self, volumes_post):
keys = (
'display_description', 'snapshot_id', 'imageRef',
'volume_type', 'metadata', 'project')
FR.json, server_id, display_name = 'ret', 'vid', 'dn'
for args in product(
('dd', None), ('sn', None), ('ir', None),
('vt', None), ({'mk': 'mv'}, None), ('pid', None)):
self.assertEqual(
self.client.create_volume(42, server_id, display_name, *args),
'ret')
kwargs = dict(zip(keys, args))
self.assertEqual(
volumes_post.mock_calls[-1],
call(42, server_id, display_name, **kwargs))
@patch('%s.volumes_action_post' % bsrest_pkg, return_value=FR())
def test_reassign_volume(self, volumes_action_post):
volume_id, project_id = 'vid', 'pid'
self.client.reassign_volume(volume_id, project_id)
volumes_action_post.assert_called_once_with(
volume_id, {"reassign": {"project": project_id}})
@patch('%s.create_snapshot' % bsrest_pkg, return_value='ret')
def test_create_snapshot(self, create_snapshot):
keys = ('force', 'display_description')
volume_id, display_name = 'vid', 'dn'
for args in product((True, False, None), ('dd', None)):
self.assertEqual(
self.client.create_snapshot(volume_id, display_name, *args),
'ret')
kwargs = dict(zip(keys, args))
self.assertEqual(
create_snapshot.mock_calls[-1],
call(volume_id, display_name=display_name, **kwargs))
if __name__ == '__main__':
from sys import argv
from kamaki.clients.test import runTestCase
......@@ -305,5 +353,11 @@ if __name__ == '__main__':
CycladesBlockStorageRestClient,
'Cyclades Block Storage Rest Client',
argv[2:])
if not argv[1:] or argv[1] == 'CycladesBlockStorageClient':
not_found = False
runTestCase(
CycladesBlockStorageClient,
'Cyclades Block Storage Client',
argv[2:])
if not_found:
print('TestCase %s not found' % argv[1])
......@@ -44,7 +44,7 @@ from kamaki.clients.astakos.test import (
from kamaki.clients.compute.test import ComputeClient, ComputeRestClient
from kamaki.clients.network.test import (NetworkClient, NetworkRestClient)
from kamaki.clients.cyclades.test import (
CycladesComputeClient, CycladesNetworkClient,
CycladesComputeClient, CycladesNetworkClient, CycladesBlockStorageClient,
CycladesComputeRestClient, CycladesBlockStorageRestClient)
from kamaki.clients.image.test import ImageClient
from kamaki.clients.storage.test import StorageClient
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment