@@ -25,139 +25,70 @@ namespace Microsoft.Azure.Commands.AzureBackup.Cmdlets
25
25
/// Create new protection policy
26
26
/// </summary>
27
27
[ Cmdlet ( VerbsCommon . Add , "AzureBackupProtectionPolicy" ) , OutputType ( typeof ( AzureBackupProtectionPolicy ) ) ]
28
- public class NewAzureBackupProtectionPolicy : AzureBackupVaultCmdletBase
29
- {
30
- [ Parameter ( Position = 0 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . PolicyName , ValueFromPipelineByPropertyName = true ) ]
28
+ public class NewAzureBackupProtectionPolicy : AzureBackupPolicyCmdletBase
29
+ {
30
+ [ Parameter ( Position = 3 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . PolicyName ) ]
31
31
[ ValidateNotNullOrEmpty ]
32
32
public string Name { get ; set ; }
33
33
34
- [ Parameter ( Position = 1 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . WorkloadType , ValueFromPipelineByPropertyName = true ) ]
34
+ [ Parameter ( Position = 4 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . WorkloadType , ValueFromPipelineByPropertyName = true ) ]
35
35
[ ValidateSet ( "VM" ) ]
36
36
public string WorkloadType { get ; set ; }
37
37
38
- [ Parameter ( Position = 2 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . BackupType , ValueFromPipelineByPropertyName = true ) ]
38
+ [ Parameter ( Position = 5 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . BackupType , ValueFromPipelineByPropertyName = true ) ]
39
39
[ ValidateSet ( "Full" ) ]
40
40
public string BackupType { get ; set ; }
41
41
42
- [ Parameter ( Position = 3 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleType , ValueFromPipelineByPropertyName = true ) ]
43
- [ ValidateSet ( "Daily" , "Weekly" ) ]
42
+ [ Parameter ( Position = 6 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleType , ValueFromPipelineByPropertyName = true ) ]
43
+ [ ValidateSet ( "Daily" , "Weekly" ) ]
44
44
public string ScheduleType { get ; set ; }
45
45
46
- [ Parameter ( Position = 4 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunDays , ValueFromPipelineByPropertyName = true ) ]
47
- [ AllowEmptyCollection ]
48
- public string [ ] ScheduleRunDays { get ; set ; }
49
-
50
- [ Parameter ( Position = 5 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunTimes , ValueFromPipelineByPropertyName = true ) ]
51
- [ ValidateNotNullOrEmpty ]
46
+ [ Parameter ( Position = 7 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunTimes , ValueFromPipelineByPropertyName = true ) ]
52
47
public DateTime ScheduleRunTimes { get ; set ; }
53
48
54
- [ Parameter ( Position = 6 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . RetentionType , ValueFromPipelineByPropertyName = true ) ]
55
- [ ValidateSet ( "Days" ) ]
49
+ [ Parameter ( Position = 8 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . RetentionType , ValueFromPipelineByPropertyName = true ) ]
50
+ [ ValidateSet ( "Days" , IgnoreCase = true ) ]
56
51
public string RetentionType { get ; set ; }
57
52
58
- [ Parameter ( Position = 7 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . RententionDuration , ValueFromPipelineByPropertyName = true ) ]
53
+ [ Parameter ( Position = 9 , Mandatory = true , HelpMessage = AzureBackupCmdletHelpMessage . RententionDuration , ValueFromPipelineByPropertyName = true ) ]
59
54
[ ValidateRange ( 1 , 30 ) ]
60
55
public int RetentionDuration { get ; set ; }
61
56
57
+ [ Parameter ( Position = 10 , Mandatory = false , HelpMessage = AzureBackupCmdletHelpMessage . ScheduleRunDays , ValueFromPipelineByPropertyName = true ) ]
58
+ [ ValidateSet ( "Monday" , "Tuesday" , "Wednesday" , "Thursday" , "Friday" , "Saturday" , "Sunday" , IgnoreCase = true ) ]
59
+ public string [ ] ScheduleRunDays { get ; set ; }
60
+
62
61
public override void ExecuteCmdlet ( )
63
62
{
64
63
base . ExecuteCmdlet ( ) ;
65
64
66
65
ExecutionBlock ( ( ) =>
67
66
{
68
- WriteVerbose ( "Making client call" ) ;
67
+ WriteDebug ( "Making client call" ) ;
69
68
70
- var backupSchedule = GetBackupSchedule ( ) ;
69
+ var backupSchedule = GetBackupSchedule ( BackupType , ScheduleType , ScheduleRunTimes ,
70
+ RetentionType , RetentionDuration , ScheduleRunDays ) ;
71
71
72
72
var addProtectionPolicyRequest = new AddProtectionPolicyRequest ( ) ;
73
73
addProtectionPolicyRequest . PolicyName = this . Name ;
74
74
addProtectionPolicyRequest . Schedule = backupSchedule ;
75
75
addProtectionPolicyRequest . WorkloadType = this . WorkloadType ;
76
76
77
- var OperationId = AzureBackupClient . ProtectionPolicy . AddAsync ( addProtectionPolicyRequest , GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
77
+ var operationId = AzureBackupClient . ProtectionPolicy . AddAsync ( addProtectionPolicyRequest , GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
78
78
79
79
WriteVerbose ( "Protection policy created successfully" ) ;
80
80
81
81
var policyListResponse = AzureBackupClient . ProtectionPolicy . ListAsync ( GetCustomRequestHeaders ( ) , CmdletCancellationToken ) . Result ;
82
82
83
- WriteVerbose ( "Received policy response" ) ;
83
+ WriteDebug ( "Received policy response" ) ;
84
84
85
85
IEnumerable < ProtectionPolicyInfo > policyObjects = null ;
86
86
policyObjects = policyListResponse . ProtectionPolicies . Where ( x => x . Name . Equals ( Name , System . StringComparison . InvariantCultureIgnoreCase ) ) ;
87
-
88
- WriteVerbose ( "Converting response" ) ;
87
+
88
+ WriteDebug ( "Converting response" ) ;
89
89
WriteAzureBackupProtectionPolicy ( policyObjects ) ;
90
90
} ) ;
91
- }
92
-
93
- public void WriteAzureBackupProtectionPolicy ( IEnumerable < ProtectionPolicyInfo > sourcePolicyList )
94
- {
95
- List < AzureBackupProtectionPolicy > targetList = new List < AzureBackupProtectionPolicy > ( ) ;
96
-
97
- foreach ( var sourcePolicy in sourcePolicyList )
98
- {
99
- targetList . Add ( new AzureBackupProtectionPolicy ( ResourceGroupName , ResourceName , sourcePolicy ) ) ;
100
- }
101
-
102
- this . WriteObject ( targetList , true ) ;
103
- }
104
-
105
- private BackupSchedule GetBackupSchedule ( )
106
- {
107
- WriteVerbose ( "Entering GetBackupSchedule" ) ;
108
-
109
- var backupSchedule = new BackupSchedule ( ) ;
110
-
111
- backupSchedule . BackupType = this . BackupType ;
112
- backupSchedule . RetentionPolicy = GetRetentionPolicy ( ) ;
113
- //Enum.Parse(ScheduleRunType, this.ScheduleType),
114
- backupSchedule . ScheduleRun = this . ScheduleType ;
115
- if ( this . ScheduleType == "Weekly" )
116
- {
117
- backupSchedule . ScheduleRunDays = GetScheduleRunDays ( ) ;
118
- }
119
- backupSchedule . ScheduleRunTimes = new List < DateTime > { this . ScheduleRunTimes } ;
120
- backupSchedule . ScheduleStartTime = this . ScheduleRunTimes ;
121
-
122
- WriteVerbose ( "Exiting GetBackupSchedule" ) ;
123
- return backupSchedule ;
124
- }
125
-
126
- private RetentionPolicy GetRetentionPolicy ( )
127
- {
128
- WriteVerbose ( "Entering RetentionPolicy" ) ;
129
- var retentionPolicy = new RetentionPolicy
130
- {
131
- RetentionType = ( RetentionDurationType ) Enum . Parse ( typeof ( RetentionDurationType ) , this . RetentionType ) ,
132
- RetentionDuration = this . RetentionDuration
133
- } ;
134
-
135
- return retentionPolicy ;
136
- }
137
-
138
- private IList < DayOfWeek > GetScheduleRunDays ( )
139
- {
140
- IList < DayOfWeek > ListofWeekDays = new List < DayOfWeek > ( ) ;
141
-
142
- foreach ( var dayOfWeek in this . ScheduleRunDays )
143
- {
144
- WriteVerbose ( "dayOfWeek" + dayOfWeek . ToString ( ) ) ;
145
- DayOfWeek item = ( DayOfWeek ) Enum . Parse ( typeof ( DayOfWeek ) , dayOfWeek , true ) ;
146
- WriteVerbose ( "Item" + item . ToString ( ) ) ;
147
- if ( ! ListofWeekDays . Contains ( item ) )
148
- {
149
- ListofWeekDays . Add ( item ) ;
150
- }
151
-
152
- else
153
- {
154
- throw new ArgumentException ( string . Format ( "Repeated Days in ScheduleRunDays" ) ) ;
155
- }
156
- }
157
-
158
- return ListofWeekDays ;
159
- }
160
-
91
+ }
161
92
}
162
93
}
163
94
0 commit comments