Skip to content

Commit 0aafa6d

Browse files
committed
Merge branch 'snapshot_support', closes #54
2 parents 4a4fc3c + 873bf65 commit 0aafa6d

20 files changed

+373
-7
lines changed

ec2stack/controllers/default.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from ec2stack.core import Ec2stackError
99
from ec2stack.services import USERS
1010
from ec2stack.providers.cloudstack import images, instances, keypairs, \
11-
passwords, security_groups, zones, volumes, tags, vpcs
11+
passwords, security_groups, zones, volumes, tags, vpcs, snapshots
1212

1313

1414
DEFAULT = Blueprint('default', __name__)
@@ -45,11 +45,13 @@ def _get_action(action):
4545
security_groups.authenticate_security_group_ingress,
4646
'CreateKeyPair': keypairs.create_keypair,
4747
'CreateSecurityGroup': security_groups.create_security_group,
48+
'CreateSnapshot': snapshots.create_snapshot,
4849
'CreateTags': tags.create_tags,
4950
'CreateVolume': volumes.create_volume,
5051
'CreateVpc': vpcs.create_vpc,
5152
'DeleteKeyPair': keypairs.delete_keypair,
5253
'DeleteSecurityGroup': security_groups.delete_security_group,
54+
'DeleteSnapshot': snapshots.delete_snapshot,
5355
'DeleteTags': tags.delete_tags,
5456
'DeleteVolume': volumes.delete_volume,
5557
'DeleteVpc': vpcs.delete_vpc,
@@ -60,6 +62,7 @@ def _get_action(action):
6062
'DescribeInstances': instances.describe_instances,
6163
'DescribeKeyPairs': keypairs.describe_keypairs,
6264
'DescribeSecurityGroups': security_groups.describe_security_groups,
65+
'DescribeSnapshots': snapshots.describe_snapshots,
6366
'DescribeTags': tags.describe_tags,
6467
'DescribeVolumes': volumes.describe_volumes,
6568
'DescribeVpcs': vpcs.describe_vpcs,
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
"""This module contains functions for handling requests in relation to snapshots.
5+
"""
6+
7+
from ec2stack import errors
8+
from ec2stack import helpers
9+
from ec2stack.providers import cloudstack
10+
from ec2stack.providers.cloudstack import requester
11+
12+
13+
@helpers.authentication_required
14+
def create_snapshot():
15+
"""
16+
Create a snapshot.
17+
18+
@return: Response.
19+
"""
20+
helpers.require_parameters(['VolumeId'])
21+
response = _create_snapshot_request()
22+
return _create_snapshot_response(response)
23+
24+
25+
def _create_snapshot_request():
26+
"""
27+
Request to create a snapshot.
28+
29+
@return: Response.
30+
"""
31+
args = {'command': 'createSnapshot', 'volumeid': helpers.get('VolumeId')}
32+
33+
response = requester.make_request_async(args)
34+
35+
return response
36+
37+
38+
def _create_snapshot_response(response):
39+
"""
40+
Generates a response for create snapshot request.
41+
42+
@param response: Response from Cloudstack.
43+
@return: Response.
44+
"""
45+
if 'errortext' in response:
46+
if 'Invalid parameter volumeid' in response['errortext']:
47+
errors.invalid_volume_id()
48+
else:
49+
response = response['snapshot']
50+
return {
51+
'template_name_or_list': 'create_snapshot.xml',
52+
'response_type': 'CreateSnapshotResponse',
53+
'response': response
54+
}
55+
56+
57+
@helpers.authentication_required
58+
def delete_snapshot():
59+
"""
60+
Delete a snapshot.
61+
62+
@return: Response.
63+
"""
64+
helpers.require_parameters(['SnapshotId'])
65+
response = _delete_snapshot_request()
66+
return _delete_snapshot_response(response)
67+
68+
69+
def _delete_snapshot_request():
70+
"""
71+
Request to delete a snapshot.
72+
73+
@return: Response.
74+
"""
75+
args = {'command': 'deleteSnapshot', 'id': helpers.get('SnapshotId')}
76+
77+
response = requester.make_request_async(args)
78+
79+
return response
80+
81+
82+
def _delete_snapshot_response(response):
83+
"""
84+
Generates a response for delete snapshot request.
85+
86+
@return: Response.
87+
"""
88+
if 'errortext' in response:
89+
if 'Invalid parameter id' in response['errortext']:
90+
errors.invalid_snapshot_id()
91+
return {
92+
'template_name_or_list': 'status.xml',
93+
'response_type': 'DeleteSnapshotResponse',
94+
'return': 'true'
95+
}
96+
97+
98+
@helpers.authentication_required
99+
def describe_snapshots():
100+
"""
101+
Describes a specific snapshot or all snapshots.
102+
103+
@return: Response.
104+
"""
105+
args = {'command': 'listSnapshots'}
106+
response = cloudstack.describe_item(
107+
args, 'snapshot', errors.invalid_snapshot_id, 'SnapshotId'
108+
)
109+
110+
return _describe_snapshot_response(
111+
response
112+
)
113+
114+
115+
def _describe_snapshot_response(response):
116+
"""
117+
Generates a response for describe snapshot request.
118+
119+
@param response: Response from Cloudstack.
120+
@return: Response.
121+
"""
122+
return {
123+
'template_name_or_list': 'snapshots.xml',
124+
'response_type': 'DescribeSnapshotsResponse',
125+
'response': response
126+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{% extends "response.xml" %}
2+
{% block response_content %}
3+
<snapshot>
4+
<snapshotId>{{ response.id }}</snapshotId>
5+
<volumeId>{{ response.volumeid }}</volumeId>
6+
<status>{{ response.state }}</status>
7+
</snapshot>
8+
{% endblock %}

ec2stack/templates/snapshots.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
{% extends "response.xml" %}
2+
{% block response_content %}
3+
<snapshotSet>
4+
{% for snapshot in response.snapshot %}
5+
<item>
6+
<snapshotId>{{ snapshot.id }}</snapshotId>
7+
<volumeId>{{ snapshot.volumeid }}</volumeId>
8+
<status>{{ response.state }}</status>
9+
</item>
10+
{% endfor %}
11+
</snapshotSet>
12+
{% endblock %}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"createsnapshotresponse": {
3+
"errorcode": 431,
4+
"uuidlist": [],
5+
"cserrorcode": 9999,
6+
"errortext": "Unable to execute API command createsnapshot due to invalid value. Invalid parameter volumeid value=120a5425-6092-4700-baaf-600fcbaa10 due to incorrect long value format, or entity does not exist or due to incorrect parameter annotation for the field in api cmd class."
7+
}
8+
}
File renamed without changes.
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"deletesnapshotresponse": {
3+
"errorcode": 431,
4+
"uuidlist": [],
5+
"cserrorcode": 9999,
6+
"errortext": "Unable to execute API command deletesnapshot due to invalid value. Invalid parameter id value=abdb81fa-f4ac-4ec6-822c-34af7cd461 due to incorrect long value format, or entity does not exist or due to incorrect parameter annotation for the field in api cmd class."
7+
}
8+
}
File renamed without changes.

tests/data/invalid_describe_image_invalid_image_id.json renamed to tests/data/invalid_describe_image_image_not_found.json

File renamed without changes.

tests/data/invalid_describe_volume_invalid_instance_id.json renamed to tests/data/invalid_describe_volume_instance_not_found.json

File renamed without changes.

0 commit comments

Comments
 (0)