This class implements provider intialization based on workload and backup management type.
        public override void ExecuteCmdlet()
        {
            ConfirmAction(
                Force.IsPresent,
                string.Format(Resources.DisableProtectionWarning, Item.Name),
                Resources.DisableProtectionMessage,
                Item.Name, () =>
                {
                    ExecutionBlock(() =>
                    {
                        base.ExecuteCmdlet();
                        PsBackupProviderManager providerManager = 
                            new PsBackupProviderManager(new Dictionary<System.Enum, object>()
                        {
                            {ItemParams.Item, Item},
                            {ItemParams.DeleteBackupData, this.DeleteBackupData},
                        }, ServiceClientAdapter);

                        IPsBackupProvider psBackupProvider = 
                            providerManager.GetProviderInstance(Item.WorkloadType, 
                            Item.BackupManagementType);

                        var itemResponse = psBackupProvider.DisableProtection();

                        // Track Response and display job details

                        HandleCreatedJob(itemResponse, Resources.DisableProtectionOperation);
                    });
                });

        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                PsBackupProviderManager providerManager = new PsBackupProviderManager(
                    new Dictionary<System.Enum, object>()
                {  
                    {ContainerParams.ContainerType, ContainerType.Windows},                
                    {ContainerParams.Name, Name}
                }, ServiceClientAdapter);

                IPsBackupProvider psBackupProvider = 
                    providerManager.GetProviderInstanceForBackupManagementServer();

                var backupServerModels = psBackupProvider.ListBackupManagementServers();
                if (!string.IsNullOrEmpty(this.Name))
                {
                    if (backupServerModels != null)
                    {
                        backupServerModels = backupServerModels.Where(x => x.Name == this.Name).ToList();
                    }
                }
                WriteObject(backupServerModels, enumerateCollection: true);
            });
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                PsBackupProviderManager providerManager = 
                    new PsBackupProviderManager(new Dictionary<System.Enum, object>(), ServiceClientAdapter);

                IPsBackupProvider psBackupProvider = 
                    providerManager.GetProviderInstance(WorkloadType, BackupManagementType);
                WriteObject(psBackupProvider.GetDefaultRetentionPolicyObject());
            });
        }
        public override void ExecuteCmdlet()
        {
            base.ExecuteCmdlet();

            PsBackupProviderManager providerManager =
                new PsBackupProviderManager(new Dictionary<System.Enum, object>()
                {
                    {ItemParams.Item, Item},
                }, ServiceClientAdapter);

            IPsBackupProvider psBackupProvider =
                providerManager.GetProviderInstance(Item.WorkloadType, Item.BackupManagementType);
            var jobResponse = psBackupProvider.TriggerBackup();

            HandleCreatedJob(jobResponse, Resources.TriggerBackupOperation);
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                WriteDebug(string.Format("Input params - Policy: {0}" +
                          "RetentionPolicy:{1}, SchedulePolicy:{2}",
                          Policy == null ? "NULL" : Policy.ToString(),
                          RetentionPolicy == null ? "NULL" : RetentionPolicy.ToString(),
                          SchedulePolicy == null ? "NULL" : SchedulePolicy.ToString()));

                // Validate policy name
                PolicyCmdletHelpers.ValidateProtectionPolicyName(Policy.Name);

                // Validate if policy already exists               
                ProtectionPolicyResponse servicePolicy = PolicyCmdletHelpers.GetProtectionPolicyByName(
                                                                              Policy.Name, ServiceClientAdapter);
                if (servicePolicy == null)
                {
                    throw new ArgumentException(string.Format(Resources.PolicyNotFoundException, 
                        Policy.Name));
                }

                PsBackupProviderManager providerManager = new PsBackupProviderManager(
                    new Dictionary<System.Enum, object>()
                { 
                    {PolicyParams.ProtectionPolicy, Policy},
                    {PolicyParams.RetentionPolicy, RetentionPolicy},
                    {PolicyParams.SchedulePolicy, SchedulePolicy},                
                }, ServiceClientAdapter);

                IPsBackupProvider psBackupProvider = providerManager.GetProviderInstance(
                    Policy.WorkloadType,
                                                                                         Policy.BackupManagementType);                
                ProtectionPolicyResponse policyResponse = psBackupProvider.ModifyPolicy();
                WriteDebug("ModifyPolicy http response from service: " + 
                    policyResponse.StatusCode.ToString());

                if(policyResponse.StatusCode == System.Net.HttpStatusCode.Accepted)
                {
                    WriteDebug("Tracking operation status URL for completion: " +
                                policyResponse.AzureAsyncOperation);

                    // Track OperationStatus URL for operation completion
                    BackUpOperationStatusResponse operationResponse =  
                        TrackingHelpers.WaitForOperationCompletionUsingStatusLink(
                            policyResponse.AzureAsyncOperation,
                            ServiceClientAdapter.GetProtectionPolicyOperationStatusByURL);

                    WriteDebug("Final operation status: " + operationResponse.OperationStatus.Status);

                    if (operationResponse.OperationStatus.Properties != null &&
                       ((OperationStatusJobsExtendedInfo)operationResponse.OperationStatus.Properties).JobIds != null)
                    {
                        // get list of jobIds and return jobResponses                    
                        WriteObject(GetJobObject(
                            ((OperationStatusJobsExtendedInfo)operationResponse.OperationStatus.Properties).JobIds));
                    }

                    if (operationResponse.OperationStatus.Status == OperationStatusValues.Failed.ToString())
                    {
                        // if operation failed, then trace error and throw exception
                        if (operationResponse.OperationStatus.OperationStatusError != null)
                        {
                            WriteDebug(string.Format(
                                         "OperationStatus Error: {0} " +
                                         "OperationStatus Code: {1}",
                                         operationResponse.OperationStatus.OperationStatusError.Message,
                                         operationResponse.OperationStatus.OperationStatusError.Code));
                        }                                     
                    }
                }
                else
                {
                    // ServiceClient will return OK if NO datasources are associated with this policy
                    WriteDebug("No datasources are associated with Policy, http response code: " +
                                policyResponse.StatusCode.ToString());
                }
            });
        }
        public override void ExecuteCmdlet()
        {
           ExecutionBlock(() =>
           {
               base.ExecuteCmdlet();

               WriteDebug(string.Format("Input params - Name:{0}, WorkloadType:{1}, " +
                          "BackupManagementType: {2}, " +
                          "RetentionPolicy:{3}, SchedulePolicy:{4}",
                          Name, WorkloadType.ToString(),
                          BackupManagementType.HasValue ? BackupManagementType.ToString() : "NULL",
                          RetentionPolicy == null ? "NULL" : RetentionPolicy.ToString(),
                          SchedulePolicy == null ? "NULL" : SchedulePolicy.ToString()));

               // validate policy name
               PolicyCmdletHelpers.ValidateProtectionPolicyName(Name);

               // Validate if policy already exists               
               if (PolicyCmdletHelpers.GetProtectionPolicyByName(Name, ServiceClientAdapter) != null)
               {
                   throw new ArgumentException(string.Format(Resources.PolicyAlreadyExistException, Name));
               }

               PsBackupProviderManager providerManager = 
                   new PsBackupProviderManager(new Dictionary<System.Enum, object>()
               {  
                   {PolicyParams.PolicyName, Name},
                   {PolicyParams.WorkloadType, WorkloadType},                   
                   {PolicyParams.RetentionPolicy, RetentionPolicy},
                   {PolicyParams.SchedulePolicy, SchedulePolicy},                
               }, ServiceClientAdapter);

               IPsBackupProvider psBackupProvider = 
                   providerManager.GetProviderInstance(WorkloadType, BackupManagementType);
               psBackupProvider.CreatePolicy();

               WriteDebug("Successfully created policy, now fetching it from service: " + Name);

               // now get the created policy and return
               ServiceClientModel.ProtectionPolicyResponse policy = PolicyCmdletHelpers.GetProtectionPolicyByName(
                                                                           Name,
                                                                           ServiceClientAdapter);
               // now convert service Policy to PSObject
               WriteObject(ConversionHelpers.GetPolicyModel(policy.Item));
           });
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                //Validate start time < end time
                base.ExecuteCmdlet();

                // initialize values to default
                DateTime rangeEnd = DateTime.UtcNow;
                DateTime rangeStart = rangeEnd.AddDays(-30);
                
                Dictionary<System.Enum, object> parameter = new Dictionary<System.Enum, object>();
                parameter.Add(RecoveryPointParams.Item, Item);

                if (this.ParameterSetName == DateTimeFilterParameterSet ||
                    this.ParameterSetName == NoFilterParameterSet)
                {
                    // if both start and end date are given by user
                    if (StartDate.HasValue && EndDate.HasValue)
                    {
                        rangeStart = StartDate.Value;
                        rangeEnd = EndDate.Value;
                    }
                    // if only start date is given by user
                    else if (StartDate.HasValue && EndDate.HasValue == false)
                    {
                        rangeStart = StartDate.Value;
                        rangeEnd = rangeStart.AddDays(30);
                    }
                    // if only end date is given by user
                    else if (EndDate.HasValue && StartDate.HasValue == false)
                    {
                        rangeEnd = EndDate.Value;
                        rangeStart = rangeEnd.AddDays(-30);
                    }

                    //User want list of RPs between given time range
                    WriteDebug(String.Format("ParameterSet = DateTimeFilterParameterSet. \n" +
                        "StartDate = {0} EndDate = {1}, Item.Name = {2}, Item.ContainerName = {3}",
                        rangeStart, rangeEnd, Item.Name, Item.ContainerName));
                    if (rangeStart >= rangeEnd)
                    {
                        throw new ArgumentException(Resources.RecoveryPointEndDateShouldBeGreater); 
                    }

                    if (rangeStart.Kind != DateTimeKind.Utc || rangeEnd.Kind != DateTimeKind.Utc)
                    {
                        throw new ArgumentException(Resources.GetRPErrorInputDatesShouldBeInUTC);
                    }

                    parameter.Add(RecoveryPointParams.StartDate, rangeStart);
                    parameter.Add(RecoveryPointParams.EndDate, rangeEnd);
                    PsBackupProviderManager providerManager = 
                        new PsBackupProviderManager(parameter, ServiceClientAdapter);
                    IPsBackupProvider psBackupProvider = 
                        providerManager.GetProviderInstance(Item.ContainerType, Item.BackupManagementType);
                    var rpList = psBackupProvider.ListRecoveryPoints();

                    WriteDebug(String.Format("RPCount in Response = {0}", rpList.Count));
                    WriteObject(rpList, enumerateCollection: true);
                }
                else if (this.ParameterSetName == RecoveryPointIdParameterSet)
                {
                    //User want details of a particular recovery point
                    WriteDebug(String.Format("ParameterSet = DateTimeFilterParameterSet. \n" +
                        "StartDate = {0} EndDate = {1}, RPId = {2}, KeyFileDownloadLocation = {3}",
                        StartDate, EndDate, RecoveryPointId, KeyFileDownloadLocation));

                    parameter.Add(RecoveryPointParams.RecoveryPointId, RecoveryPointId);
                    parameter.Add(
                        RecoveryPointParams.KeyFileDownloadLocation, KeyFileDownloadLocation);
                    PsBackupProviderManager providerManager = 
                        new PsBackupProviderManager(parameter, ServiceClientAdapter);
                    IPsBackupProvider psBackupProvider = 
                        providerManager.GetProviderInstance(Item.ContainerType, Item.BackupManagementType);
                    WriteObject(psBackupProvider.GetRecoveryPointDetails());
                }
                else
                {
                    throw new Exception(Resources.RecoveryPointUnsupportedParamet);
                }
            });
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                PsBackupProviderManager providerManager = 
                    new PsBackupProviderManager(new Dictionary<System.Enum, object>()
                {
                    {ItemParams.Container, Container},
                    {ItemParams.BackupManagementType, BackupManagementType},
                    {ItemParams.AzureVMName, Name},
                    {ItemParams.ProtectionStatus, ProtectionStatus},
                    {ItemParams.ProtectionState, ProtectionState},
                    {ItemParams.WorkloadType, WorkloadType},
                }, ServiceClientAdapter);

                IPsBackupProvider psBackupProvider = null;

                if (this.ParameterSetName == GetItemsForVaultParamSet)
                {
                    psBackupProvider = 
                        providerManager.GetProviderInstance(WorkloadType, BackupManagementType);
                }
                else
                {
                    psBackupProvider = providerManager.GetProviderInstance(WorkloadType,
                    (Container as ManagementContext).BackupManagementType);
                }

                var itemModels = psBackupProvider.ListProtectedItems();

                WriteObject(itemModels, enumerateCollection: true);
            });
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                BackupManagementType? backupManagementTypeNullable = null;
                BackupManagementType backupManagementType;
                if (BackupManagementType != null)
                {
                    Enum.TryParse<BackupManagementType>(BackupManagementType, out backupManagementType);
                    backupManagementTypeNullable = backupManagementType;
                }

                PsBackupProviderManager providerManager = 
                    new PsBackupProviderManager(new Dictionary<System.Enum, object>()
                {  
                    {ContainerParams.ContainerType, ContainerType},
                    {ContainerParams.BackupManagementType, backupManagementTypeNullable},
                    {ContainerParams.Name, Name},
                    {ContainerParams.FriendlyName, FriendlyName},
                    {ContainerParams.ResourceGroupName, ResourceGroupName},
                    {ContainerParams.Status, Status},
                }, ServiceClientAdapter);

                IPsBackupProvider psBackupProvider = 
                    providerManager.GetProviderInstance(ContainerType, backupManagementTypeNullable);
                var containerModels = psBackupProvider.ListProtectionContainers();
                WriteObject(containerModels, enumerateCollection: true);
            });
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();
                StorageAccountName = StorageAccountName.ToLower();
                ResourceIdentity identity = new ResourceIdentity();
                identity.ResourceName = StorageAccountName;
                identity.ResourceProviderNamespace = "Microsoft.ClassicStorage/storageAccounts";
                identity.ResourceProviderApiVersion = "2015-12-01";
                identity.ResourceType = string.Empty;

                ResourcesNS.Models.ResourceGetResult resource = null;
                try
                {
                    WriteDebug(String.Format("Query Microsoft.ClassicStorage with name = {0}",
                        StorageAccountName));
                    resource = RmClient.Resources.GetAsync(StorageAccountResourceGroupName,
                        identity, CancellationToken.None).Result;
                }
                catch (Exception)
                {
                    identity.ResourceProviderNamespace = "Microsoft.Storage/storageAccounts";
                    identity.ResourceProviderApiVersion = "2016-01-01";
                    resource = RmClient.Resources.GetAsync(StorageAccountResourceGroupName,
                        identity, CancellationToken.None).Result;
                }

                string storageAccountId = resource.Resource.Id;
                string storageAccountlocation = resource.Resource.Location;
                string storageAccountType = resource.Resource.Type;

                WriteDebug(String.Format("StorageId = {0}", storageAccountId));

                PsBackupProviderManager providerManager = new PsBackupProviderManager(
                    new Dictionary<System.Enum, object>()
                {
                    {RestoreBackupItemParams.RecoveryPoint, RecoveryPoint},
                    {RestoreBackupItemParams.StorageAccountId, storageAccountId},
                    {RestoreBackupItemParams.StorageAccountLocation, storageAccountlocation},
                    {RestoreBackupItemParams.StorageAccountType, storageAccountType}
                }, ServiceClientAdapter);

                IPsBackupProvider psBackupProvider = providerManager.GetProviderInstance(
                    RecoveryPoint.WorkloadType, RecoveryPoint.BackupManagementType);
                var jobResponse = psBackupProvider.TriggerRestore();

                WriteDebug(String.Format("Restore submitted"));
                HandleCreatedJob(jobResponse, Resources.RestoreOperation);
            });
        }
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();
                
                PsBackupProviderManager providerManager = 
                    new PsBackupProviderManager(new Dictionary<System.Enum, object>()
                {  
                    {ItemParams.AzureVMName, Name},
                    {ItemParams.AzureVMCloudServiceName, ServiceName},
                    {ItemParams.AzureVMResourceGroupName, ResourceGroupName},
                    {ItemParams.Policy, Policy},
                    {ItemParams.Item, Item},
                    {ItemParams.ParameterSetName, this.ParameterSetName},
                }, ServiceClientAdapter);

                IPsBackupProvider psBackupProvider = (Item != null) ? 
                    providerManager.GetProviderInstance(Item.WorkloadType, Item.BackupManagementType)
                    : providerManager.GetProviderInstance(Policy.WorkloadType);

                var itemResponse = psBackupProvider.EnableProtection();

                // Track Response and display job details

                HandleCreatedJob(itemResponse, Resources.EnableProtectionOperation);
            });
        }