/**
 * 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 * as identity from "oci-identity" ; import * as audit from "oci-audit" ; import common = require ( "oci-common" ) ; const provider = new common . ConfigFileAuthenticationDetailsProvider ( ) ; const compartmentId = provider . getTenantId ( ) ; let identityClient ; let auditClient ; export async function getSubscriptionRegions ( tenancyId ) { const listRegionSubscriptionsRequest = { tenancyId : tenancyId } ; const regions = await identityClient . listRegionSubscriptions ( listRegionSubscriptionsRequest ) ; return regions . items . map ( region => { return region . regionName ; } ) ; } export async function getCompartments ( tenancyId ) { const compartmentOcids = [ tenancyId ] ; const listCompartmentsRequest = { compartmentId : tenancyId } ; const compartments = await identityClient . listCompartments ( listCompartmentsRequest ) ; for ( let i = 0 ; i < compartments . items . length ; i ++ ) { compartmentOcids . push ( compartments . items [ i ] . id ) ; } return compartmentOcids ; } async function getAuditEvents ( audit , compartmentOcids , startTime , endTime ) { const listOfAuditEvents = [ ] ; for ( let i = 0 ; i < compartmentOcids . length ; i ++ ) { const listEventsRequest = { compartmentId : compartmentOcids [ i ] , startTime : startTime , endTime : endTime } ; try { const response = await audit . listEvents ( listEventsRequest ) ; listOfAuditEvents . push ( response . items ) ; } catch ( err ) { } } return listOfAuditEvents ; } ( async ( ) => { identityClient = new identity . IdentityClient ( { authenticationDetailsProvider : provider } ) ; auditClient = new audit . AuditClient ( { authenticationDetailsProvider : provider } ) ; const endTime = new Date ( ) ; const offset = new Date ( ) . setDate ( new Date ( ) . getDate ( ) - 5 ) ; const startTime = new Date ( offset ) ; const regions = await getSubscriptionRegions ( compartmentId ) ; const compartments = await getCompartments ( compartmentId ) ; for ( let region of regions ) { const x = common . Region . fromRegionId ( region ) ; auditClient . region = x ; const auditEvents = await getAuditEvents ( auditClient , compartments , startTime , endTime ) ; console . log ( "auditEvent: " , auditEvents ) ; } } ) ( ) ;