def test_check_dump_with_status(self): task = Task(status=Task.SYNCED) result = task.dump() result = result.replace('\n', '').replace('\r', '').replace(' ', '') match_obj = re.match(r'\{\"taskId\":\"(.*)\",\"status\":\"(.*)\"\}', result, re.M) assert(match_obj is not None), 'The json message: {} \n\n is not the expected...'.format(result)
def get_task(regionid, taskid, token=None): """ Get the status of the requested synchronization task. :param regionid: Region name how you can obtain from the Keystone service. Example: Spain2. :param taskid: The identity of the task. :param token: The token of the request to be authorized. :return: 200 - Ok if all is Ok ERROR - if the token is invalid, the region is incorrect or the task does not exist. """ # WARNING # It is for test only, we have to recover this information from the DB # in order to know the status of the synchronization of the task <taskid> message = "GET, obtain the status of the synchronization of a task: {} in the region: {}".format(taskid, regionid) logger_api.info(message) users = User.query.filter(User.task_id == taskid).all() if not users: msg = "The requested URL was not found on the server. If you entered the URL manually please" \ " check your spelling and try again." message = { "error": { "message": msg, "code": httplib.NOT_FOUND } } abort(httplib.NOT_FOUND, message) else: newtask = Task(taskid=users[0].task_id, status=users[0].status) resp = make_response(newtask.dump(), httplib.OK) resp.headers[SERVER_HEADER] = SERVER resp.headers[CONTENT_TYPE] = JSON_TYPE logger_api.info('Return result: %s', newtask.dump()) return resp
def test_check_dump_without_status(self): task = Task() result = task.dump() result = result.replace('\n', '').replace('\r', '').replace(' ', '') match_obj = re.match(r'\{\"taskId\":\"(.*)\"\}', result, re.M) assert ( match_obj is not None ), 'The json message: {} \n\n is not the expected...'.format(result)
def synchronize(regionid, token=None): """ Synchronize the images of the corresponding region defined by its regionId. The operation is asynchronous a response a taskId in order that you can follow the execution of the process. :param regionid: Region name how you can obtain from the Keystone service. Example: Spain2. :param token: The token of the request to be authorized. :return: JSON message with the identification of the created task. """ # WARNING # It is for testing only, the functionality of this method is create a new Task, # store the content of the new task with <taskid> in DB with status 'syncing' # and launch a fork to start the execution of the synchronization process. At the # end of the synchronization process, we update the DB registry with the status # 'synced' message = "POST, create a new synchronization task in the region: {}".format(regionid) logger_api.info(message) # Previously to each operation, we have to check if there is a task in the DB # with the status syncing associated to this region. users = User.query.filter(User.region == regionid).all() newtask = None if not users: newtask = Task(status=Task.SYNCING) # name and role should be returned from authorized operation, to be extended in Sprint 16.02 newuser = User(region=regionid, name=token.username, task_id=str(newtask.taskid), role='admin', status=newtask.status) db.session.add(newuser) try: db.session.commit() # Do the stuff to sync the images here... logger_api.debug("new thread") t = threading.Thread(target=run_in_thread, args=(regionid, newuser)) t.start() except Exception as e: message = ''' { "error": { "message": "Please check that you have initialized the DB. See the documentation about it.", "code": %s } } ''' % httplib.BAD_REQUEST abort(httplib.BAD_REQUEST, message) elif len(users) == 1 and users[0].region == regionid and users[0].status == Task.SYNCING: newtask = Task(taskid=users[0].task_id, status=users[0].status) if newtask is None: message = ''' { "error": { "message": "Already exist some task for this region", "code": %s } } ''' % httplib.BAD_REQUEST abort(httplib.BAD_REQUEST, message) resp = make_response(newtask.dump(), httplib.OK) resp.headers[SERVER_HEADER] = SERVER resp.headers[CONTENT_TYPE] = JSON_TYPE logger_api.info('Return result: %s', newtask.dump()) return resp