Skip to content

Commit cbe486c

Browse files
committed
Initial changes for jobs commandlets
1 parent 10e77e3 commit cbe486c

File tree

8 files changed

+656
-4
lines changed

8 files changed

+656
-4
lines changed

src/ResourceManager/AzureBackup/Commands.AzureBackup/AzureBackupCmdletHelpMessage.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,5 +25,27 @@ internal static class AzureBackupCmdletHelpMessage
2525
public const string ContainerId = "The container ID.";
2626
public const string ContainerRegistrationStatus = "The container registration status.";
2727
public const string ContainerType = "The container type.";
28+
29+
public const string JobFilterJobIdHelpMessage = "The job ID to filter jobs";
30+
public const string JobFilterJobHelpMessage = "The job for filtering";
31+
public const string JobFilterStartTimeHelpMessage = "The start time of query range";
32+
public const string JobFilterEndTimeHelpMessage = "The end time of query range";
33+
public const string JobFilterOperationHelpMessage = "The operation for filtering jos";
34+
public const string JobFilterStatusHelpMessage = "The status for filtering jobs";
35+
public const string JobFitlerVaultHelpMessage = "The vault of which jobs have to be filtered";
36+
public const string JobFilterTypeHelpMessage = "The type of workload whose jobs have to be filtered";
37+
38+
public const string JobDetailsFilterJobIdHelpMessage = "The job ID to get details";
39+
public const string JobDetailsFilterVaultHelpMessage = "The vault of which job details should be fetched";
40+
public const string JobDetailsFilterJobHelpMessage = "The job whose full details should be fetched";
41+
42+
public const string StopJobFilterJobIdHelpMessage = "The job ID to stop job";
43+
public const string StopJobFilterVaultHelpMessage = "The vault of which a job has to be stopped";
44+
public const string StopJobFilterJobHelpMessage = "The job which should be stopped";
45+
46+
public const string WaitJobFilterJobIdHelpMessage = "The job ID to stop job";
47+
public const string WaitJobFilterVaultHelpMessage = "The vault of which a job has to be stopped";
48+
public const string WaitJobFilterJobHelpMessage = "The job which should be stopped";
49+
public const string WaitJobFilterTimeoutHelpMessage = "Maximum number of seconds for which cmdlet should wait before job(s) complete running.";
2850
}
2951
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Management.Automation;
17+
using System.Collections.Generic;
18+
using System.Xml;
19+
using System.Linq;
20+
using Mgmt = Microsoft.Azure.Management.BackupServices.Models;
21+
22+
23+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
24+
{
25+
public enum JobStatus
26+
{
27+
Invalid = 0,
28+
29+
InProgress,
30+
31+
Completed,
32+
33+
Failed,
34+
35+
CompletedWithWarnings,
36+
37+
Cancelled,
38+
39+
Cancelling
40+
}
41+
42+
public enum JobOperationType
43+
{
44+
Invalid = 0,
45+
46+
Register,
47+
48+
ConfigureBackup,
49+
50+
Backup,
51+
52+
Restore,
53+
54+
UnProtect,
55+
56+
DeleteBackupData,
57+
58+
Unregister
59+
}
60+
61+
public static class AzureBackupJobHelper
62+
{
63+
public static bool IsValidStatus(string inputStatus)
64+
{
65+
JobStatus status;
66+
if(!Enum.TryParse<JobStatus>(inputStatus, out status) || status == JobStatus.Invalid)
67+
{
68+
return false;
69+
}
70+
return true;
71+
}
72+
73+
public static bool IsValidOperationType(string inputOperationType)
74+
{
75+
JobOperationType operationType;
76+
if(!Enum.TryParse<JobOperationType>(inputOperationType, out operationType) || operationType == JobOperationType.Invalid)
77+
{
78+
return false;
79+
}
80+
return true;
81+
}
82+
83+
public static bool IsJobRunning(string status)
84+
{
85+
return ((status.CompareTo("InProgress") == 0) || (status.CompareTo("Cancelling") == 0));
86+
}
87+
}
88+
}

src/ResourceManager/AzureBackup/Commands.AzureBackup/Cmdlets/Jobs/GetAzureBackupJob.cs

Lines changed: 123 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,17 +16,137 @@
1616
using System.Management.Automation;
1717
using System.Collections.Generic;
1818
using System.Xml;
19+
using System.Linq;
20+
using System.Web;
21+
using Microsoft.Azure.Management.BackupServices;
22+
using Mgmt = Microsoft.Azure.Management.BackupServices.Models;
1923

2024
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2125
{
2226
/// <summary>
23-
/// Get list of containers
27+
/// Get list of jobs pertaining to the filters specified. Gets list of all jobs created in the last 24 hours if no filters are specified.
2428
/// </summary>
25-
[Cmdlet(VerbsCommon.Get, "AzureBackupJob"), OutputType(typeof(string))]
26-
public class GetAzureBackupJob : AzureBackupCmdletBase
29+
[Cmdlet(VerbsCommon.Get, "AzureBackupJob"), OutputType(typeof(List<Mgmt.Job>), typeof(Mgmt.Job))]
30+
public class GetAzureBackupJob : AzureBackupVaultCmdletBase
2731
{
32+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.JobFilterJobIdHelpMessage)]
33+
[ValidateNotNullOrEmpty]
34+
public string JobId { get; set; }
35+
36+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.JobFilterJobHelpMessage)]
37+
[ValidateNotNull]
38+
public AzureBackupJob Job { get; set; }
39+
40+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.JobFilterStartTimeHelpMessage)]
41+
[ValidateNotNull]
42+
public DateTime? StartTime { get; set; }
43+
44+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.JobFilterEndTimeHelpMessage)]
45+
[ValidateNotNull]
46+
public DateTime? EndTime { get; set; }
47+
48+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.JobFilterStatusHelpMessage)]
49+
[ValidateSet("Cancelled", "Cancelling", "Completed", "CompletedWithWarnings", "Failed", "InProgress")]
50+
public string Status { get; set; }
51+
52+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.JobFilterTypeHelpMessage)]
53+
[ValidateSet("VM")]
54+
public string Type { get; set; }
55+
56+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.JobFilterOperationHelpMessage)]
57+
[ValidateSet("Backup", "ConfigureBackup", "DeleteBackupData", "Register", "Restore", "UnProtect", "Unregister")]
58+
public string Operation { get; set; }
59+
2860
public override void ExecuteCmdlet()
2961
{
62+
if (Job != null)
63+
{
64+
this.ResourceGroupName = Job.ResourceGroupName;
65+
this.ResourceName = Job.ResourceName;
66+
}
67+
68+
base.ExecuteCmdlet();
69+
70+
ExecutionBlock(() =>
71+
{
72+
//if (Job != null && JobId != null)
73+
//{
74+
// throw new Exception("Please use either JobID filter or Job filter but not both.");
75+
//}
76+
77+
if (Job != null)
78+
{
79+
JobId = Job.InstanceId;
80+
}
81+
82+
// validations
83+
if (!StartTime.HasValue)
84+
{
85+
WriteDebug("Setting StartTime to min value.");
86+
StartTime = new DateTime();
87+
StartTime = DateTime.MinValue;
88+
}
89+
90+
if (EndTime.HasValue && EndTime.Value <= StartTime.Value)
91+
{
92+
throw new Exception("StartTime should be greater than EndTime.");
93+
}
94+
else
95+
{
96+
if (StartTime != DateTime.MinValue)
97+
{
98+
WriteDebug("End time not set. Setting it to current time.");
99+
EndTime = DateTime.Now;
100+
}
101+
else
102+
{
103+
WriteDebug("Setting EndTime to min value.");
104+
EndTime = new DateTime();
105+
EndTime = DateTime.MinValue;
106+
}
107+
}
108+
109+
StartTime = TimeZoneInfo.ConvertTimeToUtc(StartTime.Value);
110+
EndTime = TimeZoneInfo.ConvertTimeToUtc(EndTime.Value);
111+
112+
// if user hasn't specified any filters, then default filter fetches
113+
// all jobs that were created in last 24 hours.
114+
if (StartTime == DateTime.MinValue && EndTime == DateTime.MinValue &&
115+
Operation == string.Empty && Status == string.Empty &&
116+
Type == string.Empty && JobId == string.Empty)
117+
{
118+
StartTime = DateTime.UtcNow.AddDays(-1);
119+
EndTime = DateTime.UtcNow;
120+
}
121+
122+
WriteDebug("StartTime filter is: " + System.Uri.EscapeDataString(StartTime.Value.ToString("yyyy-MM-dd hh:mm:ss tt")));
123+
WriteDebug("EndTime filter is: " + System.Uri.EscapeDataString(EndTime.Value.ToString("yyyy-MM-dd hh:mm:ss tt")));
124+
WriteDebug("Operation filter is: " + Operation);
125+
WriteDebug("Status filter is: " + Status);
126+
WriteDebug("Type filter is: " + Type);
127+
WriteDebug("JobID filter is: " + JobId);
128+
129+
JobQueryParameter queryParams = new JobQueryParameter()
130+
{
131+
StartTime = StartTime.Value.ToString("yyyy-MM-dd hh:mm:ss tt"),
132+
EndTime = EndTime.Value.ToString("yyyy-MM-dd hh:mm:ss tt"),
133+
Operation = Operation,
134+
Status = Status,
135+
Type = Type,
136+
JobId = JobId
137+
};
138+
139+
Mgmt.JobListResponse jobsList = AzureBackupClient.Job.ListAsync(queryParams, GetCustomRequestHeaders(), CmdletCancellationToken).Result;
140+
List<AzureBackupJob> retrievedJobs = new List<AzureBackupJob>();
141+
142+
foreach (Mgmt.Job serviceJob in jobsList.Jobs)
143+
{
144+
retrievedJobs.Add(new AzureBackupJob(serviceJob, ResourceGroupName, ResourceName));
145+
}
146+
147+
WriteDebug("Successfully retrieved all jobs. Number of jobs retrieved: " + retrievedJobs.Count());
148+
WriteObject(retrievedJobs);
149+
});
30150
}
31151
}
32152
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// ----------------------------------------------------------------------------------
2+
//
3+
// Copyright Microsoft Corporation
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
// Unless required by applicable law or agreed to in writing, software
9+
// distributed under the License is distributed on an "AS IS" BASIS,
10+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
11+
// See the License for the specific language governing permissions and
12+
// limitations under the License.
13+
// ----------------------------------------------------------------------------------
14+
15+
using System;
16+
using System.Management.Automation;
17+
using System.Collections.Generic;
18+
using System.Xml;
19+
using System.Linq;
20+
using Mgmt = Microsoft.Azure.Management.BackupServices.Models;
21+
22+
namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
23+
{
24+
/// <summary>
25+
/// Get full details of a job
26+
/// </summary>
27+
[Cmdlet(VerbsCommon.Get, "AzureBackupJobDetails"), OutputType(typeof(Mgmt.JobProperties))]
28+
public class GetAzureBackupJobDetils : AzureBackupVaultCmdletBase
29+
{
30+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.JobDetailsFilterJobIdHelpMessage)]
31+
[ValidateNotNullOrEmpty]
32+
public string JobID { get; set; }
33+
34+
[Parameter(Mandatory = false, HelpMessage = AzureBackupCmdletHelpMessage.JobDetailsFilterJobHelpMessage)]
35+
[ValidateNotNull]
36+
public AzureBackupJob Job { get; set; }
37+
38+
public override void ExecuteCmdlet()
39+
{
40+
if (Job != null)
41+
{
42+
this.ResourceGroupName = Job.ResourceGroupName;
43+
this.ResourceName = Job.ResourceName;
44+
}
45+
46+
base.ExecuteCmdlet();
47+
48+
ExecutionBlock(() =>
49+
{
50+
//if (Job != null && JobID != null)
51+
//{
52+
// throw new Exception("Please use either JobID filter or Job filter but not both.");
53+
//}
54+
55+
if (Job != null)
56+
{
57+
JobID = Job.InstanceId;
58+
}
59+
60+
WriteDebug("JobID filter is: " + JobID);
61+
62+
Mgmt.JobProperties serviceJobProperties = AzureBackupClient.Job.GetAsync(JobID, GetCustomRequestHeaders(), CmdletCancellationToken).Result.Job;
63+
AzureBackupJobDetails jobDetails = new AzureBackupJobDetails(serviceJobProperties, ResourceGroupName, ResourceName);
64+
65+
WriteDebug("Retrieved JobDetails from service.");
66+
WriteObject(jobDetails);
67+
});
68+
}
69+
}
70+
}

0 commit comments

Comments
 (0)