diff --git a/riocli/device/__init__.py b/riocli/device/__init__.py index b111eabb..12d63688 100644 --- a/riocli/device/__init__.py +++ b/riocli/device/__init__.py @@ -25,6 +25,7 @@ from riocli.device.list import list_devices from riocli.device.migrate import migrate_project from riocli.device.onboard import device_onboard +from riocli.device.report import report_device from riocli.device.tools import tools from riocli.device.topic import device_topics from riocli.device.vpn import toggle_vpn @@ -54,6 +55,7 @@ def device(): device.add_command(inspect_device) device.add_command(list_deployments) device.add_command(list_devices) +device.add_command(report_device) device.add_command(tools) device.add_command(toggle_vpn) device.add_command(migrate_project) diff --git a/riocli/device/report.py b/riocli/device/report.py new file mode 100644 index 00000000..e5ff56c6 --- /dev/null +++ b/riocli/device/report.py @@ -0,0 +1,48 @@ +# Copyright 2024 Rapyuta Robotics +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + + +# from datetime import datetime, timedelta + +import click +from click_help_colors import HelpColorsGroup + +from riocli.config import new_client +from riocli.constants import Colors +from riocli.utils import tabulate_data +from riocli.device.util import report_device_api_call, find_device_guid + + +@click.command( + 'report', + cls=HelpColorsGroup, + help_headers_color=Colors.YELLOW, + help_options_color=Colors.GREEN, +) +@click.argument('device-name', type=str) +def report_device(device_name: str) -> None: + """ + Report a device and get its debug logs in uploads section. + """ + try: + # client = new_client() + device_guid = find_device_guid(device_name) + # device = client.get_device(device_id=device_guid) + # tabulate_data(device, ["Device"]) + # response = report_device_api_call(device_guid=device_guid) + # click.echo(response, color=Colors.GREEN) + click.echo(f"Device {device_name} {device_guid} reported successfully.") + + except Exception as e: + click.secho(str(e), fg=Colors.RED) diff --git a/riocli/device/util.py b/riocli/device/util.py index 773194b4..ea0b13d9 100644 --- a/riocli/device/util.py +++ b/riocli/device/util.py @@ -74,6 +74,30 @@ def decorated(**kwargs: typing.Any): return decorated +def report_device_api_call(device_guid: str) -> typing.Dict: + config = Configuration() + coreapi_host = config.data.get( + 'core_api_host', 'https://gaapiserver.apps.okd4v2.prod.rapyuta.io' + ) + + url = '{}/api/device-manager/v0/error_handler/upload_debug_logs/{}'.format( + coreapi_host, device_guid + ) + + headers = config.get_auth_header() + response = ( + RestClient(url).method(HttpMethod.POST).headers(headers).execute(payload={}) + ) + + data = response.json() + + if not response.ok: + err_msg = data.get('error') + raise Exception(err_msg) + + return data + + def get_device_name(client: Client, guid: str) -> str: device = client.get_device(device_id=guid) return device.name