Skip to content

Commit

Permalink
Enhance search_index() API with a "shadow" field
Browse files Browse the repository at this point in the history
  • Loading branch information
jsuchenia committed Nov 7, 2024
1 parent 357c9be commit 2a80f15
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 6 deletions.
33 changes: 27 additions & 6 deletions moto/iot/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ def to_dict(
include_connectivity: bool = False,
include_thing_id: bool = False,
include_thing_group_names: bool = False,
include_shadows_as_json: bool = False,
) -> Dict[str, Any]:
obj = {
"thingName": self.thing_name,
Expand All @@ -102,11 +103,27 @@ def to_dict(
obj["thingId"] = self.thing_id
if include_thing_group_names:
iot_backend = iot_backends[self.account_id][self.region_name]
res: List[str] = []
for thing_group in iot_backend.list_thing_groups(None, None, None):
if self.arn in thing_group.things:
res.append(thing_group.thing_group_name)
obj["thingGroupNames"] = res
obj["thingGroupNames"] = [
thing_group.thing_group_name
for thing_group in iot_backend.list_thing_groups(None, None, None)
if self.arn in thing_group.things
]
if include_shadows_as_json:
named_shadows = {
shadow_name: shadow.to_dict()
for shadow_name, shadow in self.thing_shadows.items()
if shadow_name is not None
}
converted_response_format = {
shadow_name: {
**shadow_data["state"],
"metadata": shadow_data["metadata"],
"version": shadow_data["version"],
"hasDelta": "delta" in shadow_data["state"],
}
for shadow_name, shadow_data in named_shadows.items()
}
obj["shadow"] = json.dumps({"name": converted_response_format})
return obj

@staticmethod
Expand Down Expand Up @@ -2308,7 +2325,11 @@ def search_index(self, query_string: str) -> List[Dict[str, Any]]:
thing for thing in self.things.values() if thing.matches(query_string)
]
return [
t.to_dict(include_connectivity=True, include_thing_group_names=True)
t.to_dict(
include_connectivity=True,
include_thing_group_names=True,
include_shadows_as_json=True,
)
for t in things
]

Expand Down
24 changes: 24 additions & 0 deletions tests/test_iot/test_iot_search.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import json

import boto3
import pytest

Expand Down Expand Up @@ -52,6 +54,28 @@ def test_search_things_include_group_names():
assert resp["things"][0]["thingGroupNames"] == ["TestGroup1", "AnotherGroup"]


@mock_aws
def test_search_things_include_named_shadow():
iot_client = boto3.client("iot", region_name="ap-northeast-1")
iotdata_client = boto3.client("iot-data", region_name="ap-northeast-1")
raw_payload = b'{"state": {"desired": {"led": "on"}, "reported": {"led": "off"}}}'

thing_name = "test-thing-name"
iot_client.create_thing(thingName=thing_name)
iotdata_client.update_thing_shadow(
thingName=thing_name, shadowName="test_shadow", payload=raw_payload
)

resp = iot_client.search_index(queryString=f"thingName:{thing_name}")

assert len(resp["things"]) == 1
shadow = json.loads(resp["things"][0]["shadow"])

assert shadow["name"]["test_shadow"]["desired"] == {"led": "on"}
assert shadow["name"]["test_shadow"]["reported"] == {"led": "off"}
assert shadow["name"]["test_shadow"]["hasDelta"]


@mock_aws
@pytest.mark.parametrize(
"query_string,results",
Expand Down

0 comments on commit 2a80f15

Please sign in to comment.