From faba278f832951654c390a57296854581ccae13c Mon Sep 17 00:00:00 2001 From: Lewis Chambers Date: Thu, 13 Jun 2024 09:19:37 +0100 Subject: [PATCH] Added basic iotcore mqtt test on cli --- .gitignore | 4 +-- src/iotswarm/scripts/cli.py | 59 +++++++++++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index c88052f..b75f259 100644 --- a/.gitignore +++ b/.gitignore @@ -8,7 +8,7 @@ dist/* config.cfg aws-auth *.log -*.certs +*.certs* !.github/* docs/_* -conf.sh \ No newline at end of file +*.sh \ No newline at end of file diff --git a/src/iotswarm/scripts/cli.py b/src/iotswarm/scripts/cli.py index 7d79274..2517545 100644 --- a/src/iotswarm/scripts/cli.py +++ b/src/iotswarm/scripts/cli.py @@ -41,6 +41,65 @@ def main(ctx: click.Context, log_config: Path): main.add_command(test) +@main.command() +@click.pass_context +@click.argument( + "client-id", + type=click.STRING, +) +@click.argument("topic", type=click.STRING) +@click.argument("message", type=click.STRING) +@click.option( + "--endpoint", + type=click.STRING, + required=True, + envvar="IOT_SWARM_MQTT_ENDPOINT", + help="Endpoint of the MQTT receiving host.", +) +@click.option( + "--cert-path", + type=click.Path(exists=True), + required=True, + envvar="IOT_SWARM_MQTT_CERT_PATH", + help="Path to public key certificate for the device. Must match key assigned to the `--client-id` in the cloud provider.", +) +@click.option( + "--key-path", + type=click.Path(exists=True), + required=True, + envvar="IOT_SWARM_MQTT_KEY_PATH", + help="Path to the private key that pairs with the `--cert-path`.", +) +@click.option( + "--ca-cert-path", + type=click.Path(exists=True), + required=True, + envvar="IOT_SWARM_MQTT_CA_CERT_PATH", + help="Path to the root Certificate Authority (CA) for the MQTT host.", +) +def test_mqtt( + ctx: click.Context, + message, + topic, + client_id: str, + endpoint: str, + cert_path: str, + key_path: str, + ca_cert_path: str, +): + """Tests that a basic message can be sent via mqtt.""" + + connection = IotCoreMQTTConnection( + endpoint=endpoint, + cert_path=cert_path, + key_path=key_path, + ca_cert_path=ca_cert_path, + client_id=client_id, + ) + + connection.send_message(message, topic) + + @main.group() @click.pass_context @click.option(