Code cutdown in controllers

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

Branch: refs/heads/master
Commit: d705e338d49fb0a72ef038f7df0caff7385dbfba
Parents: 787b720
Author: BroganD1993 <darrenbro...@hotmail.com>
Authored: Tue Jun 17 23:24:17 2014 +0100
Committer: BroganD1993 <darrenbro...@hotmail.com>
Committed: Tue Jun 17 23:24:17 2014 +0100

----------------------------------------------------------------------
 gstack/controllers/instances.py | 19 +++++++------------
 gstack/controllers/networks.py  | 19 +++++++------------
 gstack/controllers/regions.py   | 24 ++++++++++++------------
 gstack/controllers/zones.py     | 14 ++++++++++++++
 4 files changed, 40 insertions(+), 36 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/d705e338/gstack/controllers/instances.py
----------------------------------------------------------------------
diff --git a/gstack/controllers/instances.py b/gstack/controllers/instances.py
index fc1ce8e..af53ab2 100755
--- a/gstack/controllers/instances.py
+++ b/gstack/controllers/instances.py
@@ -173,19 +173,14 @@ def listinstances(authorization, projectid, zone):
 
@app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances/<instance>',
 methods=['GET'])
 @authentication.required
 def getinstance(projectid, authorization, zone, instance):
-    response = _get_virtual_machine_by_name(
-        authorization=authorization,
-        instance=instance
-    )
+    args = {'command':'listVirtualMachines'}
+    kwargs = {'projectid':projectid, 'zone':zone}
+    items = controllers.describe_items(
+        authorization, args, 'virtualmachine',
+        _cloudstack_virtual_machine_to_gce, name=instance, **kwargs)
 
-    if response:
-        return helpers.create_response(
-            data=_cloudstack_virtual_machine_to_gce(
-                cloudstack_response=response,
-                projectid=projectid,
-                zone=zone
-            )
-        )
+    if items:
+        helpers.create_response(items)
     else:
         function_route = url_for(
             'getinstance',

http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/d705e338/gstack/controllers/networks.py
----------------------------------------------------------------------
diff --git a/gstack/controllers/networks.py b/gstack/controllers/networks.py
index 37ee672..c535d03 100644
--- a/gstack/controllers/networks.py
+++ b/gstack/controllers/networks.py
@@ -72,7 +72,7 @@ def _cloudstack_network_to_gce(cloudstack_response, 
selfLink=None):
     if selfLink:
         response['selfLink'] = urllib.unquote_plus(selfLink)
     else:
-        response['selfLink'] = urllib.unquote_plus(request.base_url)
+        response['selfLink'] = urllib.unquote_plus(request.base_url) + '/' + 
response['name']
 
     return response
 
@@ -93,20 +93,15 @@ def _create_populated_network_response(projectid, 
networks=None):
 @app.route('/compute/v1/projects/<projectid>/global/networks', methods=['GET'])
 @authentication.required
 def listnetworks(projectid, authorization):
-    securitygroup_list = _get_networks(
-        authorization=authorization
-    )
-
-    networks = []
-    if securitygroup_list['listsecuritygroupsresponse']:
-        for network in 
securitygroup_list['listsecuritygroupsresponse']['securitygroup']:
-            networks.append(_cloudstack_network_to_gce(
-                cloudstack_response=network,
-                selfLink=request.base_url + '/' + network['name']))
+    args = {'command':'listSecurityGroups'}
+    kwargs = {}
+    items = controllers.describe_items(
+        authorization, args, 'securitygroup',
+        _cloudstack_network_to_gce, **kwargs)
 
     populated_response = _create_populated_network_response(
         projectid,
-        networks
+        items
     )
     return helpers.create_response(data=populated_response)
 

http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/d705e338/gstack/controllers/regions.py
----------------------------------------------------------------------
diff --git a/gstack/controllers/regions.py b/gstack/controllers/regions.py
index 642e55c..88ad711 100755
--- a/gstack/controllers/regions.py
+++ b/gstack/controllers/regions.py
@@ -20,16 +20,18 @@
 
 from gstack import app
 from gstack import helpers
+from gstack import controllers
 from gstack import authentication
 from gstack.controllers import errors
 from flask import request, url_for
 
 
-def _cloudstack_region_to_gce(response_item):
+def _cloudstack_account_to_gce(cloudstack_response):
     response = {}
     response['kind'] = 'compute#region'
-    response['description'] = response_item['name']
-    response['id'] = response_item['id']
+    response['description'] = cloudstack_response['name']
+    response['name'] = cloudstack_response['name']
+    response['id'] = cloudstack_response['id']
     response['status'] = 'UP'
     return response
 
@@ -37,19 +39,17 @@ def _cloudstack_region_to_gce(response_item):
 @app.route('/compute/v1/projects/<projectid>/regions', methods=['GET'])
 @authentication.required
 def listregions(projectid, authorization):
-    cloudstack_response = _get_regions(authorization)
-
-    regions = []
-
-    if cloudstack_response['listregionsresponse']:
-        for region in cloudstack_response['listregionsresponse']['region']:
-            regions.append(_cloudstack_region_to_gce(region))
+    args = {'command':'listAccounts'}
+    kwargs = {}
+    items = controllers.describe_items(
+        authorization, args, 'account',
+        _cloudstack_account_to_gce, **kwargs)
 
     populated_response = {
         'kind': 'compute#regionList',
         'id': 'projects/' + projectid + '/regions',
         'selfLink': request.base_url,
-        'items': regions
+        'items': items
     }
     return helpers.create_response(data=populated_response)
 
@@ -64,7 +64,7 @@ def getregion(projectid, authorization, region):
 
     if region == 
cloudstack_response['listregionsresponse']['region'][0]['name']:
         return helpers.create_response(
-            data=_cloudstack_region_to_gce(
+            data=_cloudstack_account_to_gce(
                 cloudstack_response['listregionsresponse']['region'][0]
             )
         )

http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/d705e338/gstack/controllers/zones.py
----------------------------------------------------------------------
diff --git a/gstack/controllers/zones.py b/gstack/controllers/zones.py
index fa72cdb..831c048 100755
--- a/gstack/controllers/zones.py
+++ b/gstack/controllers/zones.py
@@ -25,6 +25,20 @@ from gstack.services import requester
 from gstack.controllers import errors
 
 
+def _get_zones(authorization, args=None):
+    command = 'listZones'
+    if not args:
+        args = {}
+    cloudstack_response = requester.make_request(
+        command,
+        args,
+        authorization.client_id,
+        authorization.client_secret
+    )
+
+    return cloudstack_response
+
+
 def get_zone_names(authorization):
     zone_list = _get_zones(authorization)
 

Reply via email to