-
Notifications
You must be signed in to change notification settings - Fork 3.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #11425 from anusapan/iot-device-twin
[IoT Hub] Manage IoT device twin configuration.
- Loading branch information
Showing
15 changed files
with
1,757 additions
and
428 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1,356 changes: 930 additions & 426 deletions
1,356
...ommands.IotHub.Test.ScenarioTests.IotHubDPDeviceTests/TestAzureIotHubDeviceLifecycle.json
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
95 changes: 95 additions & 0 deletions
95
src/IotHub/IotHub/IotHub/DataPlane/Device/GetAzIotHubDeviceTwin.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// 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.Devices.Shared; | ||
using Microsoft.Azure.Management.IotHub; | ||
using Microsoft.Azure.Management.IotHub.Models; | ||
using ResourceManager.Common.ArgumentCompleters; | ||
|
||
[Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubDeviceTwin", DefaultParameterSetName = ResourceParameterSet)] | ||
[OutputType(typeof(PSDeviceTwin))] | ||
public class GetAzIotHubDeviceTwin : 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; } | ||
|
||
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.RegistryWrite); | ||
PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); | ||
RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); | ||
|
||
Twin deviceTwin = registryManager.GetTwinAsync(this.DeviceId).GetAwaiter().GetResult(); | ||
|
||
if (deviceTwin == null) | ||
{ | ||
throw new ArgumentException($"The entered device \"{this.DeviceId}\" doesn't exist."); | ||
} | ||
|
||
this.WriteObject(IotHubDataPlaneUtils.ToPSDeviceTwin(deviceTwin)); | ||
} | ||
} | ||
} |
133 changes: 133 additions & 0 deletions
133
src/IotHub/IotHub/IotHub/DataPlane/Device/UpdateAzIotHubDeviceTwin.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
// ---------------------------------------------------------------------------------- | ||
// | ||
// 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; | ||
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.Devices.Shared; | ||
using Microsoft.Azure.Management.IotHub; | ||
using Microsoft.Azure.Management.IotHub.Models; | ||
using Microsoft.WindowsAzure.Commands.Utilities.Common; | ||
using Newtonsoft.Json; | ||
using ResourceManager.Common.ArgumentCompleters; | ||
|
||
[Cmdlet("Update", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubDeviceTwin", DefaultParameterSetName = ResourceParameterSet, SupportsShouldProcess = true)] | ||
[OutputType(typeof(PSDeviceTwin))] | ||
public class UpdateAzIotHubDeviceTwin : 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 = "Add or update the tags property in a device twin.")] | ||
[Parameter(Mandatory = false, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Add or update the tags property in a device twin.")] | ||
[Parameter(Mandatory = false, ParameterSetName = ResourceParameterSet, HelpMessage = "Add or update the tags property in a device twin.")] | ||
[ValidateNotNullOrEmpty] | ||
public Hashtable Tag { get; set; } | ||
|
||
[Parameter(Mandatory = false, ParameterSetName = InputObjectParameterSet, HelpMessage = "Add or update the desired property in a device twin.")] | ||
[Parameter(Mandatory = false, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Add or update the desired property in a device twin.")] | ||
[Parameter(Mandatory = false, ParameterSetName = ResourceParameterSet, HelpMessage = "Add or update the desired property in a device twin.")] | ||
[ValidateNotNullOrEmpty] | ||
public Hashtable Desired { get; set; } | ||
|
||
[Parameter(Mandatory = false, HelpMessage = "Allows to only partially update the tags and desired properties of a device twin.")] | ||
public SwitchParameter Partial { get; set; } | ||
|
||
public override void ExecuteCmdlet() | ||
{ | ||
if (ShouldProcess(this.DeviceId, Properties.Resources.UpdateIotHubDeviceTwin)) | ||
{ | ||
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); | ||
|
||
Twin deviceTwin = registryManager.GetTwinAsync(this.DeviceId).GetAwaiter().GetResult(); | ||
|
||
if (deviceTwin == null) | ||
{ | ||
throw new ArgumentException($"The entered device \"{this.DeviceId}\" doesn't exist."); | ||
} | ||
|
||
if (this.IsParameterBound(c => c.Tag)) | ||
{ | ||
deviceTwin.Tags = new TwinCollection(JsonConvert.SerializeObject(this.Tag)); | ||
} | ||
|
||
if (this.IsParameterBound(c => c.Desired)) | ||
{ | ||
deviceTwin.Properties.Desired = new TwinCollection(JsonConvert.SerializeObject(this.Desired)); | ||
} | ||
|
||
if (this.Partial.IsPresent) | ||
{ | ||
this.WriteObject(registryManager.UpdateTwinAsync(this.DeviceId, deviceTwin, deviceTwin.ETag).GetAwaiter().GetResult()); | ||
} | ||
else | ||
{ | ||
this.WriteObject(registryManager.ReplaceTwinAsync(this.DeviceId, deviceTwin, deviceTwin.ETag).GetAwaiter().GetResult()); | ||
} | ||
} | ||
} | ||
} | ||
} |
Oops, something went wrong.