/// <summary>
        /// Executes the operations associated with the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            DeviceBatchCreationRequest request;
            string deviceBatch;

            try
            {
                if (!ShouldProcess(string.Format(CultureInfo.CurrentCulture, Resources.NewPartnerCustomerDeviceBatchWhatIf, BatchId)))
                {
                    return;
                }

                request = new DeviceBatchCreationRequest
                {
                    BatchId = BatchId,
                    Devices = Devices.Select(d => new Device
                    {
                        HardwareHash        = d.HardwareHash,
                        ModelName           = d.ModelName,
                        OemManufacturerName = d.OemManufacturerName,
                        ProductKey          = d.ProductKey,
                        SerialNumber        = d.SerialNumber
                    })
                };

                deviceBatch = Partner.Customers[CustomerId].DeviceBatches.Create(new DeviceBatchCreationRequest());

                WriteObject(deviceBatch);
            }
            finally
            {
                request = null;
            }
        }
        /// <summary>
        /// Executes the create device batch scenario.
        /// </summary>
        protected override void RunScenario()
        {
            string selectedCustomerId = this.ObtainCustomerId("Enter the ID of the customer to create the device batch for");

            List <Device> devicesToBeUploaded = new List <Device>
            {
                new Device
                {
                    HardwareHash = "DummyHash123",
                    ProductKey   = "00329-00000-0003-AA606",
                    SerialNumber = "1R9-ZNP67"
                }
            };

            DeviceBatchCreationRequest newDeviceBatch = new DeviceBatchCreationRequest
            {
                BatchId = "SDKTestDeviceBatch",
                Devices = devicesToBeUploaded
            };

            var partnerOperations = this.Context.UserPartnerOperations;

            this.Context.ConsoleHelper.WriteObject(newDeviceBatch, "New Device Batch");
            this.Context.ConsoleHelper.StartProgress("Creating Device Batch");

            var trackingLocation = partnerOperations.Customers.ById(selectedCustomerId).DeviceBatches.Create(newDeviceBatch);

            this.Context.ConsoleHelper.StopProgress();
            this.Context.ConsoleHelper.WriteObject(trackingLocation, "Tracking Location to track the status");
            this.Context.ConsoleHelper.Success("Create Device Batch Request submitted successfully!");
        }
        /// <summary>
        /// Executes the operations associated with the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            DeviceBatchCreationRequest       request;
            ResourceCollection <DeviceBatch> batches;
            IEnumerable <Device>             devices;
            BatchUploadDetails status;
            string             location;

            if (!ShouldProcess(string.Format(CultureInfo.CurrentCulture, Resources.NewPartnerCustomerDeviceBatchWhatIf, BatchId)))
            {
                return;
            }

            batches = Partner.Customers[CustomerId].DeviceBatches.GetAsync().ConfigureAwait(false).GetAwaiter().GetResult();

            devices = Devices.Select(d => new Device
            {
                HardwareHash        = d.HardwareHash,
                ModelName           = d.ModelName,
                OemManufacturerName = d.OemManufacturerName,
                Policies            = d.Policies,
                ProductKey          = d.ProductKey,
                SerialNumber        = d.SerialNumber
            });

            if (batches.Items.SingleOrDefault(b => b.Id.Equals(BatchId, StringComparison.InvariantCultureIgnoreCase)) != null)
            {
                location = Partner.Customers[CustomerId].DeviceBatches[BatchId].Devices.CreateAsync(Devices.Select(d => new Device
                {
                    HardwareHash        = d.HardwareHash,
                    ModelName           = d.ModelName,
                    OemManufacturerName = d.OemManufacturerName,
                    Policies            = d.Policies,
                    ProductKey          = d.ProductKey,
                    SerialNumber        = d.SerialNumber
                })).GetAwaiter().GetResult();
            }
            else
            {
                request = new DeviceBatchCreationRequest
                {
                    BatchId = BatchId,
                    Devices = devices
                };

                location = Partner.Customers[CustomerId].DeviceBatches.CreateAsync(request).GetAwaiter().GetResult();
            }

            status = Partner.Customers[CustomerId].BatchUploadStatus.ById(location.Split('/')[4]).GetAsync().GetAwaiter().GetResult();

            while (status.Status == DeviceUploadStatusType.Processing || status.Status == DeviceUploadStatusType.Queued)
            {
                Thread.Sleep(5000);

                status = Partner.Customers[CustomerId].BatchUploadStatus.ById(location.Split('/')[4]).GetAsync().GetAwaiter().GetResult();
            }

            WriteObject(new PSBatchUploadDetails(status));
        }
        /// <summary>
        /// Creates a new devices batch along with the devices.
        /// </summary>
        /// <param name="newEntity">The new devices batch.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The location to track the status of the create.</returns>
        public async Task <string> CreateAsync(DeviceBatchCreationRequest newEntity, CancellationToken cancellationToken = default)
        {
            newEntity.AssertNotNull(nameof(newEntity));

            return(await Partner.ServiceClient.PostAsync <DeviceBatchCreationRequest, string>(
                       new Uri(
                           string.Format(
                               CultureInfo.InvariantCulture,
                               $"/{PartnerService.Instance.ApiVersion}/{PartnerService.Instance.Configuration.Apis.CreateDeviceBatch.Path}",
                               Context),
                           UriKind.Relative),
                       newEntity,
                       cancellationToken).ConfigureAwait(false));
        }
Esempio n. 5
0
        /// <summary>
        /// Creates a new devices batch along with the devices.
        /// </summary>
        /// <param name="newEntity">The new devices batch.</param>
        /// <param name="cancellationToken">A cancellation token that can be used by other objects or threads to receive notice of cancellation.</param>
        /// <returns>The location to track the status of the create.</returns>
        public async Task <string> CreateAsync(DeviceBatchCreationRequest newEntity, CancellationToken cancellationToken = default)
        {
            newEntity.AssertNotNull(nameof(newEntity));

            HttpResponseMessage response = await Partner.ServiceClient.PostAsync <DeviceBatchCreationRequest, HttpResponseMessage>(
                new Uri(
                    string.Format(
                        CultureInfo.InvariantCulture,
                        $"/{PartnerService.Instance.ApiVersion}/{PartnerService.Instance.Configuration.Apis.CreateDeviceBatch.Path}",
                        Context),
                    UriKind.Relative),
                newEntity,
                cancellationToken).ConfigureAwait(false);

            return(response.Headers.Location != null?response.Headers.Location.ToString() : string.Empty);
        }
        /// <summary>
        /// Executes the operations associated with the cmdlet.
        /// </summary>
        public override void ExecuteCmdlet()
        {
            Scheduler.RunTask(async() =>
            {
                if (ShouldProcess(string.Format(CultureInfo.CurrentCulture, Resources.NewPartnerCustomerDeviceBatchWhatIf, BatchId)))
                {
                    IPartner partner = await PartnerSession.Instance.ClientFactory.CreatePartnerOperationsAsync(CorrelationId, CancellationToken).ConfigureAwait(false);
                    DeviceBatchCreationRequest request;
                    ResourceCollection <DeviceBatch> batches;
                    IEnumerable <Device> devices;
                    BatchUploadDetails status;
                    string location;

                    batches = await partner.Customers[CustomerId].DeviceBatches.GetAsync(CancellationToken).ConfigureAwait(false);

                    devices = Devices.Select(d => new Device
                    {
                        HardwareHash        = d.HardwareHash,
                        ModelName           = d.ModelName,
                        OemManufacturerName = d.OemManufacturerName,
                        Policies            = d.Policies,
                        ProductKey          = d.ProductKey,
                        SerialNumber        = d.SerialNumber
                    });

                    if (batches.Items.SingleOrDefault(b => b.Id.Equals(BatchId, StringComparison.InvariantCultureIgnoreCase)) != null)
                    {
                        location = await partner.Customers[CustomerId].DeviceBatches[BatchId].Devices.CreateAsync(Devices.Select(d => new Device
                        {
                            HardwareHash        = d.HardwareHash,
                            ModelName           = d.ModelName,
                            OemManufacturerName = d.OemManufacturerName,
                            Policies            = d.Policies,
                            ProductKey          = d.ProductKey,
                            SerialNumber        = d.SerialNumber
                        }), CancellationToken).ConfigureAwait(false);
                    }
                    else
                    {
                        request = new DeviceBatchCreationRequest
                        {
                            BatchId = BatchId,
                            Devices = devices
                        };

                        location = await partner.Customers[CustomerId].DeviceBatches.CreateAsync(request, CancellationToken).ConfigureAwait(false);
                    }

                    status = await partner.Customers[CustomerId].BatchUploadStatus.ById(location.Split('/')[4]).GetAsync(CancellationToken).ConfigureAwait(false);

                    while (status.Status == DeviceUploadStatusType.Processing || status.Status == DeviceUploadStatusType.Queued)
                    {
                        await Task.Delay(5000, CancellationToken).ConfigureAwait(false);

                        status = await partner.Customers[CustomerId].BatchUploadStatus.ById(location.Split('/')[4]).GetAsync(CancellationToken).ConfigureAwait(false);
                    }

                    WriteObject(new PSBatchUploadDetails(status));
                }
            }, true);
        }