Skip to content

Policy cmdlets for mercury #382

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: mercury_dev
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@ public static PolicyBase GetPolicyModelForAzureSql(ServiceClientModel.Protection
ServiceClientModel.SimpleRetentionPolicy azureSqlRetentionPolicy =
(ServiceClientModel.SimpleRetentionPolicy)azureSqlPolicy.RetentionPolicy;
sqlPolicyModel.RetentionPolicy =
PolicyHelpers.GetPSSimpleRetentionPolicy(azureSqlRetentionPolicy, null);
PolicyHelpers.GetPSSimpleRetentionPolicy(azureSqlRetentionPolicy, null, "AzureSql");
return policyModel;
}

Expand Down Expand Up @@ -236,6 +236,85 @@ public static PolicyBase GetPolicyModelForAzureFileShare(ServiceClientModel.Prot
return policyModel;
}

public static PolicyBase GetPolicyModelForAzureVmWorkload(ServiceClientModel.ProtectionPolicyResource serviceClientResponse,
PolicyBase policyModel)
{
ServiceClientModel.AzureVmWorkloadProtectionPolicy azureVmWorkloadPolicy =
(ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties;

foreach (var policy in azureVmWorkloadPolicy.SubProtectionPolicy)
{
if (string.Compare(policy.PolicyType, "Full") == 0)
{
if (policy.SchedulePolicy.GetType() !=
typeof(ServiceClientModel.SimpleSchedulePolicy))
{
Logger.Instance.WriteDebug("Unknown Schedule object received: " +
policy.SchedulePolicy.GetType());
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
return null;
}
if (policy.RetentionPolicy.GetType() !=
typeof(ServiceClientModel.LongTermRetentionPolicy))
{
Logger.Instance.WriteDebug("Unknown RetentionPolicy object received: " +
policy.RetentionPolicy.GetType());
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
return null;
}
}
else if (string.Compare(policy.PolicyType, "Differential") == 0)
{
if (policy.SchedulePolicy.GetType() !=
typeof(ServiceClientModel.SimpleSchedulePolicy))
{
Logger.Instance.WriteDebug("Unknown Schedule object received: " +
policy.SchedulePolicy.GetType());
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
return null;
}
if (policy.RetentionPolicy.GetType() !=
typeof(ServiceClientModel.SimpleRetentionPolicy))
{
Logger.Instance.WriteDebug("Unknown RetentionPolicy object received: " +
policy.RetentionPolicy.GetType());
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
return null;
}
}
else if (string.Compare(policy.PolicyType, "Log") == 0)
{
if (policy.SchedulePolicy.GetType() !=
typeof(ServiceClientModel.LogSchedulePolicy))
{
Logger.Instance.WriteDebug("Unknown Schedule object received: " +
policy.SchedulePolicy.GetType());
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
return null;
}
if (policy.RetentionPolicy.GetType() !=
typeof(ServiceClientModel.SimpleRetentionPolicy))
{
Logger.Instance.WriteDebug("Unknown RetentionPolicy object received: " +
policy.RetentionPolicy.GetType());
Logger.Instance.WriteWarning(Resources.UpdateToNewAzurePowershellWarning);
return null;
}
}
}

policyModel = new AzureVmWorkloadPolicy();
AzureVmWorkloadPolicy azureVmWorkloadPolicyModel = policyModel as AzureVmWorkloadPolicy;
azureVmWorkloadPolicyModel.WorkloadType = WorkloadType.MSSQL;
azureVmWorkloadPolicyModel.BackupManagementType = BackupManagementType.AzureWorkload;
azureVmWorkloadPolicyModel.IsCompression =
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.IsCompression;
GetPSSubProtectionPolicy(azureVmWorkloadPolicyModel, serviceClientResponse,
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone);

return policyModel;
}

/// <summary>
/// Helper function to convert ps backup policy model from service response.
/// </summary>
Expand Down Expand Up @@ -263,6 +342,11 @@ public static PolicyBase GetPolicyModel(ServiceClientModel.ProtectionPolicyResou
{
policyModel = GetPolicyModelForAzureFileShare(serviceClientResponse, policyModel);
}
else if (serviceClientResponse.Properties.GetType() ==
typeof(ServiceClientModel.AzureVmWorkloadProtectionPolicy))
{
policyModel = GetPolicyModelForAzureVmWorkload(serviceClientResponse, policyModel);
}
else
{
// we will enter this case when service supports new workload and customer
Expand Down Expand Up @@ -431,6 +515,53 @@ public static List<ItemBase> GetItemModelList(IEnumerable<ServiceClientModel.Pro

return itemModels;
}

public static SettingsBase GetPSPolicySetting(ServiceClientModel.Settings policySettings)
{
SettingsBase settings = new SettingsBase();
settings.IsCompression = policySettings.IsCompression;
settings.Issqlcompression = policySettings.Issqlcompression;
settings.TimeZone = policySettings.TimeZone;

return settings;
}

public static void GetPSSubProtectionPolicy(AzureVmWorkloadPolicy azureVmWorkloadPolicyModel,
ServiceClientModel.ProtectionPolicyResource serviceClientResponse, string timeZone)
{
foreach (var subProtectionPolicy in
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).SubProtectionPolicy)
{
if (string.Compare(subProtectionPolicy.PolicyType, "Full") == 0)
{
azureVmWorkloadPolicyModel.FullBackupSchedulePolicy = PolicyHelpers.GetPSSimpleSchedulePolicy(
(ServiceClientModel.SimpleSchedulePolicy)subProtectionPolicy.SchedulePolicy,
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone);

azureVmWorkloadPolicyModel.FullBackupRetentionPolicy = PolicyHelpers.GetPSLongTermRetentionPolicy(
(ServiceClientModel.LongTermRetentionPolicy)subProtectionPolicy.RetentionPolicy,
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone);
}
else if (string.Compare(subProtectionPolicy.PolicyType, "Differential") == 0)
{
azureVmWorkloadPolicyModel.DifferentialBackupSchedulePolicy = PolicyHelpers.GetPSSimpleSchedulePolicy(
(ServiceClientModel.SimpleSchedulePolicy)subProtectionPolicy.SchedulePolicy,
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone);
azureVmWorkloadPolicyModel.DifferentialBackupRetentionPolicy = PolicyHelpers.GetPSSimpleRetentionPolicy(
(ServiceClientModel.SimpleRetentionPolicy)subProtectionPolicy.RetentionPolicy,
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone, "AzureWorkload");
}
else if (string.Compare(subProtectionPolicy.PolicyType, "Log") == 0)
{
azureVmWorkloadPolicyModel.LogBackupSchedulePolicy = PolicyHelpers.GetPSLogSchedulePolicy((ServiceClientModel.LogSchedulePolicy)
subProtectionPolicy.SchedulePolicy,
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone);
azureVmWorkloadPolicyModel.LogBackupRetentionPolicy = PolicyHelpers.GetPSSimpleRetentionPolicy((ServiceClientModel.SimpleRetentionPolicy)
subProtectionPolicy.RetentionPolicy,
((ServiceClientModel.AzureVmWorkloadProtectionPolicy)serviceClientResponse.Properties).Settings.TimeZone, "AzureWorkload");
}
}
}
#endregion
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,11 @@
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
using Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers;
using Microsoft.Azure.Commands.RecoveryServices.Backup.Properties;
using ServiceClientModel = Microsoft.Azure.Management.RecoveryServices.Backup.Models;

namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers
Expand Down Expand Up @@ -75,7 +74,7 @@ public static LongTermRetentionPolicy GetPSLongTermRetentionPolicy(
}

public static SimpleRetentionPolicy GetPSSimpleRetentionPolicy(
ServiceClientModel.SimpleRetentionPolicy hydraRetPolicy, string timeZone)
ServiceClientModel.SimpleRetentionPolicy hydraRetPolicy, string timeZone, string provider)
{
if (hydraRetPolicy == null)
{
Expand All @@ -92,7 +91,24 @@ public static SimpleRetentionPolicy GetPSSimpleRetentionPolicy(
(int)hydraRetPolicy.RetentionDuration.Count : default(int);
}

simplePolicy.Validate();
if (string.Compare(provider, "AzureSql") == 0)
{
int weeklyLimit = PolicyConstants.MaxAllowedRetentionDurationCountWeeklySql;
int monthlyLimit = PolicyConstants.MaxAllowedRetentionDurationCountMonthlySql;
int yearlyLimit = PolicyConstants.MaxAllowedRetentionDurationCountYearlySql;

if ((simplePolicy.RetentionDurationType == RetentionDurationType.Days) ||
(simplePolicy.RetentionDurationType == RetentionDurationType.Weeks &&
(simplePolicy.RetentionCount <= 0 || simplePolicy.RetentionCount > weeklyLimit)) ||
(simplePolicy.RetentionDurationType == RetentionDurationType.Months &&
(simplePolicy.RetentionCount <= 0 || simplePolicy.RetentionCount > monthlyLimit)) ||
(simplePolicy.RetentionDurationType == RetentionDurationType.Years &&
(simplePolicy.RetentionCount <= 0 || simplePolicy.RetentionCount > yearlyLimit)))
{
throw new ArgumentException(Resources.AllowedSqlRetentionRange);
}
}

return simplePolicy;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,26 @@ public static SimpleSchedulePolicy GetPSSimpleSchedulePolicy(
return psPolicy;
}

// <summary>
/// Helper function to convert ps log schedule policy from service response.
/// </summary>
public static LogSchedulePolicy GetPSLogSchedulePolicy(
ServiceClientModel.LogSchedulePolicy serviceClientPolicy, string timeZone)
{
if (serviceClientPolicy == null)
{
return null;
}

LogSchedulePolicy psPolicy = new LogSchedulePolicy();
psPolicy.ScheduleFrequencyInMins = serviceClientPolicy.ScheduleFrequencyInMins;

// safe side validation
psPolicy.Validate();

return psPolicy;
}

#endregion

#region PStoServiceClientObject conversions
Expand Down Expand Up @@ -117,6 +137,19 @@ public static ServiceClientModel.SimpleSchedulePolicy GetServiceClientSimpleSche
return serviceClientPolicy;
}

public static ServiceClientModel.LogSchedulePolicy GetServiceClientLogSchedulePolicy(
LogSchedulePolicy psPolicy)
{
if (psPolicy == null)
{
return null;
}

ServiceClientModel.LogSchedulePolicy serviceClientPolicy = new ServiceClientModel.LogSchedulePolicy();
serviceClientPolicy.ScheduleFrequencyInMins = psPolicy.ScheduleFrequencyInMins;
return serviceClientPolicy;
}

// <summary>
/// Helper function to get nullable date time list from date time list.
/// </summary>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// ----------------------------------------------------------------------------------
//
// Copyright Microsoft Corporation
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
// http://www.apache.org/licenses/LICENSE-2.0
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------------------------------------------------------------

using Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;
using System.Collections.Generic;
using ServiceClientModel = Microsoft.Azure.Management.RecoveryServices.Backup.Models;
using CmdletModel = Microsoft.Azure.Commands.RecoveryServices.Backup.Cmdlets.Models;

namespace Microsoft.Azure.Commands.RecoveryServices.Backup.Helpers
{
/// <summary>
/// Backup policy conversion helper
/// </summary>
public partial class PolicyHelpers
{
public static List<ServiceClientModel.SubProtectionPolicy> GetServiceClientSubProtectionPolicy(
SQLRetentionPolicy retentionPolicy,
SQLSchedulePolicy schedulePolicy)
{
List<ServiceClientModel.SubProtectionPolicy> subProtectionPolicy =
new List<ServiceClientModel.SubProtectionPolicy>();
if (schedulePolicy.FullBackupSchedulePolicy != null &&
retentionPolicy.FullBackupRetentionPolicy != null)
{
subProtectionPolicy.Add(new ServiceClientModel.SubProtectionPolicy("Full",
GetServiceClientSimpleSchedulePolicy(schedulePolicy.FullBackupSchedulePolicy),
GetServiceClientLongTermRetentionPolicy(retentionPolicy.FullBackupRetentionPolicy)));
}
if (schedulePolicy.DifferentialBackupSchedulePolicy != null &&
retentionPolicy.DifferentialBackupRetentionPolicy != null &&
schedulePolicy.IsDifferentialBackupEnabled)
{
subProtectionPolicy.Add(new ServiceClientModel.SubProtectionPolicy("Differential",
GetServiceClientSimpleSchedulePolicy(schedulePolicy.DifferentialBackupSchedulePolicy),
GetServiceClientSimpleRetentionPolicy(retentionPolicy.DifferentialBackupRetentionPolicy)));
}
if (schedulePolicy.LogBackupSchedulePolicy != null &&
retentionPolicy.LogBackupRetentionPolicy != null &&
schedulePolicy.IsLogBackupEnabled)
{
subProtectionPolicy.Add(new ServiceClientModel.SubProtectionPolicy("Log",
GetServiceClientLogSchedulePolicy(schedulePolicy.LogBackupSchedulePolicy),
GetServiceClientSimpleRetentionPolicy(retentionPolicy.LogBackupRetentionPolicy)));
}
return subProtectionPolicy;
}

public static List<ServiceClientModel.SubProtectionPolicy> GetServiceClientSubProtectionPolicy(
AzureVmWorkloadPolicy policy)
{
List<ServiceClientModel.SubProtectionPolicy> subProtectionPolicy =
new List<ServiceClientModel.SubProtectionPolicy>();
if (policy.FullBackupSchedulePolicy != null &&
policy.FullBackupRetentionPolicy != null)
{
subProtectionPolicy.Add(new ServiceClientModel.SubProtectionPolicy("Full",
GetServiceClientSimpleSchedulePolicy((SimpleSchedulePolicy)policy.FullBackupSchedulePolicy),
GetServiceClientLongTermRetentionPolicy((LongTermRetentionPolicy)policy.FullBackupRetentionPolicy)));
}
if (policy.DifferentialBackupSchedulePolicy != null &&
policy.DifferentialBackupRetentionPolicy != null &&
policy.IsDifferentialBackupEnabled)
{
subProtectionPolicy.Add(new ServiceClientModel.SubProtectionPolicy("Differential",
GetServiceClientSimpleSchedulePolicy((SimpleSchedulePolicy)policy.DifferentialBackupSchedulePolicy),
GetServiceClientSimpleRetentionPolicy((SimpleRetentionPolicy)policy.DifferentialBackupRetentionPolicy)));
}
if (policy.LogBackupSchedulePolicy != null &&
policy.LogBackupRetentionPolicy != null &&
policy.IsLogBackupEnabled)
{
subProtectionPolicy.Add(new ServiceClientModel.SubProtectionPolicy("Log",
GetServiceClientLogSchedulePolicy((LogSchedulePolicy)policy.LogBackupSchedulePolicy),
GetServiceClientSimpleRetentionPolicy((SimpleRetentionPolicy)policy.LogBackupRetentionPolicy)));
}
return subProtectionPolicy;
}
}
}
Loading