|
| 1 | +// ---------------------------------------------------------------------------------- |
| 2 | +// |
| 3 | +// Copyright Microsoft Corporation |
| 4 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +// you may not use this file except in compliance with the License. |
| 6 | +// You may obtain a copy of the License at |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// Unless required by applicable law or agreed to in writing, software |
| 9 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | +// See the License for the specific language governing permissions and |
| 12 | +// limitations under the License. |
| 13 | +// ---------------------------------------------------------------------------------- |
| 14 | + |
| 15 | +namespace Microsoft.Azure.Commands.Management.IotHub |
| 16 | +{ |
| 17 | + using System; |
| 18 | + using System.Collections; |
| 19 | + using System.Collections.Generic; |
| 20 | + using System.Management.Automation; |
| 21 | + using Microsoft.Azure.Commands.Management.IotHub.Common; |
| 22 | + using Microsoft.Azure.Commands.Management.IotHub.Models; |
| 23 | + using Microsoft.Azure.Devices; |
| 24 | + using Microsoft.Azure.Devices.Shared; |
| 25 | + using Microsoft.Azure.Management.IotHub; |
| 26 | + using Microsoft.Azure.Management.IotHub.Models; |
| 27 | + using Microsoft.WindowsAzure.Commands.Utilities.Common; |
| 28 | + using Newtonsoft.Json; |
| 29 | + using ResourceManager.Common.ArgumentCompleters; |
| 30 | + |
| 31 | + [Cmdlet("Set", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubDeviceTwin", DefaultParameterSetName = InputObjectParameterSet, SupportsShouldProcess = true)] |
| 32 | + [OutputType(typeof(PSDeviceTwin))] |
| 33 | + public class SetAzIotHubDeviceTwin : IotHubBaseCmdlet |
| 34 | + { |
| 35 | + private const string ResourceIdParameterSet = "ResourceIdSet"; |
| 36 | + private const string ResourceParameterSet = "ResourceSet"; |
| 37 | + private const string ResourceParameterSetForTag = "ResourceSetForTag"; |
| 38 | + private const string ResourceParameterSetForDesired = "ResourceSetForDesired"; |
| 39 | + private const string InputObjectParameterSet = "InputObjectSet"; |
| 40 | + |
| 41 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "IotHub object")] |
| 42 | + [ValidateNotNullOrEmpty] |
| 43 | + public PSIotHub InputObject { get; set; } |
| 44 | + |
| 45 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceIdParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "IotHub Resource Id")] |
| 46 | + [ValidateNotNullOrEmpty] |
| 47 | + [ResourceIdCompleter("Microsoft.Devices/IotHubs")] |
| 48 | + public string ResourceId { get; set; } |
| 49 | + |
| 50 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Resource Group")] |
| 51 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceParameterSetForTag, HelpMessage = "Name of the Resource Group")] |
| 52 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceParameterSetForDesired, HelpMessage = "Name of the Resource Group")] |
| 53 | + [ValidateNotNullOrEmpty] |
| 54 | + [ResourceGroupCompleter] |
| 55 | + public string ResourceGroupName { get; set; } |
| 56 | + |
| 57 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Iot Hub")] |
| 58 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceParameterSetForTag, HelpMessage = "Name of the Iot Hub")] |
| 59 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceParameterSetForDesired, HelpMessage = "Name of the Iot Hub")] |
| 60 | + [ValidateNotNullOrEmpty] |
| 61 | + public string IotHubName { get; set; } |
| 62 | + |
| 63 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "Target Device Id.")] |
| 64 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Target Device Id.")] |
| 65 | + [Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Target Device Id.")] |
| 66 | + [Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSetForTag, HelpMessage = "Target Device Id.")] |
| 67 | + [Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSetForDesired, HelpMessage = "Target Device Id.")] |
| 68 | + [ValidateNotNullOrEmpty] |
| 69 | + public string DeviceId { get; set; } |
| 70 | + |
| 71 | + [Parameter(Mandatory = false, ParameterSetName = InputObjectParameterSet, HelpMessage = "Replace the tags property in a device twin.")] |
| 72 | + [Parameter(Mandatory = false, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Replace the tags property in a device twin.")] |
| 73 | + [Parameter(Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Replace the tags property in a device twin.")] |
| 74 | + [Parameter(Mandatory = true, ParameterSetName = ResourceParameterSetForTag, HelpMessage = "Replace the tags property in a device twin.")] |
| 75 | + [ValidateNotNullOrEmpty] |
| 76 | + public Hashtable Tag { get; set; } |
| 77 | + |
| 78 | + [Parameter(Mandatory = false, ParameterSetName = InputObjectParameterSet, HelpMessage = "Replace the desired property in a device twin.")] |
| 79 | + [Parameter(Mandatory = false, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Replace the desired property in a device twin.")] |
| 80 | + [Parameter(Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Replace the desired property in a device twin.")] |
| 81 | + [Parameter(Mandatory = true, ParameterSetName = ResourceParameterSetForDesired, HelpMessage = "Replace the desired property in a device twin.")] |
| 82 | + [ValidateNotNullOrEmpty] |
| 83 | + public Hashtable Desired { get; set; } |
| 84 | + |
| 85 | + public override void ExecuteCmdlet() |
| 86 | + { |
| 87 | + if (ShouldProcess(this.DeviceId, Properties.Resources.SetIotHubDeviceTwin)) |
| 88 | + { |
| 89 | + IotHubDescription iotHubDescription; |
| 90 | + |
| 91 | + switch (ParameterSetName) |
| 92 | + { |
| 93 | + case InputObjectParameterSet: |
| 94 | + this.ResourceGroupName = this.InputObject.Resourcegroup; |
| 95 | + this.IotHubName = this.InputObject.Name; |
| 96 | + iotHubDescription = IotHubUtils.ConvertObject<PSIotHub, IotHubDescription>(this.InputObject); |
| 97 | + break; |
| 98 | + default: |
| 99 | + if (this.IsParameterBound(c => c.ResourceId)) |
| 100 | + { |
| 101 | + this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId); |
| 102 | + this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId); |
| 103 | + } |
| 104 | + iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName); |
| 105 | + break; |
| 106 | + } |
| 107 | + |
| 108 | + IEnumerable<SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName); |
| 109 | + SharedAccessSignatureAuthorizationRule policy = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryWrite); |
| 110 | + PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); |
| 111 | + RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); |
| 112 | + |
| 113 | + Twin deviceTwin = registryManager.GetTwinAsync(this.DeviceId).GetAwaiter().GetResult(); |
| 114 | + |
| 115 | + if (deviceTwin == null) |
| 116 | + { |
| 117 | + throw new ArgumentException($"The entered device \"{this.DeviceId}\" doesn't exist."); |
| 118 | + } |
| 119 | + |
| 120 | + if (this.IsParameterBound(c => c.Tag)) |
| 121 | + { |
| 122 | + deviceTwin.Tags = new TwinCollection(JsonConvert.SerializeObject(this.Tag)); |
| 123 | + } |
| 124 | + |
| 125 | + if (this.IsParameterBound(c => c.Desired)) |
| 126 | + { |
| 127 | + deviceTwin.Properties.Desired = new TwinCollection(JsonConvert.SerializeObject(this.Desired)); |
| 128 | + } |
| 129 | + |
| 130 | + this.WriteObject(registryManager.ReplaceTwinAsync(this.DeviceId, deviceTwin, deviceTwin.ETag).GetAwaiter().GetResult()); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | +} |
0 commit comments