Skip to content

[IoT Hub] Get/Set parent device cmdlet of Iot device. #11144

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Feb 20, 2020
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
16 changes: 14 additions & 2 deletions src/IotHub/IotHub.Test/ScenarioTests/IotHubDPDeviceTests.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,20 @@ function Test-AzureRmIotHubDeviceLifecycle
Assert-True { $updatedDevice1.StatusReason -eq 'Reason1' }

# Update iot device to edge device
$updatedDevice2 = Set-AzIoTHubDevice -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device3 -EdgeEnabled $true
Assert-True { $updatedDevice2.Capabilities.IotEdge }
$updatedDevice3 = Set-AzIoTHubDevice -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device3 -EdgeEnabled $true
Assert-True { $updatedDevice3.Capabilities.IotEdge }

# Set parent device Id
$updatedChildDevice = Set-AzIotHubDeviceParent -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1 -ParentDeviceId $device3
Assert-False { $updatedChildDevice.Capabilities.IotEdge }
Assert-True { $updatedChildDevice.Id -eq $device1 }
Assert-True { $updatedChildDevice.Scope -eq $updatedDevice3.Scope }

# Get parent device Id
$parentDevice = Get-AzIotHubDeviceParent -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1
Assert-True { $parentDevice.Capabilities.IotEdge }
Assert-True { $parentDevice.Id -eq $device3 }
Assert-True { $updatedChildDevice.Scope -eq $updatedDevice3.Scope }

# Get device detail
$iotDevice = Get-AzIotHubDevice -ResourceGroupName $ResourceGroupName -IotHubName $IotHubName -DeviceId $device1
Expand Down

Large diffs are not rendered by default.

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

Expand Down
3 changes: 3 additions & 0 deletions src/IotHub/IotHub/ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
- 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.
* Add support to get/set parent device of an IoT device. New Cmdlets are:
- Get-AzIotHubDeviceParent
- Set-AzIotHubDeviceParent

## Version 2.1.0
* Added support to manage devices in an Iot Hub. New Cmdlets are:
Expand Down
101 changes: 101 additions & 0 deletions src/IotHub/IotHub/IotHub/DataPlane/Device/GetAzIotHubDeviceParent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// ----------------------------------------------------------------------------------
//
// 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.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 + "IotHubDeviceParent", DefaultParameterSetName = ResourceParameterSet)]
[OutputType(typeof(PSDevice))]
public class GetAzIotHubDeviceParent : 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 = "Id of non-edge device.")]
[Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Id of non-edge device.")]
[Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Id of non-edge device.")]
[ValidateNotNullOrEmpty]
public string DeviceId { 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 childDevice = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult();

if (childDevice.Capabilities.IotEdge)
{
throw new ArgumentException($"The entered device \"{this.DeviceId}\" should be non-edge device.");
}

if (string.IsNullOrEmpty(childDevice.Scope))
{
throw new ArgumentException($"The entered device \"{this.DeviceId}\" doesn\'t support parent device functionality.");
}

string parentDeviceId = childDevice.Scope.Substring(Properties.Resources.DEVICE_DEVICESCOPE_PREFIX.Length, childDevice.Scope.LastIndexOf("-") - Properties.Resources.DEVICE_DEVICESCOPE_PREFIX.Length);

this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.GetDeviceAsync(parentDeviceId).GetAwaiter().GetResult()));
}
}
}
119 changes: 119 additions & 0 deletions src/IotHub/IotHub/IotHub/DataPlane/Device/SetAzIotHubDeviceParent.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
// ----------------------------------------------------------------------------------
//
// 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.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("Set", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubDeviceParent", DefaultParameterSetName = ResourceParameterSet, SupportsShouldProcess = true)]
[OutputType(typeof(PSDevice))]
public class SetAzIotHubDeviceParent : 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 = "Id of non-edge device.")]
[Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Id of non-edge device.")]
[Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Id of non-edge device.")]
[ValidateNotNullOrEmpty]
public string DeviceId { get; set; }

[Parameter(Position = 2, Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "Id of edge device.")]
[Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Id of edge device.")]
[Parameter(Position = 3, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Id of edge device.")]
[ValidateNotNullOrEmpty]
public string ParentDeviceId { get; set; }

[Parameter(Mandatory = false, HelpMessage = "Overwrites the non-edge device\'s parent device.")]
public SwitchParameter Force { get; set; }

public override void ExecuteCmdlet()
{
if (ShouldProcess(this.DeviceId, Properties.Resources.AddIotHubDevice))
{
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.RegistryWrite);
PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName);
RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString);

Device childDevice = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult();
Device parentDevice = registryManager.GetDeviceAsync(this.ParentDeviceId).GetAwaiter().GetResult();

if (childDevice.Capabilities.IotEdge)
{
throw new ArgumentException($"The entered device \"{this.DeviceId}\" should be non-edge device.");
}

if (!parentDevice.Capabilities.IotEdge)
{
throw new ArgumentException($"The entered parent device \"{this.ParentDeviceId}\" should be edge device.");
}

if (!string.IsNullOrEmpty(childDevice.Scope) && !childDevice.Scope.Equals(parentDevice.Scope) && !this.Force.IsPresent)
{
throw new ArgumentException($"The entered device \"{this.DeviceId}\" already has a parent device, please use '-Force' to overwrite.");
}

childDevice.Scope = parentDevice.Scope;

this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.UpdateDeviceAsync(childDevice).GetAwaiter().GetResult()));
}
}
}
}
18 changes: 18 additions & 0 deletions src/IotHub/IotHub/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/IotHub/IotHub/Properties/Resources.resx
Original file line number Diff line number Diff line change
Expand Up @@ -207,4 +207,10 @@
<data name="UpdateIotHubModule" xml:space="preserve">
<value>Update Module</value>
</data>
<data name="DEVICE_DEVICESCOPE_PREFIX" xml:space="preserve">
<value>ms-azure-iot-edge://</value>
</data>
<data name="SetParentDevice" xml:space="preserve">
<value>Set Parent Device</value>
</data>
</root>
6 changes: 6 additions & 0 deletions src/IotHub/IotHub/help/Az.IotHub.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,9 @@ Lists all devices or a particular device contained within an Azure IoT Hub.
### [Get-AzIotHubDeviceConnectionString](Get-AzIotHubDeviceConnectionString.md)
Get the connection string of a target IoT device in an Iot Hub.

### [Get-AzIotHubDeviceParent](Get-AzIotHubDeviceParent.md)
Get the parent device of the specified device.

### [Get-AzIotHubEventHubConsumerGroup](Get-AzIotHubEventHubConsumerGroup.md)
Gets all the eventhub consumergroups.

Expand Down Expand Up @@ -134,6 +137,9 @@ Updates the properties of an IotHub.
### [Set-AzIotHubDevice](Set-AzIotHubDevice.md)
Update an IoT Hub device.

### [Set-AzIotHubDeviceParent](Set-AzIotHubDeviceParent.md)
Set the parent device of the specified device.

### [Set-AzIotHubMessageEnrichment](Set-AzIotHubMessageEnrichment.md)
Update a message enrichment in your IoT hub.

Expand Down
Loading