// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 #include #include #include #include #include "cloudtrail_samples.h" /** * 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. * **/ // 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 allEvents; Aws::CloudTrail::Model::LookupEventsRequest request; size_t count = 0; do { if (!nextToken.empty()) { request.SetNextToken(nextToken); } Aws::CloudTrail::Model::LookupEventsOutcome outcome = cloudtrail.LookupEvents( request); if (outcome.IsSuccess()) { const Aws::Vector &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. std::cout << "Found " << allEvents.size() << " event(s)." << std::endl; 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; } } return true; } // snippet-end:[cpp.example_code.cloudtrail.LookupEvents] /* * * main function * * Usage: 'run_lookup_events' * */ #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); } Aws::ShutdownAPI(options); return 0; } #endif // TESTING_BUILD