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
5 changes: 5 additions & 0 deletions src/IotHub/IotHub.Test/ScenarioTests/IotHubDPModuleTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,11 @@ function Test-AzureRmIotHubModuleLifecycle
$modules = Get-AzIotHubModule -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1
Assert-True { $modules.Count -eq 2}

# Get module connection string
$moduleCS = Get-AzIotHubMCS -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1 -ModuleId $module2
Assert-True { $moduleCS.ModuleId -eq $module2 }
Assert-True { $moduleCS.ConnectionString -eq "HostName=$IotHubName.azure-devices.net;DeviceId=$device1;ModuleId=$module2;x509=true" }

# Get module detail
$module = Get-AzIotHubModule -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1 -ModuleId $module2
Assert-True { $module.Id -eq $module2 }
Expand Down

Large diffs are not rendered by default.

5 changes: 3 additions & 2 deletions src/IotHub/IotHub/Az.IotHub.psd1
Original file line number Diff line number Diff line change
Expand Up @@ -97,15 +97,16 @@ CmdletsToExport = 'Add-AzIotHubKey', 'Get-AzIotHubEventHubConsumerGroup',
'Get-AzIotHubDevice', 'Remove-AzIotHubDevice',
'Set-AzIotHubDevice', 'Add-AzIotHubModule',
'Get-AzIotHubModule', 'Remove-AzIotHubModule',
'Set-AzIotHubModule', 'Get-AzIotHubDeviceConnectionString'
'Set-AzIotHubModule', 'Get-AzIotHubDeviceConnectionString',
'Get-AzIotHubModuleConnectionString'
# Variables to export from this module
# VariablesToExport = @()

# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
AliasesToExport = 'Get-AzIotHubEHCG', 'Add-AzIotHubEHCG', 'Remove-AzIotHubEHCG',
'Set-AzIotHubVC', 'Get-AzIotHubCVC', 'Add-AzIotHubMsgEnrich',
'Get-AzIotHubMsgEnrich', 'Remove-AzIotHubMsgEnrich',
'Set-AzIotHubMsgEnrich', 'Get-AzIotHubDCS'
'Set-AzIotHubMsgEnrich', 'Get-AzIotHubDCS', 'Get-AzIotHubMCS'

# DSC resources to export from this module
# DscResourcesToExport = @()
Expand Down
1 change: 1 addition & 0 deletions src/IotHub/IotHub/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- Remove-AzIotHubModule
- Set-AzIotHubModule
* Add cmdlet to get the connection string of a target IoT device in an Iot Hub.
* Add cmdlet to get the connection string of a module on a target IoT device in an Iot Hub.

## Version 2.1.0
* Added support to manage devices in an Iot Hub. New Cmdlets are:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,10 @@ public override void ExecuteCmdlet()
if (this.DeviceId != null)
{
Device device = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult();
this.WriteObject(GetDeviceConnectionString(device, iotHubDescription.Properties.HostName));
if (device != null)
{
this.WriteObject(GetDeviceConnectionString(device, iotHubDescription.Properties.HostName));
}
}
else
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Management.IotHub.Models
{
/// <summary>
/// Connection string of the module.
/// </summary>

public class PSModuleConnectionString
{
/// <summary>
/// Module ID.
/// </summary>
public string ModuleId { get; set; }

/// <summary>
/// Module Connection String.
/// </summary>
public string ConnectionString { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

namespace Microsoft.Azure.Commands.Management.IotHub
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Management.Automation;
using Microsoft.Azure.Commands.Management.IotHub.Common;
using Microsoft.Azure.Commands.Management.IotHub.Models;
using Microsoft.Azure.Devices;
using Microsoft.Azure.Management.IotHub;
using Microsoft.Azure.Management.IotHub.Models;
using ResourceManager.Common.ArgumentCompleters;

[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubModuleConnectionString", DefaultParameterSetName = ResourceParameterSet)]
[Alias("Get-" + ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubMCS")]
[OutputType(typeof(PSModuleConnectionString))]
public class GetAzIotHubModuleConnectionString : IotHubBaseCmdlet
{
private const string ResourceIdParameterSet = "ResourceIdSet";
private const string ResourceParameterSet = "ResourceSet";
private const string InputObjectParameterSet = "InputObjectSet";

[Parameter(Position = 0, Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "IotHub object")]
[ValidateNotNullOrEmpty]
public PSIotHub InputObject { get; set; }

[Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Resource Group")]
[ValidateNotNullOrEmpty]
[ResourceGroupCompleter]
public string ResourceGroupName { get; set; }

[Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceIdParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "IotHub Resource Id")]
[ValidateNotNullOrEmpty]
[ResourceIdCompleter("Microsoft.Devices/IotHubs")]
public string ResourceId { get; set; }

[Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Iot Hub")]
[ValidateNotNullOrEmpty]
public string IotHubName { get; set; }

[Parameter(Position = 1, Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "Target Device Id.")]
[Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Target Device Id.")]
[Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Target Device Id.")]
[ValidateNotNullOrEmpty]
public string DeviceId { get; set; }

[Parameter(Mandatory = false, ParameterSetName = InputObjectParameterSet, HelpMessage = "Target Module Id.")]
[Parameter(Mandatory = false, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Target Module Id.")]
[Parameter(Mandatory = false, ParameterSetName = ResourceParameterSet, HelpMessage = "Target Module Id.")]
[ValidateNotNullOrEmpty]
public string ModuleId { get; set; }

[Parameter(Mandatory = false, ParameterSetName = InputObjectParameterSet, HelpMessage = "Shared access policy key type for auth.")]
[Parameter(Mandatory = false, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Shared access policy key type for auth.")]
[Parameter(Mandatory = false, ParameterSetName = ResourceParameterSet, HelpMessage = "Shared access policy key type for auth.")]
public PSKeyType KeyType { get; set; }

public override void ExecuteCmdlet()
{
IotHubDescription iotHubDescription;
if (ParameterSetName.Equals(InputObjectParameterSet))
{
this.ResourceGroupName = this.InputObject.Resourcegroup;
this.IotHubName = this.InputObject.Name;
iotHubDescription = IotHubUtils.ConvertObject<PSIotHub, IotHubDescription>(this.InputObject);
}
else
{
if (ParameterSetName.Equals(ResourceIdParameterSet))
{
this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId);
this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId);
}

iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName);
}

IEnumerable<SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName);
SharedAccessSignatureAuthorizationRule policy = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryRead);
PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName);
RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString);
Device device = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult();
if (device != null)
{
List<Module> modules = registryManager.GetModulesOnDeviceAsync(this.DeviceId).GetAwaiter().GetResult().ToList();
if (this.ModuleId != null)
{
if (modules.Any(m => m.Id.Equals(this.ModuleId)))
{
this.WriteObject(GetModuleConnectionString(modules.FirstOrDefault(m => m.Id.Equals(this.ModuleId)), iotHubDescription.Properties.HostName));
}
}
else
{
IList<PSModuleConnectionString> psModuleConnectionStringCollection = new List<PSModuleConnectionString>();
foreach (Module module in modules)
{
psModuleConnectionStringCollection.Add(GetModuleConnectionString(module, iotHubDescription.Properties.HostName));
}

this.WriteObject(psModuleConnectionStringCollection, true);
}
}
}

private PSModuleConnectionString GetModuleConnectionString(Module module, string hostName)
{
string key;
switch (module.Authentication.Type)
{
case AuthenticationType.Sas:
key = string.Format("SharedAccessKey={0}", this.KeyType.Equals(PSKeyType.primary) ? module.Authentication.SymmetricKey.PrimaryKey : module.Authentication.SymmetricKey.SecondaryKey);
break;
case AuthenticationType.SelfSigned:
case AuthenticationType.CertificateAuthority:
key = "x509=true";
break;
default:
throw new ArgumentNullException("Unable to get authentication type of device.");
}

PSModuleConnectionString psModuleConnectionString = new PSModuleConnectionString
{
ModuleId = module.Id,
ConnectionString = $"HostName={hostName};DeviceId={module.DeviceId};ModuleId={module.Id};{key}"
};

return psModuleConnectionString;
}
}
}
3 changes: 3 additions & 0 deletions src/IotHub/IotHub/help/Az.IotHub.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ Lists all message enrichments or a particular message enrichment for your IoT Hu
### [Get-AzIotHubModule](Get-AzIotHubModule.md)
Get the details of an IoT device module or list modules located on an IoT device in an IoT Hub.

### [Get-AzIotHubModuleConnectionString](Get-AzIotHubModuleConnectionString.md)
Get the connection string of a target IoT device module in an Iot Hub.

### [Get-AzIotHubQuotaMetric](Get-AzIotHubQuotaMetric.md)
Gets the Quota Metrics for an IotHub.

Expand Down
Loading