Skip to content

Commit 59aedd6

Browse files
committed
Implementation of Stop-Job cmdlet
1 parent 5934480 commit 59aedd6

File tree

7 files changed

+49
-9
lines changed

7 files changed

+49
-9
lines changed

src/ResourceManager/RecoveryServices.Backup/Cmdlets/Jobs/StopAzureRmRecoveryServicesJob.cs

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,11 @@
1515
using System;
1616
using System.Collections.Generic;
1717
using System.Management.Automation;
18+
using System.Net;
19+
using System.Threading;
1820
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
1921
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
22+
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
2023

2124
namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
2225
{
@@ -46,14 +49,23 @@ public override void ExecuteCmdlet()
4649
}
4750

4851
var cancelResponse = HydraAdapter.CancelJob(JobId);
52+
string opId = HydraHelpers.GetLastIdFromFullId(cancelResponse.Location);
53+
var cancelStatus = HydraAdapter.GetJobOperationStatus(JobId, opId);
4954

50-
while (cancelResponse.StatusCode == System.Net.HttpStatusCode.Accepted)
55+
while (cancelStatus.StatusCode == HttpStatusCode.Accepted)
5156
{
52-
57+
Thread.Sleep(15 * 1000);
58+
cancelStatus = HydraAdapter.GetJobOperationStatus(JobId, opId);
5359
}
5460

55-
var adapterResponse = HydraAdapter.GetJob(JobId);
56-
WriteObject(JobConversions.GetPSJob(adapterResponse));
61+
if (cancelStatus.StatusCode != HttpStatusCode.NoContent)
62+
{
63+
throw new Exception(string.Format(Resources.JobCouldNotCancelJob, cancelStatus.StatusCode.ToString()));
64+
}
65+
else
66+
{
67+
WriteObject(JobConversions.GetPSJob(cancelStatus.Item));
68+
}
5769
});
5870
}
5971
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Helpers/Conversions/JobConversions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ private static AzureRmRecoveryServicesAzureVmJob GetPSAzureVmJob(JobResource hyd
8686
response = new AzureRmRecoveryServicesAzureVmJob();
8787
}
8888

89-
response.InstanceId = GetJobIdFromFullId(hydraJob.Id);
89+
response.InstanceId = GetLastIdFromFullId(hydraJob.Id);
9090
response.StartTime = vmJob.StartTime.ToLocalTime();
9191
if (vmJob.EndTime > new DateTime(2000, 1, 1))
9292
{
@@ -160,7 +160,7 @@ private static AzureRmRecoveryServicesAzureVmJobErrorInfo GetPSAzureVmErrorInfo(
160160
return psErrorInfo;
161161
}
162162

163-
private static string GetJobIdFromFullId(string fullId)
163+
public static string GetLastIdFromFullId(string fullId)
164164
{
165165
string[] splitArr = fullId.Split("/".ToCharArray());
166166
return splitArr[splitArr.Length - 1];

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Helpers/HydraHelpers.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,20 @@ public static void GetSkipTokenFromNextLink(string url, out string nextLink)
7575
}
7676
}
7777

78+
/// <summary>
79+
/// Use this function to get the last part of a URL.
80+
/// Generally this is the ID of object or OperationId.
81+
/// Note: This doesn't work if the string has any extra characters
82+
/// after slash. (CSM ID's generally don't have)
83+
/// </summary>
84+
/// <param name="url"></param>
85+
/// <returns></returns>
86+
public static string GetLastIdFromFullId(string fullId)
87+
{
88+
string[] splitArr = fullId.Split("/".ToCharArray());
89+
return splitArr[splitArr.Length - 1];
90+
}
91+
7892
public static string GetHydraContainerType(CmdletModel.ContainerType containerType)
7993
{
8094
string hydraContainerType = string.Empty;

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.HydraAdapter/BMSAPIs/ContainerAPIs.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,5 @@ public BaseRecoveryServicesJobResponse RefreshContainers()
4949
BmsAdapter.GetCustomRequestHeaders(), AzureFabricName, BmsAdapter.CmdletCancellationToken).Result;
5050
return response;
5151
}
52-
5352
}
5453
}

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/Properties/Resources.Designer.cs

Lines changed: 9 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/ResourceManager/RecoveryServices.Backup/Commands.RecoveryServices.Backup.Models/Properties/Resources.resx

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -267,6 +267,10 @@
267267
<data name="JobAllowedDateTimeRangeExceeded" xml:space="preserve">
268268
<value>To filter should not be more than 30 days away from From filter.</value>
269269
</data>
270+
<data name="JobCouldNotCancelJob" xml:space="preserve">
271+
<value>Failed to cancel the job. Error code: {0}</value>
272+
<comment> </comment>
273+
</data>
270274
<data name="JobJobIdAndJobMismatch" xml:space="preserve">
271275
<value>JobID and Job object provided don't match each other.</value>
272276
</data>

src/ResourceManager/RecoveryServices.Backup/RecoveryServicesBackupCmdletBase.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets
3838
/// </summary>
3939
public abstract class RecoveryServicesBackupCmdletBase : AzureRMCmdlet
4040
{
41+
public delegate BackUpOperationStatusResponse GetOpResponse(string url);
42+
4143
// in seconds
4244
private int _defaultSleepForOperationTracking = 15;
4345

@@ -142,12 +144,12 @@ public List<AzureRmRecoveryServicesJobBase> GetJobObject(List<string> jobIds)
142144
return result;
143145
}
144146

145-
public BackUpOperationStatusResponse WaitForOperation(string url)
147+
public BackUpOperationStatusResponse WaitForOperation(string url, GetOpResponse hydraFunc)
146148
{
147149
// using this directly because it doesn't matter which function we use.
148150
// return type is same and currently we are using it in only two places.
149151
// protected item and policy.
150-
BackUpOperationStatusResponse response = HydraAdapter.GetProtectedItemOperationStatusByURL(url);
152+
BackUpOperationStatusResponse response = hydraFunc(url);
151153

152154
while (response.OperationStatus.Status == OperationStatusValues.InProgress.ToString())
153155
{

0 commit comments

Comments
 (0)