2021-11-15 13:15:43 +00:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
# include <aws/core/Aws.h>
# include <aws/cloudtrail/CloudTrailClient.h>
# include <aws/cloudtrail/model/LookupEventsRequest.h>
# include <iostream>
2024-04-29 13:26:35 -04:00
# include "cloudtrail_samples.h"
2021-11-15 13:15:43 +00:00
/**
2024-04-29 13:26:35 -04:00
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html
*
* For information on the structure of the code examples and how to build and run the examples, see
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started-code-examples.html.
*
**/
2021-11-15 13:15:43 +00:00
2024-04-29 13:26:35 -04:00
// snippet-start:[cpp.example_code.cloudtrail.LookupEvents]
// Routine which looks up events captured by AWS CloudTrail.
/*!
\param clientConfig: Aws client configuration.
\return bool: Function succeeded.
*/
bool AwsDoc : : CloudTrail : : lookupEvents (
const Aws : : Client : : ClientConfiguration & clientConfig ) {
Aws : : CloudTrail : : CloudTrailClient cloudtrail ( clientConfig ) ;
Aws : : String nextToken ; // Used for pagination.
Aws : : Vector < Aws : : CloudTrail : : Model : : Event > allEvents ;
2021-11-15 13:15:43 +00:00
2024-04-29 13:26:35 -04:00
Aws : : CloudTrail : : Model : : LookupEventsRequest request ;
2021-11-15 13:15:43 +00:00
2024-04-29 13:26:35 -04:00
size_t count = 0 ;
do {
if ( ! nextToken . empty ( ) ) {
request . SetNextToken ( nextToken ) ;
}
2021-11-15 13:15:43 +00:00
2024-04-29 13:26:35 -04:00
Aws : : CloudTrail : : Model : : LookupEventsOutcome outcome = cloudtrail . LookupEvents (
request ) ;
if ( outcome . IsSuccess ( ) ) {
const Aws : : Vector < Aws : : CloudTrail : : Model : : Event > & events = outcome . GetResult ( ) . GetEvents ( ) ;
count + = events . size ( ) ;
allEvents . insert ( allEvents . end ( ) , events . begin ( ) , events . end ( ) ) ;
nextToken = outcome . GetResult ( ) . GetNextToken ( ) ;
}
else {
std : : cerr < < " Error: " < < outcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
return false ;
}
} while ( ! nextToken . empty ( ) & & count < = 50 ) ; // Limit to 50 events.
2021-11-15 13:15:43 +00:00
2024-04-29 13:26:35 -04:00
std : : cout < < " Found " < < allEvents . size ( ) < < " event(s). " < < std : : endl ;
2021-11-15 13:15:43 +00:00
2024-04-29 13:26:35 -04:00
for ( auto & event : allEvents ) {
std : : cout < < " Event name: " < < event . GetEventName ( ) < < std : : endl ;
std : : cout < < " Event source: " < < event . GetEventSource ( ) < < std : : endl ;
std : : cout < < " Event id: " < < event . GetEventId ( ) < < std : : endl ;
std : : cout < < " Resources: " < < std : : endl ;
for ( auto & resource : event . GetResources ( ) ) {
std : : cout < < " " < < resource . GetResourceName ( ) < < std : : endl ;
}
}
2024-04-24 14:30:38 -04:00
2024-04-29 13:26:35 -04:00
return true ;
}
// snippet-end:[cpp.example_code.cloudtrail.LookupEvents]
/*
*
* main function
*
* Usage: 'run_lookup_events'
*
*/
2024-04-24 14:30:38 -04:00
2024-04-29 13:26:35 -04:00
# ifndef TESTING_BUILD
int main ( int argc , char * * argv ) {
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
Aws : : Client : : ClientConfiguration clientConfig ;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
AwsDoc : : CloudTrail : : lookupEvents ( clientConfig ) ;
2021-11-15 13:15:43 +00:00
}
2024-04-24 14:30:38 -04:00
Aws : : ShutdownAPI ( options ) ;
return 0 ;
2021-11-15 13:15:43 +00:00
}
2024-04-29 13:26:35 -04:00
# endif // TESTING_BUILD