Imporve base controller code

Project: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/repo
Commit: 
http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/commit/fbe09a54
Tree: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/tree/fbe09a54
Diff: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/diff/fbe09a54

Branch: refs/heads/master
Commit: fbe09a54519a9f60f4868a33bdc68b2568deabf3
Parents: 91cddf2
Author: BroganD1993 <darrenbro...@hotmail.com>
Authored: Tue Jun 17 20:54:57 2014 +0100
Committer: BroganD1993 <darrenbro...@hotmail.com>
Committed: Tue Jun 17 20:54:57 2014 +0100

----------------------------------------------------------------------
 gstack/controllers/__init__.py  | 24 +++++------
 gstack/controllers/disks.py     | 83 ++++--------------------------------
 gstack/controllers/firewalls.py | 30 ++-----------
 gstack/controllers/instances.py |  5 +--
 4 files changed, 24 insertions(+), 118 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/fbe09a54/gstack/controllers/__init__.py
----------------------------------------------------------------------
diff --git a/gstack/controllers/__init__.py b/gstack/controllers/__init__.py
index 51f1269..a6c7290 100644
--- a/gstack/controllers/__init__.py
+++ b/gstack/controllers/__init__.py
@@ -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
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/fbe09a54/gstack/controllers/disks.py
----------------------------------------------------------------------
diff --git a/gstack/controllers/disks.py b/gstack/controllers/disks.py
index 1898022..20fa073 100644
--- a/gstack/controllers/disks.py
+++ b/gstack/controllers/disks.py
@@ -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',

http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/fbe09a54/gstack/controllers/firewalls.py
----------------------------------------------------------------------
diff --git a/gstack/controllers/firewalls.py b/gstack/controllers/firewalls.py
index 34fb736..3286390 100755
--- a/gstack/controllers/firewalls.py
+++ b/gstack/controllers/firewalls.py
@@ -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',

http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/fbe09a54/gstack/controllers/instances.py
----------------------------------------------------------------------
diff --git a/gstack/controllers/instances.py b/gstack/controllers/instances.py
index 8ac2441..3caecc1 100755
--- a/gstack/controllers/instances.py
+++ b/gstack/controllers/instances.py
@@ -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',

Reply via email to