Example #1
0
        private async Task QueryAndDeleteIndividualEnrollments()
        {
            Console.WriteLine("\nCreating a query for enrollments...");
            QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollments");

            using (Query query = _provisioningServiceClient.CreateIndividualEnrollmentQuery(querySpecification, QueryPageSize))
            {
                while (query.HasNext())
                {
                    Console.WriteLine("\nQuerying the next enrollments...");
                    QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);

                    var items = queryResult.Items;
                    List <IndividualEnrollment> individualEnrollments = new List <IndividualEnrollment>();
                    foreach (IndividualEnrollment enrollment in items)
                    {
                        if (!individualEnrollmentsToBeRetained.Contains(enrollment.RegistrationId, StringComparer.OrdinalIgnoreCase))
                        {
                            individualEnrollments.Add(enrollment);
                            Console.WriteLine($"Individual Enrollment to be deleted: {enrollment.RegistrationId}");
                            _individualEnrollmentsDeleted++;
                        }
                    }
                    if (individualEnrollments.Count > 0)
                    {
                        await DeleteBulkIndividualEnrollments(individualEnrollments).ConfigureAwait(false);
                    }

                    await Task.Delay(1000).ConfigureAwait(false);
                }
            }
        }
Example #2
0
        public override void ExecuteCmdlet()
        {
            if (ShouldProcess(this.DpsName, DPSResources.RemoveEnrollment))
            {
                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.RegistrationId != null)
                    {
                        client.DeleteIndividualEnrollmentAsync(this.RegistrationId).GetAwaiter().GetResult();
                    }
                    else
                    {
                        QueryResult enrollments = client.CreateIndividualEnrollmentQuery(new QuerySpecification("select * from enrollments")).NextAsync().GetAwaiter().GetResult();
                        foreach (PSIndividualEnrollments enrollment in IotDpsUtils.ToPSIndividualEnrollments(enrollments.Items))
                        {
                            client.DeleteIndividualEnrollmentAsync(enrollment.RegistrationId).GetAwaiter().GetResult();
                        }
                    }

                    if (PassThru.IsPresent)
                    {
                        this.WriteObject(true);
                    }
                }
                catch
                {
                    if (PassThru.IsPresent)
                    {
                        this.WriteObject(false);
                    }
                }
            }
        }
Example #3
0
        public static async Task RunSample()
        {
            Console.WriteLine("Starting sample...");

            using (ProvisioningServiceClient provisioningServiceClient =
                    ProvisioningServiceClient.CreateFromConnectionString(_provisioningConnectionString))
            {
                #region Create a new individualEnrollment config
                Console.WriteLine("\nCreating a new individualEnrollment...");
                Attestation attestation = new TpmAttestation(_tpmEndorsementKey);
                IndividualEnrollment individualEnrollment =
                        new IndividualEnrollment(
                                SampleRegistrationId,
                                attestation);

                // The following parameters are optional. Remove it if you don't need.
                individualEnrollment.DeviceId = OptionalDeviceId;
                individualEnrollment.ProvisioningStatus = OptionalProvisioningStatus;
                #endregion

                #region Create the individualEnrollment
                Console.WriteLine("\nAdding new individualEnrollment...");
                IndividualEnrollment individualEnrollmentResult = 
                    await provisioningServiceClient.CreateOrUpdateIndividualEnrollmentAsync(individualEnrollment).ConfigureAwait(false);
                Console.WriteLine("\nIndividualEnrollment created with success.");
                Console.WriteLine(individualEnrollmentResult);
                #endregion

                #region Get info of individualEnrollment
                Console.WriteLine("\nGetting the individualEnrollment information...");
                IndividualEnrollment getResult = 
                    await provisioningServiceClient.GetIndividualEnrollmentAsync(SampleRegistrationId).ConfigureAwait(false);
                Console.WriteLine(getResult);
                #endregion

                #region Query info of individualEnrollment
                Console.WriteLine("\nCreating a query for enrollments...");
                QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollments");
                using (Query query = provisioningServiceClient.CreateIndividualEnrollmentQuery(querySpecification))
                {
                    while (query.HasNext())
                    {
                        Console.WriteLine("\nQuerying the next enrollments...");
                        QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);
                        Console.WriteLine(queryResult);
                    }
                }
                #endregion

                #region Delete info of individualEnrollment
                Console.WriteLine("\nDeleting the individualEnrollment...");
                await provisioningServiceClient.DeleteIndividualEnrollmentAsync(getResult).ConfigureAwait(false);
                #endregion
            }
        }
 /// <summary>
 /// Attempts to query all enrollments using a provisioning service client instance
 /// </summary>
 /// <param name="proxyServerAddress">The address of the proxy to be used, or null/empty if no proxy should be used</param>
 /// <returns>If the query succeeded, otherwise this method will throw</returns>
 private async Task ProvisioningServiceClient_IndividualEnrollments_Query_Ok(string proxyServerAddress)
 {
     ProvisioningServiceClient provisioningServiceClient = CreateProvisioningService(proxyServerAddress);
     var querySpecification = new QuerySpecification("SELECT * FROM enrollments");
     using (Query query = provisioningServiceClient.CreateIndividualEnrollmentQuery(querySpecification))
     {
         while (query.HasNext())
         {
             QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);
             Assert.AreEqual(queryResult.Type, QueryResultType.Enrollment);
         }
     }
 }
        /**********************************************************************************
         * Get list of individual entrollments from DPS
         *********************************************************************************/
        public async Task <IEnumerable <SelectListItem> > GetDpsEnrollments()
        {
            List <SelectListItem> enrollmentList = new List <SelectListItem>();

            // add empty one
            enrollmentList.Add(new SelectListItem {
                Value = "", Text = ""
            });

            try
            {
                QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollments");
                using (Query query = _provisioningServiceClient.CreateIndividualEnrollmentQuery(querySpecification))
                {
                    while (query.HasNext())
                    {
                        QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);

                        foreach (IndividualEnrollment enrollment in queryResult.Items)
                        {
                            // we only support symmetric key for now
                            if (enrollment.Attestation.GetType().Name.Equals("SymmetricKeyAttestation"))
                            {
                                enrollmentList.Add(new SelectListItem {
                                    Value = enrollment.RegistrationId, Text = enrollment.RegistrationId
                                });
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                _logger.LogError($"Exception in GetDpsEnrollments() : {e.Message}");
            }

            return(enrollmentList);
        }
        public async Task QueryIndividualEnrollmentsAsync()
        {
            Console.WriteLine("\nCreating a query for enrollments...");
            QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollments");

            using (Query query = _provisioningServiceClient.CreateIndividualEnrollmentQuery(querySpecification))
            {
                while (query.HasNext())
                {
                    Console.WriteLine("\nQuerying the next enrollments...");
                    QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);

                    Console.WriteLine(queryResult);
                }
            }
        }
Example #7
0
        public static async Task RunSample()
        {
            Console.WriteLine("Starting sample...");

            using (ProvisioningServiceClient provisioningServiceClient =
                       ProvisioningServiceClient.CreateFromConnectionString(_provisioningConnectionString))
            {
                #region Create a new individualEnrollment config
                Console.WriteLine("\nCreating a new set of individualEnrollments...");
                List <IndividualEnrollment> individualEnrollments = new List <IndividualEnrollment>();
                foreach (var item in _registrationIds)
                {
                    Attestation attestation = new TpmAttestation(item.Value);
                    individualEnrollments.Add(new IndividualEnrollment(item.Key, attestation));
                }
                #endregion

                #region Create the individualEnrollment
                Console.WriteLine("\nRunning the bulk operation to create the individualEnrollments...");
                BulkEnrollmentOperationResult bulkEnrollmentOperationResult =
                    await provisioningServiceClient.RunBulkEnrollmentOperationAsync(BulkOperationMode.Create, individualEnrollments).ConfigureAwait(false);

                Console.WriteLine("\nResult of the Create bulk enrollment.");
                Console.WriteLine(bulkEnrollmentOperationResult);
                #endregion

                #region Get info of individualEnrollment
                foreach (IndividualEnrollment individualEnrollment in individualEnrollments)
                {
                    String registrationId = individualEnrollment.RegistrationId;
                    Console.WriteLine($"\nGetting the {nameof(individualEnrollment)} information for {registrationId}...");
                    IndividualEnrollment getResult =
                        await provisioningServiceClient.GetIndividualEnrollmentAsync(registrationId).ConfigureAwait(false);

                    Console.WriteLine(getResult);
                }
                #endregion

                #region Query info of individualEnrollment
                Console.WriteLine("\nCreating a query for enrollments...");
                QuerySpecification querySpecification = new QuerySpecification("SELECT * FROM enrollments");
                using (Query query = provisioningServiceClient.CreateIndividualEnrollmentQuery(querySpecification, QueryPageSize))
                {
                    while (query.HasNext())
                    {
                        Console.WriteLine("\nQuerying the next enrollments...");
                        QueryResult queryResult = await query.NextAsync().ConfigureAwait(false);

                        Console.WriteLine(queryResult);
                    }
                }
                #endregion

                #region Delete info of individualEnrollment
                Console.WriteLine("\nDeleting the set of individualEnrollments...");
                bulkEnrollmentOperationResult =
                    await provisioningServiceClient.RunBulkEnrollmentOperationAsync(BulkOperationMode.Delete, individualEnrollments).ConfigureAwait(false);

                Console.WriteLine(bulkEnrollmentOperationResult);
                #endregion
            }
        }