Skip to content

Commit 12c40a8

Browse files
committed
AdvanceSchedule CMdlet support: after feedback
1 parent 8907902 commit 12c40a8

File tree

4 files changed

+219
-87
lines changed

4 files changed

+219
-87
lines changed

src/ResourceManager/Automation/Commands.Automation/Cmdlet/NewAzureAutomationSchedule.cs

Lines changed: 94 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -62,27 +62,26 @@ public NewAzureAutomationSchedule()
6262
/// <summary>
6363
/// Gets or sets the schedule days of the week.
6464
/// </summary>
65-
[Parameter(Mandatory = false, ValueFromPipelineByPropertyName = true, HelpMessage = "The list of days of the week for weekly schedule.")]
66-
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByWeekly, Mandatory = false, HelpMessage = "The weekly schedule days of the week.")]
67-
public DayOfWeek[] WeekDays { get; set; }
65+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByWeekly, Mandatory = false, HelpMessage = "The list of days of the week for weekly schedule.")]
66+
public DayOfWeek[] DaysOfWeek { get; set; }
6867

6968
/// <summary>
7069
/// Gets or sets the schedule days of the month.
7170
/// </summary>
72-
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyWithMonthDays, Mandatory = false, HelpMessage = "The list of days of the month for monthly schedule.")]
73-
public int[] MonthDays { get; set; }
71+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyDaysOfMonth, Mandatory = false, HelpMessage = "The list of days of the month for monthly schedule.")]
72+
public DaysOfMonth[] DaysOfMonth { get; set; }
7473

7574
/// <summary>
7675
/// Gets or sets the schedule day of the week.
7776
/// </summary>
78-
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyWithMonthlyOccurrence, Mandatory = false, HelpMessage = "The day of week for monthly occurrence.")]
77+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyDayOfWeek, Mandatory = false, HelpMessage = "The day of week for monthly occurrence.")]
7978
public DayOfWeek? DayOfWeek { get; set; }
8079

8180
/// <summary>
8281
/// Gets or sets the schedule day of the week.
8382
/// </summary>
84-
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyWithMonthlyOccurrence, Mandatory = false, HelpMessage = "Occurrence of the week within the month.")]
85-
public int? Occurrence { get; set; }
83+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyDayOfWeek, Mandatory = false, HelpMessage = "The Occurrence of the week within the month.")]
84+
public DayOfWeekOccurrence DayOfWeekOccurrence { get; set; }
8685

8786
/// <summary>
8887
/// Gets or sets the switch parameter to create a one time schedule.
@@ -96,8 +95,8 @@ public NewAzureAutomationSchedule()
9695
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByDaily, Mandatory = false, HelpMessage = "The schedule expiry time.")]
9796
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByHourly, Mandatory = false, HelpMessage = "The schedule expiry time.")]
9897
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByWeekly, Mandatory = false, HelpMessage = "The schedule expiry time.")]
99-
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyWithMonthDays, Mandatory = false, HelpMessage = "The schedule expiry time.")]
100-
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyWithMonthlyOccurrence, Mandatory = false, HelpMessage = "The schedule expiry time.")]
98+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyDaysOfMonth, Mandatory = false, HelpMessage = "The schedule expiry time.")]
99+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyDayOfWeek, Mandatory = false, HelpMessage = "The schedule expiry time.")]
101100
public DateTimeOffset ExpiryTime { get; set; }
102101

103102
/// <summary>
@@ -124,8 +123,8 @@ public NewAzureAutomationSchedule()
124123
/// <summary>
125124
/// Gets or sets the weekly schedule week interval.
126125
/// </summary>
127-
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyWithMonthDays, Mandatory = true, HelpMessage = "The monthly schedule month interval.")]
128-
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyWithMonthlyOccurrence, Mandatory = true, HelpMessage = "The monthly schedule month interval.")]
126+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyDaysOfMonth, Mandatory = true, HelpMessage = "The monthly schedule month interval.")]
127+
[Parameter(ParameterSetName = AutomationCmdletParameterSets.ByMonthlyDayOfWeek, Mandatory = true, HelpMessage = "The monthly schedule month interval.")]
129128
[ValidateRange(1, byte.MaxValue)]
130129
public byte MonthInterval { get; set; }
131130

@@ -157,13 +156,13 @@ protected override void AutomationProcessRecord()
157156
schedule.Interval = this.HourInterval;
158157
break;
159158
case AutomationCmdletParameterSets.ByWeekly:
160-
schedule = this.CreateWeeklyScheduleModel(schedule);
159+
schedule = this.CreateWeeklyScheduleModel();
161160
break;
162-
case AutomationCmdletParameterSets.ByMonthlyWithMonthlyOccurrence:
163-
schedule = this.CreateMonthlyScheduleModel(schedule);
161+
case AutomationCmdletParameterSets.ByMonthlyDayOfWeek:
162+
schedule = this.CreateMonthlyScheduleModel();
164163
break;
165-
case AutomationCmdletParameterSets.ByMonthlyWithMonthDays:
166-
schedule = this.CreateMonthlyScheduleModel(schedule);
164+
case AutomationCmdletParameterSets.ByMonthlyDaysOfMonth:
165+
schedule = this.CreateMonthlyScheduleModel();
167166
break;
168167
}
169168

@@ -174,52 +173,109 @@ protected override void AutomationProcessRecord()
174173
/// <summary>
175174
/// The validate.
176175
/// </summary>
177-
/// <param name="schedule">
178-
/// The schedule.
179-
/// </param>
180176
/// <returns>
181177
/// The <see cref="Schedule"/>.
182178
/// </returns>
183179
/// <exception cref="Exception">
184180
/// throws exception
185181
/// </exception>
186-
private Schedule CreateMonthlyScheduleModel(Schedule schedule)
182+
private Schedule CreateMonthlyScheduleModel()
187183
{
188184
var dayOfWeek = this.DayOfWeek.HasValue ? this.DayOfWeek.ToString() : null;
189-
if ((!string.IsNullOrWhiteSpace(dayOfWeek) && this.Occurrence == null) || (string.IsNullOrWhiteSpace(dayOfWeek) && this.Occurrence != null))
185+
if ((!string.IsNullOrWhiteSpace(dayOfWeek) && this.DayOfWeekOccurrence == 0) || (string.IsNullOrWhiteSpace(dayOfWeek) && this.DayOfWeekOccurrence != 0))
190186
{
191187
throw new Exception("for monthly occurrence, both day of week and occurrence need to be specified");
192188
}
193189

194-
var newSchedule = schedule;
195-
newSchedule.Frequency = ScheduleFrequency.Month;
196-
newSchedule.Interval = this.MonthInterval;
197-
newSchedule.MonthDays = this.MonthDays == null ? null : this.MonthDays.ToList();
198-
newSchedule.DayOfWeek = dayOfWeek;
199-
newSchedule.Occurrence = this.Occurrence;
190+
var newSchedule = new Schedule
191+
{
192+
Name = this.Name,
193+
StartTime = this.StartTime,
194+
Description = this.Description,
195+
ExpiryTime = this.ExpiryTime,
196+
Frequency = ScheduleFrequency.Month,
197+
Interval = this.MonthInterval,
198+
DaysOfMonth = this.DaysOfMonth,
199+
DayOfWeek = dayOfWeek,
200+
DayOfWeekOccurrence = this.DayOfWeekOccurrence == 0 ? null : this.DayOfWeekOccurrence.ToString()
201+
};
200202

201203
return newSchedule;
202204
}
203205

204206
/// <summary>
205207
/// The create weekly schedule model.
206208
/// </summary>
207-
/// <param name="schedule">
208-
/// The schedule.
209-
/// </param>
210209
/// <returns>
211210
/// The <see cref="Schedule"/>.
212211
/// </returns>
213-
private Schedule CreateWeeklyScheduleModel(Schedule schedule)
212+
private Schedule CreateWeeklyScheduleModel()
214213
{
215-
var newSchedule = schedule;
216-
newSchedule.Frequency = ScheduleFrequency.Week;
217-
newSchedule.Interval = this.WeekInterval;
218-
newSchedule.WeekDays = this.WeekDays == null
219-
? null
220-
: this.WeekDays.Select(day => day.ToString()).ToList();
214+
var newSchedule = new Schedule
215+
{
216+
Name = this.Name,
217+
StartTime = this.StartTime,
218+
Description = this.Description,
219+
ExpiryTime = this.ExpiryTime,
220+
Frequency = ScheduleFrequency.Week,
221+
Interval = this.WeekInterval,
222+
DaysOfWeek = this.DaysOfWeek == null
223+
? null
224+
: this.DaysOfWeek.Select(day => day.ToString()).ToList()
225+
};
221226

222227
return newSchedule;
223228
}
224229
}
230+
231+
/// <summary>
232+
/// The day of week occurrence.
233+
/// </summary>
234+
public enum DayOfWeekOccurrence
235+
{
236+
First = 1,
237+
Second = 2,
238+
Third = 3,
239+
Fourth = 4,
240+
Last = -1
241+
}
242+
243+
/// <summary>
244+
/// The day of week occurrence.
245+
/// </summary>
246+
public enum DaysOfMonth
247+
{
248+
One = 1,
249+
Two = 2,
250+
Three = 3,
251+
Four = 4,
252+
Five = 5,
253+
Six = 6,
254+
Seventh = 7,
255+
Eighth = 8,
256+
Ninth = 9,
257+
Tenth = 10,
258+
Eleventh =11,
259+
Twelfth =12,
260+
Thirteenth = 13,
261+
Fourteenth = 14,
262+
Fifteenth = 15,
263+
Sixteenth = 16,
264+
Seventeenth = 17,
265+
Eighteenth = 18,
266+
Nineteenth = 19,
267+
Twentieth = 20,
268+
TwentyFirst = 21,
269+
TwentySecond = 22,
270+
TwentyThird = 23,
271+
TwentyFourth = 24,
272+
TwentyFifth = 25,
273+
TwentySixth = 26,
274+
TwentySeventh = 27,
275+
TwentyEighth = 28,
276+
TwentyNinth = 29,
277+
Thirtieth = 30,
278+
ThirtyFirst = 31,
279+
LastDay = -1
280+
}
225281
}

src/ResourceManager/Automation/Commands.Automation/Common/AutomationClient.cs

Lines changed: 1 addition & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -336,7 +336,7 @@ public Schedule CreateSchedule(string resourceGroupName, string automationAccoun
336336
Description = schedule.Description,
337337
Interval = schedule.Interval,
338338
Frequency = schedule.Frequency.ToString(),
339-
AdvancedSchedule = this.CreateAdvancedSchedule(schedule)
339+
AdvancedSchedule = schedule.GetAdvancedSchedule()
340340
}
341341
};
342342

@@ -1571,40 +1571,6 @@ private Schedule CreateScheduleFromScheduleModel(string resourceGroupName, strin
15711571
return new Schedule(resourceGroupName, automationAccountName, schedule);
15721572
}
15731573

1574-
private AdvancedSchedule CreateAdvancedSchedule(Schedule schedule)
1575-
{
1576-
if (this.AdvancedScheduleIsNull(schedule))
1577-
{
1578-
return null;
1579-
}
1580-
1581-
var advancedSchedule = new AdvancedSchedule()
1582-
{
1583-
WeekDays = schedule.WeekDays,
1584-
MonthDays = schedule.MonthDays,
1585-
MonthlyOccurrences = string.IsNullOrWhiteSpace(schedule.DayOfWeek) && schedule.Occurrence == null
1586-
? null
1587-
: new AdvancedScheduleMonthlyOccurrence[]
1588-
{
1589-
new AdvancedScheduleMonthlyOccurrence()
1590-
{
1591-
Day = schedule.DayOfWeek,
1592-
Occurrence = schedule.Occurrence
1593-
}
1594-
}
1595-
};
1596-
1597-
return advancedSchedule;
1598-
}
1599-
1600-
private bool AdvancedScheduleIsNull(Schedule schedule)
1601-
{
1602-
return (schedule.WeekDays == null
1603-
&& schedule.MonthDays == null
1604-
&& string.IsNullOrWhiteSpace(schedule.DayOfWeek)
1605-
&&schedule.Occurrence == null);
1606-
}
1607-
16081574
private JobSchedule CreateJobScheduleFromJobScheduleModel(string resourceGroupName, string automationAccountName,
16091575
AutomationManagement.Models.JobSchedule jobSchedule)
16101576
{

src/ResourceManager/Automation/Commands.Automation/Common/AutomationCmdletParameterSet.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ internal static class AutomationCmdletParameterSets
6767
/// <summary>
6868
/// The monthly schedule parameter set.
6969
/// </summary>
70-
internal const string ByMonthlyWithMonthDays = "ByMonthlyWithMonthDays";
70+
internal const string ByMonthlyDaysOfMonth = "ByMonthlyDaysOfMonth";
7171

7272
/// <summary>
7373
/// The monthly schedule parameter set.
7474
/// </summary>
75-
internal const string ByMonthlyWithMonthlyOccurrence = "ByMonthlyWithMonthlyOccurrence";
75+
internal const string ByMonthlyDayOfWeek = "ByMonthlyDayOfWeek";
7676

7777
/// <summary>
7878
/// The Job Id parameter set.

0 commit comments

Comments
 (0)