Exemplo n.º 1
0
    async def _build_central_event_hub_target_async(self, cmd, app_id):
        from azext_iot.common._azure import get_iot_central_tokens

        tokens = get_iot_central_tokens(cmd, app_id)
        eventHubToken = tokens['eventhubSasToken']
        hostnameWithoutPrefix = eventHubToken['hostname'].split("/")[2]
        endpoint = hostnameWithoutPrefix
        path = eventHubToken["entityPath"]
        tokenExpiry = tokens['expiry']
        auth = self._build_auth_container_from_token(endpoint, path,
                                                     eventHubToken['sasToken'],
                                                     tokenExpiry)
        address = "amqps://{}/{}/$management".format(
            hostnameWithoutPrefix, eventHubToken["entityPath"])
        meta_data = await self._query_meta_data(address, path, auth)
        partition_count = meta_data[b'partition_count']
        partition_ids = []
        for i in range(int(partition_count)):
            partition_ids.append(str(i))
        partitions = partition_ids
        auth = self._build_auth_container_from_token(endpoint, path,
                                                     eventHubToken['sasToken'],
                                                     tokenExpiry)

        eventHubTarget = {
            'endpoint': endpoint,
            'path': path,
            'auth': auth,
            'partitions': partitions
        }

        return eventHubTarget
def device_twin_show(cmd,
                     device_id,
                     app_id,
                     central_dns_suffix="azureiotcentral.com"):
    from azext_iot.common._azure import get_iot_central_tokens

    tokens = get_iot_central_tokens(cmd, app_id, central_dns_suffix)
    exception = None

    # The device could be in any hub associated with the given app.
    # We must search through each IoT Hub until device is found.
    for token_group in tokens.values():
        sas_token = token_group["iothubTenantSasToken"]["sasToken"]
        endpoint = find_between(sas_token, "SharedAccessSignature sr=",
                                "&sig=")
        target = {"entity": endpoint}
        auth = BasicSasTokenAuthentication(sas_token=sas_token)
        service_sdk, errors = _bind_sdk(target, SdkType.service_sdk, auth=auth)
        try:
            return service_sdk.get_twin(device_id)
        except errors.CloudError as e:
            if exception is None:
                exception = CLIError(unpack_msrest_error(e))

    raise exception
Exemplo n.º 3
0
    def test_get_iot_central_tokens(self, fixture_requests_post,
                                    fixture_get_aad_token):
        from azext_iot.common._azure import get_iot_central_tokens

        # Test to ensure get_iot_central_tokens calls requests.post and tokens are returned
        assert get_iot_central_tokens(
            {}, 'app_id', 'api-uri').value() == 'fixture_requests_post value'
Exemplo n.º 4
0
    def get_device_twin(self, central_dns_suffix):
        from azext_iot.common._azure import get_iot_central_tokens

        tokens = get_iot_central_tokens(
            self._cmd, self._app_id, self._token, central_dns_suffix
        )

        exception = None

        # The device could be in any hub associated with the given app.
        # We must search through each IoT Hub until device is found.
        for token_group in tokens.values():
            sas_token = token_group["iothubTenantSasToken"]["sasToken"]
            endpoint = find_between(sas_token, "SharedAccessSignature sr=", "&sig=")
            target = {"entity": endpoint}
            auth = BasicSasTokenAuthentication(sas_token=sas_token)
            service_sdk = SdkResolver(target=target, auth_override=auth).get_sdk(
                SdkType.service_sdk
            )
            try:
                return service_sdk.devices.get_twin(
                    id=self._device_id, raw=True
                ).response.json()
            except CloudError as e:
                if exception is None:
                    exception = CLIError(unpack_msrest_error(e))

        raise CLIError("Could not get device twin")
Exemplo n.º 5
0
async def _build_central_event_hub_targets_async(cmd, app_id,
                                                 central_dns_suffix):
    all_tokens = get_iot_central_tokens(cmd, app_id, central_dns_suffix)
    targets = [
        await convert_token_to_target(token) for token in all_tokens.values()
    ]

    return targets
    def test_get_iot_central_tokens(self, fixture_requests_post, fixture_azure_profile):
        from azext_iot.common._azure import get_iot_central_tokens

        class Cmd:
            cli_ctx = ""

        # Test to ensure get_iot_central_tokens calls requests.post and tokens are returned
        assert (
            get_iot_central_tokens(Cmd(), "app_id", "", "api-uri").value()
            == "fixture_requests_post value"
        )