Skip to content
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions azext_iot/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -828,8 +828,11 @@
"iot device send-d2c-message"
] = """
type: command
short-summary: Send an mqtt device-to-cloud message.
The command supports sending messages with application and system properties.
short-summary: |
Send an mqtt device-to-cloud message.
The command supports sending messages with application and system properties.

Note: The command only works for symmetric key auth (SAS) based devices
examples:
- name: Basic usage
text: az iot device send-d2c-message -n {iothub_name} -d {device_id}
Expand All @@ -851,7 +854,8 @@
While the device simulation is running, the device will automatically receive
and acknowledge cloud-to-device (c2d) messages. For mqtt simulation, all c2d messages will
be acknowledged with completion. For http simulation c2d acknowledgement is based on user
selection which can be complete, reject or abandon.
selection which can be complete, reject or abandon. Additionally, mqtt simulation is only
Comment thread
avagraw marked this conversation as resolved.
supported for symmetric key auth (SAS) based devices

Note: The command by default will set content-type to application/json and content-encoding
to utf-8. This can be overriden.
Expand Down
9 changes: 9 additions & 0 deletions azext_iot/operations/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2070,6 +2070,10 @@ def _iot_device_send_message(
import ssl
import os

device = _iot_device_show(target, device_id)
if device is not None and device.get("authentication", {}).get("type", "") != "sas":
Comment thread
avagraw marked this conversation as resolved.
Outdated
raise CLIError('D2C send message command only supports symmetric key auth (SAS) based devices')

msgs = []
if properties:
properties = validate_key_value_pairs(properties)
Expand Down Expand Up @@ -2456,6 +2460,11 @@ def http_wrap(target, device_id, generator):

try:
if protocol_type == ProtocolType.mqtt.name:

device = _iot_device_show(target, device_id)
if device is not None and device.get("authentication", {}).get("type", "") != "sas":
raise CLIError('MQTT simulation is only supported for symmetric key auth (SAS) based devices')

wrap = mqtt_client_wrap(
target=target,
device_id=device_id,
Expand Down
34 changes: 34 additions & 0 deletions azext_iot/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
path_iot_hub_monitor_events_entrypoint = (
"azext_iot.operations.hub._iot_hub_monitor_events"
)
path_iot_device_show = "azext_iot.operations.hub._iot_device_show"
hub_entity = "myhub.azure-devices.net"

instance_name = generate_generic_id()
Expand Down Expand Up @@ -137,6 +138,39 @@ def fixture_monitor_events_entrypoint(mocker):
return mocker.patch(path_iot_hub_monitor_events_entrypoint)


@pytest.fixture()
def fixture_iot_device_show(mocker):
device = mocker.patch(path_iot_device_show)
device.return_value = {
"authentication": {
"symmetricKey": {
"primaryKey": "test_pk",
"secondaryKey": "test_sk"
},
"type": "sas",
"x509Thumbprint": {
"primaryThumbprint": None,
"secondaryThumbprint": None
}
},
"capabilities": {
"iotEdge": False
},
"cloudToDeviceMessageCount": 0,
"connectionState": "Disconnected",
"connectionStateUpdatedTime": "2021-05-27T00:36:11.2861732Z",
"deviceId": "Test_Device_1",
"etag": "ODgxNTgwOA==",
"generationId": "637534345627501371",
"hub": "test-iot-hub.azure-devices.net",
"lastActivityTime": "2021-05-27T00:18:16.3154299Z",
"status": "enabled",
"statusReason": None,
"statusUpdatedTime": "0001-01-01T00:00:00Z"
}
return device


# TODO: To be deprecated asap. Leverage mocked_response fixture for this functionality.
def build_mock_response(
mocker=None, status_code=200, payload=None, headers=None, **kwargs
Expand Down
2 changes: 1 addition & 1 deletion azext_iot/tests/iothub/test_iot_ext_unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,7 @@ def test_generate_sas_token(self):

class TestDeviceSimulate:
@pytest.fixture(params=[204])
def serviceclient(self, mocker, fixture_ghcs, fixture_sas, request):
def serviceclient(self, mocker, fixture_ghcs, fixture_sas, request, fixture_iot_device_show):
Comment thread
avagraw marked this conversation as resolved.
Outdated
service_client = mocker.patch(path_service_client)
service_client.return_value = build_mock_response(mocker, request.param, {})
return service_client
Expand Down