@@ -24,37 +24,44 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
2424 /// <summary>
2525 /// Create new protection policy
2626 /// </summary>
27- [ Cmdlet ( VerbsCommon . Add , "AzureBackupProtectionPolicy" ) , OutputType ( typeof ( AzureBackupProtectionPolicy ) ) ]
28- public class NewAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
29- {
27+ [ Cmdlet ( VerbsCommon . New , "AzureBackupProtectionPolicy" , DefaultParameterSetName = NoScheduleParamSet ) , OutputType ( typeof ( AzureBackupProtectionPolicy ) ) ]
28+ public class NewAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
29+ {
30+ protected const string WeeklyScheduleParamSet = "WeeklyScheduleParamSet" ;
31+ protected const string DailyScheduleParamSet = "DailyScheduleParamSet" ;
32+ protected const string NoScheduleParamSet = "DailyScheduleParamSet" ;
33+
3034 [ Parameter ( Position = 3 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . PolicyName ) ]
3135 [ ValidateNotNullOrEmpty ]
3236 public string Name { get ; set ; }
3337
3438 [ Parameter ( Position = 4 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . WorkloadType , ValueFromPipelineByPropertyName = true ) ]
35- [ ValidateSet ( "VM" ) ]
39+ [ ValidateSet ( "VM" , IgnoreCase = true ) ]
3640 public string WorkloadType { get ; set ; }
3741
3842 [ Parameter ( Position = 5 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . BackupType , ValueFromPipelineByPropertyName = true ) ]
39- [ ValidateSet ( "Full" ) ]
43+ [ ValidateSet ( "Full" , IgnoreCase = true ) ]
4044 public string BackupType { get ; set ; }
4145
42- [ Parameter ( Position = 6 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleType , ValueFromPipelineByPropertyName = true ) ]
43- [ ValidateSet ( "Daily" , "Weekly" ) ]
44- public string ScheduleType { get ; set ; }
46+ [ Parameter ( ParameterSetName = DailyScheduleParamSet , Position = 6 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . DailyScheduleType ) ]
47+ public SwitchParameter Daily { get ; set ; }
48+
49+ [ Parameter ( ParameterSetName = WeeklyScheduleParamSet , Position = 7 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . WeeklyScheduleType ) ]
50+ public SwitchParameter Weekly { get ; set ; }
4551
46- [ Parameter ( Position = 7 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunTimes , ValueFromPipelineByPropertyName = true ) ]
52+ [ Parameter ( Position = 8 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunTimes , ValueFromPipelineByPropertyName = true ) ]
4753 public DateTime ScheduleRunTimes { get ; set ; }
4854
49- [ Parameter ( Position = 8 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . RetentionType , ValueFromPipelineByPropertyName = true ) ]
55+ [ Parameter ( Position = 9 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . RetentionType , ValueFromPipelineByPropertyName = true ) ]
5056 [ ValidateSet ( "Days" , IgnoreCase = true ) ]
5157 public string RetentionType { get ; set ; }
5258
53- [ Parameter ( Position = 9 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . RententionDuration , ValueFromPipelineByPropertyName = true ) ]
54- [ ValidateRange ( 1 , 30 ) ]
59+ [ Parameter ( Position = 10 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . RententionDuration , ValueFromPipelineByPropertyName = true ) ]
5560 public int RetentionDuration { get ; set ; }
5661
57- [ Parameter ( Position = 10 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunDays , ValueFromPipelineByPropertyName = true ) ]
62+ [ Parameter ( ParameterSetName = WeeklyScheduleParamSet , Position = 11 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunDays , ValueFromPipelineByPropertyName = true ) ]
63+ [ Parameter ( ParameterSetName = NoScheduleParamSet , Position = 11 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunDays , ValueFromPipelineByPropertyName = true ) ]
64+ [ AllowEmptyCollection ]
5865 [ ValidateSet ( "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" , IgnoreCase = true ) ]
5966 public string [ ] ScheduleRunDays { get ; set ; }
6067
@@ -66,29 +73,38 @@ public override void ExecuteCmdlet()
6673 {
6774 WriteDebug ( "Making client call" ) ;
6875
69- var backupSchedule = GetBackupSchedule ( BackupType , ScheduleType , ScheduleRunTimes ,
70- RetentionType , RetentionDuration , ScheduleRunDays ) ;
76+ var ScheduleType = GetScheduelType ( ScheduleRunDays ) ;
77+
78+ var backupSchedule = azureBackupCmdletHelper . FillBackupSchedule ( BackupType , ScheduleType , ScheduleRunTimes ,
79+ RetentionType , RetentionDuration , ScheduleRunDays ) ;
7180
7281 var addProtectionPolicyRequest = new AddProtectionPolicyRequest ( ) ;
7382 addProtectionPolicyRequest . PolicyName = this . Name ;
7483 addProtectionPolicyRequest . Schedule = backupSchedule ;
75- addProtectionPolicyRequest . WorkloadType = this . WorkloadType ;
84+ addProtectionPolicyRequest . WorkloadType = Enum . Parse ( typeof ( WorkloadType ) , this . WorkloadType , true ) . ToString ( ) ;
7685
7786 var operationId = AzureBackupClient . ProtectionPolicy . AddAsync ( addProtectionPolicyRequest , GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
87+ WriteDebug ( "Protection policy created successfully" ) ;
7888
79- WriteVerbose ( "Protection policy created successfully" ) ;
80-
81- var policyListResponse = AzureBackupClient . ProtectionPolicy . ListAsync ( GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
82-
83- WriteDebug ( "Received policy response" ) ;
84-
85- IEnumerable < ProtectionPolicyInfo > policyObjects = null ;
86- policyObjects = policyListResponse . ProtectionPolicies . Where ( x => x . Name . Equals ( Name , System . StringComparison . InvariantCultureIgnoreCase ) ) ;
87-
89+ AzureBackupProtectionPolicy policyInfo = azureBackupCmdletHelper . GetAzureBackupProtectionPolicyByName ( Name , ResourceGroupName , ResourceName , Location ) ;
8890 WriteDebug ( "Converting response" ) ;
89- WriteAzureBackupProtectionPolicy ( policyObjects ) ;
91+ WriteObject ( policyInfo ) ;
9092 } ) ;
91- }
93+ }
94+
95+ private string GetScheduelType ( string [ ] ScheduleRunDays )
96+ {
97+ WriteDebug ( "ParameterSetName = " + this . ParameterSetName . ToString ( ) ) ;
98+
99+ if ( ScheduleRunDays != null && ScheduleRunDays . Length > 0 )
100+ {
101+ return ScheduleType . Weekly . ToString ( ) ;
102+ }
103+ else
104+ {
105+ return ScheduleType . Daily . ToString ( ) ;
106+ }
107+ }
92108 }
93109}
94110
0 commit comments