|
| 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.Collections.Generic; |
| 17 | +using System.Linq; |
| 18 | +using System.Management.Automation; |
| 19 | +using Microsoft.Azure.Management.SiteRecovery.Models; |
| 20 | + |
| 21 | +namespace Microsoft.Azure.Commands.SiteRecovery |
| 22 | +{ |
| 23 | + /// <summary> |
| 24 | + /// Retrieves Azure site Recovery Job. |
| 25 | + /// </summary> |
| 26 | + [Cmdlet(VerbsCommon.Get, "AzureSiteRecoveryJob", DefaultParameterSetName = ASRParameterSets.ByParam)] |
| 27 | + [OutputType(typeof(IEnumerable<ASRJob>))] |
| 28 | + public class GetAzureSiteRecoveryJob : SiteRecoveryCmdletBase |
| 29 | + { |
| 30 | + #region Parameters |
| 31 | + /// <summary> |
| 32 | + /// Gets or sets Job Name. |
| 33 | + /// </summary> |
| 34 | + [Parameter(ParameterSetName = ASRParameterSets.ByName, Mandatory = true)] |
| 35 | + [ValidateNotNullOrEmpty] |
| 36 | + public string Name { get; set; } |
| 37 | + |
| 38 | + /// <summary> |
| 39 | + /// Gets or sets Job Object. |
| 40 | + /// </summary> |
| 41 | + [Parameter(ParameterSetName = ASRParameterSets.ByObject, Mandatory = true, ValueFromPipeline = true)] |
| 42 | + [ValidateNotNullOrEmpty] |
| 43 | + public ASRJob Job { get; set; } |
| 44 | + |
| 45 | + ///// <summary> |
| 46 | + ///// Gets or sets start time. Allows to filter the list of jobs started after the given |
| 47 | + ///// start time. |
| 48 | + ///// </summary> |
| 49 | + //[Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "Represents start time of jobs to querying, jobs with the start time later than this will be returned")] |
| 50 | + //[ValidateNotNullOrEmpty] |
| 51 | + //public DateTime? StartTime { get; set; } |
| 52 | + |
| 53 | + ///// <summary> |
| 54 | + ///// Gets or sets end time. Allows to filter the list of jobs ended before it. |
| 55 | + ///// </summary> |
| 56 | + //[Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "Represents end time of jobs to query.")] |
| 57 | + //[ValidateNotNullOrEmpty] |
| 58 | + //public DateTime? EndTime { get; set; } |
| 59 | + |
| 60 | + ///// <summary> |
| 61 | + ///// Gets or sets target object id. |
| 62 | + ///// </summary> |
| 63 | + //[Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "ID of the object on which Job was targeted to.")] |
| 64 | + //[ValidateNotNullOrEmpty] |
| 65 | + //public string TargetObjectId { get; set; } |
| 66 | + |
| 67 | + /// <summary> |
| 68 | + /// Gets or sets state. Take string input for possible States of ASR Job. Use this parameter |
| 69 | + /// to get filtered view of Jobs |
| 70 | + /// </summary> |
| 71 | + /// Considered Valid states from WorkflowStatus enum in SRS (WorkflowData.cs) |
| 72 | + [Parameter(ParameterSetName = ASRParameterSets.ByParam, HelpMessage = "State of job to return.")] |
| 73 | + [ValidateNotNullOrEmpty] |
| 74 | + [ValidateSet( |
| 75 | + "NotStarted", |
| 76 | + "InProgress", |
| 77 | + "Succeeded", |
| 78 | + "Other", |
| 79 | + "Failed", |
| 80 | + "Cancelled", |
| 81 | + "Suspended")] |
| 82 | + public string State { get; set; } |
| 83 | + #endregion Parameters |
| 84 | + |
| 85 | + /// <summary> |
| 86 | + /// ProcessRecord of the command. |
| 87 | + /// </summary> |
| 88 | + public override void ExecuteCmdlet() |
| 89 | + { |
| 90 | + try |
| 91 | + { |
| 92 | + switch (this.ParameterSetName) |
| 93 | + { |
| 94 | + case ASRParameterSets.ByObject: |
| 95 | + this.Name = this.Job.Name; |
| 96 | + this.GetByName(); |
| 97 | + break; |
| 98 | + |
| 99 | + case ASRParameterSets.ByName: |
| 100 | + this.GetByName(); |
| 101 | + break; |
| 102 | + |
| 103 | + case ASRParameterSets.ByParam: |
| 104 | + default: |
| 105 | + this.GetByParam(); |
| 106 | + break; |
| 107 | + } |
| 108 | + } |
| 109 | + catch (Exception exception) |
| 110 | + { |
| 111 | + this.HandleException(exception); |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /// <summary> |
| 116 | + /// Queries by Name. |
| 117 | + /// </summary> |
| 118 | + private void GetByName() |
| 119 | + { |
| 120 | + this.WriteJob(RecoveryServicesClient.GetAzureSiteRecoveryJobDetails(this.Name).Job); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Queries by Parameters. |
| 125 | + /// </summary> |
| 126 | + private void GetByParam() |
| 127 | + { |
| 128 | + JobQueryParameter jqp = new JobQueryParameter(); |
| 129 | + |
| 130 | + //if (this.StartTime.HasValue) |
| 131 | + //{ |
| 132 | + // jqp.StartTime = |
| 133 | + // this.StartTime.Value.ToUniversalTime().ToBinary().ToString(); |
| 134 | + //} |
| 135 | + |
| 136 | + //if (this.EndTime.HasValue) |
| 137 | + //{ |
| 138 | + // jqp.EndTime = |
| 139 | + // this.EndTime.Value.ToUniversalTime().ToBinary().ToString(); |
| 140 | + //} |
| 141 | + |
| 142 | + //jqp.State = this.State; |
| 143 | + //jqp.ObjectId = this.TargetObjectId; |
| 144 | + |
| 145 | + this.WriteJobs(RecoveryServicesClient.GetAzureSiteRecoveryJob(jqp).Jobs); |
| 146 | + } |
| 147 | + |
| 148 | + /// <summary> |
| 149 | + /// Writes Job. |
| 150 | + /// </summary> |
| 151 | + /// <param name="job">JOB object</param> |
| 152 | + private void WriteJob(Microsoft.Azure.Management.SiteRecovery.Models.Job job) |
| 153 | + { |
| 154 | + this.WriteObject(new ASRJob(job)); |
| 155 | + } |
| 156 | + |
| 157 | + /// <summary> |
| 158 | + /// Writes Jobs. |
| 159 | + /// </summary> |
| 160 | + /// <param name="jobs">Job objects</param> |
| 161 | + private void WriteJobs(IList<Microsoft.Azure.Management.SiteRecovery.Models.Job> jobs) |
| 162 | + { |
| 163 | + this.WriteObject(jobs.Select(j => new ASRJob(j)), true); |
| 164 | + } |
| 165 | + } |
| 166 | +} |
0 commit comments