Return-Path: X-Original-To: apmail-cloudstack-dev-archive@www.apache.org Delivered-To: apmail-cloudstack-dev-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id DE8CF10B47 for ; Tue, 5 May 2015 11:19:17 +0000 (UTC) Received: (qmail 32285 invoked by uid 500); 5 May 2015 11:18:37 -0000 Delivered-To: apmail-cloudstack-dev-archive@cloudstack.apache.org Received: (qmail 32146 invoked by uid 500); 5 May 2015 11:18:37 -0000 Mailing-List: contact dev-help@cloudstack.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@cloudstack.apache.org Delivered-To: mailing list dev@cloudstack.apache.org Received: (qmail 30338 invoked by uid 99); 5 May 2015 11:18:36 -0000 Received: from git1-us-west.apache.org (HELO git1-us-west.apache.org) (140.211.11.23) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 05 May 2015 11:18:36 +0000 Received: by git1-us-west.apache.org (ASF Mail Server at git1-us-west.apache.org, from userid 33) id F2F21E0942; Tue, 5 May 2015 11:18:35 +0000 (UTC) Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: sebgoa@apache.org To: dev@cloudstack.apache.org Date: Tue, 05 May 2015 11:19:02 -0000 Message-Id: In-Reply-To: <84733594f2e94cf2a2556c37dc71cb4a@git.apache.org> References: <84733594f2e94cf2a2556c37dc71cb4a@git.apache.org> X-Mailer: ASF-Git Admin Mailer Subject: [28/50] [abbrv] cloudstack-gcestack git commit: Improve test coverage, clean operations Improve test coverage, clean operations Project: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/repo Commit: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/commit/08757591 Tree: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/tree/08757591 Diff: http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/diff/08757591 Branch: refs/heads/master Commit: 08757591255f8209fed8a00423fd93e31cbbcb7e Parents: d0cb4ff Author: BroganD1993 Authored: Mon Jun 23 21:31:03 2014 +0100 Committer: BroganD1993 Committed: Mon Jun 23 21:31:03 2014 +0100 ---------------------------------------------------------------------- gstack/__init__.py | 1 + gstack/configure.py | 6 +- gstack/controllers/instances.py | 12 +-- gstack/controllers/operations.py | 104 ++++++++++---------------- setup.py | 41 +++++----- tests/data/destroy_vm_async_pending.json | 14 ++++ tests/operations_tests.py | 20 +++++ tests/settings.py | 1 - 8 files changed, 103 insertions(+), 96 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/08757591/gstack/__init__.py ---------------------------------------------------------------------- diff --git a/gstack/__init__.py b/gstack/__init__.py index c4e0c6e..0624553 100644 --- a/gstack/__init__.py +++ b/gstack/__init__.py @@ -75,6 +75,7 @@ def _config_from_config_profile(config_file, profile): def configure_app(settings=None): app.config['DATA'] = os.path.abspath(os.path.dirname(__file__)) + '/data' + app.config['PATH'] = 'compute/v1/projects/' db.init_app(app) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/08757591/gstack/configure.py ---------------------------------------------------------------------- diff --git a/gstack/configure.py b/gstack/configure.py index d67cf73..242e901 100644 --- a/gstack/configure.py +++ b/gstack/configure.py @@ -19,16 +19,16 @@ import os import argparse -import ConfigParser from alembic import command +from ConfigParser import SafeConfigParser from alembic.config import Config as AlembicConfig def main(): config_folder = _create_config_folder() - _create_config_file(config_folder) _create_database() + _create_config_file(config_folder) def _create_config_folder(): @@ -66,7 +66,7 @@ def _generate_args(): def _modify_config_profile(config_file, profile): - config = ConfigParser.SafeConfigParser() + config = SafeConfigParser() config.read(config_file) if not config.has_section(profile): http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/08757591/gstack/controllers/instances.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/instances.py b/gstack/controllers/instances.py index 7ea1139..4694b14 100755 --- a/gstack/controllers/instances.py +++ b/gstack/controllers/instances.py @@ -184,13 +184,11 @@ def addinstance(authorization, projectid, zone): return errors.resource_not_found(func_route) else: - populated_response = operations.create_response( + return helpers.create_response(operations.create_async_response( projectid=projectid, operationid=deployment_result['deployvirtualmachineresponse']['jobid'], authorization=authorization - ) - - return helpers.create_response(data=populated_response) + )) @app.route('/compute/v1/projects//zones//instances/', methods=['DELETE']) @@ -209,10 +207,8 @@ def deleteinstance(projectid, authorization, zone, instance): authorization.client_secret ) - populated_response = operations.create_response( + return helpers.create_response(operations.create_async_response( projectid=projectid, operationid=deletion_result['destroyvirtualmachineresponse']['jobid'], authorization=authorization - ) - - return helpers.create_response(data=populated_response) + )) http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/08757591/gstack/controllers/operations.py ---------------------------------------------------------------------- diff --git a/gstack/controllers/operations.py b/gstack/controllers/operations.py index 048595d..361c277 100644 --- a/gstack/controllers/operations.py +++ b/gstack/controllers/operations.py @@ -38,34 +38,35 @@ def _get_async_result(authorization, args): return cloudstack_response -def _delete_instance_response(async_result, projectid): - populated_response = { - 'kind': 'compute#operation', - 'insertTime': async_result['created'], - 'operationType': 'delete', - 'name': async_result['jobid'], - 'startTime': async_result['created'], - 'selfLink': urllib.unquote_plus(helpers.get_root_url() + url_for( +def _get_instance_async_response(async_result, projectid, type): + response = {} + response['kind'] = 'compute#operation' + response['id'] = async_result['jobid'] + response['operationType'] = type + response['name'] = async_result['jobid'] + response['insertTime'] = async_result['created'] + response['startTime'] = async_result['created'] + response['selfLink'] = urllib.unquote_plus( + helpers.get_root_url() + url_for( 'getoperations', projectid=projectid, operationid=async_result['jobid'] )) - } if async_result['jobstatus'] is 0: - populated_response['targetLink'] = '' - populated_response['status'] = 'PENDING' - populated_response['progress'] = 0 + response['targetLink'] = '' + response['status'] = 'PENDING' + response['progress'] = 0 elif async_result['jobstatus'] is 1: - populated_response['status'] = 'DONE' - populated_response['zone'] = urllib.unquote_plus( + response['status'] = 'DONE' + response['zone'] = urllib.unquote_plus( helpers.get_root_url() + url_for( 'getzone', projectid=projectid, zone=async_result['jobresult']['virtualmachine']['zonename'], )) - populated_response['targetLink'] = urllib.unquote_plus( + response['targetLink'] = urllib.unquote_plus( helpers.get_root_url() + url_for( 'getinstance', @@ -73,6 +74,26 @@ def _delete_instance_response(async_result, projectid): zone=async_result['jobresult']['virtualmachine']['zonename'], instance=async_result['jobresult']['virtualmachine']['name'])) + return response + + +def _delete_instance_response(async_result, projectid): + populated_response = _get_instance_async_response(async_result, projectid, 'delete') + return populated_response + + +def _create_instance_response(async_result, projectid, authorization): + populated_response = _get_instance_async_response(async_result, projectid, 'insert') + populated_response['user'] = async_result['userid'] + + if async_result['jobstatus'] is 1: + _add_sshkey_metadata( + authorization=authorization, + publickey=publickey_storage[projectid], + instanceid=async_result['jobresult']['virtualmachine']['id'] + ) + print populated_response + return populated_response @@ -82,7 +103,6 @@ def _add_sshkey_metadata(authorization, publickey, instanceid): split_publickey = [l[i:i + n] for i in range(0, len(l), n)] i = 0 for datasegment in split_publickey: - print datasegment _add_sshkey_metadata_segment( authorization, str(i) + '-sshkey-segment', datasegment, instanceid) i = i + 1 @@ -105,55 +125,7 @@ def _add_sshkey_metadata_segment(authorization, keyname, value, instanceid): ) -def _create_instance_response(async_result, projectid, authorization): - populated_response = { - 'kind': 'compute#operation', - 'id': async_result['jobid'], - 'operationType': 'insert', - 'name': async_result['jobid'], - 'user': async_result['userid'], - 'insertTime': async_result['created'], - 'startTime': async_result['created'], - 'selfLink': urllib.unquote_plus(helpers.get_root_url() + url_for( - 'getoperations', - projectid=projectid, - operationid=async_result['jobid'] - )) - } - - if async_result['jobstatus'] is 0: - # handle pending case - populated_response['targetLink'] = '' - populated_response['status'] = 'PENDING' - populated_response['progress'] = 0 - elif async_result['jobstatus'] is 1: - # handle successful case - populated_response['status'] = 'DONE' - populated_response['id'] = async_result['jobid'] - populated_response['zone'] = urllib.unquote_plus( - helpers.get_root_url() + - url_for( - 'getzone', - projectid=projectid, - zone=async_result['jobresult']['virtualmachine']['zonename'], - )) - populated_response['targetLink'] = urllib.unquote_plus( - helpers.get_root_url() + - url_for( - 'getinstance', - projectid=projectid, - zone=async_result['jobresult']['virtualmachine']['zonename'], - instance=async_result['jobresult']['virtualmachine']['name'])) - _add_sshkey_metadata( - authorization=authorization, - publickey=publickey_storage[projectid], - instanceid=async_result['jobresult']['virtualmachine']['id'] - ) - - return populated_response - - -def create_response(authorization, projectid, operationid): +def create_async_response(authorization, projectid, operationid): async_result = _get_async_result( authorization=authorization, args={'jobId': operationid} @@ -184,7 +156,7 @@ def create_response(authorization, projectid, operationid): @app.route('/compute/v1/projects//global/operations/', methods=['GET']) @authentication.required def getoperations(authorization, operationid, projectid): - return helpers.create_response(create_response( + return helpers.create_response(create_async_response( authorization=authorization, operationid=operationid, projectid=projectid http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/08757591/setup.py ---------------------------------------------------------------------- diff --git a/setup.py b/setup.py index 13600cf..770e70d 100755 --- a/setup.py +++ b/setup.py @@ -36,27 +36,32 @@ def read(fname): return open(os.path.join(os.path.dirname(__file__), fname)).read().strip() setup( - name="gstack", + name='gstack', version=VERSION, - description="A GCE interface to Apache CloudStack", - author="Ian Duffy, Darren Brogan, Sebastien Goasguen", - author_email="ian@ianduffy.ie, brogand93@darrenbrogan.ie, runseb@gmail.com", - long_description="A Google Compute Engine compliant interface to the Apache CloudStack API", - url="https://github.com/NOPping/gstack", - platforms=("Any"), - license="LICENSE.txt", - package_data={'': ['LICENSE.txt', 'data/*'], - 'ec2stack': ['templates/*.json']}, + description='A GCE interface to Apache CloudStack', + author='Ian Duffy, Darren Brogan, Sebastien Goasguen', + author_email='ian@ianduffy.ie, brogand93@darrenbrogan.ie, runseb@gmail.com', + long_description='A Google Compute Engine compliant interface to the Apache CloudStack API', + url='https://github.com/NOPping/gstack', + platforms=('Any'), + license='LICENSE.txt', + package_data={'': ['LICENSE.txt', 'data/*', 'migrations/*']}, packages=[ - "gstack", "gstack.controllers", "gstack.models", - "gstack.services", "gstack.data", "pyoauth2"], + 'gstack', + 'gstack.controllers', + 'gstack.models', + 'gstack.services', + 'gstack.data', + 'pyoauth2', + 'migrations'], + include_package_data=True, install_requires=[ - "requests==0.14", - "pycrypto==2.6", - "pyopenssl", - "Flask-SQLAlchemy", - "flask", - "alembic" + 'requests==0.14', + 'pycrypto==2.6', + 'pyopenssl', + 'Flask-SQLAlchemy', + 'flask', + 'alembic' ], classifiers=[ 'Development Status :: 3 - Alpha', http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/08757591/tests/data/destroy_vm_async_pending.json ---------------------------------------------------------------------- diff --git a/tests/data/destroy_vm_async_pending.json b/tests/data/destroy_vm_async_pending.json new file mode 100644 index 0000000..65ec50c --- /dev/null +++ b/tests/data/destroy_vm_async_pending.json @@ -0,0 +1,14 @@ +{ + "queryasyncjobresultresponse": { + "jobprocstatus": 0, + "created": "2014-06-23T22:24:21+0200", + "cmd": "org.apache.cloudstack.api.command.user.vm.DestroyVMCmd", + "userid": "26a772da-dc25-4f2b-b0f1-e095e3717a30", + "jobstatus": 0, + "jobid": "678bbff2-3e45-4c6a-af5c-0859b8ab8170", + "jobresultcode": 0, + "jobinstanceid": "35de6b8f-8155-4843-bd84-dec137ff784e", + "jobinstancetype": "VirtualMachine", + "accountid": "ddbdf378-e8d9-47e0-964b-661d0d8414b8" + } +} http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/08757591/tests/operations_tests.py ---------------------------------------------------------------------- diff --git a/tests/operations_tests.py b/tests/operations_tests.py new file mode 100644 index 0000000..ac6ccb1 --- /dev/null +++ b/tests/operations_tests.py @@ -0,0 +1,20 @@ +#!/usr/bin/env python +# encoding: utf-8 + +import mock + +from gstack.helpers import read_file +from . import GStackAppTestCase + +class OperationsTestCase(GStackAppTestCase): + + def test_query_operation(self): + get = mock.Mock() + get.return_value.text = read_file('tests/data/destroy_vm_async_pending.json') + get.return_value.status_code = 200 + + with mock.patch('requests.get', get): + headers = {'authorization': 'Bearer ' + str(GStackAppTestCase.access_token)} + response = self.get('/compute/v1/projects/exampleproject/global/operations/exampleoperation', headers=headers) + + self.assert_ok(response) \ No newline at end of file http://git-wip-us.apache.org/repos/asf/cloudstack-gcestack/blob/08757591/tests/settings.py ---------------------------------------------------------------------- diff --git a/tests/settings.py b/tests/settings.py index 278c50d..e6f93ed 100644 --- a/tests/settings.py +++ b/tests/settings.py @@ -1,4 +1,3 @@ -PATH = 'compute/v1/projects/' GSTACK_BIND_ADDRESS = 'localhost' GSTACK_PORT = '5000' CLOUDSTACK_HOST = 'api.exoscale.ch'