Пример #1
0
    def post(self):
        """Creates a new progress log with the fields provided in the
        request body.

        Log level can only be one of the levels defined by LogLevel.
        A progress log must be at least linked to a system run and can
        optionally be linked to an import attempt.

        Returns:
            The created progress log as a datastore Entity object with
            log_id set. Otherwise, (error message, error code), where
            the error message is a string and the error code is an int.
        """
        args = progress_log.ProgressLog.parser.parse_args()
        args.pop(_LOG.log_id, None)
        args.setdefault('time_logged', utils.utctime())

        valid, err, code = validation.is_progress_log_valid(args)
        if not valid:
            return err, code

        valid, err, code = validation.required_fields_present(
            (_LOG.run_id, _LOG.level, _LOG.message), args)
        if not valid:
            return err, code

        run_id = args.get('run_id')
        attempt_id = args.get('attempt_id')
        run = None
        attempt = None

        with self.client.transaction():
            if run_id:
                run = self.run_database.get(run_id)
                if not run:
                    return validation.get_not_found_error(_RUN.run_id, run_id)
            if attempt_id:
                attempt = self.attempt_database.get(attempt_id)
                if not attempt:
                    return validation.get_not_found_error(
                        _ATTEMPT.attempt_id, attempt_id)
            if run and attempt:
                if attempt_id not in run[_RUN.import_attempts]:
                    return ('The import attempt specified by the attempt_id '
                            f'{attempt_id} in the request body is not '
                            'executed by the system run specified by the '
                            f'run_id {run_id} in the request body',
                            http.HTTPStatus.CONFLICT)

            log = self.log_database.get(make_new=True)
            log.update(args)
            log = self.log_database.save(log, save_content=True)
            if run:
                self.run_database.save(add_log_to_entity(log.key.name, run))
            if attempt:
                self.attempt_database.save(
                    add_log_to_entity(log.key.name, attempt))
            return log
Пример #2
0
    def patch(self, attempt_id):
        """Modifies the value of a field of an existing import attempt.

        The attempt_id and run_id of an existing import attempt resource are
        forbidden to be patched.

        Args:
            attempt_id: ID of the import attempt as a string

        Returns:
            The import attempt with the attempt_id if successful as a
            datastore Entity object. Otherwise, (error message, error code),
            where the error message is a string and the error code is an int.
        """
        args = ImportAttempt.parser.parse_args()
        if (_MODEL.attempt_id in args or _MODEL.run_id in args or
                _MODEL.logs in args):
            return validation.get_patch_forbidden_error(
                (_MODEL.attempt_id, _MODEL.run_id, _MODEL.logs))
        valid, err, code = validation.is_import_attempt_valid(
            args, attempt_id=attempt_id)
        if not valid:
            return err, code

        with self.client.transaction():
            import_attempt = self.database.get(attempt_id)
            if not import_attempt:
                return validation.get_not_found_error(_MODEL.attempt_id,
                                                      attempt_id)
            import_attempt.update(args)
            return self.database.save(import_attempt)
Пример #3
0
    def patch(self, run_id):
        """Modifies the value of a field of an existing system run.

        The run_id and import_attempts of an existing system run are
        forbidden to be patched.

        Args:
            run_id: ID of the system run as a string

        Returns:
            The system run with the run_id if successful as a
            datastore Entity object. Otherwise, (error message, error code),
            where the error message is a string and the error code is an int.
        """
        args = SystemRunByID.parser.parse_args()
        if _MODEL.run_id in args or _MODEL.import_attempts in args:
            return validation.get_patch_forbidden_error(
                (_MODEL.run_id, _MODEL.import_attempts))
        valid, err, code = validation.is_system_run_valid(args, run_id=run_id)
        if not valid:
            return err, code

        with self.client.transaction():
            run = self.database.get(run_id)
            if not run:
                return validation.get_not_found_error(_MODEL.run_id, run_id)
            run.update(args)
            return self.database.save(run)
Пример #4
0
    def get(self, log_id):
        """Queries the progress logs by its log_id.

        Args:
            log_id: ID of the progress log as a string.

        Returns:
            A list of progress logs with messages loaded of the
            system run if successful. Otherwise, (error message, error code).
        """
        log = self.log_database.get(log_id, load_content=True)
        if not log:
            return validation.get_not_found_error(_LOG.log_id, log_id)
        return log
Пример #5
0
    def get(self, run_id):
        """Retrieves a system run by its run_id.

        Args:
            run_id: ID of the system run as a string

        Returns:
            The system run with the run_id if successful as a
            datastore Entity object. Otherwise, (error message, error code),
            where the error message is a string and the error code is an int.
        """
        run = self.database.get(run_id)
        if not run:
            return validation.get_not_found_error(_MODEL.run_id, run_id)
        return run
Пример #6
0
    def get(self, run_id):
        """Queries the progress logs of a system run.

        Args:
            run_id: ID of the system run as a string.

        Returns:
            A list of progress logs each as a datastore Entity object with
            messages loaded of the system run if successful. Otherwise,
            (error message, error code).
        """
        run = self.run_database.get(run_id)
        if not run:
            return validation.get_not_found_error(_RUN.run_id, run_id)
        log_ids = run.get(_RUN.logs, [])
        return self.log_database.load_logs(log_ids)
Пример #7
0
    def get(self, attempt_id):
        """Retrieves an import attempt by its attempt_id.

        Args:
            attempt_id: ID of the import attempt as a string

        Returns:
            The import attempt with the attempt_id if successful as a
            datastore Entity object. Otherwise, (error message, error code),
            where the error message is a string and the error code is an int.
        """
        import_attempt = self.database.get(attempt_id)
        if not import_attempt:
            return validation.get_not_found_error(_MODEL.attempt_id,
                                                  attempt_id)
        return import_attempt
Пример #8
0
    def get(self, attempt_id):
        """Queries the progress logs of an import attempt.

        Args:
            attempt_id: ID of the import attempt as a string.

        Returns:
           A list of progress logs each as a datastore Entity object with
            messages loaded of the import attempt if successful. Otherwise,
            (error message, error code).
        """
        attempt = self.attempt_database.get(attempt_id)
        if not attempt:
            return validation.get_not_found_error(_ATTEMPT.attempt_id,
                                                  attempt_id)
        log_ids = attempt.get(_ATTEMPT.logs, [])
        return self.log_database.load_logs(log_ids)
Пример #9
0
    def _get_helper(cls, database, id_field, entity_id):
        """Retrieves an entity specified by its entity_id from the database.

        Args:
            database: Instance of one of SystemRunDatabase,
                ImportAttemptDatabase, and ProgressLogDatabase.
            id_field: Name of the ID field of the entity as a string.
            entity_id: ID of the entity as a string.

        Returns:
            The entity with the entity_id if successful as a datastore Entity
            object. Otherwise, (error message, error code), where the error
            message is a string and the error code is an int.
        """
        entity = database.get(entity_id)
        if not entity:
            return validation.get_not_found_error(id_field, entity_id)
        return entity
Пример #10
0
    def post(self):
        """Creates a new import attempt with the fields provided in the
        request body.

        run_id must be present in the request body to link the import attempt
        to a system run.

        Returns:
            The created import attempt as a datastore Entity object with
            attempt_id set. Otherwise, (error message, error code), where
            the error message is a string and the error code is an int.
        """
        args = import_attempt.ImportAttempt.parser.parse_args()
        valid, err, code = validation.is_import_attempt_valid(args)
        if not valid:
            return err, code
        present, err, code = validation.required_fields_present(
            (_ATTEMPT.run_id,), args)
        if not present:
            return err, code

        # Only the API can modify these fields
        args.pop(_ATTEMPT.attempt_id, None)
        args.pop(_ATTEMPT.logs, None)
        import_attempt.set_import_attempt_default_values(args)

        with self.client.transaction():
            # The system run pointed to by this import attempt needs to
            # point back at the import attempt.
            run_id = args[_ATTEMPT.run_id]
            run = self.run_database.get(run_id)
            if not run:
                return validation.get_not_found_error(_ATTEMPT.run_id, run_id)

            attempt = self.database.get(make_new=True)
            attempt.update(args)
            self.database.save(attempt)

            attempts = run.setdefault(_RUN.import_attempts, [])
            attempts.append(attempt.key.name)
            self.run_database.save(run)

            return attempt
Пример #11
0
 def test_get_not_found_error(self):
     """Tests get_not_found_error."""
     err, code = validation.get_not_found_error('fielddd', 'idddd')
     self.assertEqual('fielddd idddd not found', err)
     self.assertEqual(404, code)