Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: ensure that partition ids are populated for az iot hub monitor-events #709

Merged
merged 6 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ Release History
0.24.1
+++++++++++++++

** IoT Hub updates **

* Fix for `az iot hub monitor-events` when the IoT Hub has no partition Id's populated.

**DPS updates**

* Fix for `az iot dps enrollement-group registration list` to support paging.
Expand Down
43 changes: 24 additions & 19 deletions azext_iot/monitor/builders/hub_target_builder.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import asyncio
import uamqp

from azure.cli.core.azclierror import CLIInternalError
from azext_iot.common.sas_token_auth import SasTokenAuthentication
from azext_iot.common.utility import parse_entity, unicode_binary_map, url_encode_str
from azext_iot.monitor.builders._common import query_meta_data
Expand Down Expand Up @@ -72,27 +73,31 @@ async def _evaluate_redirect(self, endpoint):
await receive_client.close_async()

async def _build_iot_hub_target_async(self, target):
if "events" not in target:
endpoint = AmqpBuilder.build_iothub_amqp_endpoint_from_target(target)
_, update = await self._evaluate_redirect(endpoint)
target["events"] = update["events"]
endpoint = target["events"]["endpoint"]
path = target["events"]["path"]
auth = self._build_auth_container(target)
meta_data = await query_meta_data(
address=target["events"]["address"],
path=target["events"]["path"],
auth=auth,
endpoint = AmqpBuilder.build_iothub_amqp_endpoint_from_target(target)
_, update = await self._evaluate_redirect(endpoint)
target["events"] = update["events"]
endpoint = target["events"]["endpoint"]
path = target["events"]["path"]
auth = self._build_auth_container(target)
meta_data = await query_meta_data(
address=target["events"]["address"],
path=target["events"]["path"],
auth=auth,
)
partition_count = meta_data.get(b"partition_count")

# if partition count is None or 0, throw
if not partition_count or not int(partition_count):
raise CLIInternalError(
f"{target['entity'].split('.')[0]} has no partition count. Please contact a support "
"representative to fix your IoT Hub."
)
partition_count = meta_data[b"partition_count"]
partition_ids = []

partitions = [partition.decode("utf-8") for partition in meta_data.get(b"partition_ids", [])]
if not partitions:
for i in range(int(partition_count)):
partition_ids.append(str(i))
target["events"]["partition_ids"] = partition_ids
else:
endpoint = target["events"]["endpoint"]
path = target["events"]["path"]
partitions = target["events"]["partition_ids"]
partitions.append(str(i))
target["events"]["partition_ids"] = partitions
auth = self._build_auth_container(target)

return Target(hostname=endpoint, path=path, partitions=partitions, auth=auth)
2 changes: 1 addition & 1 deletion azext_iot/monitor/telemetry.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ async def _initiate_event_monitor(
target: Target, enqueued_time_utc, on_message_received, timeout=0
):
if not target.partitions:
logger.debug("No Event Hub partitions found to listen on.")
logger.warning("No Event Hub partitions found to listen on.")
return

coroutines = []
Expand Down