Skip to content

Commit e9889b5

Browse files
committed
Added Azure File Share support to New, Modify and Remove Policy
1 parent 22ed694 commit e9889b5

File tree

6 files changed

+292
-80
lines changed

6 files changed

+292
-80
lines changed

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Models/CommonModels/Utils.cs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ public class PolicyConstants
4747
// month constants
4848
public const int NumOfMonthsInYear = 12;
4949

50+
// SQL constants
5051
public const int MaxAllowedRetentionDurationCountWeeklySql = 520;
5152
public const int MaxAllowedRetentionDurationCountMonthlySql = 120;
5253
public const int MaxAllowedRetentionDurationCountYearlySql = 10;
@@ -277,5 +278,30 @@ public static WorkloadType GetPsWorkloadType(string workloadType)
277278
throw new Exception("Unsupported WorkloadType: " + workloadType);
278279
}
279280
}
281+
282+
/// <summary>
283+
/// Returns the Service Client backup management type given the PS workload type.
284+
/// </summary>
285+
/// <param name="workloadType">PS workload type</param>
286+
/// <returns>Service Client workload type</returns>
287+
public static string GetServiceClientWorkloadType(string workloadType)
288+
{
289+
if (workloadType == WorkloadType.AzureVM.ToString())
290+
{
291+
return ServiceClientModel.WorkloadType.VM;
292+
}
293+
if (workloadType == WorkloadType.AzureSQLDatabase.ToString())
294+
{
295+
return ServiceClientModel.WorkloadType.AzureSqlDb;
296+
}
297+
if (workloadType == WorkloadType.AzureFiles.ToString())
298+
{
299+
return ServiceClientModel.WorkloadType.AzureFileShare;
300+
}
301+
else
302+
{
303+
throw new Exception("Unsupported WorkloadType: " + workloadType);
304+
}
305+
}
280306
}
281307
}

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/AzureWorkloadProviderHelper.cs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,73 @@ public List<ProtectedItemResource> ListProtectedItemsByContainer(
235235
return ConversionHelpers.GetContainerModelList(listResponse);
236236
}
237237

238+
public void ValidateSimpleSchedulePolicy(CmdletModel.SchedulePolicyBase policy)
239+
{
240+
if (policy == null || policy.GetType() != typeof(CmdletModel.SimpleSchedulePolicy))
241+
{
242+
throw new ArgumentException(string.Format(Resources.InvalidSchedulePolicyException,
243+
typeof(CmdletModel.SimpleSchedulePolicy).ToString()));
244+
}
245+
246+
// call validation
247+
policy.Validate();
248+
}
249+
250+
public void ValidateLongTermRetentionPolicy(CmdletModel.RetentionPolicyBase policy)
251+
{
252+
if (policy == null || policy.GetType() != typeof(CmdletModel.LongTermRetentionPolicy))
253+
{
254+
throw new ArgumentException(
255+
string.Format(
256+
Resources.InvalidRetentionPolicyException,
257+
typeof(CmdletModel.LongTermRetentionPolicy).ToString()));
258+
}
259+
260+
// perform validation
261+
policy.Validate();
262+
}
263+
264+
public DateTime GenerateRandomScheduleTime()
265+
{
266+
//Schedule time will be random to avoid the load in service (same is in portal as well)
267+
Random rand = new Random();
268+
int hour = rand.Next(0, 24);
269+
int minute = (rand.Next(0, 2) == 0) ? 0 : 30;
270+
return new DateTime(DateTime.Now.Year,
271+
DateTime.Now.Month,
272+
DateTime.Now.Day,
273+
hour,
274+
minute,
275+
00,
276+
DateTimeKind.Utc);
277+
}
278+
279+
public void CopyScheduleTimeToRetentionTimes(CmdletModel.LongTermRetentionPolicy retPolicy,
280+
CmdletModel.SimpleSchedulePolicy schPolicy)
281+
{
282+
// schedule runTimes is already validated if in UTC/not during validate()
283+
// now copy times from schedule to retention policy
284+
if (retPolicy.IsDailyScheduleEnabled && retPolicy.DailySchedule != null)
285+
{
286+
retPolicy.DailySchedule.RetentionTimes = schPolicy.ScheduleRunTimes;
287+
}
288+
289+
if (retPolicy.IsWeeklyScheduleEnabled && retPolicy.WeeklySchedule != null)
290+
{
291+
retPolicy.WeeklySchedule.RetentionTimes = schPolicy.ScheduleRunTimes;
292+
}
293+
294+
if (retPolicy.IsMonthlyScheduleEnabled && retPolicy.MonthlySchedule != null)
295+
{
296+
retPolicy.MonthlySchedule.RetentionTimes = schPolicy.ScheduleRunTimes;
297+
}
298+
299+
if (retPolicy.IsYearlyScheduleEnabled && retPolicy.YearlySchedule != null)
300+
{
301+
retPolicy.YearlySchedule.RetentionTimes = schPolicy.ScheduleRunTimes;
302+
}
303+
}
304+
238305
public List<CmdletModel.RecoveryPointBase> ListRecoveryPoints(Dictionary<Enum, object> ProviderData)
239306
{
240307
string vaultName = (string)ProviderData[CmdletModel.VaultParams.VaultName];

src/ResourceManager/RecoveryServices/Commands.RecoveryServices.Backup.Providers/Providers/AzureFilesPsBackupProvider.cs

Lines changed: 151 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ public class AzureFilesPsBackupProvider : IPsBackupProvider
3737
{
3838
private const int defaultOperationStatusRetryTimeInMilliSec = 5 * 1000; // 5 sec
3939
private const string separator = ";";
40+
private const CmdletModel.RetentionDurationType defaultFileRetentionType =
41+
CmdletModel.RetentionDurationType.Days;
42+
private const int defaultFileRetentionCount = 30;
4043

4144
Dictionary<Enum, object> ProviderData { get; set; }
4245

@@ -429,12 +432,126 @@ public List<RecoveryPointBase> ListRecoveryPoints()
429432

430433
public ProtectionPolicyResource CreatePolicy()
431434
{
432-
throw new NotImplementedException();
435+
return CreateorModifyPolicy().Body;
433436
}
434437

435438
public RestAzureNS.AzureOperationResponse<ProtectionPolicyResource> ModifyPolicy()
436439
{
437-
throw new NotImplementedException();
440+
return CreateorModifyPolicy();
441+
}
442+
443+
private RestAzureNS.AzureOperationResponse<ProtectionPolicyResource> CreateorModifyPolicy()
444+
{
445+
string vaultName = (string)ProviderData[VaultParams.VaultName];
446+
string resourceGroupName = (string)ProviderData[VaultParams.ResourceGroupName];
447+
string policyName = ProviderData.ContainsKey(PolicyParams.PolicyName) ?
448+
(string)ProviderData[PolicyParams.PolicyName] : null;
449+
RetentionPolicyBase retentionPolicy =
450+
ProviderData.ContainsKey(PolicyParams.RetentionPolicy) ?
451+
(RetentionPolicyBase)ProviderData[PolicyParams.RetentionPolicy] :
452+
null;
453+
SchedulePolicyBase schedulePolicy =
454+
ProviderData.ContainsKey(PolicyParams.SchedulePolicy) ?
455+
(SchedulePolicyBase)ProviderData[PolicyParams.SchedulePolicy] :
456+
null;
457+
PolicyBase policy =
458+
ProviderData.ContainsKey(PolicyParams.ProtectionPolicy) ?
459+
(PolicyBase)ProviderData[PolicyParams.ProtectionPolicy] :
460+
null;
461+
ProtectionPolicyResource serviceClientRequest = new ProtectionPolicyResource();
462+
463+
if (policy != null)
464+
{
465+
// do validations
466+
ValidateAzureFileProtectionPolicy(policy);
467+
Logger.Instance.WriteDebug("Validation of Protection Policy is successful");
468+
469+
// RetentionPolicy and SchedulePolicy both should not be empty
470+
if (retentionPolicy == null && schedulePolicy == null)
471+
{
472+
throw new ArgumentException(Resources.BothRetentionAndSchedulePoliciesEmpty);
473+
}
474+
475+
// validate RetentionPolicy and SchedulePolicy
476+
if (schedulePolicy != null)
477+
{
478+
AzureWorkloadProviderHelper.ValidateSimpleSchedulePolicy(schedulePolicy);
479+
((AzureFileSharePolicy)policy).SchedulePolicy = schedulePolicy;
480+
Logger.Instance.WriteDebug("Validation of Schedule policy is successful");
481+
}
482+
if (retentionPolicy != null)
483+
{
484+
AzureWorkloadProviderHelper.ValidateLongTermRetentionPolicy(retentionPolicy);
485+
((AzureFileSharePolicy)policy).RetentionPolicy = retentionPolicy;
486+
Logger.Instance.WriteDebug("Validation of Retention policy is successful");
487+
}
488+
489+
// copy the backupSchedule time to retentionPolicy after converting to UTC
490+
AzureWorkloadProviderHelper.CopyScheduleTimeToRetentionTimes(
491+
(CmdletModel.LongTermRetentionPolicy)((AzureFileSharePolicy)policy).RetentionPolicy,
492+
(CmdletModel.SimpleSchedulePolicy)((AzureFileSharePolicy)policy).SchedulePolicy);
493+
Logger.Instance.WriteDebug("Copy of RetentionTime from with SchedulePolicy to RetentionPolicy is successful");
494+
495+
// Now validate both RetentionPolicy and SchedulePolicy matches or not
496+
PolicyHelpers.ValidateLongTermRetentionPolicyWithSimpleRetentionPolicy(
497+
(CmdletModel.LongTermRetentionPolicy)((AzureFileSharePolicy)policy).RetentionPolicy,
498+
(CmdletModel.SimpleSchedulePolicy)((AzureFileSharePolicy)policy).SchedulePolicy);
499+
Logger.Instance.WriteDebug("Validation of Retention policy with Schedule policy is successful");
500+
501+
// construct Service Client policy request
502+
AzureFileShareProtectionPolicy azureFileShareProtectionPolicy = new AzureFileShareProtectionPolicy();
503+
azureFileShareProtectionPolicy.RetentionPolicy = PolicyHelpers.GetServiceClientLongTermRetentionPolicy(
504+
(CmdletModel.LongTermRetentionPolicy)((AzureFileSharePolicy)policy).RetentionPolicy);
505+
azureFileShareProtectionPolicy.SchedulePolicy = PolicyHelpers.GetServiceClientSimpleSchedulePolicy(
506+
(CmdletModel.SimpleSchedulePolicy)((AzureFileSharePolicy)policy).SchedulePolicy);
507+
azureFileShareProtectionPolicy.TimeZone = DateTimeKind.Utc.ToString();
508+
azureFileShareProtectionPolicy.WorkLoadType = ConversionUtils.GetServiceClientWorkloadType(policy.WorkloadType.ToString());
509+
serviceClientRequest.Properties = azureFileShareProtectionPolicy;
510+
511+
}
512+
else
513+
{
514+
CmdletModel.WorkloadType workloadType =
515+
(CmdletModel.WorkloadType)ProviderData[PolicyParams.WorkloadType];
516+
517+
// do validations
518+
ValidateAzureFilesWorkloadType(workloadType);
519+
AzureWorkloadProviderHelper.ValidateSimpleSchedulePolicy(schedulePolicy);
520+
Logger.Instance.WriteDebug("Validation of Schedule policy is successful");
521+
522+
// validate RetentionPolicy
523+
AzureWorkloadProviderHelper.ValidateLongTermRetentionPolicy(retentionPolicy);
524+
Logger.Instance.WriteDebug("Validation of Retention policy is successful");
525+
526+
// update the retention times from backupSchedule to retentionPolicy after converting to UTC
527+
AzureWorkloadProviderHelper.CopyScheduleTimeToRetentionTimes((CmdletModel.LongTermRetentionPolicy)retentionPolicy,
528+
(CmdletModel.SimpleSchedulePolicy)schedulePolicy);
529+
Logger.Instance.WriteDebug("Copy of RetentionTime from with SchedulePolicy to RetentionPolicy is successful");
530+
531+
// Now validate both RetentionPolicy and SchedulePolicy together
532+
PolicyHelpers.ValidateLongTermRetentionPolicyWithSimpleRetentionPolicy(
533+
(CmdletModel.LongTermRetentionPolicy)retentionPolicy,
534+
(CmdletModel.SimpleSchedulePolicy)schedulePolicy);
535+
Logger.Instance.WriteDebug("Validation of Retention policy with Schedule policy is successful");
536+
537+
// construct Service Client policy request
538+
AzureFileShareProtectionPolicy azureFileShareProtectionPolicy = new AzureFileShareProtectionPolicy();
539+
azureFileShareProtectionPolicy.RetentionPolicy = PolicyHelpers.GetServiceClientLongTermRetentionPolicy(
540+
(CmdletModel.LongTermRetentionPolicy)retentionPolicy);
541+
azureFileShareProtectionPolicy.RetentionPolicy = PolicyHelpers.GetServiceClientLongTermRetentionPolicy(
542+
(CmdletModel.LongTermRetentionPolicy)retentionPolicy);
543+
azureFileShareProtectionPolicy.SchedulePolicy = PolicyHelpers.GetServiceClientSimpleSchedulePolicy(
544+
(CmdletModel.SimpleSchedulePolicy)schedulePolicy);
545+
azureFileShareProtectionPolicy.TimeZone = DateTimeKind.Utc.ToString();
546+
azureFileShareProtectionPolicy.WorkLoadType = ConversionUtils.GetServiceClientWorkloadType(workloadType.ToString());
547+
serviceClientRequest.Properties = azureFileShareProtectionPolicy;
548+
}
549+
550+
return ServiceClientAdapter.CreateOrUpdateProtectionPolicy(
551+
policyName = policyName ?? policy.Name,
552+
serviceClientRequest,
553+
vaultName: vaultName,
554+
resourceGroupName: resourceGroupName);
438555
}
439556

440557
public RPMountScriptDetails ProvisionItemLevelRecoveryAccess()
@@ -466,12 +583,28 @@ public void DeletePolicy()
466583

467584
public SchedulePolicyBase GetDefaultSchedulePolicyObject()
468585
{
469-
throw new NotImplementedException();
586+
CmdletModel.SimpleSchedulePolicy defaultSchedule = new CmdletModel.SimpleSchedulePolicy();
587+
defaultSchedule.ScheduleRunFrequency = CmdletModel.ScheduleRunType.Daily;
588+
589+
DateTime scheduleTime = AzureWorkloadProviderHelper.GenerateRandomScheduleTime();
590+
defaultSchedule.ScheduleRunTimes = new List<DateTime>();
591+
defaultSchedule.ScheduleRunTimes.Add(scheduleTime);
592+
593+
return defaultSchedule;
470594
}
471595

472596
public RetentionPolicyBase GetDefaultRetentionPolicyObject()
473597
{
474-
throw new NotImplementedException();
598+
CmdletModel.LongTermRetentionPolicy defaultRetention = new CmdletModel.LongTermRetentionPolicy();
599+
DateTime retentionTime = AzureWorkloadProviderHelper.GenerateRandomScheduleTime();
600+
601+
//Daily Retention policy
602+
defaultRetention.IsDailyScheduleEnabled = true;
603+
defaultRetention.DailySchedule = new CmdletModel.DailyRetentionSchedule();
604+
defaultRetention.DailySchedule.RetentionTimes = new List<DateTime>();
605+
defaultRetention.DailySchedule.RetentionTimes.Add(retentionTime);
606+
defaultRetention.DailySchedule.DurationCountInDays = defaultFileRetentionCount;
607+
return defaultRetention;
475608
}
476609

477610
public List<ItemBase> ListProtectedItems()
@@ -611,5 +744,19 @@ private void ValidateAzureStorageBackupManagementType(
611744
backupManagementType.ToString()));
612745
}
613746
}
747+
748+
private void ValidateAzureFileProtectionPolicy(PolicyBase policy)
749+
{
750+
if (policy == null || policy.GetType() != typeof(AzureFileSharePolicy))
751+
{
752+
throw new ArgumentException(string.Format(Resources.InvalidProtectionPolicyException,
753+
typeof(AzureFileSharePolicy).ToString()));
754+
}
755+
756+
ValidateAzureFilesWorkloadType(policy.WorkloadType);
757+
758+
// call validation
759+
policy.Validate();
760+
}
614761
}
615762
}

0 commit comments

Comments
 (0)