|
| 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 Newtonsoft.Json; |
| 26 | + using ResourceManager.Common.ArgumentCompleters; |
| 27 | + |
| 28 | + [Cmdlet("Get", ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubDeviceConnectionString", DefaultParameterSetName = ResourceParameterSet)] |
| 29 | + [Alias("Get-" + ResourceManager.Common.AzureRMConstants.AzureRMPrefix + "IotHubDCS")] |
| 30 | + [OutputType(typeof(PSDeviceConnectionString))] |
| 31 | + public class GetAzIotHubDeviceConnectionString : IotHubBaseCmdlet |
| 32 | + { |
| 33 | + private const string ResourceIdParameterSet = "ResourceIdSet"; |
| 34 | + private const string ResourceParameterSet = "ResourceSet"; |
| 35 | + private const string InputObjectParameterSet = "InputObjectSet"; |
| 36 | + |
| 37 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = InputObjectParameterSet, ValueFromPipeline = true, HelpMessage = "IotHub object")] |
| 38 | + [ValidateNotNullOrEmpty] |
| 39 | + public PSIotHub InputObject { get; set; } |
| 40 | + |
| 41 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Resource Group")] |
| 42 | + [ValidateNotNullOrEmpty] |
| 43 | + [ResourceGroupCompleter] |
| 44 | + public string ResourceGroupName { get; set; } |
| 45 | + |
| 46 | + [Parameter(Position = 0, Mandatory = true, ParameterSetName = ResourceIdParameterSet, ValueFromPipelineByPropertyName = true, HelpMessage = "IotHub Resource Id")] |
| 47 | + [ValidateNotNullOrEmpty] |
| 48 | + [ResourceIdCompleter("Microsoft.Devices/IotHubs")] |
| 49 | + public string ResourceId { get; set; } |
| 50 | + |
| 51 | + [Parameter(Position = 1, Mandatory = true, ParameterSetName = ResourceParameterSet, HelpMessage = "Name of the Iot Hub")] |
| 52 | + [ValidateNotNullOrEmpty] |
| 53 | + public string IotHubName { get; set; } |
| 54 | + |
| 55 | + [Parameter(Mandatory = false, ParameterSetName = InputObjectParameterSet, HelpMessage = "Target Device Id.")] |
| 56 | + [Parameter(Mandatory = false, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Target Device Id.")] |
| 57 | + [Parameter(Mandatory = false, ParameterSetName = ResourceParameterSet, HelpMessage = "Target Device Id.")] |
| 58 | + public string DeviceId { get; set; } |
| 59 | + |
| 60 | + [Parameter(Mandatory = false, ParameterSetName = InputObjectParameterSet, HelpMessage = "Shared access policy key type for auth.")] |
| 61 | + [Parameter(Mandatory = false, ParameterSetName = ResourceIdParameterSet, HelpMessage = "Shared access policy key type for auth.")] |
| 62 | + [Parameter(Mandatory = false, ParameterSetName = ResourceParameterSet, HelpMessage = "Shared access policy key type for auth.")] |
| 63 | + public PSKeyType KeyType { get; set; } |
| 64 | + |
| 65 | + public override void ExecuteCmdlet() |
| 66 | + { |
| 67 | + IotHubDescription iotHubDescription; |
| 68 | + if (ParameterSetName.Equals(InputObjectParameterSet)) |
| 69 | + { |
| 70 | + this.ResourceGroupName = this.InputObject.Resourcegroup; |
| 71 | + this.IotHubName = this.InputObject.Name; |
| 72 | + iotHubDescription = IotHubUtils.ConvertObject<PSIotHub, IotHubDescription>(this.InputObject); |
| 73 | + } |
| 74 | + else |
| 75 | + { |
| 76 | + if (ParameterSetName.Equals(ResourceIdParameterSet)) |
| 77 | + { |
| 78 | + this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId); |
| 79 | + this.IotHubName = IotHubUtils.GetIotHubName(this.ResourceId); |
| 80 | + } |
| 81 | + |
| 82 | + iotHubDescription = this.IotHubClient.IotHubResource.Get(this.ResourceGroupName, this.IotHubName); |
| 83 | + } |
| 84 | + |
| 85 | + IEnumerable<SharedAccessSignatureAuthorizationRule> authPolicies = this.IotHubClient.IotHubResource.ListKeys(this.ResourceGroupName, this.IotHubName); |
| 86 | + SharedAccessSignatureAuthorizationRule policy = IotHubUtils.GetPolicy(authPolicies, PSAccessRights.RegistryRead); |
| 87 | + PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName); |
| 88 | + RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString); |
| 89 | + if (this.DeviceId != null) |
| 90 | + { |
| 91 | + Device device = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult(); |
| 92 | + this.WriteObject(GetDeviceConnectionString(device, iotHubDescription.Properties.HostName)); |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + IEnumerable<string> deviceResults = registryManager.CreateQuery("Select * from Devices").GetNextAsJsonAsync().GetAwaiter().GetResult(); |
| 97 | + IList<PSDeviceConnectionString> psDeviceConnectionStringCollection = new List<PSDeviceConnectionString>(); |
| 98 | + foreach (string deviceResult in deviceResults) |
| 99 | + { |
| 100 | + Device device = registryManager.GetDeviceAsync(JsonConvert.DeserializeObject<Device>(deviceResult).Id).GetAwaiter().GetResult(); |
| 101 | + psDeviceConnectionStringCollection.Add(GetDeviceConnectionString(device, iotHubDescription.Properties.HostName)); |
| 102 | + } |
| 103 | + |
| 104 | + this.WriteObject(psDeviceConnectionStringCollection, true); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private PSDeviceConnectionString GetDeviceConnectionString(Device device, string hostName) |
| 109 | + { |
| 110 | + string key; |
| 111 | + switch (device.Authentication.Type) |
| 112 | + { |
| 113 | + case AuthenticationType.Sas: |
| 114 | + key = string.Format("SharedAccessKey={0}", this.KeyType.Equals(PSKeyType.primary) ? device.Authentication.SymmetricKey.PrimaryKey : device.Authentication.SymmetricKey.SecondaryKey); |
| 115 | + break; |
| 116 | + case AuthenticationType.SelfSigned: |
| 117 | + case AuthenticationType.CertificateAuthority: |
| 118 | + key = "x509=true"; |
| 119 | + break; |
| 120 | + default: |
| 121 | + throw new ArgumentNullException("Unable to get authentication type of device."); |
| 122 | + } |
| 123 | + |
| 124 | + PSDeviceConnectionString psDeviceConnectionString = new PSDeviceConnectionString |
| 125 | + { |
| 126 | + DeviceId = device.Id, |
| 127 | + ConnectionString = $"HostName={hostName};DeviceId={device.Id};{key}" |
| 128 | + }; |
| 129 | + |
| 130 | + return psDeviceConnectionString; |
| 131 | + } |
| 132 | + } |
| 133 | +} |
0 commit comments