@@ -37,6 +37,9 @@ public class AzureFilesPsBackupProvider : IPsBackupProvider
37
37
{
38
38
private const int defaultOperationStatusRetryTimeInMilliSec = 5 * 1000 ; // 5 sec
39
39
private const string separator = ";" ;
40
+ private const CmdletModel . RetentionDurationType defaultFileRetentionType =
41
+ CmdletModel . RetentionDurationType . Days ;
42
+ private const int defaultFileRetentionCount = 30 ;
40
43
41
44
Dictionary < Enum , object > ProviderData { get ; set ; }
42
45
@@ -429,12 +432,126 @@ public List<RecoveryPointBase> ListRecoveryPoints()
429
432
430
433
public ProtectionPolicyResource CreatePolicy ( )
431
434
{
432
- throw new NotImplementedException ( ) ;
435
+ return CreateorModifyPolicy ( ) . Body ;
433
436
}
434
437
435
438
public RestAzureNS . AzureOperationResponse < ProtectionPolicyResource > ModifyPolicy ( )
436
439
{
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 ) ;
438
555
}
439
556
440
557
public RPMountScriptDetails ProvisionItemLevelRecoveryAccess ( )
@@ -466,12 +583,28 @@ public void DeletePolicy()
466
583
467
584
public SchedulePolicyBase GetDefaultSchedulePolicyObject ( )
468
585
{
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 ;
470
594
}
471
595
472
596
public RetentionPolicyBase GetDefaultRetentionPolicyObject ( )
473
597
{
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 ;
475
608
}
476
609
477
610
public List < ItemBase > ListProtectedItems ( )
@@ -611,5 +744,19 @@ private void ValidateAzureStorageBackupManagementType(
611
744
backupManagementType . ToString ( ) ) ) ;
612
745
}
613
746
}
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
+ }
614
761
}
615
762
}
0 commit comments