Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
9 changed files
with
347 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,123 @@ | ||
#!/usr/bin/env python | ||
# encoding: utf-8 | ||
|
||
"""This module contains functions for handling requests in relation to snapshots. | ||
""" | ||
|
||
import uuid | ||
|
||
from flask import current_app | ||
|
||
from ec2stack import errors | ||
from ec2stack import helpers | ||
from ec2stack.providers import cloudstack | ||
from ec2stack.providers.cloudstack import requester, zones | ||
|
||
|
||
@helpers.authentication_required | ||
def create_snapshot(): | ||
""" | ||
Create a snapshot. | ||
@return: Response. | ||
""" | ||
response = _create_snapshot_request() | ||
return _create_snapshot_response(response) | ||
|
||
|
||
def _create_snapshot_request(): | ||
""" | ||
Request to create a snapshot. | ||
@return: Response. | ||
""" | ||
args = {'command': 'createSnapshot'} | ||
|
||
response = requester.make_request_async(args) | ||
|
||
return response | ||
|
||
|
||
def _create_snapshot_response(response): | ||
""" | ||
Generates a response for create snapshot request. | ||
@param response: Response from Cloudstack. | ||
@return: Response. | ||
""" | ||
|
||
response = response['snapshot'] | ||
return { | ||
'template_name_or_list': 'create_snapshot.xml', | ||
'response_type': 'CreateSnapshotResponse', | ||
'response': response | ||
} | ||
|
||
|
||
@helpers.authentication_required | ||
def delete_snapshot(): | ||
""" | ||
Delete a snapshot. | ||
@return: Response. | ||
""" | ||
helpers.require_parameters(['SnapshotId']) | ||
_delete_snapshot_request() | ||
return _delete_snapshot_response() | ||
|
||
|
||
def _delete_snapshot_request(): | ||
""" | ||
Request to delete a snapshot. | ||
@return: Response. | ||
""" | ||
args = {'command': 'deleteSnapshot', 'id': helpers.get('SnapshotId')} | ||
|
||
response = requester.make_request_async(args) | ||
|
||
return response | ||
|
||
|
||
def _delete_snapshot_response(): | ||
""" | ||
Generates a response for delete snapshot request. | ||
@return: Response. | ||
""" | ||
return { | ||
'template_name_or_list': 'status.xml', | ||
'response_type': 'DeleteSnapshotResponse', | ||
'return': 'true' | ||
} | ||
|
||
|
||
@helpers.authentication_required | ||
def describe_snapshots(): | ||
""" | ||
Describes a specific snapshot or all snapshots. | ||
@return: Response. | ||
""" | ||
args = {'command': 'listSnapshots'} | ||
response = cloudstack.describe_item( | ||
args, 'snapshot', errors.invalid_snapshot_id, 'SnapshotId' | ||
) | ||
|
||
return _describe_snapshot_response( | ||
response | ||
) | ||
|
||
|
||
def _describe_snapshot_response(response): | ||
""" | ||
Generates a response for describe snapshot request. | ||
@param response: Response from Cloudstack. | ||
@return: Response. | ||
""" | ||
return { | ||
'template_name_or_list': 'snapshots.xml', | ||
'response_type': 'DescribeSnapshotsResponse', | ||
'response': response | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,8 @@ | ||
{% extends "response.xml" %} | ||
{% block response_content %} | ||
<snapshot> | ||
<snapshotId>{{ response.id }}</snapshotId> | ||
<volumeId>{{ snapshot.volumeid }}</volumeId> | ||
<status>{{ response.state }}</status> | ||
</snapshot> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,12 @@ | ||
{% extends "response.xml" %} | ||
{% block response_content %} | ||
<snapshotSet> | ||
{% for snapshot in response.snapshot %} | ||
<item> | ||
<snapshotId>{{ snapshot.id }}</snapshotId> | ||
<volumeId>{{ snapshot.volumeid }}</volumeId> | ||
<status>{{ response.state }}</status> | ||
</item> | ||
{% endfor %} | ||
</snapshotSet> | ||
{% endblock %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,25 @@ | ||
{ | ||
"queryasyncjobresultresponse": { | ||
"accountid": "f7ca69ba-1cd5-11e4-b589-080027ce083d", | ||
"userid": "f7ca8d3c-1cd5-11e4-b589-080027ce083d", | ||
"cmd": "org.apache.cloudstack.api.command.user.vmsnapshot.CreateVMSnapshotCmd", | ||
"jobstatus": 1, | ||
"jobprocstatus": 0, | ||
"jobresultcode": 0, | ||
"jobresulttype": "object", | ||
"jobresult": { | ||
"vmsnapshot": { | ||
"id": "e0e04b84-6178-49a7-9d2f-a2cc6a82d4e4", | ||
"name": "i-2-12-VM_VS_20140805203303", | ||
"state": "Ready", | ||
"displayname": "testsnapshot", | ||
"virtualmachineid": "daa492b4-bd09-46b0-a4ad-142e187ecdbe", | ||
"current": true, | ||
"type": "Disk", | ||
"created": "2014-08-05T21:33:04+0100" | ||
} | ||
}, | ||
"created": "2014-08-05T21:33:04+0100", | ||
"jobid": "8f89700a-6bd0-4b4b-a3f9-272fff1e8fc7" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,16 @@ | ||
{ | ||
"queryasyncjobresultresponse": { | ||
"accountid": "f7ca69ba-1cd5-11e4-b589-080027ce083d", | ||
"userid": "f7ca8d3c-1cd5-11e4-b589-080027ce083d", | ||
"cmd": "org.apache.cloudstack.api.command.user.vmsnapshot.DeleteVMSnapshotCmd", | ||
"jobstatus": 1, | ||
"jobprocstatus": 0, | ||
"jobresultcode": 0, | ||
"jobresulttype": "object", | ||
"jobresult": { | ||
"success": true | ||
}, | ||
"created": "2014-08-05T21:37:50+0100", | ||
"jobid": "f94a67b2-e389-4790-ae89-f19968d31318" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,16 @@ | ||
{ | ||
"listvmsnapshotresponse": { | ||
"count": 1, | ||
"vmSnapshot": [{ | ||
"id": "examplesnapshot", | ||
"name": "i-2-12-VM_VS_20140805203432", | ||
"state": "Ready", | ||
"displayname": "testsnapshot1", | ||
"virtualmachineid": "daa492b4-bd09-46b0-a4ad-142e187ecdbe", | ||
"parentName": "testsnapshot", | ||
"current": true, | ||
"type": "Disk", | ||
"created": "2014-08-05T21:34:32+0100" | ||
}] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,25 @@ | ||
{ | ||
"listvmsnapshotresponse": { | ||
"count": 2, | ||
"vmSnapshot": [{ | ||
"id": "9f2f067f-3ae7-427d-95d9-3c338dcbd5a3", | ||
"name": "i-2-12-VM_VS_20140805203432", | ||
"state": "Ready", | ||
"displayname": "testsnapshot1", | ||
"virtualmachineid": "daa492b4-bd09-46b0-a4ad-142e187ecdbe", | ||
"parentName": "testsnapshot", | ||
"current": true, | ||
"type": "Disk", | ||
"created": "2014-08-05T21:34:32+0100" | ||
}, { | ||
"id": "e0e04b84-6178-49a7-9d2f-a2cc6a82d4e4", | ||
"name": "i-2-12-VM_VS_20140805203303", | ||
"state": "Ready", | ||
"displayname": "testsnapshot", | ||
"virtualmachineid": "daa492b4-bd09-46b0-a4ad-142e187ecdbe", | ||
"current": false, | ||
"type": "Disk", | ||
"created": "2014-08-05T21:33:04+0100" | ||
}] | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@@ -0,0 +1,118 @@ | ||
#!/usr/bin/env python | ||
# encoding: utf-8 | ||
|
||
from base64 import b64encode | ||
|
||
import mock | ||
import json | ||
|
||
from ec2stack.helpers import read_file, generate_signature | ||
from . import Ec2StackAppTestCase | ||
|
||
|
||
class SnapshotTestCase(Ec2StackAppTestCase): | ||
|
||
def test_create_snapshot(self): | ||
data = self.get_example_data() | ||
data['Action'] = 'CreateSnapshot' | ||
data['VolumeId'] = 'daa492b4-bd09-46b0-a4ad-142e187ecdbe' | ||
data['Signature'] = generate_signature(data, 'POST', 'localhost', '/') | ||
|
||
get = mock.Mock() | ||
get.return_value.text = read_file( | ||
'tests/data/valid_create_snapshot.json' | ||
) | ||
get.return_value.status_code = 200 | ||
|
||
with mock.patch('requests.get', get): | ||
response = self.post( | ||
'/', | ||
data=data | ||
) | ||
|
||
self.assert_ok(response) | ||
assert 'CreateSnapshotResponse' in response.data | ||
|
||
def test_describe_snapshots(self): | ||
data = self.get_example_data() | ||
data['Action'] = 'DescribeSnapshots' | ||
data['Signature'] = generate_signature(data, 'POST', 'localhost', '/') | ||
|
||
get = mock.Mock() | ||
get.return_value.text = read_file( | ||
'tests/data/valid_describe_snapshots.json' | ||
) | ||
get.return_value.status_code = 200 | ||
|
||
with mock.patch('requests.get', get): | ||
response = self.post( | ||
'/', | ||
data=data | ||
) | ||
|
||
self.assert_ok(response) | ||
assert 'DescribeSnapshotsResponse' in response.data | ||
|
||
def test_describe_snapshot_by_name(self): | ||
data = self.get_example_data() | ||
data['Action'] = 'DescribeVpcs' | ||
data['SnapshotId'] = 'examplesnapshot' | ||
data['Signature'] = generate_signature(data, 'POST', 'localhost', '/') | ||
|
||
get = mock.Mock() | ||
get.return_value.text = read_file( | ||
'tests/data/valid_describe_snapshot.json' | ||
) | ||
get.return_value.status_code = 200 | ||
|
||
with mock.patch('requests.get', get): | ||
response = self.post( | ||
'/', | ||
data=data | ||
) | ||
|
||
self.assert_ok(response) | ||
assert 'DescribeSnapshotsResponse' in response.data | ||
assert 'examplesnapshot' in response.data | ||
|
||
def test_describe_snapshot_by_name_invalid_name(self): | ||
data = self.get_example_data() | ||
data['Action'] = 'DescribeVpcs' | ||
data['SnapshotId'] = 'invalidsnapshot' | ||
data['Signature'] = generate_signature(data, 'POST', 'localhost', '/') | ||
|
||
get = mock.Mock() | ||
get.return_value.text = read_file( | ||
'tests/data/valid_describe_snapshot.json' | ||
) | ||
get.return_value.status_code = 200 | ||
|
||
with mock.patch('requests.get', get): | ||
response = self.post( | ||
'/', | ||
data=data | ||
) | ||
|
||
self.assert_bad_request(response) | ||
assert 'InvalidSnapshotID.NotFound' in response.data | ||
|
||
def test_delete_snapshot(self): | ||
data = self.get_example_data() | ||
data['Action'] = 'DeleteSnapshot' | ||
data['SnapshotId'] = 'Test' | ||
data['Signature'] = generate_signature(data, 'POST', 'localhost', '/') | ||
|
||
get = mock.Mock() | ||
get.return_value.text = read_file( | ||
'tests/data/valid_delete_snapshot.json' | ||
) | ||
get.return_value.status_code = 200 | ||
|
||
with mock.patch('requests.get', get): | ||
response = self.post( | ||
'/', | ||
data=data | ||
) | ||
|
||
self.assert_ok(response) | ||
assert 'DeleteSnapshotResponse' in response.data |