[Python] How to update an existing enrollment in Azure DPS?

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    I'm having a really hard time trying to update an existing enrollment in Azure DPS, ie. in the event of having lost the symmetric keys and generating new ones to be added to the existing enrollment.

    From what I see in the Azure examples, this should work. But it doesn't. I'm still getting error 409201: Enrollment already exists.

    Am I doing something wrong here? Or is there an issue with the Python IoT Hub SDK that causes updating an existing enrollment not to work?

    def _create_or_update_enrollment(
    device_id: str,
    external_id: str,
    iothub_hostname: str,
    device_provisioning_client: DeviceProvisioningClient,
    ):
    primary_key = _generate_symmetric_key()
    secondary_key = _generate_symmetric_key()
    device_provisioning_client = DeviceProvisioningClient.from_connection_string(
    DPS_CONNECTION_STRING
    )

    try:
    enrollment = device_provisioning_client.enrollment.get(device_id)
    except Exception as e:
    _logger.debug(f"Unable to get enrollment for {device_id}: {e}")
    enrollment = {}

    enrollment.update({
    "registrationId": device_id,
    "attestation": {
    "type": "symmetricKey",
    "symmetricKey": {"primaryKey": primary_key, "secondaryKey": secondary_key},
    },
    "iotHubs": [iothub_hostname],
    "deviceId": device_id,
    "initialTwin": {
    "properties": {"desired": {"key": "value"}},
    "tags": {"externalId": external_id, "endpoint": "messages"},
    },
    "allocationPolicy": "static",
    })

    new_enrollment = device_provisioning_client.enrollment.create_or_update(
    id=device_id, enrollment=enrollment
    )
    return new_enrollment

    Continue reading...

Compartilhe esta Página