Skip to content

Commit

Permalink
fix(device): keep device inspect backwards compatible
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitrgadiya committed Jan 22, 2025
1 parent c4f4105 commit 67b5f5d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
22 changes: 16 additions & 6 deletions riocli/device/inspect.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@
# limitations under the License.
import click
from click_help_colors import HelpColorsCommand
from munch import Munch
from rapyuta_io.clients import Device

from riocli.config import new_client
from riocli.config import get_config_from_context
from riocli.constants import Colors
from riocli.device.util import name_to_guid
from riocli.utils import inspect_with_format
Expand All @@ -36,26 +37,35 @@
)
@click.argument("device-name", type=str)
@name_to_guid
def inspect_device(format_type: str, device_name: str, device_guid: str) -> None:
@click.pass_context
def inspect_device(ctx: click.Context, format_type: str, device_name: str, device_guid: str) -> None:
"""Print the details of a device.
You can specify the format of the output using the --format flag.
The default format is yaml. You can choose between json and yaml.
"""
try:
client = new_client()
config = get_config_from_context(ctx)
client = config.new_client()
v2_client = config.new_v2_client()

device = client.get_device(device_id=device_guid)
data = make_device_inspectable(device)
daemon = v2_client.get_device_daemons(device_guid=device_guid)

data = make_device_inspectable(device, daemon)
inspect_with_format(data, format_type)
except Exception as e:
click.secho(str(e), fg=Colors.RED)


def make_device_inspectable(device: Device) -> dict:
def make_device_inspectable(device: Device, daemon: Munch) -> dict:
data = {}
for key, val in device.items():
if key.startswith("_") or key in ["deviceId"]:
if key.startswith("_") or key in ["deviceId", "daemons_status"]:
continue
data[key] = val

if daemon is not None and daemon.get("status") is not None:
data["daemons_status"] = daemon.status

return data
15 changes: 15 additions & 0 deletions riocli/v2client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -1251,3 +1251,18 @@ def poll_disk(
retry_count, sleep_interval, status.status
)
)

# Devices
def get_device_daemons(self, device_guid: str) -> Munch:
url = "{}/v2/devices/daemons/{}/".format(self._host, device_guid)
headers = self._get_auth_header()
response = RestClient(url).method(HttpMethod.GET).headers(headers).execute()

handle_server_errors(response)

data = json.loads(response.text)
if not response.ok:
err_msg = data.get("error")
raise Exception("device daemons: {}".format(err_msg))

return munchify(data)

0 comments on commit 67b5f5d

Please sign in to comment.