|
| 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.Generic; |
| 19 | + using System.Management.Automation; |
| 20 | + using Microsoft.Azure.Commands.Management.IotHub.Common; |
| 21 | + using Microsoft.Azure.Commands.Management.IotHub.Models; |
| 22 | + using Microsoft.Azure.Devices; |
| 23 | + using Microsoft.Azure.Management.IotHub; |
| 24 | + using Microsoft.Azure.Management.IotHub.Models; |
| 25 | + using ResourceManager.Common.ArgumentCompleters; |
| 26 | + |
| 27 | + [Cmdlet("Set", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubDeviceParent", DefaultParameterSetName = ResourceParameterSet, SupportsShouldProcess = true)] |
| 28 | + [OutputType(typeof(PSDevice))] |
| 29 | + public class SetAzIotHubDeviceParent : IotHubBaseCmdlet |
| 30 | + { |
| 31 | + private const string ResourceIdParameterSet = "ResourceIdSet"; |
| 32 | + private const string ResourceParameterSet = "ResourceSet"; |
| 33 | + private const string InputObjectParameterSet = "InputObjectSet"; |
| 34 | + |
| 35 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "IotHub object")] |
| 36 | + [ValidateNotNullOrEmpty] |
| 37 | + public PSIotHub InputObject { get; set; } |
| 38 | + |
| 39 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Resource Group")] |
| 40 | + [ValidateNotNullOrEmpty] |
| 41 | + [ResourceGroupCompleter] |
| 42 | + public string ResourceGroupName { get; set; } |
| 43 | + |
| 44 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceIdParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "IotHub Resource Id")] |
| 45 | + [ValidateNotNullOrEmpty] |
| 46 | + [ResourceIdCompleter("Microsoft.Devices/IotHubs")] |
| 47 | + public string ResourceId { get; set; } |
| 48 | + |
| 49 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Iot Hub")] |
| 50 | + [ValidateNotNullOrEmpty] |
| 51 | + public string IotHubName { get; set; } |
| 52 | + |
| 53 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "Id of non-edge device.")] |
| 54 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Id of non-edge device.")] |
| 55 | + [Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Id of non-edge device.")] |
| 56 | + [ValidateNotNullOrEmpty] |
| 57 | + public string DeviceId { get; set; } |
| 58 | + |
| 59 | + [Parameter(Position = 2, Mandatory = true, ParameterSetName = InputObjectParameterSet, HelpMessage = "Id of edge device.")] |
| 60 | + [Parameter(Position = 2, Mandatory = true, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Id of edge device.")] |
| 61 | + [Parameter(Position = 3, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Id of edge device.")] |
| 62 | + [ValidateNotNullOrEmpty] |
| 63 | + public string ParentDeviceId { get; set; } |
| 64 | + |
| 65 | + [Parameter(Mandatory = false, HelpMessage = "Overwrites the non-edge device\'s parent device.")] |
| 66 | + public SwitchParameter Force { get; set; } |
| 67 | + |
| 68 | + public override void ExecuteCmdlet() |
| 69 | + { |
| 70 | + if (ShouldProcess(this.DeviceId, Properties.Resources.AddIotHubDevice)) |
| 71 | + { |
| 72 | + IotHubDescription iotHubDescription; |
| 73 | + if (ParameterSetName.Equals(InputObjectParameterSet)) |
| 74 | + { |
| 75 | + this.ResourceGroupName = this.InputObject.Resourcegroup; |
| 76 | + this.IotHubName = this.InputObject.Name; |
| 77 | + iotHubDescription = IotHubUtils.ConvertObject<PSIotHub, IotHubDescription>(this.InputObject); |
| 78 | + } |
| 79 | + else |
| 80 | + { |
| 81 | + if (ParameterSetName.Equals(ResourceIdParameterSet)) |
| 82 | + { |
| 83 | + this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId); |
| 84 | + this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId); |
| 85 | + } |
| 86 | + |
| 87 | + iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName); |
| 88 | + } |
| 89 | + |
| 90 | + IEnumerable<SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName); |
| 91 | + SharedAccessSignatureAuthorizationRule policy = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryWrite); |
| 92 | + PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); |
| 93 | + RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); |
| 94 | + |
| 95 | + Device childDevice = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult(); |
| 96 | + Device parentDevice = registryManager.GetDeviceAsync(this.ParentDeviceId).GetAwaiter().GetResult(); |
| 97 | + |
| 98 | + if (childDevice.Capabilities.IotEdge) |
| 99 | + { |
| 100 | + throw new ArgumentException($"The entered device \"{this.DeviceId}\" should be non-edge device."); |
| 101 | + } |
| 102 | + |
| 103 | + if (!parentDevice.Capabilities.IotEdge) |
| 104 | + { |
| 105 | + throw new ArgumentException($"The entered parent device \"{this.ParentDeviceId}\" should be edge device."); |
| 106 | + } |
| 107 | + |
| 108 | + if (!string.IsNullOrEmpty(childDevice.Scope) && !childDevice.Scope.Equals(parentDevice.Scope) && !this.Force.IsPresent) |
| 109 | + { |
| 110 | + throw new ArgumentException($"The entered device \"{this.DeviceId}\" already has a parent device, please use '-Force' to overwrite."); |
| 111 | + } |
| 112 | + |
| 113 | + childDevice.Scope = parentDevice.Scope; |
| 114 | + |
| 115 | + this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.UpdateDeviceAsync(childDevice).GetAwaiter().GetResult())); |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | +} |
0 commit comments