From 100bfd5f1281e88756c610ad53fd86793e58e1c5 Mon Sep 17 00:00:00 2001 From: Ayman Izzeldin Date: Fri, 6 Sep 2024 10:33:13 +0100 Subject: [PATCH] fix wrong operator used in dynamodb expressions use & instead of and --- streaming_status/data_sources/device_ledger.py | 6 +++--- streaming_status/data_sources/fleet_index.py | 9 +++++---- 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/streaming_status/data_sources/device_ledger.py b/streaming_status/data_sources/device_ledger.py index ed7e57c..d931923 100644 --- a/streaming_status/data_sources/device_ledger.py +++ b/streaming_status/data_sources/device_ledger.py @@ -99,7 +99,7 @@ def _build_scan_params( return params -def find_device(provider: str | None, organization: str | None, device_name: str): +def find_device(provider: str | None, organization: str | None, device_name: str) -> dict | None: key = {"serialNumber": device_name} device_info = dynamodb.Table(config.device_ledger_table_name).get_item(Key=key).get("Item", {}) device_provider: str = device_info.get("jwtGroup") # type: ignore @@ -167,7 +167,7 @@ def list_providers( condition = Attr("org").eq(organization) if name_like: name_like_condition = Attr("jwtGroup").begins_with(name_like) - condition = condition and name_like_condition if condition else name_like_condition + condition = condition & name_like_condition if condition else name_like_condition params: dict = {"IndexName": config.device_ledger_groups_index_name} if condition: @@ -201,7 +201,7 @@ def _list_organizations_for_provider( condition: ConditionBase = Key("jwtGroup").eq(provider) if name_like: name_like_condition = Key("org").begins_with(name_like) - condition = condition and name_like_condition if condition else name_like_condition + condition = condition & name_like_condition params: dict = {} if page: diff --git a/streaming_status/data_sources/fleet_index.py b/streaming_status/data_sources/fleet_index.py index 7085a7a..4cd3120 100644 --- a/streaming_status/data_sources/fleet_index.py +++ b/streaming_status/data_sources/fleet_index.py @@ -22,7 +22,7 @@ def list_devices( page: str | None = None, page_size: int | None = None, active_only: bool = True, -): +) -> tuple[str | None, list[dict]]: query = f"attributes.{ThingAttributeNames.REGISTRATION_WAY}:*" provider_quoted = provider.replace('"', '\\"') if provider else None @@ -51,10 +51,11 @@ def list_devices( logger.debug("search index query: %s", query) fleet_result = iot_client.search_index(queryString=query, **request_params) - return fleet_result.get("nextToken"), fleet_result.get("things") or [] + things: list[dict] = fleet_result.get("things") # type: ignore + return fleet_result.get("nextToken"), things or [] -def find_device(provider: str | None, organization: str | None, device_name: str): +def find_device(provider: str | None, organization: str | None, device_name: str) -> dict | None: if not device_name_regex.fullmatch(device_name): raise AppError.invalid_argument(f"name must match the regex: {device_name_regex.pattern}") if (provider is not None and '"' in provider) or (organization is not None and '"' in organization): @@ -70,7 +71,7 @@ def find_device(provider: str | None, organization: str | None, device_name: str if not result["things"]: return None - return result["things"][0] + return result["things"][0] # type: ignore def update_device_active_state(device_name: str, active: bool):