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));
            });
        }
Exemple #2
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);
                }
            });
        }