Example #1
0
        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.RegistryRead);
            PSIotHubConnectionString psIotHubConnectionString = IotHubUtils.ToPSIotHubConnectionString(policy, iotHubDescription.Properties.HostName);
            RegistryManager          registryManager          = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString);

            IList <Device> parentDevices = new List <Device>();

            if (this.DeviceId != null)
            {
                Device parentDevice = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult();

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

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

                parentDevices.Add(parentDevice);
            }
            else
            {
                IEnumerable <string> deviceResults = registryManager.CreateQuery(IotHubDataPlaneUtils.GetEdgeDevices()).GetNextAsJsonAsync().GetAwaiter().GetResult();
                foreach (string deviceResult in deviceResults)
                {
                    Device d = JsonConvert.DeserializeObject <Device>(deviceResult);
                    parentDevices.Add(registryManager.GetDeviceAsync(d.Id).GetAwaiter().GetResult());
                }
            }

            IList <PSDevicesChildren> psDevicesChildren = new List <PSDevicesChildren>();

            foreach (Device parentDevice in parentDevices)
            {
                PSDevicesChildren psDeviceChildren = new PSDevicesChildren
                {
                    DeviceId         = parentDevice.Id,
                    ChildrenDeviceId = new List <string>()
                };

                IList <Device>       childDevices  = new List <Device>();
                IEnumerable <string> deviceResults = registryManager.CreateQuery(IotHubDataPlaneUtils.GetNonEdgeDevices(parentDevice.Scope)).GetNextAsJsonAsync().GetAwaiter().GetResult();

                foreach (string deviceResult in deviceResults)
                {
                    Device d = JsonConvert.DeserializeObject <Device>(deviceResult);
                    psDeviceChildren.ChildrenDeviceId.Add(d.Id);
                }

                psDevicesChildren.Add(psDeviceChildren);
            }

            this.WriteObject(psDevicesChildren, true);
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.DeviceId, Properties.Resources.RemoveIotHubDeviceChildren))
            {
                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);

                Device parentDevice = registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult();

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

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

                IList <Device> childDevices = new List <Device>();

                if (this.Children != null)
                {
                    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) && !childDevice.Scope.Equals(parentDevice.Scope))
                        {
                            throw new ArgumentException($"The entered children device \"{childDeviceId}\" isn\'t assigned as a child of entered device \"{this.DeviceId}\".");
                        }

                        childDevices.Add(childDevice);
                    }
                }
                else
                {
                    IEnumerable <string> deviceResults = registryManager.CreateQuery(IotHubDataPlaneUtils.GetNonEdgeDevices(parentDevice.Scope)).GetNextAsJsonAsync().GetAwaiter().GetResult();
                    foreach (string deviceResult in deviceResults)
                    {
                        Device d = JsonConvert.DeserializeObject <Device>(deviceResult);
                        childDevices.Add(registryManager.GetDeviceAsync(d.Id).GetAwaiter().GetResult());
                    }
                }

                try
                {
                    foreach (Device childDevice in childDevices)
                    {
                        childDevice.Scope = string.Empty;
                        registryManager.UpdateDeviceAsync(childDevice).GetAwaiter().GetResult();
                    }

                    if (PassThru.IsPresent)
                    {
                        this.WriteObject(true);
                    }
                }
                catch
                {
                    if (PassThru.IsPresent)
                    {
                        this.WriteObject(false);
                    }
                }
            }
        }