@@ -205,11 +205,13 @@ protected override void ProcessRecordInternal()
205
205
case ListActionRulesParameterSet :
206
206
case ListActionRulesByTargetResourceIdParameterSet :
207
207
case ListActionRulesByTargetResourceGroupParameterSet :
208
- IPage < ActionRule > actionRuleList = new Page < ActionRule > ( ) ;
208
+ IPage < ActionRule > pageResult = new Page < ActionRule > ( ) ;
209
209
List < ActionRule > resultList = new List < ActionRule > ( ) ;
210
+ bool listByResourceGroup = false ;
211
+
210
212
if ( string . IsNullOrWhiteSpace ( ResourceGroupName ) )
211
213
{
212
- actionRuleList = this . AlertsManagementClient . ActionRules . ListBySubscriptionWithHttpMessagesAsync (
214
+ pageResult = this . AlertsManagementClient . ActionRules . ListBySubscriptionWithHttpMessagesAsync (
213
215
targetResource : TargetResourceId ,
214
216
targetResourceType : TargetResourceType ,
215
217
targetResourceGroup : TargetResourceGroup ,
@@ -222,22 +224,11 @@ protected override void ProcessRecordInternal()
222
224
name : Name
223
225
) . Result . Body ;
224
226
225
- // Deal with paging in response
226
- resultList = actionRuleList . ToList ( ) ;
227
- var nextPageLink = actionRuleList . NextPageLink ;
228
- while ( ! string . IsNullOrEmpty ( nextPageLink ) )
229
- {
230
- var pageResult = this . AlertsManagementClient . ActionRules . ListBySubscriptionNextWithHttpMessagesAsync ( nextPageLink ) ;
231
- foreach ( var pageItem in pageResult . Result . Body )
232
- {
233
- resultList . Add ( pageItem ) ;
234
- }
235
- nextPageLink = pageResult . Result . Body . NextPageLink ;
236
- }
227
+ listByResourceGroup = false ;
237
228
}
238
229
else
239
230
{
240
- actionRuleList = this . AlertsManagementClient . ActionRules . ListByResourceGroupWithHttpMessagesAsync (
231
+ pageResult = this . AlertsManagementClient . ActionRules . ListByResourceGroupWithHttpMessagesAsync (
241
232
resourceGroupName : ResourceGroupName ,
242
233
targetResource : TargetResourceId ,
243
234
targetResourceType : TargetResourceType ,
@@ -251,24 +242,58 @@ protected override void ProcessRecordInternal()
251
242
name : Name
252
243
) . Result . Body ;
253
244
254
- // Deal with paging in response
255
- resultList = actionRuleList . ToList ( ) ;
256
- var nextPageLink = actionRuleList . NextPageLink ;
257
- while ( ! string . IsNullOrEmpty ( nextPageLink ) )
245
+ listByResourceGroup = true ;
246
+ }
247
+
248
+ // Deal with paging in response
249
+ ulong first = MyInvocation . BoundParameters . ContainsKey ( "First" ) ? this . PagingParameters . First : ulong . MaxValue ;
250
+ ulong skip = MyInvocation . BoundParameters . ContainsKey ( "Skip" ) ? this . PagingParameters . Skip : 0 ;
251
+
252
+ // Any items before this count should be return
253
+ ulong lastCount = MyInvocation . BoundParameters . ContainsKey ( "First" ) ? skip + first : ulong . MaxValue ;
254
+ ulong currentCount = 0 ;
255
+ var nextPageLink = pageResult . NextPageLink ;
256
+
257
+ do
258
+ {
259
+ List < ActionRule > tempList = pageResult . ToList ( ) ;
260
+ if ( currentCount + ( ulong ) tempList . Count - 1 < skip )
261
+ {
262
+ // skip the whole chunk if they are all in skip
263
+ currentCount += ( ulong ) tempList . Count ;
264
+ }
265
+ else
258
266
{
259
- var pageResult = this . AlertsManagementClient . ActionRules . ListByResourceGroupNextWithHttpMessagesAsync ( nextPageLink ) ;
260
- foreach ( var pageItem in pageResult . Result . Body )
267
+ foreach ( ActionRule currentActionRule in tempList )
261
268
{
262
- resultList . Add ( pageItem ) ;
269
+ // not return "skip" count of items in the begin, and only return "first" count of items after that.
270
+ if ( currentCount >= skip && currentCount < lastCount )
271
+ {
272
+ resultList . Add ( currentActionRule ) ;
273
+ }
274
+ currentCount ++ ;
275
+ if ( currentCount >= lastCount )
276
+ {
277
+ break ;
278
+ }
263
279
}
264
- nextPageLink = pageResult . Result . Body . NextPageLink ;
265
280
}
266
- }
267
281
268
- foreach ( var result in resultList )
269
- {
270
- WriteObject ( result . Properties . GetType ( ) ) ;
271
- }
282
+ if ( ! string . IsNullOrEmpty ( nextPageLink ) )
283
+ {
284
+ if ( listByResourceGroup )
285
+ {
286
+ pageResult = this . AlertsManagementClient . ActionRules . ListByResourceGroupNextWithHttpMessagesAsync ( nextPageLink ) . Result . Body ;
287
+ }
288
+ else
289
+ {
290
+ pageResult = this . AlertsManagementClient . ActionRules . ListBySubscriptionNextWithHttpMessagesAsync ( nextPageLink ) . Result . Body ;
291
+ }
292
+ nextPageLink = pageResult . NextPageLink ;
293
+ }
294
+
295
+ } while ( ! string . IsNullOrEmpty ( nextPageLink ) && currentCount < lastCount ) ;
296
+
272
297
WriteObject ( resultList . Select ( ( r ) => new PSActionRule ( r ) ) , enumerateCollection : true ) ;
273
298
break ;
274
299
0 commit comments