public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Name, DPSResources.RemoveAccessPolicy))
            {
                if (ParameterSetName.Equals(InputObjectParameterSet))
                {
                    this.ResourceGroupName = this.InputObject.ResourceGroupName;
                    this.Name    = this.InputObject.Name;
                    this.KeyName = this.InputObject.KeyName;
                }

                if (ParameterSetName.Equals(ResourceIdParameterSet))
                {
                    this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                    this.Name = IotDpsUtils.GetIotDpsName(this.ResourceId);
                }

                ProvisioningServiceDescription provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.Name);
                IList <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> currentIotDpsAccessPolicyList = GetIotDpsAccessPolicy(this.ResourceGroupName, this.Name);
                SharedAccessSignatureAuthorizationRuleAccessRightsDescription         iotDpsAccessPolicy            = GetIotDpsAccessPolicy(this.ResourceGroupName, this.Name, this.KeyName);
                IList <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> updatedIotDpsAccessPolicyList = currentIotDpsAccessPolicyList.Where(x => x.KeyName != iotDpsAccessPolicy.KeyName).ToList();
                provisioningServiceDescription.Properties.AuthorizationPolicies = updatedIotDpsAccessPolicyList;
                IotDpsCreateOrUpdate(this.ResourceGroupName, this.Name, provisioningServiceDescription);

                if (PassThru)
                {
                    this.WriteObject(true);
                }
            }
        }
예제 #2
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Name, DPSResources.RemoveLinkedHub))
            {
                if (ParameterSetName.Equals(InputObjectParameterSet))
                {
                    this.ResourceGroupName = this.InputObject.ResourceGroupName;
                    this.Name          = this.InputObject.Name;
                    this.LinkedHubName = this.InputObject.LinkedHubName;
                }

                if (ParameterSetName.Equals(ResourceIdParameterSet))
                {
                    this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                    this.Name = IotDpsUtils.GetIotDpsName(this.ResourceId);
                }

                ProvisioningServiceDescription      provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.Name);
                IList <IotHubDefinitionDescription> linkedHubs = GetIotDpsHubs(this.ResourceGroupName, this.Name);
                IotHubDefinitionDescription         linkedHub  = linkedHubs.FirstOrDefault(hubs => hubs.Name.Equals(this.LinkedHubName));
                provisioningServiceDescription.Properties.IotHubs = linkedHubs.Where(x => x.ConnectionString != linkedHub.ConnectionString).ToList();
                IotDpsCreateOrUpdate(this.ResourceGroupName, this.Name, provisioningServiceDescription);

                if (PassThru)
                {
                    this.WriteObject(true);
                }
            }
        }
예제 #3
0
        private void SetIotDpsCertificate()
        {
            string   certificate = string.Empty;
            FileInfo fileInfo    = new FileInfo(this.Path);

            switch (fileInfo.Extension.ToLower(CultureInfo.InvariantCulture))
            {
            case ".cer":
                var certificateByteContent = AzureSession.Instance.DataStore.ReadFileAsBytes(this.Path);
                certificate = Convert.ToBase64String(certificateByteContent);
                break;

            case ".pem":
                certificate = AzureSession.Instance.DataStore.ReadFileAsText(this.Path);
                break;

            default:
                certificate = this.Path;
                break;
            }

            certificate = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(certificate));

            VerificationCodeRequest verificationCodeRequest = new VerificationCodeRequest();

            verificationCodeRequest.Certificate = certificate;

            CertificateResponse certificateResponse = this.IotDpsClient.DpsCertificate.VerifyCertificate(this.CertificateName, this.Etag, verificationCodeRequest, this.ResourceGroupName, this.Name);

            this.WriteObject(IotDpsUtils.ToPSCertificateResponse(certificateResponse));
        }
예제 #4
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Name, DPSResources.RemoveCertificate))
            {
                if (ParameterSetName.Equals(InputObjectParameterSet))
                {
                    this.ResourceGroupName = this.InputObject.ResourceGroupName;
                    this.Name            = this.InputObject.Name;
                    this.CertificateName = this.InputObject.CertificateName;
                    this.Etag            = this.InputObject.Etag;
                }

                if (ParameterSetName.Equals(ResourceIdParameterSet))
                {
                    this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                    this.Name            = IotDpsUtils.GetIotDpsName(this.ResourceId);
                    this.CertificateName = IotDpsUtils.GetIotDpsCertificateName(this.ResourceId);
                }

                this.IotDpsClient.DpsCertificate.Delete(this.ResourceGroupName, this.Etag, this.Name, this.CertificateName);

                if (PassThru)
                {
                    this.WriteObject(true);
                }
            }
        }
예제 #5
0
        private void UpdateIotDpsAccessPolicy()
        {
            ArrayList accessRights = new ArrayList();
            PSAccessRightsDescription psAccessRightsDescription;

            foreach (string permission in this.Permissions)
            {
                if (!Enum.TryParse <PSAccessRightsDescription>(permission.Trim(), true, out psAccessRightsDescription))
                {
                    throw new ArgumentException("Invalid access policy permission");
                }
                else
                {
                    accessRights.Add(psAccessRightsDescription.ToString());
                }
            }

            ProvisioningServiceDescription provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.Name);
            IList <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> iotDpsAccessPolicyList = GetIotDpsAccessPolicy(this.ResourceGroupName, this.Name);

            SharedAccessSignatureAuthorizationRuleAccessRightsDescription iotDpsAccessPolicy = GetIotDpsAccessPolicy(this.ResourceGroupName, this.Name, this.KeyName);

            foreach (SharedAccessSignatureAuthorizationRuleAccessRightsDescription accessPolicy in iotDpsAccessPolicyList)
            {
                if (accessPolicy.KeyName.Equals(iotDpsAccessPolicy.KeyName))
                {
                    accessPolicy.Rights = string.Join(", ", accessRights.ToArray());
                }
            }

            provisioningServiceDescription.Properties.AuthorizationPolicies = iotDpsAccessPolicyList;
            IotDpsCreateOrUpdate(this.ResourceGroupName, this.Name, provisioningServiceDescription);

            this.WriteObject(IotDpsUtils.ToPSSharedAccessSignatureAuthorizationRuleAccessRightsDescription(GetIotDpsAccessPolicy(this.ResourceGroupName, this.Name, this.KeyName), this.ResourceGroupName, this.Name), false);
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Name, DPSResources.UpdateDeviceProvisioningService))
            {
                if (ParameterSetName.Equals(InputObjectCreateUpdateParameterSet) || ParameterSetName.Equals(InputObjectUpdateParameterSet))
                {
                    this.ResourceGroupName = this.InputObject.ResourceGroupName;
                    this.Name = this.InputObject.Name;
                }

                if (ParameterSetName.Equals(ResourceIdCreateUpdateParameterSet) || ParameterSetName.Equals(ResourceIdUpdateParameterSet))
                {
                    this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                    this.Name = IotDpsUtils.GetIotDpsName(this.ResourceId);
                }

                switch (ParameterSetName)
                {
                case InputObjectCreateUpdateParameterSet:
                case ResourceIdCreateUpdateParameterSet:
                case ResourceCreateUpdateParameterSet:
                    this.CreateUpdateIotDps();
                    break;

                case InputObjectUpdateParameterSet:
                case ResourceIdUpdateParameterSet:
                case ResourceUpdateParameterSet:
                    this.UpdateIotDps();
                    break;
                }
            }
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Name, DPSResources.NewCertificateVerificationCode))
            {
                switch (ParameterSetName)
                {
                case InputObjectParameterSet:
                    this.ResourceGroupName = this.InputObject.ResourceGroupName;
                    this.Name            = this.InputObject.Name;
                    this.CertificateName = this.InputObject.CertificateName;
                    this.Etag            = this.InputObject.Etag;
                    this.GetIotDpsCertificateVerificationCode();
                    break;

                case ResourceIdParameterSet:
                    this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                    this.Name            = IotDpsUtils.GetIotDpsName(this.ResourceId);
                    this.CertificateName = IotDpsUtils.GetIotDpsCertificateName(this.ResourceId);
                    this.GetIotDpsCertificateVerificationCode();
                    break;

                case ResourceParameterSet:
                    this.GetIotDpsCertificateVerificationCode();
                    break;

                default:
                    throw new ArgumentException("BadParameterSetName");
                }
            }
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Name, DPSResources.UpdateLinkedHub))
            {
                switch (ParameterSetName)
                {
                case InputObjectParameterSet:
                    this.ResourceGroupName = this.InputObject.ResourceGroupName;
                    this.Name          = this.InputObject.Name;
                    this.LinkedHubName = this.InputObject.LinkedHubName;
                    this.UpdateIotDpsLinkedHub();
                    break;

                case ResourceIdParameterSet:
                    this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                    this.Name = IotDpsUtils.GetIotDpsName(this.ResourceId);
                    this.UpdateIotDpsLinkedHub();
                    break;

                case ResourceParameterSet:
                    this.UpdateIotDpsLinkedHub();
                    break;

                default:
                    throw new ArgumentException("BadParameterSetName");
                }
            }
        }
예제 #9
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Name, DPSResources.AddDeviceProvisioningService))
            {
                if (string.IsNullOrEmpty(this.Location))
                {
                    ResourceGroup resourceGroup = ResourceManagementClient.ResourceGroups.Get(this.ResourceGroupName);
                    this.Location = resourceGroup.Location;
                }

                var provisioningServiceDescription = new ProvisioningServiceDescription()
                {
                    Location   = this.Location,
                    Properties = new IotDpsPropertiesDescription(),
                    Sku        = new IotDpsSkuInfo()
                };

                if (this.AllocationPolicy != null)
                {
                    PSAllocationPolicy psAllocationPolicy;
                    if (Enum.TryParse <PSAllocationPolicy>(this.AllocationPolicy, true, out psAllocationPolicy))
                    {
                        provisioningServiceDescription.Properties.AllocationPolicy = psAllocationPolicy.ToString();
                    }
                    else
                    {
                        throw new ArgumentException("Invalid Allocation Policy");
                    }
                }

                if (this.SkuName != null)
                {
                    PSIotDpsSku psIotDpsSku;
                    if (Enum.TryParse <PSIotDpsSku>(this.SkuName, true, out psIotDpsSku))
                    {
                        provisioningServiceDescription.Sku.Name = psIotDpsSku.ToString();
                    }
                    else
                    {
                        throw new ArgumentException("Invalid Sku");
                    }
                }
                else
                {
                    provisioningServiceDescription.Sku = new IotDpsSkuInfo(IotDpsSku.S1);
                }

                if (this.IsParameterBound(c => c.Tag))
                {
                    provisioningServiceDescription.Tags = this.Tag.Cast <DictionaryEntry>().ToDictionary(kvp => (string)kvp.Key, kvp => (string)kvp.Value);
                }

                IotDpsCreateOrUpdate(this.ResourceGroupName, this.Name, provisioningServiceDescription);
                this.WriteObject(IotDpsUtils.ToPSProvisioningServiceDescription(GetIotDpsResource(this.ResourceGroupName, this.Name)), false);
            }
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.DpsName, DPSResources.RemoveEnrollmentGroup))
            {
                ProvisioningServiceDescription provisioningServiceDescription;
                if (ParameterSetName.Equals(InputObjectParameterSet))
                {
                    this.ResourceGroupName         = this.DpsObject.ResourceGroupName;
                    this.DpsName                   = this.DpsObject.Name;
                    provisioningServiceDescription = IotDpsUtils.ConvertObject <PSProvisioningServiceDescription, ProvisioningServiceDescription>(this.DpsObject);
                }
                else
                {
                    if (ParameterSetName.Equals(ResourceIdParameterSet))
                    {
                        this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                        this.DpsName           = IotDpsUtils.GetIotDpsName(this.ResourceId);
                    }

                    provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.DpsName);
                }

                IEnumerable <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> authPolicies = this.IotDpsClient.IotDpsResource.ListKeys(this.DpsName, this.ResourceGroupName);
                SharedAccessSignatureAuthorizationRuleAccessRightsDescription policy = IotDpsUtils.GetPolicy(authPolicies, PSAccessRightsDescription.EnrollmentWrite);
                PSIotDpsConnectionString  psIotDpsConnectionString = IotDpsUtils.ToPSIotDpsConnectionString(policy, provisioningServiceDescription.Properties.ServiceOperationsHostName);
                ProvisioningServiceClient client = ProvisioningServiceClient.CreateFromConnectionString(psIotDpsConnectionString.PrimaryConnectionString);

                try
                {
                    if (this.Name != null)
                    {
                        client.DeleteEnrollmentGroupAsync(this.Name).GetAwaiter().GetResult();
                    }
                    else
                    {
                        QueryResult enrollments = client.CreateEnrollmentGroupQuery(new QuerySpecification("select * from enrollments")).NextAsync().GetAwaiter().GetResult();
                        foreach (PSEnrollmentGroups enrollment in IotDpsUtils.ToPSEnrollmentGroups(enrollments.Items))
                        {
                            client.DeleteEnrollmentGroupAsync(enrollment.EnrollmentGroupId).GetAwaiter().GetResult();
                        }
                    }

                    if (PassThru.IsPresent)
                    {
                        this.WriteObject(true);
                    }
                }
                catch
                {
                    if (PassThru.IsPresent)
                    {
                        this.WriteObject(false);
                    }
                }
            }
        }
        private void UpdateIotDpsLinkedHub()
        {
            ProvisioningServiceDescription provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.Name);
            IotHubDefinitionDescription    iotHub = provisioningServiceDescription.Properties.IotHubs.FirstOrDefault(x => x.Name.Equals(this.LinkedHubName, StringComparison.OrdinalIgnoreCase));

            iotHub.ApplyAllocationPolicy = this.ApplyAllocationPolicy.IsPresent;
            iotHub.AllocationWeight      = this.AllocationWeight ?? iotHub.AllocationWeight;
            IotDpsCreateOrUpdate(this.ResourceGroupName, this.Name, provisioningServiceDescription);
            this.WriteObject(IotDpsUtils.ToPSIotHubDefinitionDescription(GetIotDpsHubs(this.ResourceGroupName, this.Name, this.LinkedHubName), this.ResourceGroupName, this.Name), false);
        }
예제 #12
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.DpsName, DPSResources.RemoveRegistration))
            {
                ProvisioningServiceDescription provisioningServiceDescription;
                if (ParameterSetName.Equals(InputObjectParameterSet))
                {
                    this.ResourceGroupName         = this.DpsObject.ResourceGroupName;
                    this.DpsName                   = this.DpsObject.Name;
                    provisioningServiceDescription = IotDpsUtils.ConvertObject <PSProvisioningServiceDescription, ProvisioningServiceDescription>(this.DpsObject);
                }
                else
                {
                    if (ParameterSetName.Equals(ResourceIdParameterSet))
                    {
                        this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                        this.DpsName           = IotDpsUtils.GetIotDpsName(this.ResourceId);
                    }

                    provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.DpsName);
                }

                IEnumerable <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> authPolicies = this.IotDpsClient.IotDpsResource.ListKeys(this.DpsName, this.ResourceGroupName);
                SharedAccessSignatureAuthorizationRuleAccessRightsDescription policy = IotDpsUtils.GetPolicy(authPolicies, PSAccessRightsDescription.EnrollmentWrite);
                PSIotDpsConnectionString  psIotDpsConnectionString = IotDpsUtils.ToPSIotDpsConnectionString(policy, provisioningServiceDescription.Properties.ServiceOperationsHostName);
                ProvisioningServiceClient client = ProvisioningServiceClient.CreateFromConnectionString(psIotDpsConnectionString.PrimaryConnectionString);

                try
                {
                    client.DeleteDeviceRegistrationStateAsync(this.RegistrationId).GetAwaiter().GetResult();

                    if (PassThru.IsPresent)
                    {
                        this.WriteObject(true);
                    }
                }
                catch
                {
                    if (PassThru.IsPresent)
                    {
                        this.WriteObject(false);
                    }
                    else
                    {
                        throw;
                    }
                }
            }
        }
예제 #13
0
        public override void ExecuteCmdlet()
        {
            ProvisioningServiceDescription provisioningServiceDescription;

            if (ParameterSetName.Equals(InputObjectParameterSet))
            {
                this.ResourceGroupName         = this.DpsObject.ResourceGroupName;
                this.DpsName                   = this.DpsObject.Name;
                provisioningServiceDescription = IotDpsUtils.ConvertObject <PSProvisioningServiceDescription, ProvisioningServiceDescription>(this.DpsObject);
            }
            else
            {
                if (ParameterSetName.Equals(ResourceIdParameterSet))
                {
                    this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                    this.DpsName           = IotDpsUtils.GetIotDpsName(this.ResourceId);
                }

                provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.DpsName);
            }

            IEnumerable <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> authPolicies = this.IotDpsClient.IotDpsResource.ListKeys(this.DpsName, this.ResourceGroupName);
            SharedAccessSignatureAuthorizationRuleAccessRightsDescription policy = IotDpsUtils.GetPolicy(authPolicies, PSAccessRightsDescription.EnrollmentWrite);
            PSIotDpsConnectionString  psIotDpsConnectionString = IotDpsUtils.ToPSIotDpsConnectionString(policy, provisioningServiceDescription.Properties.ServiceOperationsHostName);
            ProvisioningServiceClient client = ProvisioningServiceClient.CreateFromConnectionString(psIotDpsConnectionString.PrimaryConnectionString);

            if ((!this.IsParameterBound(c => c.RegistrationId) && !this.IsParameterBound(c => c.EnrollmentId)) ||
                (this.IsParameterBound(c => c.RegistrationId) && this.IsParameterBound(c => c.EnrollmentId)))
            {
                throw new ArgumentException("Please provide either RegistrationId or EnrollmentId.");
            }

            if (this.IsParameterBound(c => c.RegistrationId))
            {
                DeviceRegistrationState result = client.GetDeviceRegistrationStateAsync(this.RegistrationId).GetAwaiter().GetResult();
                this.WriteObject(IotDpsUtils.ToPSDeviceRegistrationState(result));
            }

            if (this.IsParameterBound(c => c.EnrollmentId))
            {
                string      query   = this.IsParameterBound(c => c.Query) ? this.Query : "select * from enrollments";
                QueryResult results = client.CreateEnrollmentGroupRegistrationStateQuery(new QuerySpecification(query), this.EnrollmentId).NextAsync().GetAwaiter().GetResult();
                this.WriteObject(IotDpsUtils.ToPSDeviceRegistrationStates(results.Items), true);
            }
        }
        private void AddIotDpsCertificate()
        {
            string   certificate = string.Empty;
            FileInfo fileInfo    = new FileInfo(this.Path);

            switch (fileInfo.Extension.ToLower(CultureInfo.InvariantCulture))
            {
            case ".cer":
                var certificateByteContent = AzureSession.Instance.DataStore.ReadFileAsBytes(this.Path);
                certificate = Convert.ToBase64String(certificateByteContent);
                break;

            case ".pem":
                certificate = AzureSession.Instance.DataStore.ReadFileAsText(this.Path);
                break;

            default:
                certificate = this.Path;
                break;
            }

            certificate = Encoding.UTF8.GetString(Encoding.UTF8.GetBytes(certificate));

            CertificateBodyDescription certificateBodyDescription = new CertificateBodyDescription();

            certificateBodyDescription.Certificate = certificate;

            CertificateResponse certificateResponse;

            if (this.Etag != null)
            {
                certificateResponse = this.IotDpsClient.DpsCertificate.CreateOrUpdate(this.ResourceGroupName, this.Name, this.CertificateName, certificateBodyDescription, this.Etag);
            }
            else
            {
                certificateResponse = this.IotDpsClient.DpsCertificate.CreateOrUpdate(this.ResourceGroupName, this.Name, this.CertificateName, certificateBodyDescription);
            }

            this.WriteObject(IotDpsUtils.ToPSCertificateResponse(certificateResponse));
        }
        public override void ExecuteCmdlet()
        {
            ProvisioningServiceDescription provisioningServiceDescription;

            if (ParameterSetName.Equals(InputObjectParameterSet))
            {
                this.ResourceGroupName         = this.DpsObject.ResourceGroupName;
                this.DpsName                   = this.DpsObject.Name;
                provisioningServiceDescription = IotDpsUtils.ConvertObject <PSProvisioningServiceDescription, ProvisioningServiceDescription>(this.DpsObject);
            }
            else
            {
                if (ParameterSetName.Equals(ResourceIdParameterSet))
                {
                    this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                    this.DpsName           = IotDpsUtils.GetIotDpsName(this.ResourceId);
                }

                provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.DpsName);
            }

            IEnumerable <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> authPolicies = this.IotDpsClient.IotDpsResource.ListKeys(this.DpsName, this.ResourceGroupName);
            SharedAccessSignatureAuthorizationRuleAccessRightsDescription policy = IotDpsUtils.GetPolicy(authPolicies, PSAccessRightsDescription.EnrollmentWrite);
            PSIotDpsConnectionString  psIotDpsConnectionString = IotDpsUtils.ToPSIotDpsConnectionString(policy, provisioningServiceDescription.Properties.ServiceOperationsHostName);
            ProvisioningServiceClient client = ProvisioningServiceClient.CreateFromConnectionString(psIotDpsConnectionString.PrimaryConnectionString);

            if (this.RegistrationId != null)
            {
                IndividualEnrollment result = client.GetIndividualEnrollmentAsync(this.RegistrationId).GetAwaiter().GetResult();
                this.WriteObject(IotDpsUtils.ToPSIndividualEnrollment(result));
            }
            else
            {
                QueryResult enrollments = client.CreateIndividualEnrollmentQuery(new QuerySpecification("select * from enrollments")).NextAsync().GetAwaiter().GetResult();
                this.WriteObject(IotDpsUtils.ToPSIndividualEnrollments(enrollments.Items), true);
            }
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(Name, DPSResources.RemoveDeviceProvisioningService))
            {
                if (ParameterSetName.Equals(InputObjectParameterSet))
                {
                    this.ResourceGroupName = this.InputObject.ResourceGroupName;
                    this.Name = this.InputObject.Name;
                }

                if (ParameterSetName.Equals(ResourceIdParameterSet))
                {
                    this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                    this.Name = IotDpsUtils.GetIotDpsName(this.ResourceId);
                }

                this.IotDpsClient.IotDpsResource.Delete(this.Name, this.ResourceGroupName);

                if (PassThru)
                {
                    this.WriteObject(true);
                }
            }
        }
        public override void ExecuteCmdlet()
        {
            switch (ParameterSetName)
            {
            case InputObjectParameterSet:
                this.ResourceGroupName = this.DpsObject.ResourceGroupName;
                this.Name = this.DpsObject.Name;
                this.GetIotDpsAccessPolicy();
                break;

            case ResourceIdParameterSet:
                this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                this.Name = IotDpsUtils.GetIotDpsName(this.ResourceId);
                this.GetIotDpsAccessPolicy();
                break;

            case ResourceParameterSet:
                this.GetIotDpsAccessPolicy();
                break;

            default:
                throw new ArgumentException("BadParameterSetName");
            }
        }
 private void WritePSObject(IotHubDefinitionDescription iotDpsHub)
 {
     this.WriteObject(IotDpsUtils.ToPSIotHubDefinitionDescription(iotDpsHub, this.ResourceGroupName, this.Name), false);
 }
 private void WritePSObjects(IEnumerable <ProvisioningServiceDescription> provisioningServicesDescription)
 {
     this.WriteObject(IotDpsUtils.ToPSProvisioningServicesDescription(provisioningServicesDescription), true);
 }
 private void WritePSObject(ProvisioningServiceDescription provisioningServiceDescription)
 {
     this.WriteObject(IotDpsUtils.ToPSProvisioningServiceDescription(provisioningServiceDescription), false);
 }
 private void WritePSObject(SharedAccessSignatureAuthorizationRuleAccessRightsDescription iotDpsAccessPolicy)
 {
     this.WriteObject(IotDpsUtils.ToPSSharedAccessSignatureAuthorizationRuleAccessRightsDescription(iotDpsAccessPolicy, this.ResourceGroupName, this.Name), false);
 }
 private void WritePSObjects(IList <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> iotDpsAccessPolicies)
 {
     this.WriteObject(IotDpsUtils.ToPSSharedAccessSignatureAuthorizationRuleAccessRightsCollection(iotDpsAccessPolicies), true);
 }
예제 #23
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.DpsName, DPSResources.AddEnrollmentGroup))
            {
                ProvisioningServiceDescription provisioningServiceDescription;
                if (ParameterSetName.Equals(InputObjectParameterSet))
                {
                    this.ResourceGroupName         = this.DpsObject.ResourceGroupName;
                    this.DpsName                   = this.DpsObject.Name;
                    provisioningServiceDescription = IotDpsUtils.ConvertObject <PSProvisioningServiceDescription, ProvisioningServiceDescription>(this.DpsObject);
                }
                else
                {
                    if (ParameterSetName.Equals(ResourceIdParameterSet))
                    {
                        this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                        this.DpsName           = IotDpsUtils.GetIotDpsName(this.ResourceId);
                    }

                    provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.DpsName);
                }

                IEnumerable <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> authPolicies = this.IotDpsClient.IotDpsResource.ListKeys(this.DpsName, this.ResourceGroupName);
                SharedAccessSignatureAuthorizationRuleAccessRightsDescription policy = IotDpsUtils.GetPolicy(authPolicies, PSAccessRightsDescription.EnrollmentWrite);
                PSIotDpsConnectionString  psIotDpsConnectionString = IotDpsUtils.ToPSIotDpsConnectionString(policy, provisioningServiceDescription.Properties.ServiceOperationsHostName);
                ProvisioningServiceClient client = ProvisioningServiceClient.CreateFromConnectionString(psIotDpsConnectionString.PrimaryConnectionString);

                EnrollmentGroup enrollment = client.GetEnrollmentGroupAsync(this.Name).GetAwaiter().GetResult();

                if (enrollment != null)
                {
                    // Updating ProvisioningStatus

                    if (this.IsParameterBound(c => c.ProvisioningStatus))
                    {
                        enrollment.ProvisioningStatus = (ProvisioningStatus)Enum.Parse(typeof(ProvisioningStatus), this.ProvisioningStatus.ToString());
                    }

                    // Updating InitialTwinState

                    if (this.IsParameterBound(c => c.Tag) || this.IsParameterBound(c => c.Desired))
                    {
                        TwinCollection tags = this.IsParameterBound(c => c.Tag) ? new TwinCollection(JsonConvert.SerializeObject(this.Tag)) : (enrollment.InitialTwinState != null ? enrollment.InitialTwinState.Tags : new TwinCollection());
                        TwinCollection desiredProperties = this.IsParameterBound(c => c.Desired) ? new TwinCollection(JsonConvert.SerializeObject(this.Desired)) : (enrollment.InitialTwinState != null ? enrollment.InitialTwinState.DesiredProperties : new TwinCollection());
                        enrollment.InitialTwinState = new TwinState(tags, desiredProperties);
                    }

                    // Updating Capabilities

                    if (this.IsParameterBound(c => c.EdgeEnabled))
                    {
                        enrollment.Capabilities = new DeviceCapabilities()
                        {
                            IotEdge = this.EdgeEnabled
                        };
                    }

                    // Updating ReprovisionPolicy

                    if (this.IsParameterBound(c => c.ReprovisionPolicy))
                    {
                        switch (this.ReprovisionPolicy)
                        {
                        case PSReprovisionType.reprovisionandmigratedata:
                            enrollment.ReprovisionPolicy = new ReprovisionPolicy()
                            {
                                UpdateHubAssignment = true, MigrateDeviceData = true
                            };
                            break;

                        case PSReprovisionType.reprovisionandresetdata:
                            enrollment.ReprovisionPolicy = new ReprovisionPolicy()
                            {
                                UpdateHubAssignment = true, MigrateDeviceData = false
                            };
                            break;

                        case PSReprovisionType.never:
                            enrollment.ReprovisionPolicy = new ReprovisionPolicy()
                            {
                                UpdateHubAssignment = false, MigrateDeviceData = false
                            };
                            break;
                        }
                    }

                    // Updating AllocationPolicy and Hub

                    if (this.IsParameterBound(c => c.IotHubHostName) && this.IsParameterBound(c => c.AllocationPolicy))
                    {
                        throw new ArgumentException("\"IotHubHostName\" is not required when allocation-policy is defined.");
                    }

                    if (this.IsParameterBound(c => c.IotHubHostName) && this.IsParameterBound(c => c.IotHub))
                    {
                        throw new ArgumentException("\"IotHubHostName\" is not required when IotHub is defined.");
                    }

                    if (this.IsParameterBound(c => c.IotHubHostName))
                    {
                        enrollment.IotHubHostName             = this.IotHubHostName;
                        enrollment.CustomAllocationDefinition = null;
                        enrollment.AllocationPolicy           = null;
                        enrollment.IotHubs = null;
                    }

                    if (this.IsParameterBound(c => c.AllocationPolicy))
                    {
                        enrollment.AllocationPolicy = (Devices.Provisioning.Service.AllocationPolicy)Enum.Parse(typeof(Devices.Provisioning.Service.AllocationPolicy), this.AllocationPolicy.ToString());
                    }

                    switch (enrollment.AllocationPolicy)
                    {
                    case Devices.Provisioning.Service.AllocationPolicy.Static:
                        if (this.IsParameterBound(c => c.IotHub))
                        {
                            if (this.IotHub.Length > 1)
                            {
                                throw new ArgumentException("Please provide only one hub when allocation-policy is defined as Static.");
                            }

                            enrollment.IotHubs = this.IotHub;
                        }
                        enrollment.CustomAllocationDefinition = null;
                        enrollment.IotHubHostName             = null;
                        break;

                    case Devices.Provisioning.Service.AllocationPolicy.Custom:
                        if (enrollment.CustomAllocationDefinition == null)
                        {
                            if (!this.IsParameterBound(c => c.WebhookUrl))
                            {
                                throw new ArgumentException("Please provide an Azure function url when allocation-policy is defined as Custom.");
                            }

                            if (!this.IsParameterBound(c => c.ApiVersion))
                            {
                                throw new ArgumentException("Please provide an Azure function api-version when allocation-policy is defined as Custom.");
                            }
                        }

                        string webhookUrl = string.Empty, apiVersion = string.Empty;
                        webhookUrl = this.IsParameterBound(c => c.WebhookUrl) ? this.WebhookUrl : enrollment.CustomAllocationDefinition.WebhookUrl;
                        apiVersion = this.IsParameterBound(c => c.ApiVersion) ? this.ApiVersion : enrollment.CustomAllocationDefinition.ApiVersion;

                        enrollment.CustomAllocationDefinition = new CustomAllocationDefinition()
                        {
                            WebhookUrl = webhookUrl, ApiVersion = apiVersion
                        };
                        enrollment.IotHubHostName = null;
                        if (this.IsParameterBound(c => c.IotHub))
                        {
                            enrollment.IotHubs = this.IotHub;
                        }
                        break;

                    case Devices.Provisioning.Service.AllocationPolicy.Hashed:
                    case Devices.Provisioning.Service.AllocationPolicy.GeoLatency:
                        if (this.IsParameterBound(c => c.IotHub))
                        {
                            enrollment.IotHubs = this.IotHub;
                        }
                        enrollment.CustomAllocationDefinition = null;
                        enrollment.IotHubHostName             = null;
                        break;

                    default:
                        if (this.IsParameterBound(c => c.IotHub))
                        {
                            throw new ArgumentException("Please provide allocation policy.");
                        }
                        break;
                    }
                }
                else
                {
                    throw new ArgumentException("The enrollment doesn't exist.");
                }

                EnrollmentGroup result = client.CreateOrUpdateEnrollmentGroupAsync(enrollment).GetAwaiter().GetResult();
                this.WriteObject(IotDpsUtils.ToPSEnrollmentGroup(result));
            }
        }
        private void GetIotDpsCertificateVerificationCode()
        {
            VerificationCodeResponse verificationCodeResponse = this.IotDpsClient.DpsCertificate.GenerateVerificationCode(this.CertificateName, this.Etag, this.ResourceGroupName, this.Name);

            this.WriteObject(IotDpsUtils.ToPSVerificationCodeResponse(verificationCodeResponse));
        }
        private void UpdateIotDps()
        {
            ProvisioningServiceDescription updatedProvisioningServiceDescription = new ProvisioningServiceDescription();

            if (this.Reset.IsPresent)
            {
                updatedProvisioningServiceDescription = this.IotDpsClient.IotDpsResource.Update(this.ResourceGroupName, this.Name, IotDpsUtils.ToTagsResource(this.Tag.Cast <DictionaryEntry>().ToDictionary(kvp => (string)kvp.Key, kvp => (string)kvp.Value)));
            }
            else
            {
                ProvisioningServiceDescription provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.Name);
                foreach (var tag in provisioningServiceDescription.Tags)
                {
                    if (!this.Tag.ContainsKey(tag.Key))
                    {
                        this.Tag.Add(tag.Key, tag.Value);
                    }
                }
                updatedProvisioningServiceDescription = this.IotDpsClient.IotDpsResource.Update(this.ResourceGroupName, this.Name, IotDpsUtils.ToTagsResource(this.Tag.Cast <DictionaryEntry>().ToDictionary(kvp => (string)kvp.Key, kvp => (string)kvp.Value)));
            }

            this.WritePSObject(updatedProvisioningServiceDescription);
        }
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.DpsName, DPSResources.AddEnrollment))
            {
                ProvisioningServiceDescription provisioningServiceDescription;
                if (ParameterSetName.Equals(InputObjectParameterSet))
                {
                    this.ResourceGroupName         = this.DpsObject.ResourceGroupName;
                    this.DpsName                   = this.DpsObject.Name;
                    provisioningServiceDescription = IotDpsUtils.ConvertObject <PSProvisioningServiceDescription, ProvisioningServiceDescription>(this.DpsObject);
                }
                else
                {
                    if (ParameterSetName.Equals(ResourceIdParameterSet))
                    {
                        this.ResourceGroupName = IotDpsUtils.GetResourceGroupName(this.ResourceId);
                        this.DpsName           = IotDpsUtils.GetIotDpsName(this.ResourceId);
                    }

                    provisioningServiceDescription = GetIotDpsResource(this.ResourceGroupName, this.DpsName);
                }

                IEnumerable <SharedAccessSignatureAuthorizationRuleAccessRightsDescription> authPolicies = this.IotDpsClient.IotDpsResource.ListKeys(this.DpsName, this.ResourceGroupName);
                SharedAccessSignatureAuthorizationRuleAccessRightsDescription policy = IotDpsUtils.GetPolicy(authPolicies, PSAccessRightsDescription.EnrollmentWrite);
                PSIotDpsConnectionString  psIotDpsConnectionString = IotDpsUtils.ToPSIotDpsConnectionString(policy, provisioningServiceDescription.Properties.ServiceOperationsHostName);
                ProvisioningServiceClient client = ProvisioningServiceClient.CreateFromConnectionString(psIotDpsConnectionString.PrimaryConnectionString);

                Attestation    attestation = null;
                TwinCollection tags = new TwinCollection(), desiredProperties = new TwinCollection();

                if (this.IsParameterBound(c => c.Tag))
                {
                    tags = new TwinCollection(JsonConvert.SerializeObject(this.Tag));
                }

                if (this.IsParameterBound(c => c.Desired))
                {
                    desiredProperties = new TwinCollection(JsonConvert.SerializeObject(this.Desired));
                }

                switch (this.AttestationType)
                {
                case PSAttestationMechanismType.SymmetricKey:
                    if ((this.IsParameterBound(c => c.PrimaryKey) && !this.IsParameterBound(c => c.SecondaryKey)) ||
                        (!this.IsParameterBound(c => c.PrimaryKey) && this.IsParameterBound(c => c.SecondaryKey)))
                    {
                        throw new ArgumentException("Please provide both primary and secondary key.");
                    }
                    else
                    {
                        attestation = new SymmetricKeyAttestation(this.PrimaryKey, this.SecondaryKey);
                    }
                    break;

                case PSAttestationMechanismType.Tpm:
                    if (this.IsParameterBound(c => c.EndorsementKey))
                    {
                        attestation = new TpmAttestation(this.EndorsementKey, this.IsParameterBound(c => c.StorageRootKey) ? this.StorageRootKey : null);
                    }
                    else
                    {
                        throw new ArgumentException("Endorsement key is requried.");
                    }
                    break;

                case PSAttestationMechanismType.X509:
                    if (!this.IsParameterBound(c => c.PrimaryCertificate) && !this.IsParameterBound(c => c.SecondaryCertificate))
                    {
                        if (!this.IsParameterBound(c => c.PrimaryCAName))
                        {
                            throw new ArgumentException("Primary CA reference cannot be null or empty.");
                        }

                        if (this.IsParameterBound(c => c.SecondaryCAName))
                        {
                            attestation = X509Attestation.CreateFromCAReferences(this.PrimaryCAName, this.SecondaryCAName);
                        }
                        else
                        {
                            attestation = X509Attestation.CreateFromCAReferences(this.PrimaryCAName);
                        }
                    }
                    else if (!this.IsParameterBound(c => c.PrimaryCAName) && !this.IsParameterBound(c => c.SecondaryCAName))
                    {
                        string primaryCer = string.Empty, secondaryCer = string.Empty;

                        if (!this.IsParameterBound(c => c.PrimaryCertificate))
                        {
                            throw new ArgumentException("Primary certificate cannot be null or empty.");
                        }

                        primaryCer = IotDpsUtils.GetCertificateString(this.PrimaryCertificate);

                        if (this.IsParameterBound(c => c.SecondaryCertificate))
                        {
                            secondaryCer = IotDpsUtils.GetCertificateString(this.PrimaryCertificate);

                            if (this.IsParameterBound(c => c.RootCertificate))
                            {
                                attestation = X509Attestation.CreateFromRootCertificates(primaryCer, secondaryCer);
                            }
                            else
                            {
                                attestation = X509Attestation.CreateFromClientCertificates(primaryCer, secondaryCer);
                            }
                        }
                        else
                        {
                            if (this.IsParameterBound(c => c.RootCertificate))
                            {
                                attestation = X509Attestation.CreateFromRootCertificates(primaryCer);
                            }
                            else
                            {
                                attestation = X509Attestation.CreateFromClientCertificates(primaryCer);
                            }
                        }
                    }
                    else
                    {
                        throw new ArgumentException("Please provide either CA reference or X509 certificate.");
                    }
                    break;

                default:
                    throw new ArgumentException("Please provide valid attestation mechanism.");
                }

                IndividualEnrollment enrollment = new IndividualEnrollment(this.RegistrationId, attestation);

                if (this.IsParameterBound(c => c.DeviceId))
                {
                    enrollment.DeviceId = this.DeviceId;
                }

                enrollment.InitialTwinState = new TwinState(tags, desiredProperties);
                enrollment.Capabilities     = new DeviceCapabilities()
                {
                    IotEdge = this.EdgeEnabled.IsPresent
                };

                switch (this.ReprovisionPolicy)
                {
                case PSReprovisionType.reprovisionandmigratedata:
                    enrollment.ReprovisionPolicy = new ReprovisionPolicy()
                    {
                        UpdateHubAssignment = true, MigrateDeviceData = true
                    };
                    break;

                case PSReprovisionType.reprovisionandresetdata:
                    enrollment.ReprovisionPolicy = new ReprovisionPolicy()
                    {
                        UpdateHubAssignment = true, MigrateDeviceData = false
                    };
                    break;

                case PSReprovisionType.never:
                    enrollment.ReprovisionPolicy = new ReprovisionPolicy()
                    {
                        UpdateHubAssignment = false, MigrateDeviceData = false
                    };
                    break;
                }

                if (this.IsParameterBound(c => c.AllocationPolicy))
                {
                    if (this.IsParameterBound(c => c.IotHubHostName))
                    {
                        throw new ArgumentException("\"IotHubHostName\" is not required when allocation-policy is defined.");
                    }

                    if (this.AllocationPolicy.Equals(PSAllocationPolicy.Static))
                    {
                        if (this.IsParameterBound(c => c.IotHub))
                        {
                            if (this.IotHub.Length > 1)
                            {
                                throw new ArgumentException("Please provide only one hub when allocation-policy is defined as Static.");
                            }
                        }
                        else
                        {
                            throw new ArgumentException("Please provide a hub to be assigned with device.");
                        }
                    }

                    if (this.AllocationPolicy.Equals(PSAllocationPolicy.Custom))
                    {
                        if (!this.IsParameterBound(c => c.WebhookUrl))
                        {
                            throw new ArgumentException("Please provide an Azure function url when allocation-policy is defined as Custom.");
                        }

                        if (!this.IsParameterBound(c => c.ApiVersion))
                        {
                            throw new ArgumentException("Please provide an Azure function api-version when allocation-policy is defined as Custom.");
                        }

                        enrollment.CustomAllocationDefinition = new CustomAllocationDefinition()
                        {
                            WebhookUrl = this.WebhookUrl, ApiVersion = this.ApiVersion
                        };
                    }

                    enrollment.AllocationPolicy = (Devices.Provisioning.Service.AllocationPolicy)Enum.Parse(typeof(Devices.Provisioning.Service.AllocationPolicy), this.AllocationPolicy.ToString());
                    enrollment.IotHubs          = this.IotHub;
                }
                else
                {
                    if (this.IsParameterBound(c => c.IotHub))
                    {
                        throw new ArgumentException("Please provide allocation policy.");
                    }

                    if (this.IsParameterBound(c => c.IotHubHostName))
                    {
                        enrollment.IotHubHostName = this.IotHubHostName;
                    }
                }

                if (this.IsParameterBound(c => c.ProvisioningStatus))
                {
                    enrollment.ProvisioningStatus = (ProvisioningStatus)Enum.Parse(typeof(ProvisioningStatus), this.ProvisioningStatus.ToString());
                }

                IndividualEnrollment result = client.CreateOrUpdateIndividualEnrollmentAsync(enrollment).GetAwaiter().GetResult();
                this.WriteObject(IotDpsUtils.ToPSIndividualEnrollment(result));
            }
        }
 private void WritePSObjects(IList <IotHubDefinitionDescription> iotDpsHubs)
 {
     this.WriteObject(IotDpsUtils.ToPSIotHubDefinitionDescription(iotDpsHubs), true);
 }
 private void WritePSObjects(IList <CertificateResponse> iotDpsCertificates)
 {
     this.WriteObject(IotDpsUtils.ToPSCertificates(iotDpsCertificates), true);
 }
 private void WritePSObject(CertificateResponse iotDpsCertificate)
 {
     this.WriteObject(IotDpsUtils.ToPSCertificateResponse(iotDpsCertificate), false);
 }