private void ValidateAzureVMWorkloadType(CmdletModel.WorkloadType type)
 {
     if (type != CmdletModel.WorkloadType.AzureVM)
     {
         throw new ArgumentException(string.Format(Resources.UnExpectedWorkLoadTypeException,
                                                   CmdletModel.WorkloadType.AzureVM.ToString(),
                                                   type.ToString()));
     }
 }
 private void ValidateAzureVMWorkloadType(CmdletModel.WorkloadType itemWorkloadType,
                                          CmdletModel.WorkloadType policyWorkloadType)
 {
     ValidateAzureVMWorkloadType(itemWorkloadType);
     ValidateAzureVMWorkloadType(policyWorkloadType);
     if (itemWorkloadType != policyWorkloadType)
     {
         throw new ArgumentException(string.Format(Resources.UnExpectedWorkLoadTypeException,
                                                   CmdletModel.WorkloadType.AzureVM.ToString(),
                                                   itemWorkloadType.ToString()));
     }
 }
        private RestAzureNS.AzureOperationResponse <ProtectionPolicyResource> CreateorModifyPolicy()
        {
            string vaultName         = (string)ProviderData[VaultParams.VaultName];
            string resourceGroupName = (string)ProviderData[VaultParams.ResourceGroupName];
            string policyName        = ProviderData.ContainsKey(PolicyParams.PolicyName) ?
                                       (string)ProviderData[PolicyParams.PolicyName] : null;
            RetentionPolicyBase retentionPolicy =
                ProviderData.ContainsKey(PolicyParams.RetentionPolicy) ?
                (RetentionPolicyBase)ProviderData[PolicyParams.RetentionPolicy] :
                null;
            SchedulePolicyBase schedulePolicy =
                ProviderData.ContainsKey(PolicyParams.SchedulePolicy) ?
                (SchedulePolicyBase)ProviderData[PolicyParams.SchedulePolicy] :
                null;
            PolicyBase policy =
                ProviderData.ContainsKey(PolicyParams.ProtectionPolicy) ?
                (PolicyBase)ProviderData[PolicyParams.ProtectionPolicy] :
                null;
            bool fixForInconsistentItems = ProviderData.ContainsKey(PolicyParams.FixForInconsistentItems) ?
                                           (bool)ProviderData[PolicyParams.FixForInconsistentItems] : false;
            ProtectionPolicyResource serviceClientRequest = new ProtectionPolicyResource();

            if (policy != null)
            {
                // do validations
                ValidateAzureWorkloadProtectionPolicy(policy);
                Logger.Instance.WriteDebug("Validation of Protection Policy is successful");

                // RetentionPolicy and SchedulePolicy both should not be empty
                if (retentionPolicy == null && schedulePolicy == null)
                {
                    if (fixForInconsistentItems == false)
                    {
                        throw new ArgumentException(Resources.BothRetentionAndSchedulePoliciesEmpty);
                    }
                    AzureVmWorkloadProtectionPolicy azureVmWorkloadModifyPolicy = new AzureVmWorkloadProtectionPolicy();
                    azureVmWorkloadModifyPolicy.Settings = new Settings("UTC",
                                                                        ((AzureVmWorkloadPolicy)policy).IsCompression,
                                                                        ((AzureVmWorkloadPolicy)policy).IsCompression);
                    azureVmWorkloadModifyPolicy.WorkLoadType         = ConversionUtils.GetServiceClientWorkloadType(policy.WorkloadType.ToString());
                    azureVmWorkloadModifyPolicy.SubProtectionPolicy  = new List <SubProtectionPolicy>();
                    azureVmWorkloadModifyPolicy.SubProtectionPolicy  = PolicyHelpers.GetServiceClientSubProtectionPolicy((AzureVmWorkloadPolicy)policy);
                    azureVmWorkloadModifyPolicy.MakePolicyConsistent = true;
                    serviceClientRequest.Properties = azureVmWorkloadModifyPolicy;
                }
                else
                {
                    // validate RetentionPolicy and SchedulePolicy
                    if (schedulePolicy != null)
                    {
                        AzureWorkloadProviderHelper.ValidateSQLSchedulePolicy(schedulePolicy);
                        AzureWorkloadProviderHelper.GetUpdatedSchedulePolicy(policy, (SQLSchedulePolicy)schedulePolicy);
                        Logger.Instance.WriteDebug("Validation of Schedule policy is successful");
                    }
                    if (retentionPolicy != null)
                    {
                        AzureWorkloadProviderHelper.ValidateSQLRetentionPolicy(retentionPolicy);
                        AzureWorkloadProviderHelper.GetUpdatedRetentionPolicy(policy, (SQLRetentionPolicy)retentionPolicy);
                        Logger.Instance.WriteDebug("Validation of Retention policy is successful");
                    }

                    // copy the backupSchedule time to retentionPolicy after converting to UTC
                    AzureWorkloadProviderHelper.CopyScheduleTimeToRetentionTimes(
                        (CmdletModel.LongTermRetentionPolicy)((AzureVmWorkloadPolicy)policy).FullBackupRetentionPolicy,
                        (CmdletModel.SimpleSchedulePolicy)((AzureVmWorkloadPolicy)policy).FullBackupSchedulePolicy);
                    Logger.Instance.WriteDebug("Copy of RetentionTime from with SchedulePolicy to RetentionPolicy is successful");

                    // Now validate both RetentionPolicy and SchedulePolicy matches or not
                    PolicyHelpers.ValidateLongTermRetentionPolicyWithSimpleRetentionPolicy(
                        (CmdletModel.LongTermRetentionPolicy)((AzureVmWorkloadPolicy)policy).FullBackupRetentionPolicy,
                        (CmdletModel.SimpleSchedulePolicy)((AzureVmWorkloadPolicy)policy).FullBackupSchedulePolicy);
                    Logger.Instance.WriteDebug("Validation of Retention policy with Schedule policy is successful");

                    // construct Service Client policy request
                    AzureVmWorkloadProtectionPolicy azureVmWorkloadProtectionPolicy = new AzureVmWorkloadProtectionPolicy();
                    azureVmWorkloadProtectionPolicy.Settings = new Settings("UTC",
                                                                            ((AzureVmWorkloadPolicy)policy).IsCompression,
                                                                            ((AzureVmWorkloadPolicy)policy).IsCompression);
                    azureVmWorkloadProtectionPolicy.WorkLoadType        = ConversionUtils.GetServiceClientWorkloadType(policy.WorkloadType.ToString());
                    azureVmWorkloadProtectionPolicy.SubProtectionPolicy = new List <SubProtectionPolicy>();
                    azureVmWorkloadProtectionPolicy.SubProtectionPolicy = PolicyHelpers.GetServiceClientSubProtectionPolicy((AzureVmWorkloadPolicy)policy);
                    serviceClientRequest.Properties = azureVmWorkloadProtectionPolicy;
                }
            }
            else
            {
                CmdletModel.WorkloadType workloadType =
                    (CmdletModel.WorkloadType)ProviderData[PolicyParams.WorkloadType];

                // do validations
                ValidateAzureWorkloadWorkloadType(workloadType);
                AzureWorkloadProviderHelper.ValidateSQLSchedulePolicy(schedulePolicy);
                Logger.Instance.WriteDebug("Validation of Schedule policy is successful");

                // validate RetentionPolicy
                AzureWorkloadProviderHelper.ValidateSQLRetentionPolicy(retentionPolicy);
                Logger.Instance.WriteDebug("Validation of Retention policy is successful");

                // update the retention times from backupSchedule to retentionPolicy after converting to UTC
                AzureWorkloadProviderHelper.CopyScheduleTimeToRetentionTimes(((SQLRetentionPolicy)retentionPolicy).FullBackupRetentionPolicy,
                                                                             ((SQLSchedulePolicy)schedulePolicy).FullBackupSchedulePolicy);
                Logger.Instance.WriteDebug("Copy of RetentionTime from with SchedulePolicy to RetentionPolicy is successful");

                // Now validate both RetentionPolicy and SchedulePolicy together
                PolicyHelpers.ValidateLongTermRetentionPolicyWithSimpleRetentionPolicy(
                    (SQLRetentionPolicy)retentionPolicy,
                    (SQLSchedulePolicy)schedulePolicy);
                Logger.Instance.WriteDebug("Validation of Retention policy with Schedule policy is successful");

                // construct Service Client policy request
                AzureVmWorkloadProtectionPolicy azureVmWorkloadProtectionPolicy = new AzureVmWorkloadProtectionPolicy();
                azureVmWorkloadProtectionPolicy.Settings = new Settings("UTC",
                                                                        ((SQLSchedulePolicy)schedulePolicy).IsCompression,
                                                                        ((SQLSchedulePolicy)schedulePolicy).IsCompression);
                azureVmWorkloadProtectionPolicy.WorkLoadType        = ConversionUtils.GetServiceClientWorkloadType(workloadType.ToString());
                azureVmWorkloadProtectionPolicy.SubProtectionPolicy = new List <SubProtectionPolicy>();
                azureVmWorkloadProtectionPolicy.SubProtectionPolicy = PolicyHelpers.GetServiceClientSubProtectionPolicy(
                    (SQLRetentionPolicy)retentionPolicy,
                    (SQLSchedulePolicy)schedulePolicy);
                serviceClientRequest.Properties = azureVmWorkloadProtectionPolicy;
            }

            return(ServiceClientAdapter.CreateOrUpdateProtectionPolicy(
                       policyName = policyName ?? policy.Name,
                       serviceClientRequest,
                       vaultName: vaultName,
                       resourceGroupName: resourceGroupName));
        }
Exemplo n.º 4
0
        private RestAzureNS.AzureOperationResponse <ProtectionPolicyResource> CreateorModifyPolicy()
        {
            string vaultName         = (string)ProviderData[VaultParams.VaultName];
            string resourceGroupName = (string)ProviderData[VaultParams.ResourceGroupName];
            string policyName        = ProviderData.ContainsKey(PolicyParams.PolicyName) ?
                                       (string)ProviderData[PolicyParams.PolicyName] : null;
            RetentionPolicyBase retentionPolicy =
                ProviderData.ContainsKey(PolicyParams.RetentionPolicy) ?
                (RetentionPolicyBase)ProviderData[PolicyParams.RetentionPolicy] :
                null;
            SchedulePolicyBase schedulePolicy =
                ProviderData.ContainsKey(PolicyParams.SchedulePolicy) ?
                (SchedulePolicyBase)ProviderData[PolicyParams.SchedulePolicy] :
                null;
            PolicyBase policy =
                ProviderData.ContainsKey(PolicyParams.ProtectionPolicy) ?
                (PolicyBase)ProviderData[PolicyParams.ProtectionPolicy] :
                null;
            ProtectionPolicyResource serviceClientRequest = new ProtectionPolicyResource();

            if (policy != null)
            {
                // do validations
                ValidateAzureFileProtectionPolicy(policy);
                Logger.Instance.WriteDebug("Validation of Protection Policy is successful");

                // RetentionPolicy and SchedulePolicy both should not be empty
                if (retentionPolicy == null && schedulePolicy == null)
                {
                    throw new ArgumentException(Resources.BothRetentionAndSchedulePoliciesEmpty);
                }

                // validate RetentionPolicy and SchedulePolicy
                if (schedulePolicy != null)
                {
                    AzureWorkloadProviderHelper.ValidateSimpleSchedulePolicy(schedulePolicy);
                    ((AzureFileSharePolicy)policy).SchedulePolicy = schedulePolicy;
                    Logger.Instance.WriteDebug("Validation of Schedule policy is successful");
                }
                if (retentionPolicy != null)
                {
                    AzureWorkloadProviderHelper.ValidateLongTermRetentionPolicy(retentionPolicy);
                    ((AzureFileSharePolicy)policy).RetentionPolicy = retentionPolicy;
                    Logger.Instance.WriteDebug("Validation of Retention policy is successful");
                }

                // copy the backupSchedule time to retentionPolicy after converting to UTC
                AzureWorkloadProviderHelper.CopyScheduleTimeToRetentionTimes(
                    (CmdletModel.LongTermRetentionPolicy)((AzureFileSharePolicy)policy).RetentionPolicy,
                    (CmdletModel.SimpleSchedulePolicy)((AzureFileSharePolicy)policy).SchedulePolicy);
                Logger.Instance.WriteDebug("Copy of RetentionTime from with SchedulePolicy to RetentionPolicy is successful");

                // Now validate both RetentionPolicy and SchedulePolicy matches or not
                PolicyHelpers.ValidateLongTermRetentionPolicyWithSimpleRetentionPolicy(
                    (CmdletModel.LongTermRetentionPolicy)((AzureFileSharePolicy)policy).RetentionPolicy,
                    (CmdletModel.SimpleSchedulePolicy)((AzureFileSharePolicy)policy).SchedulePolicy);
                Logger.Instance.WriteDebug("Validation of Retention policy with Schedule policy is successful");

                // construct Service Client policy request
                AzureFileShareProtectionPolicy azureFileShareProtectionPolicy = new AzureFileShareProtectionPolicy();
                azureFileShareProtectionPolicy.RetentionPolicy = PolicyHelpers.GetServiceClientLongTermRetentionPolicy(
                    (CmdletModel.LongTermRetentionPolicy)((AzureFileSharePolicy)policy).RetentionPolicy);
                azureFileShareProtectionPolicy.SchedulePolicy = PolicyHelpers.GetServiceClientSimpleSchedulePolicy(
                    (CmdletModel.SimpleSchedulePolicy)((AzureFileSharePolicy)policy).SchedulePolicy);
                azureFileShareProtectionPolicy.TimeZone     = DateTimeKind.Utc.ToString().ToUpper();
                azureFileShareProtectionPolicy.WorkLoadType = ConversionUtils.GetServiceClientWorkloadType(policy.WorkloadType.ToString());
                serviceClientRequest.Properties             = azureFileShareProtectionPolicy;
            }
            else
            {
                CmdletModel.WorkloadType workloadType =
                    (CmdletModel.WorkloadType)ProviderData[PolicyParams.WorkloadType];

                // do validations
                ValidateAzureFilesWorkloadType(workloadType);
                AzureWorkloadProviderHelper.ValidateSimpleSchedulePolicy(schedulePolicy);
                Logger.Instance.WriteDebug("Validation of Schedule policy is successful");

                // validate RetentionPolicy
                AzureWorkloadProviderHelper.ValidateLongTermRetentionPolicy(retentionPolicy);
                Logger.Instance.WriteDebug("Validation of Retention policy is successful");

                // update the retention times from backupSchedule to retentionPolicy after converting to UTC
                AzureWorkloadProviderHelper.CopyScheduleTimeToRetentionTimes((CmdletModel.LongTermRetentionPolicy)retentionPolicy,
                                                                             (CmdletModel.SimpleSchedulePolicy)schedulePolicy);
                Logger.Instance.WriteDebug("Copy of RetentionTime from with SchedulePolicy to RetentionPolicy is successful");

                // Now validate both RetentionPolicy and SchedulePolicy together
                PolicyHelpers.ValidateLongTermRetentionPolicyWithSimpleRetentionPolicy(
                    (CmdletModel.LongTermRetentionPolicy)retentionPolicy,
                    (CmdletModel.SimpleSchedulePolicy)schedulePolicy);
                Logger.Instance.WriteDebug("Validation of Retention policy with Schedule policy is successful");

                // construct Service Client policy request
                AzureFileShareProtectionPolicy azureFileShareProtectionPolicy = new AzureFileShareProtectionPolicy();
                azureFileShareProtectionPolicy.RetentionPolicy = PolicyHelpers.GetServiceClientLongTermRetentionPolicy(
                    (CmdletModel.LongTermRetentionPolicy)retentionPolicy);
                azureFileShareProtectionPolicy.RetentionPolicy = PolicyHelpers.GetServiceClientLongTermRetentionPolicy(
                    (CmdletModel.LongTermRetentionPolicy)retentionPolicy);
                azureFileShareProtectionPolicy.SchedulePolicy = PolicyHelpers.GetServiceClientSimpleSchedulePolicy(
                    (CmdletModel.SimpleSchedulePolicy)schedulePolicy);
                azureFileShareProtectionPolicy.TimeZone     = DateTimeKind.Utc.ToString().ToUpper();
                azureFileShareProtectionPolicy.WorkLoadType = ConversionUtils.GetServiceClientWorkloadType(workloadType.ToString());
                serviceClientRequest.Properties             = azureFileShareProtectionPolicy;
            }

            return(ServiceClientAdapter.CreateOrUpdateProtectionPolicy(
                       policyName = policyName ?? policy.Name,
                       serviceClientRequest,
                       vaultName: vaultName,
                       resourceGroupName: resourceGroupName));
        }