Continue refactor
Project: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/commit/0f104a2e Tree: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/tree/0f104a2e Diff: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/diff/0f104a2e Branch: refs/heads/master Commit: 0f104a2e95f727faa96e2edb5b12044c21409f3d Parents: 823aee6 Author: BroganD1993 <darrenbro...@hotmail.com> Authored: Wed Jun 18 22:11:33 2014 +0100 Committer: BroganD1993 <darrenbro...@hotmail.com> Committed: Wed Jun 18 22:11:33 2014 +0100 ---------------------------------------------------------------------- gstack/controllers/__init__.py | 10 ++-- gstack/controllers/images.py | 14 ++---- gstack/controllers/instances.py | 5 +- gstack/controllers/machine_type.py | 26 +++-------- gstack/controllers/networks.py | 34 ++++---------- gstack/controllers/project.py | 5 ++ gstack/controllers/regions.py | 21 +++------ gstack/controllers/zones.py | 2 +- tests/data/valid_get_account.json | 66 +++++++++++++++++++++++++++ tests/data/valid_get_security_group.json | 33 ++++++++++++++ tests/networks_tests.py | 8 ++-- tests/project_tests.py | 6 +-- 12 files changed, 146 insertions(+), 84 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/gstack/controllers/__init__.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/__init__.py b/gstack/controllers/__init__.py index 57400fb..0b31b42 100644 --- a/gstack/controllers/__init__.py +++ b/gstack/controllers/__init__.py @@ -52,7 +52,7 @@ def _get_items(authorization, args=None): return response -def _get_item_with_name(authorization, name, args, type): +def get_item_with_name(authorization, name, args, type): response = _get_items( authorization=authorization, args=args @@ -68,15 +68,15 @@ def _get_item_with_name(authorization, name, args, type): return None -def get_item_with_name_or_error(authorization, name, args, type, error, func_route, to_cloudstack, **kwargs): - cloudstack_item = _get_item_with_name(authorization, name, args, type) +def get_item_with_name_or_error(authorization, name, args, type, func_route, to_cloudstack, **kwargs): + cloudstack_item = get_item_with_name(authorization, name, args, type) if cloudstack_item: return helpers.create_response(to_cloudstack( cloudstack_response=cloudstack_item, **kwargs )) else: - return error(func_route) + return errors.resource_not_found(func_route) def _get_requested_items(authorization, args, type, to_cloudstack, **kwargs): name = None @@ -88,7 +88,7 @@ def _get_requested_items(authorization, args, type, to_cloudstack, **kwargs): items = [] if name: - cloudstack_item = _get_item_with_name( + cloudstack_item = get_item_with_name( authorization=authorization, name=name, args=args, http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/gstack/controllers/images.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/images.py b/gstack/controllers/images.py index be550a2..6f5d226 100755 --- a/gstack/controllers/images.py +++ b/gstack/controllers/images.py @@ -38,11 +38,10 @@ def _create_populated_image_response(projectid, images=None): return populated_response -def _cloudstack_template_to_gce(cloudstack_response, selfLink=None): +def _cloudstack_template_to_gce(cloudstack_response): translate_image_status = { 'True': 'Ready', - 'False': 'Failed' - } + 'False': 'Failed'} response = {} response['kind'] = 'compute#image' @@ -50,13 +49,8 @@ def _cloudstack_template_to_gce(cloudstack_response, selfLink=None): response['creationTimestamp'] = cloudstack_response['created'] response['name'] = cloudstack_response['name'] response['description'] = cloudstack_response['displaytext'] - response['status'] = translate_image_status[ - str(cloudstack_response['isready'])] - - if selfLink: - response['selfLink'] = urllib.unquote_plus(selfLink) - else: - response['selfLink'] = urllib.unquote_plus(request.base_url) + '/' + response['name'] + response['status'] = translate_image_status[str(cloudstack_response['isready'])] + response['selfLink'] = urllib.unquote_plus(request.base_url) + '/' + response['name'] return response http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/gstack/controllers/instances.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/instances.py b/gstack/controllers/instances.py index e33c094..b11eadb 100755 --- a/gstack/controllers/instances.py +++ b/gstack/controllers/instances.py @@ -175,8 +175,9 @@ def listinstances(authorization, projectid, zone): def getinstance(projectid, authorization, zone, instance): func_route = url_for('getinstance', projectid=projectid, zone=zone, instance=instance) args = {'command':'listVirtualMachines'} - return controllers.get_item_with_name_or_error(authorization, zone, args, 'zone', - errors.resource_not_found(func_route)) + return controllers.get_item_with_name_or_error( + authorization, instance, args, 'zone', func_route, + _cloudstack_zone_to_gce, **{'projectid':projectid}) @app.route('/compute/v1/projects/<projectid>/zones/<zone>/instances', methods=['POST']) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/gstack/controllers/machine_type.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/machine_type.py b/gstack/controllers/machine_type.py index 5b2c4ed..719d9a4 100755 --- a/gstack/controllers/machine_type.py +++ b/gstack/controllers/machine_type.py @@ -87,23 +87,9 @@ def listmachinetype(projectid, authorization, zone): @app.route('/compute/v1/projects/<projectid>/zones/<zone>/machineTypes/<machinetype>', methods=['GET']) @authentication.required def getmachinetype(projectid, authorization, zone, machinetype): - response = get_machinetype_by_name( - authorization=authorization, - machinetype=machinetype - ) - - if response: - return helpers.create_response( - data=_cloudstack_service_offering_to_gce( - cloudstack_response=response, - projectid=projectid, - zone=zone - ) - ) - else: - func_route = url_for( - 'getmachinetype', - projectid=projectid, - machinetype=machinetype, - zone=zone) - return errors.resource_not_found(func_route) + func_route = url_for('getmachinetype', projectid=projectid, zone=zone, machinetype=machinetype) + args = {'command':'listServiceOfferings'} + kwargs = {'projectid':projectid, 'zone':zone} + return controllers.get_item_with_name_or_error( + authorization, machinetype, args, 'serviceoffering', func_route, + _cloudstack_service_offering_to_gce, **kwargs) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/gstack/controllers/networks.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/networks.py b/gstack/controllers/networks.py index c535d03..e101fd6 100644 --- a/gstack/controllers/networks.py +++ b/gstack/controllers/networks.py @@ -26,7 +26,6 @@ from gstack import app, authentication from gstack.services import requester from gstack.controllers import errors - def _add_network(authorization, args=None): command = 'createSecurityGroup' if not args: @@ -43,8 +42,8 @@ def _add_network(authorization, args=None): def _delete_network(authorization, projectid, network): - network_response = get_network_by_name(authorization, network) - + args = {'command':'SecurityGroups'} + network_response = controllers.get_item_with_name(authorization, network, args, 'securitygroup') if not network_response: return None @@ -62,17 +61,13 @@ def _delete_network(authorization, projectid, network): ) -def _cloudstack_network_to_gce(cloudstack_response, selfLink=None): +def _cloudstack_network_to_gce(cloudstack_response): response = {} response['kind'] = 'compute#network' response['id'] = cloudstack_response['id'] response['name'] = cloudstack_response['name'] response['description'] = cloudstack_response['description'] - - if selfLink: - response['selfLink'] = urllib.unquote_plus(selfLink) - else: - response['selfLink'] = urllib.unquote_plus(request.base_url) + '/' + response['name'] + response['selfLink'] = urllib.unquote_plus(request.base_url) + '/' + response['name'] return response @@ -109,21 +104,12 @@ def listnetworks(projectid, authorization): @app.route('/compute/v1/projects/<projectid>/global/networks/<network>', methods=['GET']) @authentication.required def getnetwork(projectid, authorization, network): - response = get_network_by_name( - authorization=authorization, - securitygroup=network - ) - - if response: - return helpers.create_response( - data=_cloudstack_network_to_gce(response) - ) - else: - func_route = url_for( - 'getnetwork', - projectid=projectid, - network=network) - return errors.resource_not_found(func_route) + func_route = url_for('getnetwork', projectid=projectid, network=network) + args = {'command':'listSecurityGroups'} + kwargs = {} + return controllers.get_item_with_name_or_error( + authorization, network, args, 'securitygroup', func_route, + _cloudstack_network_to_gce, **kwargs) @app.route('/compute/v1/projects/<projectid>/global/networks', methods=['POST']) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/gstack/controllers/project.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/project.py b/gstack/controllers/project.py index 96e2eef..4381429 100755 --- a/gstack/controllers/project.py +++ b/gstack/controllers/project.py @@ -28,6 +28,10 @@ import json import urllib import collections +def _get_account_by_name(authorization, projectid): + args = {'command':'listAccounts'} + return controllers.get_item_with_name(authorization, projectid, args, 'account') + def _list_ssh_keys(authorization): command = 'listTags' @@ -129,6 +133,7 @@ def _cloudstack_project_to_gce(cloudstack_response, metadata=None): @authentication.required def getproject(authorization, projectid): project = _get_account_by_name(authorization, projectid) + print project if project: metadata = {} http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/gstack/controllers/regions.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/regions.py b/gstack/controllers/regions.py index 88ad711..6c15362 100755 --- a/gstack/controllers/regions.py +++ b/gstack/controllers/regions.py @@ -42,7 +42,7 @@ def listregions(projectid, authorization): args = {'command':'listAccounts'} kwargs = {} items = controllers.describe_items( - authorization, args, 'account', + authorization, args, 'region', _cloudstack_account_to_gce, **kwargs) populated_response = { @@ -57,17 +57,8 @@ def listregions(projectid, authorization): @app.route('/compute/v1/projects/<projectid>/regions/<region>', methods=['GET']) @authentication.required def getregion(projectid, authorization, region): - cloudstack_response = _get_regions( - authorization=authorization, - args={'name': region} - ) - - if region == cloudstack_response['listregionsresponse']['region'][0]['name']: - return helpers.create_response( - data=_cloudstack_account_to_gce( - cloudstack_response['listregionsresponse']['region'][0] - ) - ) - else: - function_route = url_for('getregion', projectid=projectid, region=region) - return errors.resource_not_found(function_route) + func_route = url_for('getregion', projectid=projectid, region=region) + args = {'command':'listAccounts'} + return controllers.get_item_with_name_or_error( + authorization, region, args, 'region', func_route, + _cloudstack_account_to_gce, **{}) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/gstack/controllers/zones.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/zones.py b/gstack/controllers/zones.py index 76f1733..e515777 100755 --- a/gstack/controllers/zones.py +++ b/gstack/controllers/zones.py @@ -92,6 +92,6 @@ def getzone(projectid, authorization, zone): func_route = url_for('getzone', projectid=projectid, zone=zone) args = {'command':'listZones'} return controllers.get_item_with_name_or_error( - authorization, zone, args, 'zone', errors.resource_not_found, func_route, + authorization, zone, args, 'zone', func_route, _cloudstack_zone_to_gce, **{}) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/tests/data/valid_get_account.json ---------------------------------------------------------------------- diff --git a/tests/data/valid_get_account.json b/tests/data/valid_get_account.json new file mode 100644 index 0000000..65f3fcf --- /dev/null +++ b/tests/data/valid_get_account.json @@ -0,0 +1,66 @@ +{ + "secondarystoragetotal": 0, + "primarystorageavailable": "Unlimited", + "domain": "brogan...@darrenbrogan.ie", + "domainid": "42f2b0d0-3953-485f-984d-b8d67185d358", + "vpclimit": "Unlimited", + "iplimit": "20", + "memorytotal": 0, + "secondarystorageavailable": "Unlimited", + "vmtotal": 0, + "cputotal": 0, + "vpctotal": 0, + "id": "ddbdf378-e8d9-47e0-964b-661d0d8414b8", + "networkavailable": "Unlimited", + "projectlimit": "Unlimited", + "networklimit": "Unlimited", + "iptotal": -27, + "volumetotal": 0, + "snapshotlimit": "0", + "state": "enabled", + "networktotal": 0, + "accounttype": 0, + "cpuavailable": "Unlimited", + "primarystoragetotal": 0, + "templatelimit": "0", + "snapshottotal": 0, + "templateavailable": "0", + "vmlimit": "20", + "vpcavailable": "0", + "primarystoragelimit": "Unlimited", + "volumelimit": "20", + "templatetotal": 0, + "secondarystoragelimit": "Unlimited", + "user": [ + { + "username": "accountname", + "account": "brogan...@darrenbrogan.ie", + "domainid": "42f2b0d0-3953-485f-984d-b8d67185d358", + "firstname": "brogand93", + "created": "2013-09-17T15:35:34+0200", + "lastname": "darrenbrogan.ie", + "iscallerchilddomain": false, + "domain": "brogan...@darrenbrogan.ie", + "email": "brogan...@darrenbrogan.ie", + "secretkey": "lM9fLm8XQwezvLOd10Qt3wXH7j9mRgaKbEg3nRDnj7FtlF3yx54EWd9mR5sB1ec5LQDV6gjpy6sfDo6ndUeeww", + "state": "enabled", + "apikey": "0ZyexOgzlfTx076LYBFz4oT_ShJvxUcezgvsW6gvRZ_BloSsb5uUTAm-FtHvKBvAUP0S7ZN6bIcRf4zEtbm3PQ", + "accounttype": 0, + "timezone": "CET", + "id": "26a772da-dc25-4f2b-b0f1-e095e3717a30", + "isdefault": false, + "accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8" + } + ], + "projectavailable": "Unlimited", + "isdefault": false, + "memoryavailable": "Unlimited", + "projecttotal": 0, + "volumeavailable": "20", + "name": "accountname", + "vmavailable": "20", + "ipavailable": "0", + "memorylimit": "Unlimited", + "cpulimit": "Unlimited", + "snapshotavailable": "0" +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/tests/data/valid_get_security_group.json ---------------------------------------------------------------------- diff --git a/tests/data/valid_get_security_group.json b/tests/data/valid_get_security_group.json new file mode 100644 index 0000000..14a7412 --- /dev/null +++ b/tests/data/valid_get_security_group.json @@ -0,0 +1,33 @@ +{ + "egressrule": [], + "account": "example-account", + "domainid": "66d69e46-a95b-437b-ac6c-bcaa5331999d", + "description": "Default Security Group", + "tags": [], + "domain": "example-account", + "ingressrule": [ + { + "protocol": "tcp", + "cidr": "0.0.0.0/0", + "startport": 22, + "endport": 22, + "ruleid": "3d92cc70-8c84-4e8a-9989-6efcd7ff7905" + }, + { + "protocol": "icmp", + "cidr": "0.0.0.0/0", + "ruleid": "2ba7dd7b-13b2-49ae-bf8e-26ffadd32c9e", + "icmpcode": 0, + "icmptype": 0 + }, + { + "protocol": "tcp", + "cidr": "0.0.0.0/0", + "startport": 8080, + "endport": 8080, + "ruleid": "c4562b3c-d1b0-4844-a771-3c3434e1a5d0" + } + ], + "id": "1f95ee9b-b291-48c1-9492-0eee632677e3", + "name": "networkname" +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/tests/networks_tests.py ---------------------------------------------------------------------- diff --git a/tests/networks_tests.py b/tests/networks_tests.py index edb8d33..452d3a8 100644 --- a/tests/networks_tests.py +++ b/tests/networks_tests.py @@ -100,10 +100,10 @@ class NetworksTestCase(GStackAppTestCase): get.return_value.status_code = 200 get_networks = mock.Mock() - get_networks.return_value = json.loads(read_file('tests/data/valid_describe_security_group.json')) + get_networks.return_value = None with mock.patch('requests.get', get): - with mock.patch('gstack.controllers.networks._get_networks', get_networks): + with mock.patch('gstack.controllers.get_item_with_name', get_networks): headers = { 'authorization': 'Bearer ' + str(GStackAppTestCase.access_token), } @@ -119,10 +119,10 @@ class NetworksTestCase(GStackAppTestCase): get.return_value.status_code = 200 get_networks = mock.Mock() - get_networks.return_value = json.loads(read_file('tests/data/valid_describe_security_group.json')) + get_networks.return_value = json.loads(read_file('tests/data/valid_get_security_group.json')) with mock.patch('requests.get', get): - with mock.patch('gstack.controllers.networks._get_networks', get_networks): + with mock.patch('gstack.controllers.get_item_with_name', get_networks): headers = { 'authorization': 'Bearer ' + str(GStackAppTestCase.access_token), } http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/0f104a2e/tests/project_tests.py ---------------------------------------------------------------------- diff --git a/tests/project_tests.py b/tests/project_tests.py index d3a58ee..9576ffd 100644 --- a/tests/project_tests.py +++ b/tests/project_tests.py @@ -11,14 +11,14 @@ class ProjectsTestCase(GStackAppTestCase): def test_get_project(self): get = mock.Mock() - get.return_value = json.loads(read_file('tests/data/valid_describe_account.json')) + get.return_value = json.loads(read_file('tests/data/valid_get_account.json')) get_tags = mock.Mock() get_tags.return_value.text = read_file('tests/data/valid_describe_tags.json') get_tags.return_value.status_code = 200 with mock.patch('requests.get', get_tags): - with(mock.patch('gstack.controllers.project._get_accounts', get)): + with(mock.patch('gstack.controllers.get_item_with_name', get)): headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)} response = self.get('/compute/v1/projects/accountname', headers=headers) @@ -33,7 +33,7 @@ class ProjectsTestCase(GStackAppTestCase): get_tags.return_value.status_code = 200 with mock.patch('requests.get', get_tags): - with(mock.patch('gstack.controllers.project._get_accounts', get)): + with(mock.patch('gstack.controllers._get_items', get)): headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)} response = self.get('/compute/v1/projects/invalidaccountname', headers=headers)