def get_mgmt_iothub_client(cmd, raise_if_error=False): try: return iot_hub_service_factory(cmd.cli_ctx) except CLIError as e: if raise_if_error: raise e return None
def _initialize_client(self): if not self.client: if getattr(self.cmd, "cli_ctx", None): self.client = iot_hub_service_factory(self.cmd.cli_ctx) else: self.client = self.cmd self.sub_id = self.client.config.subscription_id
def test_hub_monitor_events(self): for cg in LIVE_CONSUMER_GROUPS: self.cmd( "az iot hub consumer-group create --hub-name {} --resource-group {} --name {}" .format(LIVE_HUB, LIVE_RG, cg), checks=[self.check("name", cg)], ) from azext_iot.operations.hub import iot_device_send_message from azext_iot._factory import iot_hub_service_factory from azure.cli.core.mock import DummyCli cli_ctx = DummyCli() client = iot_hub_service_factory(cli_ctx) device_count = 10 device_ids = self.generate_device_names(device_count) # Test with invalid connection string self.cmd( "iot hub monitor-events -t 1 -y --login {}".format( self.connection_string + "zzz"), expect_failure=True, ) # Create and Simulate Devices for i in range(device_count): self.cmd( "iot hub device-identity create -d {} -n {} -g {}".format( device_ids[i], LIVE_HUB, LIVE_RG), checks=[self.check("deviceId", device_ids[i])], ) enqueued_time = calculate_millisec_since_unix_epoch_utc() for i in range(device_count): execute_onthread( method=iot_device_send_message, args=[ client, device_ids[i], LIVE_HUB, '{\r\n"payload_data1":"payload_value1"\r\n}', "$.mid=12345;key0=value0;key1=1", 1, LIVE_RG, None, 0, ], max_runs=1, ) # Monitor events for all devices and include sys, anno, app self.command_execute_assert( "iot hub monitor-events -n {} -g {} --cg {} --et {} -t 5 -y -p sys anno app" .format(LIVE_HUB, LIVE_RG, LIVE_CONSUMER_GROUPS[0], enqueued_time), device_ids + [ "system", "annotations", "application", '"message_id": "12345"', '"key0": "value0"', '"key1": "1"', ], ) # Monitor events for a single device self.command_execute_assert( "iot hub monitor-events -n {} -g {} -d {} --cg {} --et {} -t 5 -y -p all" .format(LIVE_HUB, LIVE_RG, device_ids[0], LIVE_CONSUMER_GROUPS[1], enqueued_time), [ device_ids[0], "system", "annotations", "application", '"message_id": "12345"', '"key0": "value0"', '"key1": "1"', ], ) # Monitor events with device-id wildcards self.command_execute_assert( "iot hub monitor-events -n {} -g {} -d {} --et {} -t 5 -y -p sys anno app" .format(LIVE_HUB, LIVE_RG, PREFIX_DEVICE + "*", enqueued_time), device_ids, ) # Monitor events for specific devices using query language device_subset_include = device_ids[:device_count // 2] device_include_string = ", ".join( ["'" + deviceId + "'" for deviceId in device_subset_include]) query_string = "select * from devices where deviceId in [{}]".format( device_include_string) self.command_execute_assert( 'iot hub monitor-events -n {} -g {} --device-query "{}" --et {} -t 5 -y -p sys anno app' .format(LIVE_HUB, LIVE_RG, query_string, enqueued_time), device_subset_include, ) # Expect failure for excluded devices device_subset_exclude = device_ids[device_count // 2:] with pytest.raises(Exception): self.command_execute_assert( 'iot hub monitor-events -n {} -g {} --device-query "{}" --et {} -t 5 -y -p sys anno app' .format(LIVE_HUB, LIVE_RG, query_string, enqueued_time), device_subset_exclude, ) # Monitor events with --login parameter self.command_execute_assert( "iot hub monitor-events -t 5 -y -p all --cg {} --et {} --login {}". format(LIVE_CONSUMER_GROUPS[2], enqueued_time, self.connection_string), device_ids, ) enqueued_time = calculate_millisec_since_unix_epoch_utc() # Send messages that have JSON payload, but do not pass $.ct property execute_onthread( method=iot_device_send_message, args=[ client, device_ids[i], LIVE_HUB, '{\r\n"payload_data1":"payload_value1"\r\n}', "", 1, LIVE_RG, None, 1, ], max_runs=1, ) # Monitor messages for ugly JSON output self.command_execute_assert( "iot hub monitor-events -n {} -g {} --cg {} --et {} -t 5 -y". format(LIVE_HUB, LIVE_RG, LIVE_CONSUMER_GROUPS[0], enqueued_time), ["\\r\\n"], ) # Monitor messages and parse payload as JSON with the --ct parameter self.command_execute_assert( "iot hub monitor-events -n {} -g {} --cg {} --et {} -t 5 --ct application/json -y" .format(LIVE_HUB, LIVE_RG, LIVE_CONSUMER_GROUPS[1], enqueued_time), ['"payload_data1": "payload_value1"'], ) enqueued_time = calculate_millisec_since_unix_epoch_utc() # Send messages that have JSON payload and have $.ct property execute_onthread( method=iot_device_send_message, args=[ client, device_ids[i], LIVE_HUB, '{\r\n"payload_data1":"payload_value1"\r\n}', "$.ct=application/json", 1, LIVE_RG, ], max_runs=1, ) # Monitor messages for pretty JSON output self.command_execute_assert( "iot hub monitor-events -n {} -g {} --cg {} --et {} -t 5 -y". format(LIVE_HUB, LIVE_RG, LIVE_CONSUMER_GROUPS[0], enqueued_time), ['"payload_data1": "payload_value1"'], ) # Monitor messages with yaml output self.command_execute_assert( "iot hub monitor-events -n {} -g {} --cg {} --et {} -t 5 -y -o yaml" .format(LIVE_HUB, LIVE_RG, LIVE_CONSUMER_GROUPS[1], enqueued_time), ["payload_data1: payload_value1"], ) enqueued_time = calculate_millisec_since_unix_epoch_utc() # Send messages that have improperly formatted JSON payload and a $.ct property execute_onthread( method=iot_device_send_message, args=[ client, device_ids[i], LIVE_HUB, '{\r\n"payload_data1""payload_value1"\r\n}', "$.ct=application/json", 1, LIVE_RG, ], max_runs=1, ) # Monitor messages to ensure it returns improperly formatted JSON self.command_execute_assert( "iot hub monitor-events -n {} -g {} --cg {} --et {} -t 5 -y". format(LIVE_HUB, LIVE_RG, LIVE_CONSUMER_GROUPS[0], enqueued_time), ['{\\r\\n\\"payload_data1\\"\\"payload_value1\\"\\r\\n}'], ) for cg in LIVE_CONSUMER_GROUPS: self.cmd( "az iot hub consumer-group delete --hub-name {} --resource-group {} --name {}" .format(LIVE_HUB, LIVE_RG, cg), expect_failure=False, )
def test_uamqp_device_messaging(self): device_count = 1 device_ids = self.generate_device_names(device_count) self.cmd( "iot hub device-identity create -d {} -n {} -g {}".format( device_ids[0], LIVE_HUB, LIVE_RG), checks=[self.check("deviceId", device_ids[0])], ) test_body = str(uuid4()) test_props = "key0={};key1={}".format(str(uuid4()), str(uuid4())) test_cid = str(uuid4()) test_mid = str(uuid4()) test_ct = "text/plain" test_et = int((time() + 3600) * 1000) # milliseconds since epoch test_ce = "utf8" self.kwargs["c2d_json_send_data"] = json.dumps({"data": str(uuid4())}) # Send C2D message self.cmd( """iot device c2d-message send -d {} -n {} -g {} --data '{}' --cid {} --mid {} --ct {} --expiry {} --ce {} --props {}""".format( device_ids[0], LIVE_HUB, LIVE_RG, test_body, test_cid, test_mid, test_ct, test_et, test_ce, test_props, ), checks=self.is_empty(), ) result = self.cmd( "iot device c2d-message receive -d {} --hub-name {} -g {}".format( device_ids[0], LIVE_HUB, LIVE_RG)).get_output_in_json() assert result["data"] == test_body system_props = result["properties"]["system"] assert system_props["ContentEncoding"] == test_ce assert system_props["ContentType"] == test_ct assert system_props["iothub-correlationid"] == test_cid assert system_props["iothub-messageid"] == test_mid assert system_props["iothub-expiry"] assert system_props[ "iothub-to"] == "/devices/{}/messages/devicebound".format( device_ids[0]) # Ack is tested in message feedback tests assert system_props["iothub-ack"] == "none" app_props = result["properties"]["app"] assert app_props == validate_key_value_pairs(test_props) # Implicit etag assertion etag = result["etag"] self.cmd( "iot device c2d-message complete -d {} --hub-name {} -g {} --etag {}" .format(device_ids[0], LIVE_HUB, LIVE_RG, etag), checks=self.is_empty(), ) # Send C2D message via --login + application/json content ype test_ct = "application/json" test_mid = str(uuid4()) self.cmd( """iot device c2d-message send -d {} --login {} --data '{}' --cid {} --mid {} --ct {} --expiry {} --ce {} --ack positive --props {}""".format( device_ids[0], self.connection_string, "{c2d_json_send_data}", test_cid, test_mid, test_ct, test_et, test_ce, test_props, ), checks=self.is_empty(), ) result = self.cmd( "iot device c2d-message receive -d {} --login {}".format( device_ids[0], self.connection_string)).get_output_in_json() assert result["data"] == self.kwargs["c2d_json_send_data"] system_props = result["properties"]["system"] assert system_props["ContentEncoding"] == test_ce assert system_props["ContentType"] == test_ct assert system_props["iothub-correlationid"] == test_cid assert system_props["iothub-messageid"] == test_mid assert system_props["iothub-expiry"] assert system_props[ "iothub-to"] == "/devices/{}/messages/devicebound".format( device_ids[0]) assert system_props["iothub-ack"] == "positive" app_props = result["properties"]["app"] assert app_props == validate_key_value_pairs(test_props) etag = result["etag"] self.cmd( "iot device c2d-message reject -d {} --etag {} --login {}".format( device_ids[0], etag, self.connection_string), checks=self.is_empty(), ) # Test waiting for ack from c2d send from azext_iot.operations.hub import iot_simulate_device from azext_iot._factory import iot_hub_service_factory from azure.cli.core.mock import DummyCli cli_ctx = DummyCli() client = iot_hub_service_factory(cli_ctx) token, thread = execute_onthread( method=iot_simulate_device, args=[ client, device_ids[0], LIVE_HUB, "complete", "Ping from c2d ack wait test", 2, 5, "http", ], max_runs=4, return_handle=True, ) self.cmd( "iot device c2d-message send -d {} --ack {} --login {} --wait -y". format(device_ids[0], "full", self.connection_string)) token.set() thread.join() # Error - invalid wait when no ack requested self.cmd( "iot device c2d-message send -d {} --login {} --wait -y".format( device_ids[0], self.connection_string), expect_failure=True, ) # Error - content-type is application/json but data is not. self.cmd( "iot device c2d-message send -d {} --login {} --ct application/json --data notjson" .format(device_ids[0], self.connection_string), expect_failure=True, ) # Error - expiry is in the past. self.cmd( "iot device c2d-message send -d {} --login {} --expiry {}".format( device_ids[0], self.connection_string, int(time() * 1000)), expect_failure=True, )
def get_iot_hub_connection_string( cmd, hub_name, resource_group_name, policy_name="iothubowner", key_type="primary", include_events=False, login=None, ): """ Function used to build up dictionary of IoT Hub connection string parts Args: cmd (object): Knack cmd hub_name (str): IoT Hub name resource_group_name (str): name of Resource Group contianing IoT Hub policy_name (str): Security policy name for shared key; e.g. 'iothubowner'(default) key_type (str): Shared key; either 'primary'(default) or 'secondary' include_events (bool): Include key event hub properties Returns: (dict): of connection string elements. Raises: CLIError: on input validation failure. """ result = {} target_hub = None policy = None if login: try: decomposed = parse_iot_hub_connection_string(login) decomposed_lower = dict( (k.lower(), v) for k, v in decomposed.items()) except ValueError as e: raise CLIError(e) result = {} result["cs"] = login result["policy"] = decomposed_lower["sharedaccesskeyname"] result["primarykey"] = decomposed_lower["sharedaccesskey"] result["entity"] = decomposed_lower["hostname"] return result client = None if getattr(cmd, "cli_ctx", None): client = iot_hub_service_factory(cmd.cli_ctx) else: client = cmd def _find_iot_hub_from_list(hubs, hub_name): if hubs: return next( (hub for hub in hubs if hub_name.lower() == hub.name.lower()), None) return None if resource_group_name is None: hubs = client.list_by_subscription() if not hubs: raise CLIError("No IoT Hub found in current subscription.") target_hub = _find_iot_hub_from_list(hubs, hub_name) else: try: target_hub = client.get(resource_group_name, hub_name) except Exception: pass if target_hub is None: raise CLIError( "No IoT Hub found with name {} in current subscription.".format( hub_name)) try: addprops = getattr(target_hub, "additional_properties", None) resource_group_name = (addprops.get("resourcegroup") if addprops else getattr(target_hub, "resourcegroup", None)) policy = client.get_keys_for_key_name(resource_group_name, target_hub.name, policy_name) except Exception: pass if policy is None: raise CLIError("No keys found for policy {} of IoT Hub {}.".format( policy_name, hub_name)) result["cs"] = CONN_STR_TEMPLATE.format( target_hub.properties.host_name, policy.key_name, policy.primary_key if key_type == "primary" else policy.secondary_key, ) result["entity"] = target_hub.properties.host_name result["policy"] = policy_name result["primarykey"] = policy.primary_key result["secondarykey"] = policy.secondary_key result["subscription"] = client.config.subscription_id result["resourcegroup"] = resource_group_name result["location"] = target_hub.location result["sku_tier"] = target_hub.sku.tier.value if include_events: events = {} events["endpoint"] = trim_from_start( target_hub.properties.event_hub_endpoints["events"].endpoint, "sb://").strip("/") events["partition_count"] = target_hub.properties.event_hub_endpoints[ "events"].partition_count events["path"] = target_hub.properties.event_hub_endpoints[ "events"].path events["partition_ids"] = target_hub.properties.event_hub_endpoints[ "events"].partition_ids result["events"] = events return result
def get_iot_hub_connection_string( cmd, hub_name, resource_group_name, policy_name='iothubowner', key_type='primary', include_events=False, login=None): """ Function used to build up dictionary of IoT Hub connection string parts Args: cmd (object): Knack cmd hub_name (str): IoT Hub name resource_group_name (str): name of Resource Group contianing IoT Hub policy_name (str): Security policy name for shared key; e.g. 'iothubowner'(default) key_type (str): Shared key; either 'primary'(default) or 'secondary' include_events (bool): Include key event hub properties Returns: (dict): of connection string elements. Raises: CLIError: on input validation failure. """ result = {} target_hub = None policy = None if login: try: decomposed = parse_iot_hub_connection_string(login) except ValueError as e: raise CLIError(e) result = {} result['cs'] = login result['policy'] = decomposed['SharedAccessKeyName'] result['primarykey'] = decomposed['SharedAccessKey'] result['entity'] = decomposed['HostName'] return result client = None if getattr(cmd, 'cli_ctx', None): client = iot_hub_service_factory(cmd.cli_ctx) else: client = cmd def _find_iot_hub_from_list(hubs, hub_name): if hubs: return next((hub for hub in hubs if hub_name.lower() == hub.name.lower()), None) return None if resource_group_name is None: hubs = client.list_by_subscription() if not hubs: raise CLIError('No IoT Hub found in current subscription.') target_hub = _find_iot_hub_from_list(hubs, hub_name) else: try: target_hub = client.get(resource_group_name, hub_name) except Exception: pass if target_hub is None: raise CLIError( 'No IoT Hub found with name {} in current subscription.'.format(hub_name)) try: addprops = getattr(target_hub, 'additional_properties', None) resource_group_name = addprops.get('resourcegroup') if addprops else getattr( target_hub, 'resourcegroup', None) policy = client.get_keys_for_key_name(resource_group_name, target_hub.name, policy_name) except Exception: pass if policy is None: raise CLIError( 'No keys found for policy {} of IoT Hub {}.'.format(policy_name, hub_name) ) result['cs'] = CONN_STR_TEMPLATE.format( target_hub.properties.host_name, policy.key_name, policy.primary_key if key_type == 'primary' else policy.secondary_key) result['entity'] = target_hub.properties.host_name result['policy'] = policy_name result['primarykey'] = policy.primary_key result['secondarykey'] = policy.secondary_key result['subscription'] = client.config.subscription_id result['resourcegroup'] = resource_group_name if include_events: events = {} events['endpoint'] = trim_from_start(target_hub.properties.event_hub_endpoints['events'].endpoint, 'sb://').strip('/') events['partition_count'] = target_hub.properties.event_hub_endpoints['events'].partition_count events['path'] = target_hub.properties.event_hub_endpoints['events'].path events['partition_ids'] = target_hub.properties.event_hub_endpoints['events'].partition_ids result['events'] = events return result