public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                if (Job != null)
                {
                    Vault = new AzureRMBackupVault(Job.ResourceGroupName, Job.ResourceName, Job.Location);
                }

                InitializeAzureBackupCmdlet(Vault);

                if (Job != null)
                {
                    JobId = Job.InstanceId;
                }

                if (Type != null)
                {
                    Type = AzureBackupJobHelper.GetTypeForService(Type);
                }

                // validations
                if (!From.HasValue)
                {
                    if (To.HasValue)
                    {
                        throw new Exception(Resources.AzureBackupJobArguementException);
                    }
                    WriteDebug(Resources.SettingStartTime);
                    From = AzureBackupJobHelper.MinimumAllowedDate;
                }

                if (To.HasValue && To.Value > From.Value)
                {
                    // everything is good. don't do anything
                }
                else if (To.HasValue && To.Value <= From.Value)
                {
                    throw new Exception(Resources.TimeFilterNotCorrect);
                }
                else
                {
                    if (From != AzureBackupJobHelper.MinimumAllowedDate)
                    {
                        WriteDebug(Resources.EndTimeNotSet);
                        To = DateTime.Now;
                    }
                    else
                    {
                        WriteDebug(Resources.SettingEndTime);
                        To = AzureBackupJobHelper.MinimumAllowedDate;
                    }
                }

                From = TimeZoneInfo.ConvertTimeToUtc(From.Value);
                To   = TimeZoneInfo.ConvertTimeToUtc(To.Value);

                // if user hasn't specified any filters, then default filter fetches
                // all jobs that were created in last 24 hours.
                if (From == AzureBackupJobHelper.MinimumAllowedDate && To == AzureBackupJobHelper.MinimumAllowedDate &&
                    string.IsNullOrEmpty(Operation) && string.IsNullOrEmpty(Status) &&
                    string.IsNullOrEmpty(Type) && string.IsNullOrEmpty(JobId))
                {
                    From = DateTime.UtcNow.AddDays(-1);
                    To   = DateTime.UtcNow;
                }

                DateTimeFormatInfo format = new CultureInfo("en-US").DateTimeFormat;
                WriteDebug(String.Format(Resources.StartTimeFilter, System.Uri.EscapeDataString(From.Value.ToString("yyyy-MM-dd hh:mm:ss tt", format))));
                WriteDebug(String.Format(Resources.EndTimeFilter, System.Uri.EscapeDataString(To.Value.ToString("yyyy-MM-dd hh:mm:ss tt", format))));
                WriteDebug(String.Format(Resources.OperationFilter, Operation));
                WriteDebug(String.Format(Resources.StatusFilter, Status));
                WriteDebug(String.Format(Resources.TypeFilter, Type));
                WriteDebug(String.Format(Resources.JobIdFilter, JobId));

                Mgmt.CSMJobQueryObject queryParams = new Mgmt.CSMJobQueryObject()
                {
                    StartTime    = From.Value.ToString("yyyy-MM-dd hh:mm:ss tt", format),
                    EndTime      = To.Value.ToString("yyyy-MM-dd hh:mm:ss tt", format),
                    Operation    = Operation,
                    Status       = Status,
                    WorkloadType = Type,
                    Name         = JobId
                };

                var jobsList = AzureBackupClient.ListJobs(Vault.ResourceGroupName, Vault.Name, queryParams);
                List <AzureRMBackupJob> retrievedJobs = new List <AzureRMBackupJob>();

                foreach (Mgmt.CSMJobResponse serviceJob in jobsList)
                {
                    // TODO: Initialize vault from Job object when vault is made optional
                    retrievedJobs.Add(new AzureRMBackupJob(Vault, serviceJob.Properties, serviceJob.Name));
                }

                WriteDebug(String.Format(Resources.JobRetrieved, retrievedJobs.Count()));

                if (retrievedJobs.Count == 1)
                {
                    WriteObject(retrievedJobs.First());
                }
                else
                {
                    WriteObject(retrievedJobs);
                }
            });
        }
Example #2
0
        public override void ExecuteCmdlet()
        {
            List <string>      specifiedJobs = new List <string>();
            AzureRMBackupVault Vault         = null;

            if (Job != null)
            {
                if ((Job is PSObject) && (((PSObject)Job).ImmediateBaseObject is List <AzureRMBackupJob>))
                {
                    foreach (AzureRMBackupJob jobToWait in (((PSObject)Job).ImmediateBaseObject as List <AzureRMBackupJob>))
                    {
                        Vault = new AzureRMBackupVault(jobToWait.ResourceGroupName, jobToWait.ResourceName, jobToWait.Location);
                        specifiedJobs.Add(jobToWait.InstanceId);
                    }
                }
                else if (Job is List <AzureRMBackupJob> )
                {
                    WriteDebug(Resources.AzureBackupJobInputType);
                    foreach (AzureRMBackupJob jobToWait in (Job as List <AzureRMBackupJob>))
                    {
                        Vault = new AzureRMBackupVault(jobToWait.ResourceGroupName, jobToWait.ResourceName, jobToWait.Location);
                        specifiedJobs.Add(jobToWait.InstanceId);
                    }
                }
                else if ((Job is PSObject) && (((PSObject)Job).ImmediateBaseObject is AzureRMBackupJob))
                {
                    AzureRMBackupJob azureJob = ((Job as PSObject).ImmediateBaseObject as AzureRMBackupJob);
                    Vault = new AzureRMBackupVault(azureJob.ResourceGroupName, azureJob.ResourceName, azureJob.Location);
                    specifiedJobs.Add(azureJob.InstanceId);
                }
                else if (Job is AzureRMBackupJob)
                {
                    Vault = new AzureRMBackupVault((Job as AzureRMBackupJob).ResourceGroupName, (Job as AzureRMBackupJob).ResourceName, (Job as AzureRMBackupJob).Location);
                    specifiedJobs.Add((Job as AzureRMBackupJob).InstanceId);
                }
                else if ((Job is PSObject) && (((PSObject)Job).ImmediateBaseObject is AzureRMBackupJobDetails))
                {
                    AzureRMBackupJob azureJob = ((Job as PSObject).ImmediateBaseObject as AzureRMBackupJobDetails);
                    Vault = new AzureRMBackupVault(azureJob.ResourceGroupName, azureJob.ResourceName, azureJob.Location);
                    specifiedJobs.Add(azureJob.InstanceId);
                }
                else if (Job is AzureRMBackupJobDetails)
                {
                    Vault = new AzureRMBackupVault((Job as AzureRMBackupJobDetails).ResourceGroupName, (Job as AzureRMBackupJobDetails).ResourceName, (Job as AzureRMBackupJobDetails).Location);
                    specifiedJobs.Add((Job as AzureRMBackupJobDetails).InstanceId);
                }
            }

            WriteDebug(String.Format(Resources.NumberOfJobsForWaiting, specifiedJobs.Count));

            if (specifiedJobs.Count == 0)
            {
                WriteDebug(Resources.QuittingWaitJob);
                return;
            }

            InitializeAzureBackupCmdlet(Vault);

            ExecutionBlock(() =>
            {
                if (!TimeOut.HasValue)
                {
                    TimeOut = Int64.MaxValue;
                }

                List <string> pendingJobs = new List <string>(specifiedJobs);
                DateTime waitingStartTime = DateTime.UtcNow;

                while (true)
                {
                    WriteDebug(Resources.QueryingJobs);

                    if (DateTime.UtcNow.Subtract(waitingStartTime).TotalSeconds >= TimeOut)
                    {
                        WriteDebug(Resources.TimeOutWaitInJob);
                        break;
                    }

                    bool areJobsRunning = false;

                    for (int i = 0; i < pendingJobs.Count; i++)
                    {
                        Mgmt.CSMJobDetailsResponse retrievedJob = AzureBackupClient.GetJobDetails(Vault.ResourceGroupName, Vault.Name, pendingJobs[i]);
                        if (AzureBackupJobHelper.IsJobRunning(retrievedJob.JobDetailedProperties.Status))
                        {
                            areJobsRunning = true;
                        }
                        else
                        {
                            pendingJobs.RemoveAt(i);
                            i--;
                        }
                    }

                    if (!areJobsRunning)
                    {
                        WriteDebug(Resources.AllJobsCompleted);
                        break;
                    }

                    TestMockSupport.Delay(30 * 1000);
                }

                IList <AzureRMBackupJob> finalJobs = new List <AzureRMBackupJob>();
                foreach (string jobId in specifiedJobs)
                {
                    Mgmt.CSMJobDetailsResponse retrievedJob = AzureBackupClient.GetJobDetails(Vault.ResourceGroupName, Vault.Name, jobId);
                    finalJobs.Add(new AzureRMBackupJob(Vault, retrievedJob.JobDetailedProperties, retrievedJob.Name));
                }

                if (finalJobs.Count == 1)
                {
                    WriteObject(finalJobs.First());
                }
                else
                {
                    WriteObject(finalJobs);
                }
            });
        }