diff --git a/kci b/kci index 406f11cd70..286699e2a5 100755 --- a/kci +++ b/kci @@ -19,6 +19,7 @@ from kernelci.cli import ( # pylint: disable=unused-import api as kci_api, config as kci_config, docker as kci_docker, + event as kci_event, storage as kci_storage, user as kci_user, ) diff --git a/kernelci/cli/event.py b/kernelci/cli/event.py new file mode 100644 index 0000000000..820f4bde41 --- /dev/null +++ b/kernelci/cli/event.py @@ -0,0 +1,83 @@ +# SPDX-License-Identifier: LGPL-2.1-or-later +# +# Copyright (C) 2023 Collabora Limited +# Author: Guillaume Tucker +# Author: Jeny Sadadia + +"""Tool to interact with the Pub/Sub interface and message queues""" + +import sys +import json + +import click + +import kernelci.api +import kernelci.api.helper +import kernelci.config +from . import Args, kci + + +@kci.group(name='event') +def kci_event(): + """Interact with Pub/Sub and message queue events""" + + +@kci_event.command(secrets=True) +@click.argument('channel') +@Args.config +@Args.api +def subscribe(config, api, channel, secrets): + """Subscribe to a Pub/Sub channel""" + configs = kernelci.config.load(config) + api_config = configs['api'][api] + api = kernelci.api.get_api(api_config, secrets.api.token) + sub_id = api.subscribe(channel) + click.echo(sub_id) + + +@kci_event.command(secrets=True) +@click.argument('sub_id') +@Args.config +@Args.api +def unsubscribe(config, api, sub_id, secrets): + """Unsubscribe from a Pub/Sub channel""" + configs = kernelci.config.load(config) + api_config = configs['api'][api] + api = kernelci.api.get_api(api_config, secrets.api.token) + api.unsubscribe(sub_id) + + +@kci_event.command(secrets=True) +@click.option('--is-json', help="Parse input data as JSON", is_flag=True) +@Args.config +@Args.api +@click.argument('channel') +def send(config, api, is_json, channel, secrets): + """Read some data on stdin and send it as an event on a channel""" + configs = kernelci.config.load(config) + api_config = configs['api'][api] + api = kernelci.api.get_api(api_config, secrets.api.token) + data = sys.stdin.read() + if is_json: + data = json.loads(data) + api.send_event(channel, {'data': data}) + + +@kci_event.command(secrets=True) +@click.argument('sub_id') +@Args.config +@Args.api +@Args.indent +def receive(config, api, indent, sub_id, secrets): + """Wait and receive an event from a subscription and print on stdout""" + configs = kernelci.config.load(config) + api_config = configs['api'][api] + api = kernelci.api.get_api(api_config, secrets.api.token) + helper = kernelci.api.helper.APIHelper(api) + event = helper.receive_event_data(sub_id) + if isinstance(event, str): + click.echo(event.strip()) + elif isinstance(event, dict): + click.echo(json.dumps(event, indent=indent)) + else: + click.echo(event) diff --git a/kernelci/legacy/cli/__init__.py b/kernelci/legacy/cli/__init__.py index 514b059176..9b76859f5b 100644 --- a/kernelci/legacy/cli/__init__.py +++ b/kernelci/legacy/cli/__init__.py @@ -13,7 +13,6 @@ api, job, node, - pubsub, sched, show, storage, @@ -24,7 +23,6 @@ 'api': api.main, 'job': job.main, 'node': node.main, - 'pubsub': pubsub.main, 'sched': sched.main, 'show': show.main, 'storage': storage.main, diff --git a/kernelci/legacy/cli/pubsub.py b/kernelci/legacy/cli/pubsub.py deleted file mode 100644 index 6f469bcc75..0000000000 --- a/kernelci/legacy/cli/pubsub.py +++ /dev/null @@ -1,82 +0,0 @@ -# SPDX-License-Identifier: LGPL-2.1-or-later -# -# Copyright (C) 2023 Collabora Limited -# Author: Guillaume Tucker - -"""Tool to interact with the Pub/Sub interface""" - -import json -import sys - -import kernelci - -from .base import Args, sub_main -from .base_api import APICommand - - -class cmd_subscribe(APICommand): # pylint: disable=invalid-name - """Subscribe to a Pub/Sub channel""" - args = APICommand.args + [Args.api_token, Args.channel] - - def _api_call(self, api, configs, args): - sub_id = api.subscribe(args.channel) - print(sub_id) - return True - - -class cmd_unsubscribe(APICommand): # pylint: disable=invalid-name - """Unscribe from a Pub/Sub channel""" - args = APICommand.args + [Args.api_token, Args.sub_id] - - def _api_call(self, api, configs, args): - api.unsubscribe(args.sub_id) - return True - - -class cmd_send_event(APICommand): # pylint: disable=invalid-name - """Read some data on stdin and send it as an event on a channel""" - args = APICommand.args + [Args.api_token, Args.channel] - opt_args = [ - { - 'name': '--type', - 'help': "CloudEvent type, api.kernelci.org by default", - }, - { - 'name': '--source', - 'help': "CloudEvent source, https://api.kernelci.org/ by default", - }, - { - 'name': '--json', - 'action': 'store_true', - 'help': "Parse input data as JSON", - }, - ] - - def _api_call(self, api, configs, args): - data = sys.stdin.read() - if args.json: - data = json.loads(data) - api.send_event(args.channel, {'data': data}) - return True - - -class cmd_receive_event(APICommand): # pylint: disable=invalid-name - """Wait and receive an event from a subscription and print on stdout""" - args = APICommand.args + [Args.api_token, Args.sub_id] - opt_args = APICommand.opt_args + [Args.indent] - - def _api_call(self, api, configs, args): - helper = kernelci.api.helper.APIHelper(api) - event = helper.receive_event_data(args.sub_id) - if isinstance(event, str): - print(event.strip()) - elif isinstance(event, dict): - self._print_json(event, args.indent) - else: - print(event) - return True - - -def main(args=None): - """Entry point for the command line tool""" - sub_main("pubsub", globals(), args)