Пример #1
0
def _device_interface_elements(cmd, device_id, interface, target_type,
                               hub_name, resource_group_name, login):
    discovery = IotHubDiscovery(cmd)
    target = discovery.get_target(hub_name=hub_name,
                                  resource_group_name=resource_group_name,
                                  login=login)
    service_sdk, errors = _bind_sdk(target, SdkType.service_sdk)
    interface_elements = []
    try:
        payload = {"id": {}}
        payload["id"] = interface
        target_payload = shell_safe_json_parse(str(payload))
        interface_def = service_sdk.invoke_interface_command(
            device_id, INTERFACE_MODELDEFINITION, INTERFACE_COMMANDNAME,
            target_payload)
        if interface_def and interface_def.get("contents"):
            interface_contents = interface_def.get("contents")
            for content in interface_contents:
                if isinstance(content.get("@type"),
                              list) and target_type in content.get("@type"):
                    interface_elements.append(content)
                elif content.get("@type") == target_type:
                    interface_elements.append(content)
        return interface_elements
    except errors.CloudError as e:
        raise CLIError(unpack_msrest_error(e))
    except Exception:
        # returning an empty collection to continue
        return []
Пример #2
0
    def test_get_target_by_cstring(self, fixture_cmd, get_mgmt_client):
        discovery = IotHubDiscovery(cmd=fixture_cmd)

        fake_login = (
            "HostName=CoolIoTHub.azure-devices.net;SharedAccessKeyName=iothubowner;"
            "SharedAccessKey=AB+c/+5nm2XpDXcffhnGhnxz/TVF4m5ag7AuVIGwchj="
        )
        parsed_fake_login = parse_iot_hub_connection_string(fake_login)

        target = discovery.get_target(
            hub_name=None, resource_group_name=None, login=fake_login
        )

        # Ensure no ARM calls are made
        assert get_mgmt_client.call_count == 0

        assert target["cs"] == fake_login
        assert target["entity"] == parsed_fake_login["HostName"]
        assert target["policy"] == parsed_fake_login["SharedAccessKeyName"]
        assert target["primarykey"] == parsed_fake_login["SharedAccessKey"]

        target = discovery.get_target_by_cstring(fake_login)

        # Ensure no ARM calls are made
        assert get_mgmt_client.call_count == 0

        assert target["cs"] == fake_login
        assert target["entity"] == parsed_fake_login["HostName"]
        assert target["policy"] == parsed_fake_login["SharedAccessKeyName"]
        assert target["primarykey"] == parsed_fake_login["SharedAccessKey"]
Пример #3
0
def iot_digitaltwin_property_update(
    cmd,
    interface_payload,
    device_id,
    hub_name=None,
    resource_group_name=None,
    login=None,
):
    if exists(interface_payload):
        interface_payload = str(read_file_content(interface_payload))

    target_json = None
    try:
        target_json = shell_safe_json_parse(interface_payload)
    except ValueError:
        pass

    if target_json:
        interface_payload = target_json

    discovery = IotHubDiscovery(cmd)
    target = discovery.get_target(hub_name=hub_name,
                                  resource_group_name=resource_group_name,
                                  login=login)
    service_sdk, errors = _bind_sdk(target, SdkType.service_sdk)
    try:
        result = service_sdk.update_interfaces(device_id,
                                               interfaces=interface_payload)
        return result
    except errors.CloudError as e:
        raise CLIError(unpack_msrest_error(e))
    def test_iothub_targets(self):
        discovery = IotHubDiscovery(self.cmd_shell)

        cs_target1 = discovery.get_target_by_cstring(self.connection_string)
        assert_target(cs_target1, True)

        cs_target2 = discovery.get_target(hub_name=None,
                                          login=self.connection_string)
        assert_target(cs_target2, True)

        auto_target = discovery.get_target(hub_name=LIVE_HUB)
        assert_target(auto_target, rg=LIVE_RG)

        auto_target = discovery.get_target(hub_name=LIVE_HUB, rg=LIVE_RG)
        assert_target(auto_target, rg=LIVE_RG)

        desired_target = discovery.get_target(
            hub_name=LIVE_HUB,
            policy_name=self.desired_policy_name,
            include_events=True)
        assert_target(desired_target, rg=LIVE_RG, include_events=True)

        sub_targets = discovery.get_targets()
        [assert_target(tar) for tar in sub_targets]

        rg_targets = discovery.get_targets(rg=LIVE_RG, include_events=True)
        [
            assert_target(tar, rg=LIVE_RG, include_events=True)
            for tar in rg_targets
        ]

        assert len(rg_targets) <= len(sub_targets)
Пример #5
0
 def __init__(self, cmd, hub_name, rg, login=None):
     self.cmd = cmd
     self.hub_name = hub_name
     self.rg = rg
     self.discovery = IotHubDiscovery(cmd)
     self.target = self.discovery.get_target(
         hub_name=self.hub_name,
         rg=self.rg,
         login=login,
     )
     self.resolver = SdkResolver(self.target)
Пример #6
0
def iot_digitaltwin_invoke_command(
    cmd,
    interface,
    device_id,
    command_name,
    command_payload=None,
    timeout=10,
    hub_name=None,
    resource_group_name=None,
    login=None,
):
    device_interfaces = _iot_digitaltwin_interface_list(
        cmd, device_id, hub_name, resource_group_name, login)
    interface_list = _get_device_default_interface_dict(device_interfaces)

    target_interface = next(
        (item for item in interface_list if item["name"] == interface), None)

    if not target_interface:
        raise CLIError("Target interface is not implemented by the device!")

    if command_payload:
        if exists(command_payload):
            command_payload = str(read_file_content(command_payload))

        target_json = None
        try:
            target_json = shell_safe_json_parse(command_payload)
        except ValueError:
            pass

        if target_json or isinstance(target_json, bool):
            command_payload = target_json

    discovery = IotHubDiscovery(cmd)
    target = discovery.get_target(hub_name=hub_name,
                                  resource_group_name=resource_group_name,
                                  login=login)
    service_sdk, errors = _bind_sdk(target, SdkType.service_sdk)
    try:
        result = service_sdk.invoke_interface_command(
            device_id,
            interface,
            command_name,
            command_payload,
            connect_timeout_in_seconds=timeout,
            response_timeout_in_seconds=timeout,
        )
        return result
    except errors.CloudError as e:
        raise CLIError(unpack_msrest_error(e))
Пример #7
0
def _iot_digitaltwin_interface_list(cmd,
                                    device_id,
                                    hub_name=None,
                                    resource_group_name=None,
                                    login=None):
    discovery = IotHubDiscovery(cmd)
    target = discovery.get_target(hub_name=hub_name,
                                  resource_group_name=resource_group_name,
                                  login=login)
    service_sdk, errors = _bind_sdk(target, SdkType.service_sdk)
    try:
        device_interfaces = service_sdk.get_interfaces(device_id)
        return device_interfaces
    except errors.CloudError as e:
        raise CLIError(unpack_msrest_error(e))
def _ensure_dps_hub_link(self, dps, rg, hub):
    cli = embedded_cli.EmbeddedCLI()
    hubs = cli.invoke("iot dps linked-hub list --dps-name {} -g {}".format(
        dps, rg)).as_json()
    if not len(hubs) or not len(
            list(
                filter(
                    lambda linked_hub: linked_hub["name"] ==
                    "{}.azure-devices.net".format(hub),
                    hubs,
                ))):
        discovery = IotHubDiscovery(self.cmd_shell)
        target_hub = discovery.get_target(hub, rg)
        cli.invoke(
            "iot dps linked-hub create --dps-name {} -g {} --connection-string {} --location {}"
            .format(dps, rg, target_hub.get("cs"), target_hub.get("location")))
Пример #9
0
 def __init__(self,
              cmd,
              hub_name,
              rg,
              login=None,
              auth_type_dataplane=None):
     self.cmd = cmd
     self.hub_name = hub_name
     self.rg = rg
     self.discovery = IotHubDiscovery(cmd)
     self.target = self.discovery.get_target(
         hub_name=self.hub_name,
         resource_group_name=self.rg,
         login=login,
         auth_type=auth_type_dataplane,
     )
     self.resolver = SdkResolver(self.target)
    def test_iothub_discovery(self):
        discovery = IotHubDiscovery(self.cmd_shell)

        iothub = discovery.find_iothub(hub_name=LIVE_HUB)
        assert iothub.name == LIVE_HUB

        auto_policy = discovery.find_policy(hub_name=LIVE_HUB,
                                            rg=LIVE_RG).as_dict()
        rights_set = set(auto_policy["rights"].split(", "))
        assert rights_set == PRIVILEDGED_ACCESS_RIGHTS_SET

        # Assumption - Test Iothub includes the vanilla iothubowner policy
        desired_policy = discovery.find_policy(
            hub_name=LIVE_HUB,
            rg=LIVE_RG,
            policy_name=self.desired_policy_name).as_dict()
        assert desired_policy["key_name"] == self.desired_policy_name

        policies = discovery.get_policies(hub_name=LIVE_HUB, rg=LIVE_RG)
        assert len(policies)

        # Example for leveraging discovery to build cstring for every policy on target IotHub
        cstrings = [
            discovery._build_target(iothub=iothub, policy=p)["cs"]
            for p in policies
        ]
        assert len(cstrings)

        sub_hubs = discovery.get_iothubs()
        assert sub_hubs

        filtered_sub_hubs = [
            hub for hub in sub_hubs if hub.as_dict()["name"] == LIVE_HUB
        ]
        assert filtered_sub_hubs

        rg_hubs = discovery.get_iothubs(rg=LIVE_RG)
        assert rg_hubs

        filtered_rg_hubs = [
            hub for hub in rg_hubs if hub.as_dict()["name"] == LIVE_HUB
        ]
        assert filtered_rg_hubs

        assert len(rg_hubs) <= len(sub_hubs)
Пример #11
0
class IoTHubProvider(object):
    def __init__(self, cmd, hub_name, rg, login=None):
        self.cmd = cmd
        self.hub_name = hub_name
        self.rg = rg
        self.discovery = IotHubDiscovery(cmd)
        self.target = self.discovery.get_target(
            hub_name=self.hub_name,
            rg=self.rg,
            login=login,
        )
        self.resolver = SdkResolver(self.target)

    def get_sdk(self, sdk_type):
        return self.resolver.get_sdk(sdk_type)