GetProtectionPolicyByName() public static method

Fetches policies by name
public static GetProtectionPolicyByName ( string policyName, ServiceClientAdapter serviceClientAdapter ) : Microsoft.Azure.Management.RecoveryServices.Backup.Models.ProtectionPolicyResponse
policyName string Name of the policy to be fetched
serviceClientAdapter Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.ServiceClientAdapterNS.ServiceClientAdapter Service client adapter with which to make calls
return Microsoft.Azure.Management.RecoveryServices.Backup.Models.ProtectionPolicyResponse
コード例 #1
0
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId);
                string vaultName         = resourceIdentifier.ResourceName;
                string resourceGroupName = resourceIdentifier.ResourceGroupName;

                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,
                        vaultName: vaultName,
                        resourceGroupName: resourceGroupName) != null)
                {
                    throw new ArgumentException(string.Format(Resources.PolicyAlreadyExistException, Name));
                }

                Dictionary <Enum, object> providerParameters = new Dictionary <Enum, object>();
                providerParameters.Add(VaultParams.VaultName, vaultName);
                providerParameters.Add(VaultParams.ResourceGroupName, resourceGroupName);
                providerParameters.Add(PolicyParams.PolicyName, Name);
                providerParameters.Add(PolicyParams.WorkloadType, WorkloadType);
                providerParameters.Add(PolicyParams.RetentionPolicy, RetentionPolicy);
                providerParameters.Add(PolicyParams.SchedulePolicy, SchedulePolicy);

                PsBackupProviderManager providerManager = new PsBackupProviderManager(providerParameters, 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.ProtectionPolicyResource policy = PolicyCmdletHelpers.GetProtectionPolicyByName(
                    Name,
                    ServiceClientAdapter,
                    vaultName: vaultName,
                    resourceGroupName: resourceGroupName);

                // now convert service Policy to PSObject
                WriteObject(ConversionHelpers.GetPolicyModel(policy));
            }, ShouldProcess(Name, VerbsCommon.New));
        }
        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));
            });
        }
コード例 #3
0
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId);
                string vaultName         = resourceIdentifier.ResourceName;
                string resourceGroupName = resourceIdentifier.ResourceGroupName;

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

                if (ParameterSetName == PolicyNameParamSet)
                {
                    // validate policyName
                    PolicyCmdletHelpers.ValidateProtectionPolicyName(Name);

                    // query service
                    ServiceClientModel.ProtectionPolicyResource policy =
                        PolicyCmdletHelpers.GetProtectionPolicyByName(
                            Name,
                            ServiceClientAdapter,
                            vaultName: vaultName,
                            resourceGroupName: resourceGroupName);
                    if (policy == null)
                    {
                        throw new ArgumentException(string.Format(Resources.PolicyNotFoundException, Name));
                    }

                    WriteObject(ConversionHelpers.GetPolicyModel(policy));
                }
                else
                {
                    List <PolicyBase> policyList     = new List <PolicyBase>();
                    string serviceClientProviderType = null;

                    switch (this.ParameterSetName)
                    {
                    case WorkloadParamSet:
                        if (WorkloadType == Models.WorkloadType.AzureVM)
                        {
                            serviceClientProviderType =
                                ServiceClientHelpers.GetServiceClientProviderType(Models.WorkloadType.AzureVM);
                        }
                        else if (WorkloadType == Models.WorkloadType.AzureSQLDatabase)
                        {
                            serviceClientProviderType = ServiceClientHelpers.GetServiceClientProviderType(Models.WorkloadType.AzureSQLDatabase);
                        }
                        else if (WorkloadType == Models.WorkloadType.AzureFiles)
                        {
                            serviceClientProviderType = ServiceClientHelpers.GetServiceClientProviderType(Models.WorkloadType.AzureFiles);
                        }
                        else if (WorkloadType == Models.WorkloadType.MSSQL)
                        {
                            serviceClientProviderType = ServiceClientHelpers.GetServiceClientProviderType(Models.WorkloadType.MSSQL);
                        }
                        break;

                    case WorkloadBackupMangementTypeParamSet:
                        if (WorkloadType == Models.WorkloadType.AzureVM)
                        {
                            if (BackupManagementType != Models.BackupManagementType.AzureVM)
                            {
                                throw new ArgumentException(
                                    Resources.AzureVMUnsupportedBackupManagementTypeException);
                            }
                            serviceClientProviderType = ServiceClientHelpers.
                                                        GetServiceClientProviderType(Models.WorkloadType.AzureVM);
                        }
                        else if (WorkloadType == Models.WorkloadType.AzureSQLDatabase)
                        {
                            if (BackupManagementType != Models.BackupManagementType.AzureSQL)
                            {
                                throw new ArgumentException(
                                    Resources.AzureSqlUnsupportedBackupManagementTypeException);
                            }
                            serviceClientProviderType =
                                ServiceClientHelpers.GetServiceClientProviderType(
                                    Models.WorkloadType.AzureSQLDatabase);
                        }
                        else if (WorkloadType == Models.WorkloadType.AzureFiles)
                        {
                            if (BackupManagementType != Models.BackupManagementType.AzureStorage)
                            {
                                throw new ArgumentException(
                                    Resources.AzureFileUnsupportedBackupManagementTypeException);
                            }
                            serviceClientProviderType =
                                ServiceClientHelpers.GetServiceClientProviderType(
                                    Models.WorkloadType.AzureFiles);
                        }
                        else if (WorkloadType == Models.WorkloadType.AzureFiles)
                        {
                            if (BackupManagementType != Models.BackupManagementType.AzureStorage)
                            {
                                throw new ArgumentException(
                                    Resources.AzureFileUnsupportedBackupManagementTypeException);
                            }
                            serviceClientProviderType =
                                ServiceClientHelpers.GetServiceClientProviderType(
                                    Models.WorkloadType.AzureFiles);
                        }
                        else if (WorkloadType == Models.WorkloadType.MSSQL)
                        {
                            if (BackupManagementType != Models.BackupManagementType.AzureWorkload)
                            {
                                throw new ArgumentException(
                                    Resources.AzureFileUnsupportedBackupManagementTypeException);
                            }
                            serviceClientProviderType =
                                ServiceClientHelpers.GetServiceClientProviderType(
                                    Models.WorkloadType.MSSQL);
                        }
                        else
                        {
                            throw new ArgumentException(string.Format(
                                                            Resources.UnsupportedWorkloadBackupManagementTypeException,
                                                            WorkloadType.ToString(),
                                                            BackupManagementType.ToString()));
                        }
                        break;

                    default:
                        break;
                    }

                    var backupManagementTypeFilter = serviceClientProviderType;


                    ODataQuery <ServiceClientModel.ProtectionPolicyQueryObject> queryParams
                        = new ODataQuery <ServiceClientModel.ProtectionPolicyQueryObject>(
                              q => q.BackupManagementType == backupManagementTypeFilter);

                    WriteDebug("going to query service to get list of policies");
                    List <ServiceClientModel.ProtectionPolicyResource> respList =
                        ServiceClientAdapter.ListProtectionPolicy(
                            queryParams,
                            vaultName: vaultName,
                            resourceGroupName: resourceGroupName);
                    WriteDebug("Successfully got response from service");

                    policyList = ConversionHelpers.GetPolicyModelList(respList);
                    WriteObject(policyList, enumerateCollection: true);
                }
            });
        }
コード例 #4
0
        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());
                }
            });
        }
コード例 #5
0
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

                ResourceIdentifier resourceIdentifier = new ResourceIdentifier(VaultId);
                string vaultName         = resourceIdentifier.ResourceName;
                string resourceGroupName = resourceIdentifier.ResourceGroupName;

                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
                ProtectionPolicyResource servicePolicy = PolicyCmdletHelpers.GetProtectionPolicyByName(
                    Policy.Name,
                    ServiceClientAdapter,
                    vaultName: vaultName,
                    resourceGroupName: resourceGroupName);
                if (servicePolicy == null)
                {
                    throw new ArgumentException(string.Format(Resources.PolicyNotFoundException,
                                                              Policy.Name));
                }

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

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

                if (policyResponse.Response.StatusCode == System.Net.HttpStatusCode.Accepted)
                {
                    // Track OperationStatus URL for operation completion

                    string policyName = Policy.Name;

                    ServiceClientModel.OperationStatus operationStatus =
                        TrackingHelpers.GetOperationStatus(
                            policyResponse,
                            operationId =>
                            ServiceClientAdapter.GetProtectionPolicyOperationStatus(
                                policyName,
                                operationId,
                                vaultName: vaultName,
                                resourceGroupName: resourceGroupName));

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

                    if (operationStatus.Properties != null &&
                        ((OperationStatusJobsExtendedInfo)operationStatus.Properties)
                        .JobIds != null)
                    {
                        // get list of jobIds and return jobResponses
                        WriteObject(GetJobObject(
                                        ((OperationStatusJobsExtendedInfo)operationStatus.Properties).JobIds,
                                        vaultName: vaultName,
                                        resourceGroupName: resourceGroupName));
                    }

                    if (operationStatus.Status == OperationStatusValues.Failed)
                    {
                        // if operation failed, then trace error and throw exception
                        if (operationStatus.Error != null)
                        {
                            WriteDebug(string.Format(
                                           "OperationStatus Error: {0} " +
                                           "OperationStatus Code: {1}",
                                           operationStatus.Error.Message,
                                           operationStatus.Error.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.Response.StatusCode.ToString());
                }
            }, ShouldProcess(Policy.Name, VerbsCommon.Set));
        }
コード例 #6
0
        public override void ExecuteCmdlet()
        {
            ExecutionBlock(() =>
            {
                base.ExecuteCmdlet();

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

                if (this.ParameterSetName == PolicyNameParamSet)
                {
                    // validate policyName
                    PolicyCmdletHelpers.ValidateProtectionPolicyName(Name);

                    // query service
                    ServiceClientModel.ProtectionPolicyResponse policy =
                        PolicyCmdletHelpers.GetProtectionPolicyByName(
                            Name,
                            ServiceClientAdapter);
                    if (policy == null)
                    {
                        throw new ArgumentException(string.Format(Resources.PolicyNotFoundException, Name));
                    }

                    WriteObject(ConversionHelpers.GetPolicyModel(policy.Item));
                }
                else
                {
                    List <PolicyBase> policyList     = new List <PolicyBase>();
                    string serviceClientProviderType = null;

                    switch (this.ParameterSetName)
                    {
                    case WorkloadParamSet:
                        if (WorkloadType == Models.WorkloadType.AzureVM)
                        {
                            serviceClientProviderType =
                                ServiceClientHelpers.GetServiceClientProviderType(Models.WorkloadType.AzureVM);
                        }
                        break;

                    case WorkloadBackupMangementTypeParamSet:
                        if (WorkloadType == Models.WorkloadType.AzureVM)
                        {
                            if (BackupManagementType != Models.BackupManagementType.AzureVM)
                            {
                                throw new ArgumentException(
                                    Resources.AzureVMUnsupportedBackupManagementTypeException);
                            }
                            serviceClientProviderType = ServiceClientHelpers.
                                                        GetServiceClientProviderType(Models.WorkloadType.AzureVM);
                        }
                        else
                        {
                            throw new ArgumentException(string.Format(
                                                            Resources.UnsupportedWorkloadBackupManagementTypeException,
                                                            WorkloadType.ToString(),
                                                            BackupManagementType.ToString()));
                        }
                        break;

                    default:
                        break;
                    }

                    ServiceClientModel.ProtectionPolicyQueryParameters queryParams =
                        new ServiceClientModel.ProtectionPolicyQueryParameters()
                    {
                        BackupManagementType = serviceClientProviderType
                    };

                    WriteDebug("going to query service to get list of policies");
                    ServiceClientModel.ProtectionPolicyListResponse respList =
                        ServiceClientAdapter.ListProtectionPolicy(queryParams);
                    WriteDebug("Successfully got response from service");

                    policyList = ConversionHelpers.GetPolicyModelList(respList);
                    WriteObject(policyList, enumerateCollection: true);
                }
            });
        }