|
16 | 16 | using System.Management.Automation;
|
17 | 17 | using System.Collections.Generic;
|
18 | 18 | 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; |
19 | 23 |
|
20 | 24 | namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
|
21 | 25 | {
|
22 | 26 | /// <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. |
24 | 28 | /// </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 |
27 | 31 | {
|
| 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 | + |
28 | 60 | public override void ExecuteCmdlet()
|
29 | 61 | {
|
| 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 | + }); |
30 | 150 | }
|
31 | 151 | }
|
32 | 152 | }
|
|
0 commit comments