Microsoft Azure IoT Hub Management Client Library for programmatic management of Azure IoT Hub resources through the Azure Resource Manager API
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
Device lifecycle management operations including bulk import/export of device identities, registry statistics, and device-related job management for large-scale IoT deployments. These operations support managing thousands to millions of devices efficiently through Azure Storage-based bulk operations.
Retrieve comprehensive statistics about the device registry including device counts by status for monitoring and capacity planning.
def get_stats(resource_group_name: str, resource_name: str, **kwargs) -> RegistryStatistics:
"""
Get statistics from an IoT hub device registry.
Args:
resource_group_name: Name of the resource group
resource_name: Name of the IoT hub resource
Returns:
RegistryStatistics: Device count statistics including total, enabled, and disabled devices
"""Large-scale device identity management through Azure Storage blob containers, supporting millions of device operations efficiently.
def export_devices(
resource_group_name: str,
resource_name: str,
export_devices_parameters: ExportDevicesRequest,
**kwargs
) -> JobResponse:
"""
Export all device identities to Azure Storage blob container.
Args:
resource_group_name: Name of the resource group
resource_name: Name of the IoT hub resource
export_devices_parameters: Export configuration including storage details
Returns:
JobResponse: Export job details for tracking progress
"""
def import_devices(
resource_group_name: str,
resource_name: str,
import_devices_parameters: ImportDevicesRequest,
**kwargs
) -> JobResponse:
"""
Import, update, or delete device identities from blob storage.
Args:
resource_group_name: Name of the resource group
resource_name: Name of the IoT hub resource
import_devices_parameters: Import configuration including storage details and import mode
Returns:
JobResponse: Import job details for tracking progress
"""Monitor and manage long-running device operations including import/export jobs and other device management tasks.
def list_jobs(resource_group_name: str, resource_name: str, **kwargs) -> ItemPaged[JobResponse]:
"""
Get list of all jobs in an IoT hub.
Args:
resource_group_name: Name of the resource group
resource_name: Name of the IoT hub resource
Returns:
ItemPaged[JobResponse]: Paginated list of all jobs with status and details
"""
def get_job(resource_group_name: str, resource_name: str, job_id: str, **kwargs) -> JobResponse:
"""
Get details of a specific job from IoT hub.
Args:
resource_group_name: Name of the resource group
resource_name: Name of the IoT hub resource
job_id: Unique identifier of the job
Returns:
JobResponse: Detailed job information including status, progress, and results
"""from azure.identity import DefaultAzureCredential
from azure.mgmt.iothub import IotHubClient
from azure.mgmt.iothub.models import ExportDevicesRequest
credential = DefaultAzureCredential()
client = IotHubClient(credential, "subscription-id")
# Configure export to Azure Storage
export_request = ExportDevicesRequest(
export_blob_container_uri="https://mystorageaccount.blob.core.windows.net/exports",
exclude_keys=False, # Include device keys in export
export_blob_name="device-export.json"
)
# Start export job
job = client.iot_hub_resource.export_devices(
"my-resource-group",
"my-iot-hub",
export_request
)
print(f"Export job started: {job.job_id}")
print(f"Job status: {job.status}")
print(f"Job type: {job.type}")from azure.mgmt.iothub.models import ImportDevicesRequest, JobType
# Configure import from Azure Storage
import_request = ImportDevicesRequest(
input_blob_container_uri="https://mystorageaccount.blob.core.windows.net/imports",
output_blob_container_uri="https://mystorageaccount.blob.core.windows.net/results",
input_blob_name="devices-to-import.json",
type=JobType.IMPORT
)
# Start import job
job = client.iot_hub_resource.import_devices(
"my-resource-group",
"my-iot-hub",
import_request
)
print(f"Import job started: {job.job_id}")# Get specific job details
job_details = client.iot_hub_resource.get_job(
"my-resource-group",
"my-iot-hub",
"job-id-from-previous-operation"
)
print(f"Job {job_details.job_id}:")
print(f" Status: {job_details.status}")
print(f" Type: {job_details.type}")
print(f" Start Time: {job_details.start_time_utc}")
print(f" End Time: {job_details.end_time_utc}")
print(f" Progress: {job_details.progress}%")
if job_details.failure_reason:
print(f" Failure Reason: {job_details.failure_reason}")
# List all jobs for monitoring
all_jobs = list(client.iot_hub_resource.list_jobs("my-resource-group", "my-iot-hub"))
running_jobs = [job for job in all_jobs if job.status == "Running"]
print(f"Currently running jobs: {len(running_jobs)}")# Get device statistics
stats = client.iot_hub_resource.get_stats("my-resource-group", "my-iot-hub")
print(f"Device Registry Statistics:")
print(f" Total devices: {stats.total_device_count}")
print(f" Enabled devices: {stats.enabled_device_count}")
print(f" Disabled devices: {stats.disabled_device_count}")
# Calculate utilization
if stats.total_device_count > 0:
enabled_percentage = (stats.enabled_device_count / stats.total_device_count) * 100
print(f" Enabled devices: {enabled_percentage:.1f}%")class RegistryStatistics:
"""
IoT Hub device registry statistics.
Attributes:
total_device_count: Total number of devices in registry (readonly)
enabled_device_count: Number of enabled devices (readonly)
disabled_device_count: Number of disabled devices (readonly)
"""
total_device_count: Optional[int]
enabled_device_count: Optional[int]
disabled_device_count: Optional[int]
class ExportDevicesRequest:
"""
Device export operation configuration.
Attributes:
export_blob_container_uri: Azure Storage container URI for export
exclude_keys: Whether to exclude device keys from export
export_blob_name: Name of the export blob file (optional)
authentication_type: Authentication method for storage access
identity: Managed identity for storage access (optional)
"""
export_blob_container_uri: str
exclude_keys: bool
export_blob_name: Optional[str]
authentication_type: Optional[AuthenticationType]
identity: Optional[ManagedIdentity]
class ImportDevicesRequest:
"""
Device import operation configuration.
Attributes:
input_blob_container_uri: Azure Storage container URI containing import data
output_blob_container_uri: Azure Storage container URI for operation results
input_blob_name: Name of the input blob file (optional)
output_blob_name: Name of the output blob file (optional)
type: Type of import operation (import, update, delete)
authentication_type: Authentication method for storage access
identity: Managed identity for storage access (optional)
"""
input_blob_container_uri: str
output_blob_container_uri: str
input_blob_name: Optional[str]
output_blob_name: Optional[str]
type: Optional[JobType]
authentication_type: Optional[AuthenticationType]
identity: Optional[ManagedIdentity]
class JobResponse:
"""
Job operation details and status.
Attributes:
job_id: Unique job identifier (readonly)
start_time_utc: Job start timestamp (readonly)
end_time_utc: Job completion timestamp (readonly)
type: Job type (import, export, etc.)
status: Current job status
progress: Job completion percentage (readonly)
input_blob_container_uri: Input storage container URI (readonly)
input_blob_uri: Input blob URI (readonly)
output_blob_container_uri: Output storage container URI (readonly)
output_blob_uri: Output blob URI (readonly)
exclude_keys_in_export: Whether keys were excluded in export (readonly)
failure_reason: Reason for job failure if applicable (readonly)
"""
job_id: Optional[str]
start_time_utc: Optional[datetime]
end_time_utc: Optional[datetime]
type: Optional[JobType]
status: Optional[JobStatus]
progress: Optional[int]
input_blob_container_uri: Optional[str]
input_blob_uri: Optional[str]
output_blob_container_uri: Optional[str]
output_blob_uri: Optional[str]
exclude_keys_in_export: Optional[bool]
failure_reason: Optional[str]Install with Tessl CLI
npx tessl i tessl/pypi-azure-mgmt-iothub