Skip to content
Permalink
Browse files
Imporve base controller code
  • Loading branch information
brogand93 committed Jun 17, 2014
1 parent 91cddf2 commit fbe09a54519a9f60f4868a33bdc68b2568deabf3
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 118 deletions.
@@ -32,6 +32,8 @@ def filter_by_name(data, name):
for item in data:
if item['name'] == name:
return item
elif 'displayname' in item and item['displayname'] == name:
return item

return None

@@ -66,7 +68,7 @@ def _get_item_with_name(authorization, name, args, type):
return None


def _get_requested_items(authorization, args, type, projectid, to_cloudstack, zone):
def _get_requested_items(authorization, args, type, to_cloudstack, zone, projectid):
name = None
filter = helpers.get_filter(request.args)

@@ -85,8 +87,7 @@ def _get_requested_items(authorization, args, type, projectid, to_cloudstack, zo
if cloudstack_item:
items.append(
to_cloudstack(
cloudstack_response=cloudstack_item,
projectid=projectid, zone=zone
cloudstack_response=cloudstack_item, zone=zone, projectid=projectid
)
)
else:
@@ -95,33 +96,28 @@ def _get_requested_items(authorization, args, type, projectid, to_cloudstack, zo
for cloudstack_item in cloudstack_items[type]:
items.append(
to_cloudstack(
cloudstack_response=cloudstack_item,
projectid=projectid, zone=zone,
)
cloudstack_response=cloudstack_item, zone=zone, projectid=projectid)
)

return items


def describe_items_aggregated(authorization, args, type, projectid, to_cloudstack):
def describe_items_aggregated(authorization, args, type, gce_type, projectid, to_cloudstack):
from gstack.controllers import zones
args['listAll'] = 'true'

items = {}

zone_list = zones.get_zone_names(authorization=authorization)

for zone in zone_list:
zone_items = _get_requested_items(authorization, args, type, projectid, to_cloudstack, zone)

zone_items = _get_requested_items(authorization, args, type, to_cloudstack, zone, projectid)

items['zone/' + zone] = {}
items['zone/' + zone]['instances'] = zone_items
items['zone/' + zone][gce_type] = zone_items

return items


def describe_items(authorization, args, type, projectid, zone, to_cloudstack):
items = _get_requested_items(authorization, args, type, projectid, to_cloudstack, zone)
def describe_items(authorization, args, type, zone, projectid, to_cloudstack):
items = _get_requested_items(authorization, args, type, to_cloudstack, zone, projectid)

return items
@@ -88,44 +88,10 @@ def _cloudstack_volume_to_gce(cloudstack_response, projectid, zone):
@app.route('/compute/v1/projects/<projectid>/aggregated/disks', methods=['GET'])
@authentication.required
def aggregatedlistdisks(projectid, authorization):
disk_list = _get_disks(authorization=authorization)
zone_list = zones.get_zone_names(authorization=authorization)

disk = None
filter = helpers.get_filter(request.args)

if 'name' in filter:
disk = filter['name']

items = {}

for zone in zone_list:
zone_disks = []
if disk:
disk = get_disk_by_name(
authorization=authorization,
disk=disk
)
if disk:
zone_disks.append(
_cloudstack_volume_to_gce(
cloudstack_response=disk,
projectid=projectid,
zone=zone,
)
)

elif disk_list['listvolumesresponse']:
for disk in disk_list['listvolumesresponse']['volume']:
zone_disks.append(
_cloudstack_volume_to_gce(
cloudstack_response=disk,
projectid=projectid,
zone=zone,
)
)
items['zone/' + zone] = {}
items['zone/' + zone]['disks'] = zone_disks
args = {'command':'listVolumes'}
items = controllers.describe_items_aggregated(
authorization, args, 'volume',
projectid, _cloudstack_volume_to_gce)

populated_response = {
'kind': 'compute#diskAggregatedList',
@@ -140,43 +106,10 @@ def aggregatedlistdisks(projectid, authorization):
@app.route('/compute/v1/projects/<projectid>/zones/<zone>/disks', methods=['GET'])
@authentication.required
def listdisks(projectid, authorization, zone):
disk = None
filter = helpers.get_filter(request.args)

if 'name' in filter:
disk = filter['name']

items = []

if disk:
disk_list = _get_disks(
authorization=authorization,
args={'keyword': disk}
)
if disk_list['listvolumesresponse']:
disk = controllers.filter_by_name(
data=disk_list['listvolumesresponse']['volume'],
name=disk
)
if disk:
items.append(
_cloudstack_volume_to_gce(
cloudstack_response=disk,
projectid=projectid,
zone=zone
)
)
else:
disk_list = _get_disks(authorization=authorization)
if disk_list['listvolumesresponse']:
for disk in disk_list['listvolumesresponse']['volume']:
items.append(
_cloudstack_volume_to_gce(
cloudstack_response=disk,
projectid=projectid,
zone=zone
)
)
args = {'command':'listVolumes'}
items = controllers.describe_items(
authorization, args, 'volume',
projectid, zone, _cloudstack_volume_to_gce)

populated_response = {
'kind': 'compute#imageList',
@@ -61,32 +61,10 @@ def _cloudstack_securitygroup_to_gce(response_item):
@app.route('/compute/v1/projects/<projectid>/global/firewalls', methods=['GET'])
@authentication.required
def listsecuritygroups(projectid, authorization):
command = 'listSecurityGroups'
args = {}
cloudstack_response = requester.make_request(
command,
args,
authorization.client_id,
authorization.client_secret
)

cloudstack_response = cloudstack_response

app.logger.debug(
'Processing request for aggregated list Firewalls\n'
'Project: ' + projectid + '\n' +
json.dumps(cloudstack_response, indent=4, separators=(',', ': '))
)

firewalls = []
if cloudstack_response['listsecuritygroupsresponse']:
res = cloudstack_response['listsecuritygroupsresponse']
for response_item in res['securitygroup']:
firewalls.append(response_item)

items = []
for fw in firewalls:
items.append(_cloudstack_securitygroup_to_gce(fw))
args = {'command':'listSecurityGroups'}
items = controllers.describe_items(
authorization, args, 'securitygroup',
projectid, zone, _cloudstack_securitygroup_to_gce)

populated_response = {
'kind': 'compute#firewallList',
@@ -145,7 +145,6 @@ def _cloudstack_virtual_machine_to_gce(cloudstack_response, zone, projectid):
))
response['zone'] = zone

print response
return response


@@ -172,7 +171,7 @@ def _get_virtual_machine_by_name(authorization, instance):
def aggregatedlistinstances(authorization, projectid):
args = {'command':'listVirtualMachines'}
items = controllers.describe_items_aggregated(
authorization, args, 'virtualmachine',
authorization, args, 'virtualmachine', 'instances',
projectid, _cloudstack_virtual_machine_to_gce)

populated_response = {
@@ -190,7 +189,7 @@ def listinstances(authorization, projectid, zone):
args = {'command':'listVirtualMachines'}
items = controllers.describe_items(
authorization, args, 'virtualmachine',
projectid, zone, _cloudstack_virtual_machine_to_gce)
zone, projectid, _cloudstack_virtual_machine_to_gce)

populated_response = {
'kind': 'compute#instance_list',

0 comments on commit fbe09a5

Please sign in to comment.