Ejemplo n.º 1
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)
Ejemplo n.º 2
0
    def test_is_import_attempt_valid(self):
        """Tests is_import_attempt_valid."""
        attempt = {
            'attempt_id': 'attempt',
            'status': 'created',
            'time_created': '2020-07-28T20:22:18.311294+00:00',
            'time_completed': '2020-07-28T20:22:18.311294+00:00'
        }
        valid, _, _ = validation.is_import_attempt_valid(attempt, 'attempt')
        self.assertTrue(valid)

        # Not UTC
        attempt = {
            'attempt_id': 'attempt',
            'status': 'created',
            'time_created': '2020-07-28T13:21:50.665294-07:00',
            'time_completed': '2020-07-28T20:22:18.311294+00:00'
        }
        valid, _, _ = validation.is_import_attempt_valid(attempt, 'attempt')
        self.assertFalse(valid)

        # status not defined
        attempt = {
            'attempt_id': 'attempt',
            'status': 'not-defined',
            'time_created': '2020-07-28T20:22:18.311294+00:00',
            'time_completed': '2020-07-28T20:22:18.311294+00:00'
        }
        valid, _, _ = validation.is_import_attempt_valid(attempt, 'attempt')
        self.assertFalse(valid)

        # attempt_id not match
        attempt = {
            'attempt_id': 'attempt',
            'status': 'created',
            'time_created': '2020-07-28T20:22:18.311294+00:00',
            'time_completed': '2020-07-28T20:22:18.311294+00:00'
        }
        valid, _, _ = validation.is_import_attempt_valid(attempt, 'not-match')
        self.assertFalse(valid)
Ejemplo n.º 3
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