Ejemplo n.º 1
0
def run_command(
    cmd,
    app_id: str,
    device_id: str,
    command_name: str,
    content: str,
    interface_id=None,
    token=None,
    central_dns_suffix=CENTRAL_ENDPOINT,
    api_version=ApiVersion.v1.value,
):
    if not isinstance(content, str):
        raise CLIError("content must be a string: {}".format(content))

    payload = utility.process_json_arg(content, argument_name="content")

    if api_version == ApiVersion.preview.value:
        provider = CentralDeviceProviderPreview(cmd=cmd,
                                                app_id=app_id,
                                                token=token)
    else:
        provider = CentralDeviceProviderV1(cmd=cmd, app_id=app_id, token=token)

    return provider.run_command(
        device_id=device_id,
        interface_id=interface_id,
        command_name=command_name,
        payload=payload,
        central_dns_suffix=central_dns_suffix,
    )
Ejemplo n.º 2
0
def create_device(
    cmd,
    app_id: str,
    device_id: str,
    device_name=None,
    template=None,
    simulated=False,
    token=None,
    central_dns_suffix=CENTRAL_ENDPOINT,
    api_version=ApiVersion.v1.value,
):
    if simulated and not template:
        raise CLIError(
            "Error: if you supply --simulated you must also specify --template"
        )

    if api_version == ApiVersion.preview.value:
        provider = CentralDeviceProviderPreview(cmd=cmd,
                                                app_id=app_id,
                                                token=token)
    else:
        provider = CentralDeviceProviderV1(cmd=cmd, app_id=app_id, token=token)

    return provider.create_device(
        device_id=device_id,
        device_name=device_name,
        template=template,
        simulated=simulated,
        central_dns_suffix=central_dns_suffix,
    )
Ejemplo n.º 3
0
 def __init__(
     self,
     cmd: AzCliCommand,
     app_id: str,
     token: str,
     consumer_group: str,
     central_handler_args: CentralHandlerArguments,
     central_dns_suffix: str,
 ):
     central_device_provider = CentralDeviceProviderV1(cmd=cmd,
                                                       app_id=app_id,
                                                       token=token)
     central_template_provider = CentralDeviceTemplateProviderV1(
         cmd=cmd, app_id=app_id, token=token)
     self._targets = self._build_targets(
         cmd=cmd,
         app_id=app_id,
         token=token,
         consumer_group=consumer_group,
         central_dns_suffix=central_dns_suffix,
     )
     self._handler = self._build_handler(
         central_device_provider=central_device_provider,
         central_template_provider=central_template_provider,
         central_handler_args=central_handler_args,
     )
Ejemplo n.º 4
0
def run_manual_failback(
    cmd,
    app_id: str,
    device_id: str,
    token=None,
    central_dns_suffix=CENTRAL_ENDPOINT,
):
    provider = CentralDeviceProviderV1(cmd=cmd, app_id=app_id, token=token)
    return provider.run_manual_failback(device_id=device_id,
                                        central_dns_suffix=central_dns_suffix)
Ejemplo n.º 5
0
def registration_summary(
    cmd,
    app_id: str,
    token=None,
    central_dns_suffix=CENTRAL_ENDPOINT,
):
    provider = CentralDeviceProviderV1(
        cmd=cmd,
        app_id=app_id,
        token=token,
    )
    return provider.get_device_registration_summary(
        central_dns_suffix=central_dns_suffix, )
Ejemplo n.º 6
0
def registration_info(
    cmd,
    app_id: str,
    device_id,
    token=None,
    central_dns_suffix=CENTRAL_ENDPOINT,
):
    provider = CentralDeviceProviderV1(cmd=cmd, app_id=app_id, token=token)

    return provider.get_device_registration_info(
        device_id=device_id,
        central_dns_suffix=central_dns_suffix,
        device_status=None,
    )
Ejemplo n.º 7
0
def list_devices(
    cmd,
    app_id: str,
    token=None,
    central_dns_suffix=CENTRAL_ENDPOINT,
    api_version=ApiVersion.v1.value,
):
    if api_version == ApiVersion.preview.value:
        provider = CentralDeviceProviderPreview(cmd=cmd,
                                                app_id=app_id,
                                                token=token)
    else:
        provider = CentralDeviceProviderV1(cmd=cmd, app_id=app_id, token=token)

    return provider.list_devices(central_dns_suffix=central_dns_suffix)
Ejemplo n.º 8
0
def get_credentials(
    cmd,
    app_id: str,
    device_id,
    token=None,
    central_dns_suffix=CENTRAL_ENDPOINT,
):
    provider = CentralDeviceProviderV1(
        cmd=cmd,
        app_id=app_id,
        token=token,
    )
    return provider.get_device_credentials(
        device_id=device_id,
        central_dns_suffix=central_dns_suffix,
    )
Ejemplo n.º 9
0
def run_manual_failover(
    cmd,
    app_id: str,
    device_id: str,
    ttl_minutes=None,
    token=None,
    central_dns_suffix=CENTRAL_ENDPOINT,
):
    if ttl_minutes and ttl_minutes < 1:
        raise CLIError(
            "TTL value should be a positive integer: {}".format(ttl_minutes))

    provider = CentralDeviceProviderV1(cmd=cmd, app_id=app_id, token=token)
    return provider.run_manual_failover(
        device_id=device_id,
        ttl_minutes=ttl_minutes,
        central_dns_suffix=central_dns_suffix,
    )
Ejemplo n.º 10
0
    def test_should_return_device(self, mock_device_svc,
                                  mock_device_template_svc):
        # setup
        provider = CentralDeviceProviderV1(cmd=None, app_id=app_id)
        mock_device_svc.get_device.return_value = self._device
        mock_device_template_svc.get_device_template.return_value = (
            self._device_template)

        # act
        device = provider.get_device("someDeviceId")
        # check that caching is working
        device = provider.get_device("someDeviceId")

        # verify
        # call counts should be at most 1 since the provider has a cache
        assert mock_device_svc.get_device.call_count == 1
        assert mock_device_svc.get_device_template.call_count == 0
        assert device == self._device
Ejemplo n.º 11
0
 def _create_parser(
     self,
     device_template: central_models.TemplateV1,
     message: Message,
     args: CommonParserArguments,
 ):
     device_provider = CentralDeviceProviderV1(cmd=None, app_id=None)
     template_provider = CentralDeviceTemplateProviderV1(cmd=None,
                                                         app_id=None)
     device_provider.get_device = mock.MagicMock(
         return_value=central_models.DeviceV1({}))
     template_provider.get_device_template = mock.MagicMock(
         return_value=device_template)
     return central_parser.CentralParser(
         message=message,
         central_device_provider=device_provider,
         central_template_provider=template_provider,
         common_parser_args=args,
     )
Ejemplo n.º 12
0
def get_command_history(
    cmd,
    app_id: str,
    device_id: str,
    command_name: str,
    token=None,
    interface_id=None,
    central_dns_suffix=CENTRAL_ENDPOINT,
    api_version=ApiVersion.v1.value,
):
    if api_version == ApiVersion.preview.value:
        provider = CentralDeviceProviderPreview(cmd=cmd,
                                                app_id=app_id,
                                                token=token)
    else:
        provider = CentralDeviceProviderV1(cmd=cmd, app_id=app_id, token=token)

    return provider.get_command_history(
        device_id=device_id,
        interface_id=interface_id,
        command_name=command_name,
        central_dns_suffix=central_dns_suffix,
    )
Ejemplo n.º 13
0
 def __init__(
     self,
     cmd,
     app_id: str,
     device_id: str,
     token: str,
     central_dns_suffix=CENTRAL_ENDPOINT,
 ):
     self._cmd = cmd
     self._app_id = app_id
     self._device_id = device_id
     self._token = token
     self._central_dns_suffix = central_dns_suffix
     self._device_twin_provider = CentralDeviceTwinProvider(
         cmd=self._cmd,
         app_id=self._app_id,
         token=self._token,
         device_id=self._device_id,
     )
     self._central_device_provider = CentralDeviceProviderV1(
         cmd=self._cmd, app_id=self._app_id, token=self._token)
     self._central_template_provider = CentralDeviceTemplateProviderV1(
         cmd=self._cmd, app_id=self._app_id, token=self._token)
     self._template = self._get_device_template()