@@ -24,37 +24,44 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
24
24
/// <summary>
25
25
/// Create new protection policy
26
26
/// </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
+
30
34
[ Parameter ( Position = 3 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . PolicyName ) ]
31
35
[ ValidateNotNullOrEmpty ]
32
36
public string Name { get ; set ; }
33
37
34
38
[ Parameter ( Position = 4 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . WorkloadType , ValueFromPipelineByPropertyName = true ) ]
35
- [ ValidateSet ( "VM" ) ]
39
+ [ ValidateSet ( "VM" , IgnoreCase = true ) ]
36
40
public string WorkloadType { get ; set ; }
37
41
38
42
[ Parameter ( Position = 5 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . BackupType , ValueFromPipelineByPropertyName = true ) ]
39
- [ ValidateSet ( "Full" ) ]
43
+ [ ValidateSet ( "Full" , IgnoreCase = true ) ]
40
44
public string BackupType { get ; set ; }
41
45
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 ; }
45
51
46
- [ Parameter ( Position = 7 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunTimes , ValueFromPipelineByPropertyName = true ) ]
52
+ [ Parameter ( Position = 8 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunTimes , ValueFromPipelineByPropertyName = true ) ]
47
53
public DateTime ScheduleRunTimes { get ; set ; }
48
54
49
- [ Parameter ( Position = 8 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . RetentionType , ValueFromPipelineByPropertyName = true ) ]
55
+ [ Parameter ( Position = 9 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . RetentionType , ValueFromPipelineByPropertyName = true ) ]
50
56
[ ValidateSet ( "Days" , IgnoreCase = true ) ]
51
57
public string RetentionType { get ; set ; }
52
58
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 ) ]
55
60
public int RetentionDuration { get ; set ; }
56
61
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 ]
58
65
[ ValidateSet ( "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" , IgnoreCase = true ) ]
59
66
public string [ ] ScheduleRunDays { get ; set ; }
60
67
@@ -66,29 +73,38 @@ public override void ExecuteCmdlet()
66
73
{
67
74
WriteDebug ( "Making client call" ) ;
68
75
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 ) ;
71
80
72
81
var addProtectionPolicyRequest = new AddProtectionPolicyRequest ( ) ;
73
82
addProtectionPolicyRequest . PolicyName = this . Name ;
74
83
addProtectionPolicyRequest . Schedule = backupSchedule ;
75
- addProtectionPolicyRequest . WorkloadType = this . WorkloadType ;
84
+ addProtectionPolicyRequest . WorkloadType = Enum . Parse ( typeof ( WorkloadType ) , this . WorkloadType , true ) . ToString ( ) ;
76
85
77
86
var operationId = AzureBackupClient . ProtectionPolicy . AddAsync ( addProtectionPolicyRequest , GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
87
+ WriteDebug ( "Protection policy created successfully" ) ;
78
88
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 ) ;
88
90
WriteDebug ( "Converting response" ) ;
89
- WriteAzureBackupProtectionPolicy ( policyObjects ) ;
91
+ WriteObject ( policyInfo ) ;
90
92
} ) ;
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
+ }
92
108
}
93
109
}
94
110
0 commit comments