def regenerate_bundle(): """ Submit a request to regenerate an operator bundle image. :rtype: flask.Response :raise ValidationError: if required parameters are not supplied """ payload = flask.request.get_json() if not isinstance(payload, dict): raise ValidationError('The input data must be a JSON object') request = RequestRegenerateBundle.from_json(payload) db.session.add(request) db.session.commit() messaging.send_message_for_state_change(request, new_batch_msg=True) error_callback = failed_request_callback.s(request.id) handle_regenerate_bundle_request.apply_async( args=[ payload['from_bundle_image'], payload.get('organization'), request.id ], link_error=error_callback, queue=_get_user_queue(), ) flask.current_app.logger.debug('Successfully scheduled request %d', request.id) return flask.jsonify(request.to_json()), 201
def test_abort_when_downgrading_from_regenerate_bundle_request( app, auth_env, client, db): """Verify downgrade is prevented if "regenerate-bundle" requests exist.""" total_requests = 20 # flask_login.current_user is used in Request*.from_json which requires a request context with app.test_request_context(environ_base=auth_env): # Always add a RequestRegenerateBundle to ensure sufficient test data is available data = {'from_bundle_image': 'quay.io/namespace/bundle-image:latest'} request = RequestRegenerateBundle.from_json(data) db.session.add(request) # One request was already added, let's add the remaining ones for i in range(total_requests - 1): request_class = random.choice( (RequestAdd, RequestRm, RequestRegenerateBundle)) if request_class == RequestAdd: data = { 'binary_image': 'quay.io/namespace/binary_image:latest', 'bundles': [f'quay.io/namespace/bundle:{i}'], 'from_index': f'quay.io/namespace/repo:{i}', } request = RequestAdd.from_json(data) elif request_class == RequestRm: data = { 'binary_image': 'quay.io/namespace/binary_image:latest', 'operators': [f'operator-{i}'], 'from_index': f'quay.io/namespace/repo:{i}', } request = RequestRm.from_json(data) else: data = { 'from_bundle_image': 'quay.io/namespace/bundle-image:latest' } request = RequestRegenerateBundle.from_json(data) db.session.add(request) db.session.add(request) db.session.commit() # flask_migrate raises a SystemExit exception regardless of what's raised from the # downgrade function. This exception doesn't hold a reference to the RuntimeError # we expect from the downgrade function in the migration script. The best we can # do is catch the SystemExit exception. with pytest.raises(SystemExit): flask_migrate.downgrade(revision=INITIAL_DB_REVISION)
def regenerate_bundle_batch(): """ Submit a batch of requests to regenerate operator bundle images. :rtype: flask.Response :raise ValidationError: if required parameters are not supplied """ payload = flask.request.get_json() Batch.validate_batch_request_params(payload) batch = Batch(annotations=payload.get('annotations')) db.session.add(batch) requests = [] # Iterate through all the build requests and verify that the requests are valid before # committing them and scheduling the tasks for build_request in payload['build_requests']: try: request = RequestRegenerateBundle.from_json(build_request, batch) except ValidationError as e: # Rollback the transaction if any of the build requests are invalid db.session.rollback() raise ValidationError( f'{str(e).rstrip(".")}. This occurred on the build request in ' f'index {payload["build_requests"].index(build_request)}.') db.session.add(request) requests.append(request) db.session.commit() messaging.send_messages_for_new_batch_of_requests(requests) request_jsons = [] # This list will be used for the log message below and avoids the need of having to iterate # through the list of requests another time request_id_strs = [] for build_request, request in zip(payload['build_requests'], requests): request_jsons.append(request.to_json()) request_id_strs.append(str(request.id)) error_callback = failed_request_callback.s(request.id) handle_regenerate_bundle_request.apply_async( args=[ build_request['from_bundle_image'], build_request.get('organization'), request.id, ], link_error=error_callback, queue=_get_user_queue(), ) flask.current_app.logger.debug( 'Successfully scheduled the batch %d with requests: %s', batch.id, ', '.join(request_id_strs), ) return flask.jsonify(request_jsons), 201