public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.ModuleId, Properties.Resources.AddIotHubModule))
            {
                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);

                PSAuthenticationMechanism auth = new PSAuthenticationMechanism();
                PSModule module = new PSModule();
                module.Id       = this.ModuleId;
                module.DeviceId = 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;
                }
                module.Authentication = auth;

                RegistryManager registryManager = RegistryManager.CreateFromConnectionString(psIotHubConnectionString.PrimaryConnectionString);
                this.WriteObject(IotHubDataPlaneUtils.ToPSModule(registryManager.AddModuleAsync(IotHubDataPlaneUtils.ToModule(module)).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();
                    }
                }
            }
        }
示例#3
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.DeviceId, Properties.Resources.UpdateIotHubDevice))
            {
                IotHubDescription iotHubDescription = null;
                switch (ParameterSetName)
                {
                case InputObjectParameterSetForAuth:
                case InputObjectParameterSetForStatus:
                case InputObjectParameterSetForEdgeEnabled:
                    this.ResourceGroupName = this.InputObject.Resourcegroup;
                    this.IotHubName        = this.InputObject.Name;
                    iotHubDescription      = IotHubUtils.ConvertObject <PSIotHub, IotHubDescription>(this.InputObject);
                    break;

                case ResourceIdParameterSetForAuth:
                case ResourceIdParameterSetForStatus:
                case ResourceIdParameterSetForEdgeEnabled:
                    this.ResourceGroupName = IotHubUtils.GetResourceGroupName(this.ResourceId);
                    this.IotHubName        = IotHubUtils.GetIotHubName(this.ResourceId);
                    break;
                }

                if (iotHubDescription == null)
                {
                    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);

                PSDevice device = IotHubDataPlaneUtils.ToPSDevice(registryManager.GetDeviceAsync(this.DeviceId).GetAwaiter().GetResult());

                switch (ParameterSetName)
                {
                case InputObjectParameterSetForAuth:
                case ResourceIdParameterSetForAuth:
                case ResourceParameterSetForAuth:
                    PSAuthenticationMechanism auth = new PSAuthenticationMechanism();
                    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;
                    break;

                case InputObjectParameterSetForStatus:
                case ResourceIdParameterSetForStatus:
                case ResourceParameterSetForStatus:
                    device.Status       = this.Status;
                    device.StatusReason = this.StatusReason;
                    break;

                case InputObjectParameterSetForEdgeEnabled:
                case ResourceIdParameterSetForEdgeEnabled:
                case ResourceParameterSetForEdgeEnabled:
                    device.Capabilities.IotEdge = this.EdgeEnabled;
                    break;
                }

                this.WriteObject(IotHubDataPlaneUtils.ToPSDevice(registryManager.UpdateDeviceAsync(IotHubDataPlaneUtils.ToDevice(device)).GetAwaiter().GetResult()));
            }
        }