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);

                PSDeviceCapabilities psDeviceCapabilities = new PSDeviceCapabilities();
                psDeviceCapabilities.IotEdge = this.EdgeEnabled.IsPresent;

                PSAuthenticationMechanism auth = new PSAuthenticationMechanism();

                PSDevice device = new PSDevice();
                device.Id = this.DeviceId;
                switch (this.AuthMethod)
                {
                case PSDeviceAuthType.x509_thumbprint:
                    auth.Type           = PSAuthenticationType.SelfSigned;
                    auth.X509Thumbprint = new PSX509Thumbprint();
                    auth.X509Thumbprint.PrimaryThumbprint   = this.authTypeDynamicParameter.PrimaryThumbprint;
                    auth.X509Thumbprint.SecondaryThumbprint = this.authTypeDynamicParameter.SecondaryThumbprint;
                    break;

                case PSDeviceAuthType.x509_ca:
                    auth.Type = PSAuthenticationType.CertificateAuthority;
                    break;

                default:
                    auth.SymmetricKey = new PSSymmetricKey();
                    auth.Type         = PSAuthenticationType.Sas;
                    break;
                }
                device.Authentication = auth;
                device.Capabilities   = psDeviceCapabilities;
                device.Status         = this.Status;
                device.StatusReason   = this.StatusReason;

                RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString);
                this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.AddDeviceAsync(IotHubDataPlaneUtils.ToDevice(device)).GetAwaiter().GetResult()));
            }
        }
示例#2
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.DeviceId, Properties.Resources.AddIotHubDevice))
            {
                IotHubDescription iotHubDescription;
                IList <Device>    childDevices = null;
                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);

                PSDeviceCapabilities psDeviceCapabilities = new PSDeviceCapabilities();
                psDeviceCapabilities.IotEdge = this.EdgeEnabled.IsPresent;

                PSAuthenticationMechanism auth = new PSAuthenticationMechanism();

                PSDevice device = new PSDevice();
                device.Id = this.DeviceId;
                switch (this.AuthMethod)
                {
                case PSDeviceAuthType.x509_thumbprint:
                    auth.Type           = PSAuthenticationType.SelfSigned;
                    auth.X509Thumbprint = new PSX509Thumbprint();
                    auth.X509Thumbprint.PrimaryThumbprint   = this.authTypeDynamicParameter.PrimaryThumbprint;
                    auth.X509Thumbprint.SecondaryThumbprint = this.authTypeDynamicParameter.SecondaryThumbprint;
                    break;

                case PSDeviceAuthType.x509_ca:
                    auth.Type = PSAuthenticationType.CertificateAuthority;
                    break;

                default:
                    auth.SymmetricKey = new PSSymmetricKey();
                    auth.Type         = PSAuthenticationType.Sas;
                    break;
                }
                device.Authentication = auth;
                device.Capabilities   = psDeviceCapabilities;
                device.Status         = this.Status;
                device.StatusReason   = this.StatusReason;

                if (this.EdgeEnabled.IsPresent)
                {
                    if (this.Children != null)
                    {
                        childDevices = new List <Device>();
                        foreach (string childDeviceId in this.Children)
                        {
                            Device childDevice = registryManager.GetDeviceAsync(childDeviceId).GetAwaiter().GetResult();

                            if (childDevice == null)
                            {
                                throw new ArgumentException($"The entered children device \"{childDeviceId}\" doesn't exist.");
                            }

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

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

                            childDevices.Add(childDevice);
                        }
                    }
                }
                else
                {
                    if (this.ParentDeviceId != null)
                    {
                        Device parentDevice = registryManager.GetDeviceAsync(this.ParentDeviceId).GetAwaiter().GetResult();

                        if (parentDevice == null)
                        {
                            throw new ArgumentException($"The entered parent device \"{this.ParentDeviceId}\" doesn't exist.");
                        }

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

                        device.Scope = parentDevice.Scope;
                    }
                }

                Device newDevice = registryManager.AddDeviceAsync(IotHubDataPlaneUtils.ToDevice(device)).GetAwaiter().GetResult();
                this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(newDevice));

                if (this.EdgeEnabled.IsPresent && childDevices != null)
                {
                    foreach (Device childDevice in childDevices)
                    {
                        childDevice.Scope = newDevice.Scope;
                        registryManager.UpdateDeviceAsync(childDevice).GetAwaiter().GetResult();
                    }
                }
            }
        }