Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
14 changes: 7 additions & 7 deletions azext_iot/_help.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,24 +107,24 @@
"iot hub connection-string show"
] = """
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These improvements look great

type: command
short-summary: Show the connection strings for an IoT Hub.
short-summary: Show the connection strings for the specified IoT Hubs using the given policy name and key.
examples:
- name: Show the connection string for all IoT Hubs in a subscription using the default policy and primary key.
- name: Show the connection strings for all active state IoT Hubs in a subscription using the default policy and primary key.
text: >
az iot hub connection-string show
- name: Show the connection string for all IoT Hubs in a resource group using the default policy and primary key.
- name: Show the connection strings for all active state IoT Hubs in a resource group using the default policy and primary key.
text: >
az iot hub connection-string show --resource-group MyResourceGroup
- name: Show all the connection string of an IoT Hub using primary key.
- name: Show all connection strings of the given IoT Hub using primary key.
text: >
az iot hub connection-string show -n MyIotHub --all
- name: Show the connection string of an IoT Hub using default policy and primary key.
- name: Show the connection string of the given IoT Hub using the default policy and primary key.
text: >
az iot hub connection-string show -n MyIotHub
- name: Show the connection string of an IoT Hub using policy 'service' and secondary key.
- name: Show the connection string of the given IoT Hub using policy 'service' and secondary key.
text: >
az iot hub connection-string show -n MyIotHub --policy-name service --key-type secondary
- name: Show the eventhub compatible connection string of an IoT Hub\'s default eventhub.
- name: Show the eventhub compatible connection string of the given IoT Hub\'s default eventhub.
text: >
az iot hub connection-string show -n MyIotHub --default-eventhub
"""
Expand Down
8 changes: 4 additions & 4 deletions azext_iot/_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,12 @@ def load_arguments(self, _):
"key_type",
options_list=["--key-type", "--kt"],
arg_type=get_enum_type(KeyType),
help="Shared access policy key type for auth.",
help="Shared access policy key type for authentication.",
)
context.argument(
"policy_name",
options_list=["--policy-name", "--pn"],
help="Shared access policy to use for auth.",
help="Shared access policy to use for authentication.",
)
context.argument(
"duration",
Expand Down Expand Up @@ -266,13 +266,13 @@ def load_arguments(self, _):
context.argument(
"show_all",
options_list=["--show-all", "--all"],
help="Allow to show all shared access policies.",
help="Show all shared access policies for the respective IoT Hub.",
)
context.argument(
"default_eventhub",
arg_type=get_three_state_flag(),
options_list=["--default-eventhub", "--eh"],
help="Flag indicating the connection string returned is for the default EventHub endpoint. Default: false",
help="Flag indicating the connection string returned is for the default EventHub endpoint. Default: false.",
Comment thread
vilit1 marked this conversation as resolved.
)

with self.argument_context("iot hub job") as context:
Expand Down
29 changes: 20 additions & 9 deletions azext_iot/operations/hub.py
Original file line number Diff line number Diff line change
Expand Up @@ -2651,15 +2651,26 @@ def conn_str_getter(hub):
discovery, hub, policy_name, key_type, show_all, default_eventhub
)

return [
{
"name": hub.name,
"connectionString": conn_str_getter(hub)
if show_all
else conn_str_getter(hub)[0],
}
for hub in hubs if hub.properties.state == IoTHubStateType.Active.value
]
connection_strings = []
for hub in hubs:
if hub.properties.state == IoTHubStateType.Active.value:
Comment thread
vilit1 marked this conversation as resolved.
try:
connection_strings.append({
"name": hub.name,
"connectionString": conn_str_getter(hub)
if show_all
else conn_str_getter(hub)[0],
})
except:
logger.warning(f"Warning: The IoT Hub {hub.name} in resource group " +
f"{hub.additional_properties['resourcegroup']} does " +
f"not have the target policy {policy_name}.")
else:
logger.warning(f"Warning: The IoT Hub {hub.name} in resource group " +
f"{hub.additional_properties['resourcegroup']} is skipped " +
"because the hub is not active.")
return connection_strings

hub = discovery.find_iothub(hub_name, resource_group_name)
if hub:
conn_str = _get_hub_connection_string(
Expand Down
26 changes: 21 additions & 5 deletions azext_iot/tests/iothub/test_iot_ext_int.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,17 +74,33 @@ def test_hub(self):
# Test 'az iot hub connection-string show'
conn_str_pattern = r'^HostName={0}.azure-devices.net;SharedAccessKeyName=iothubowner;SharedAccessKey='.format(
LIVE_HUB)
conn_str_eventhub_pattern = r'^Endpoint=sb://'
conn_str_eventhub_pattern = (r'^Endpoint=sb://(.+?)servicebus.windows.net/;SharedAccessKeyName='
r'iothubowner;SharedAccessKey=(.+?);EntityPath=')
defaultpolicy = "iothubowner"
nonexistantpolicy = "badpolicy"

# TODO: Temporarily disable to support warning on missing policy.
# hubs_in_sub = self.cmd('iot hub connection-string show').get_output_in_json()
# hubs_in_rg = self.cmd('iot hub connection-string show -g {}'.format(LIVE_RG)).get_output_in_json()
# assert len(hubs_in_sub) >= len(hubs_in_rg)
hubs_in_sub = self.cmd('iot hub connection-string show').get_output_in_json()
hubs_in_rg = self.cmd('iot hub connection-string show -g {}'.format(LIVE_RG)).get_output_in_json()
assert len(hubs_in_sub) >= len(hubs_in_rg)

self.cmd('iot hub connection-string show -n {0}'.format(LIVE_HUB), checks=[
self.check_pattern('connectionString', conn_str_pattern)
])

self.cmd('iot hub connection-string show -n {0} --pn {1}'.format(LIVE_HUB, defaultpolicy), checks=[
self.check_pattern('connectionString', conn_str_pattern)
])

self.cmd(
'iot hub connection-string show -n {0} --pn {1}'.format(LIVE_HUB, nonexistantpolicy),
expect_failure=True,
)

self.cmd(
'iot hub connection-string show --pn {0}'.format(nonexistantpolicy),
checks=[self.check('length(@)', 0)]
)

self.cmd('iot hub connection-string show -n {0} --eh'.format(LIVE_HUB), checks=[
self.check_pattern('connectionString', conn_str_eventhub_pattern)
])
Expand Down