/// <summary>
 /// Begin async task to update a device job.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.StorSimple.IDeviceJobOperations.
 /// </param>
 /// <param name='deviceId'>
 /// Required. id of device
 /// </param>
 /// <param name='jobId'>
 /// Required. id of job to update
 /// </param>
 /// <param name='updateRequest'>
 /// Required. Request data, contains the action to be taken
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Required. The Custom Request Headers which client must use.
 /// </param>
 /// <returns>
 /// This is the Task Response for all Async Calls
 /// </returns>
 public static GuidTaskResponse BeginUpdateDeviceJob(this IDeviceJobOperations operations, string deviceId, string jobId, UpdateDeviceJobRequest updateRequest, CustomRequestHeaders customRequestHeaders)
 {
     return Task.Factory.StartNew((object s) => 
     {
         return ((IDeviceJobOperations)s).BeginUpdateDeviceJobAsync(deviceId, jobId, updateRequest, customRequestHeaders);
     }
     , operations, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default).Unwrap().GetAwaiter().GetResult();
 }
 /// <summary>
 /// Stop the specified device job.
 /// </summary>
 /// <param name="instanceId">Instance id of the job to be stopped.</param>
 public TaskStatusInfo StopDeviceJob(string deviceId, string instanceId)
 {
     var updateRequest = new UpdateDeviceJobRequest{
         DeviceJobAction = DeviceJobAction.Cancel
     };
     var stopJobResponse = this.GetStorSimpleClient().DeviceJob.UpdateDeviceJob(deviceId, instanceId, updateRequest, this.GetCustomRequestHeaders());
     return stopJobResponse;
 }
        public void CanStopDeviceJobTest()
        {
            using (var context = UndoContext.Current)
            {
                context.Start();

                var client = GetServiceClient<StorSimpleManagementClient>();

                // Get all devices.
                var devices = client.Devices.List(GetCustomRequestHeaders());

                // Get hold of an online device.
                var onlineDevice = devices.LastOrDefault(device => device.Status == DeviceStatus.Online);
                if (onlineDevice == null)
                {
                    throw new ArgumentException("Need an online device to try scheduling and retrieving device jobs");
                }

                CreateSupportingVolumeAndBackupPolicy(onlineDevice);

                //Get hold of a backup policy 
                BackupPolicy policy;
                var policiesResponse = client.BackupPolicy.List(onlineDevice.DeviceId, GetCustomRequestHeaders());
                if (policiesResponse == null)
                {
                    policy = null;
                }
                else
                {
                    policy = policiesResponse.BackupPolicies.FirstOrDefault();
                }
                if (policy == null)
                {
                    throw new ArgumentException("The online device must have a backup policy to schedule and get device jobs");
                }

                // Schedule a backup
                var backupNowRequest = new BackupNowRequest();
                backupNowRequest.Type = BackupType.CloudSnapshot;
                var taskStatus = client.Backup.Create(onlineDevice.DeviceId, policy.InstanceId, backupNowRequest, GetCustomRequestHeaders());

                // Make sure the backup job was created
                Assert.NotNull(taskStatus);
                Assert.True(taskStatus.Status == AsyncTaskStatus.Completed);
                Assert.True(taskStatus.Result == AsyncTaskResult.Succeeded);

                // Get all device jobs and get hold of a job that is running
                var jobsResponse = client.DeviceJob.Get(onlineDevice.DeviceId, null, null, null, null, null, 0, 10, GetCustomRequestHeaders());
                Assert.NotNull(jobsResponse);
                var runningCancellableJob = jobsResponse.DeviceJobList.FirstOrDefault(x => x.Status.Equals("Running") && x.IsJobCancellable);
                if (runningCancellableJob == null)
                {
                    throw new ArgumentException("No running cancellable job available to stop");
                }

                var updateRequest = new UpdateDeviceJobRequest ();
                updateRequest.DeviceJobAction = DeviceJobAction.Cancel;
                var updateTaskStatus = client.DeviceJob.UpdateDeviceJob(runningCancellableJob.Device.InstanceId, runningCancellableJob.InstanceId, updateRequest, GetCustomRequestHeaders());

                //Assert the job status
                Assert.NotNull(updateTaskStatus);
                Assert.True(updateTaskStatus.Status == AsyncTaskStatus.Completed);
                Assert.True(updateTaskStatus.Result == AsyncTaskResult.Succeeded);
            }
        }
 /// <summary>
 /// Begin async task to update a device job.
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.StorSimple.IDeviceJobOperations.
 /// </param>
 /// <param name='deviceId'>
 /// Required. id of device
 /// </param>
 /// <param name='jobId'>
 /// Required. id of job to update
 /// </param>
 /// <param name='updateRequest'>
 /// Required. Request data, contains the action to be taken
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Required. The Custom Request Headers which client must use.
 /// </param>
 /// <returns>
 /// This is the Task Response for all Async Calls
 /// </returns>
 public static Task<GuidTaskResponse> BeginUpdateDeviceJobAsync(this IDeviceJobOperations operations, string deviceId, string jobId, UpdateDeviceJobRequest updateRequest, CustomRequestHeaders customRequestHeaders)
 {
     return operations.BeginUpdateDeviceJobAsync(deviceId, jobId, updateRequest, customRequestHeaders, CancellationToken.None);
 }
 /// <summary>
 /// Update a device job
 /// </summary>
 /// <param name='operations'>
 /// Reference to the
 /// Microsoft.WindowsAzure.Management.StorSimple.IDeviceJobOperations.
 /// </param>
 /// <param name='deviceId'>
 /// Required. id of device
 /// </param>
 /// <param name='jobId'>
 /// Required. id of job to update
 /// </param>
 /// <param name='updateRequest'>
 /// Required. Request data, contains the action to be taken
 /// </param>
 /// <param name='customRequestHeaders'>
 /// Required. The Custom Request Headers which client must use.
 /// </param>
 /// <returns>
 /// Info about the async task
 /// </returns>
 public static Task<TaskStatusInfo> UpdateDeviceJobAsync(this IDeviceJobOperations operations, string deviceId, string jobId, UpdateDeviceJobRequest updateRequest, CustomRequestHeaders customRequestHeaders)
 {
     return operations.UpdateDeviceJobAsync(deviceId, jobId, updateRequest, customRequestHeaders, CancellationToken.None);
 }