/**
 * Copyright (c) 2020, 2021 Oracle and/or its affiliates.  All rights reserved.
 * This software is dual-licensed to you under the Universal Permissive License (UPL) 1.0 as shown at https://oss.oracle.com/licenses/upl or Apache License 2.0 as shown at http://www.apache.org/licenses/LICENSE-2.0. You may choose either license.
 */ import common = require ( "oci-common" ) ; import * as identity from "oci-identity" ; import { paginatedRecordsWithLimit , paginatedResponsesWithLimit } from "oci-common" ; const provider = new common . ConfigFileAuthenticationDetailsProvider ( ) ; const compartmentId = provider . getTenantId ( ) ; ( async ( ) => { const identityClient = new identity . IdentityClient ( { authenticationDetailsProvider : provider } ) ; let listUserReq ; listUserReq = { compartmentId : compartmentId } ; console . log ( "---testing load all compartments---" ) ; const listCompartmentsRequest = { compartmentId : compartmentId } ; let compartmentsList = await identityClient . listAllCompartments ( listCompartmentsRequest ) ; console . log ( "comartmentsList: " , compartmentsList ) ; console . log ( "--- Eager load all results ---" ) ; let eagerLoad = await paginatedRecordsWithLimit ( listUserReq , req => identityClient . listUsers ( listUserReq ) ) ; eagerLoad . forEach ( e => { console . log ( `User: ${ e . value . name }` ) ; } ) ; console . log ( "Total Records: " , eagerLoad . length ) ; console . log ( "--- Eager load up to limit ---" ) ; listUserReq = { compartmentId : compartmentId } ; let eagerLoadLimit = await paginatedRecordsWithLimit ( listUserReq , req => identityClient . listUsers ( listUserReq ) , 10 ) ; eagerLoadLimit . forEach ( e => { console . log ( `User: ${ e . value . name }` ) ; } ) ; console . log ( "Total Records " , eagerLoadLimit . length ) ; console . log ( "--- Lazy load all results - yield values ---" ) ; listUserReq = { compartmentId : compartmentId , limit : 3 } ; let lazyLoadAllValues = await paginatedRecordsWithLimit ( listUserReq , req => identityClient . listUsers ( listUserReq ) ) ; for ( let i = 0 ; i < lazyLoadAllValues . length ; i ++ ) { let record = lazyLoadAllValues [ i ] ; console . log ( `User: ${ record . value . name }` ) ; } console . log ( `Total Results: ` , lazyLoadAllValues . length ) ; console . log ( "--- Lazy load all results - yield raw responses ---" ) ; listUserReq = { compartmentId : compartmentId , limit : 3 } ; let countRawResponse = 0 ; const lazyLoadAllResponses = await paginatedResponsesWithLimit ( listUserReq , req => identityClient . listUsers ( listUserReq ) ) ; for ( let i = 0 ; i < lazyLoadAllResponses . length ; i ++ ) { let record = lazyLoadAllResponses [ i ] ; record . items . forEach ( ( item ) => { countRawResponse ++ ; console . log ( `Iteration: ${ i }, User: ${ item . name }` ) ; } ) ; } console . log ( `Total Results: ` , countRawResponse ) ; console . log ( "--- Lazy load up to limit - yield values ---" ) ; listUserReq = { compartmentId : compartmentId , limit : 3 } ; const lazyLoadValuesLimit = await paginatedRecordsWithLimit ( listUserReq , req => identityClient . listUsers ( req ) , 7 ) ; for ( let i = 0 ; i < lazyLoadValuesLimit . length ; i ++ ) { let record = lazyLoadValuesLimit [ i ] ; console . log ( `User: ${ record . value . name }` ) ; } console . log ( `Total Results: ` , lazyLoadValuesLimit . length ) ; console . log ( "--- Lazy load up to limit - yield raw responses ---" ) ; listUserReq = { compartmentId : compartmentId , limit : 3 } ; const lazyLoadResponsesLimit = await paginatedResponsesWithLimit ( listUserReq , req => identityClient . listUsers ( req ) , 7 ) ; let rawResponse = 0 ; for ( let i = 0 ; i < lazyLoadResponsesLimit . length ; i ++ ) { let record = lazyLoadResponsesLimit [ i ] ; record . items . forEach ( ( item ) => { rawResponse ++ ; console . log ( `Iteration: ${ i }, User: ${ item . name }` ) ; } ) ; } console . log ( "Total Results: " , rawResponse ) ; } ) ( ) ;