Skip to content
Permalink
Browse files
Cut down on repeated code in controllers
  • Loading branch information
brogand93 committed Feb 21, 2014
1 parent 48ee9aa commit 35fdc50ca6c09f7f862d15e80b6d61a4bfc74e43
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 26 deletions.
@@ -0,0 +1,28 @@
#!/usr/bin/env python
# encoding: utf-8

from ec2stack.helpers import *
from ec2stack.providers.cloudstack import requester, translator


def describe_item_by_id(item_id, request_function):
args = {
'id': item_id
}

return request_function(args)


def get_items_from_response(response, item_type,
cloudstack_item_attributes_to_aws):
items = []
if response:
for cloudstack_item in response[item_type]:
items.append(
translator.cloudstack_item_to_aws(
cloudstack_item,
cloudstack_item_attributes_to_aws
)
)

return items
@@ -3,6 +3,7 @@

from ec2stack.helpers import *
from ec2stack.providers.cloudstack import requester, translator
from ec2stack.providers.cloudstack.cloudstack_helpers import *


cloudstack_instance_attributes_to_aws = {
@@ -31,7 +32,7 @@ def describe_instance_attribute():
instance_id = get('InstanceId', request.form)
attribute = get('Attribute', request.form)

response = _describe_virtual_machine_by_id(instance_id)
response = describe_item_by_id(instance_id, _describe_virtual_machines_request)
virtual_machine = response['virtualmachine'][0]

instance_attribute = translator.cloudstack_item_attribute_to_aws(
@@ -57,7 +58,8 @@ def _describe_virtual_machines_request(args=None):

def _describe_all_instances():
response = _describe_virtual_machines_request()
instances = _get_instances_from_response(response)
instances = get_items_from_response(
response, 'virtualmachine', cloudstack_instance_attributes_to_aws)

return instances

@@ -67,30 +69,9 @@ def _describe_specific_instances():
instances = []

for instance_id in instance_ids:
response = _describe_virtual_machine_by_id(instance_id)
instances = instances + _get_instances_from_response(response)

return instances


def _describe_virtual_machine_by_id(instance_id):
args = {
'id': instance_id
}

return _describe_virtual_machines_request(args)


def _get_instances_from_response(response):
instances = []
if response:
for virtual_machine in response['virtualmachine']:
instances.append(
translator.cloudstack_item_to_aws(
virtual_machine,
cloudstack_instance_attributes_to_aws
)
)
response = describe_item_by_id(instance_id, _describe_virtual_machines_request)
instances = instances + get_items_from_response(
response, 'virtualmachine', cloudstack_instance_attributes_to_aws)

return instances

0 comments on commit 35fdc50

Please sign in to comment.