Skip to content
Permalink
Browse files
Add support for start / terminate instance
  • Loading branch information
brogand93 committed Mar 1, 2014
1 parent d2d354b commit 1029fcd0253c0a8bd4a14bf5d092f5a6b7e5c500
Show file tree
Hide file tree
Showing 5 changed files with 125 additions and 2 deletions.
@@ -51,6 +51,8 @@ def _get_action(action):
security_groups.revoke_security_group_egress,
'RevokeSecurityGroupIngress':
security_groups.revoke_security_group_ingress,
'StartInstances': instances.start_instance,
'TerminateInstances': instances.terminate_instance,
}

if action in actions:
@@ -2,6 +2,7 @@
# encoding: utf-8

from ec2stack.providers import cloudstack
from ec2stack.providers.cloudstack import requester

from ec2stack import helpers, errors

@@ -54,3 +55,70 @@ def _describe_instance_attribute_response(response):
}

return response


@helpers.authentication_required
def start_instance():
helpers.require_parameters(['InstanceId.1'])
instance_id = helpers.get('InstanceId.1')
previous_instance_state_description = describe_instance_by_id(instance_id)
new_instance_state_description = _start_instance_request(instance_id)
return _start_instance_response(
previous_instance_state_description,
new_instance_state_description
)


def _start_instance_request(instance_id):
args = {'command': 'startVirtualMachine',
'id': instance_id}

response = requester.make_request_async(args)

response = response['virtualmachine']

return response


def _start_instance_response(previous_state, new_state):
response = {
'template_name_or_list': 'change_instance_state.xml',
'response_type': 'StartInstancesResponse',
'previous_state': previous_state,
'new_state': new_state
}

return response

@helpers.authentication_required
def terminate_instance():
helpers.require_parameters(['InstanceId.1'])
instance_id = helpers.get('InstanceId.1')
previous_instance_state_description = describe_instance_by_id(instance_id)
new_instance_state_description = _terminate_instance_request(instance_id)
return _terminate_instance_response(
previous_instance_state_description,
new_instance_state_description
)


def _terminate_instance_request(instance_id):
args = {'command': 'destroyVirtualMachine',
'id': instance_id}

response = requester.make_request_async(args)

response = response['virtualmachine']

return response


def _terminate_instance_response(previous_state, new_state):
response = {
'template_name_or_list': 'change_instance_state.xml',
'response_type': 'TerminateInstancesResponse',
'previous_state': previous_state,
'new_state': new_state
}

return response
@@ -0,0 +1,50 @@
{% extends "response.xml" %}
{% block response_content %}
<instancesSet>
<item>
<instanceId>{{ new_state.id }}</instanceId>
<currentState>
{% if new_state.state == 'Starting' %}
<code>0</code>
<name>pending</name>
{% elif new_state.state == 'Running' %}
<code>16</code>
<name>running</name>
{% elif new_state.state == 'Destroyed' %}
<code>32</code>
<name>shutting-down</name>
{% elif new_state.state == 'Stopping' %}
<code>64</code>
<name>stopping</name>
{% elif new_state.state == 'Expunging' %}
<code>48</code>
<name>terminated</name>
{% elif new_state.state == 'Stopped' %}
<code>80</code>
<name>stopped</name>
{% endif %}
</currentState>
<previousState>
{% if previous_state.state == 'Starting' %}
<code>0</code>
<name>pending</name>
{% elif previous_state.state == 'Running' %}
<code>16</code>
<name>running</name>
{% elif new_state.state == 'Destroyed' %}
<code>32</code>
<name>shutting-down</name>
{% elif previous_state.state == 'Stopping' %}
<code>64</code>
<name>stopping</name>
{% elif previous_state.state == 'Expunging' %}
<code>48</code>
<name>terminated</name>
{% elif previous_state.state == 'Stopped' %}
<code>80</code>
<name>stopped</name>
{% endif %}
</previousState>
</item>
</instancesSet>
{% endblock %}
@@ -7,9 +7,9 @@
{{ image.id }}
</imageId>
<name>{{ image.name }}</name>
{% if image.isready == 'true' %}
{% if image.isready == true %}
<imageState>available</imageState>
{% elif image.isready == 'false' %}
{% elif image.isready == false %}
<imageState>unavailable</imageState>
{% endif %}
<hypervisor>{{ image.hypervisor }}</hypervisor>
@@ -16,6 +16,9 @@
{% elif instance.state == 'Running' %}
<code>16</code>
<name>running</name>
{% elif new_state.state == 'Destroyed' %}
<code>32</code>
<name>shutting-down</name>
{% elif instance.state == 'Stopping' %}
<code>64</code>
<name>stopping</name>

0 comments on commit 1029fcd

Please sign in to comment.