SIGN IN SIGN UP

Welcome to the AWS Code Examples Repository. This repo contains code examples used in the AWS documentation, AWS SDK Developer Guides, and more. For more information, see the Readme.md file below.

0 0 59 Java
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2021-11-15 13:15:43 +00:00
/**
* Before running this C++ code example, set up your development environment, including your credentials.
*
* For more information, see the following documentation topic:
2022-10-14 09:27:14 -04:00
* https://docs.aws.amazon.com/sdk-for-cpp/v1/developer-guide/getting-started.html.
*
2022-10-12 14:52:10 -04:00
* 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.
*
* Purpose
*
2022-10-12 14:52:10 -04:00
* Demonstrates listing all IAM users.
*
*/
2021-11-15 13:15:43 +00:00
// snippet-start:[iam.cpp.list_users.inc]
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
#include <aws/iam/model/ListUsersRequest.h>
#include <iomanip>
#include <iostream>
#include "iam_samples.h"
2021-11-15 13:15:43 +00:00
// snippet-end:[iam.cpp.list_users.inc]
2022-10-12 14:52:10 -04:00
//! List all IAM users.
/*!
\sa listUsers()
\param clientConfig: Aws client configuration.
\return bool: Successful completion.
*/
2022-10-12 14:52:10 -04:00
// snippet-start:[iam.cpp.list_users.code]
bool AwsDoc::IAM::listUsers(const Aws::Client::ClientConfiguration &clientConfig) {
const Aws::String DATE_FORMAT = "%Y-%m-%d";
Aws::IAM::IAMClient iam(clientConfig);
Aws::IAM::Model::ListUsersRequest request;
2021-11-15 13:15:43 +00:00
bool done = false;
bool header = false;
2022-10-12 14:52:10 -04:00
while (!done) {
auto outcome = iam.ListUsers(request);
2022-10-12 14:52:10 -04:00
if (!outcome.IsSuccess()) {
std::cerr << "Failed to list iam users:" <<
outcome.GetError().GetMessage() << std::endl;
return false;
}
2021-11-15 13:15:43 +00:00
2022-10-12 14:52:10 -04:00
if (!header) {
std::cout << std::left << std::setw(32) << "Name" <<
std::setw(30) << "ID" << std::setw(64) << "Arn" <<
std::setw(20) << "CreateDate" << std::endl;
header = true;
}
2021-11-15 13:15:43 +00:00
const auto &users = outcome.GetResult().GetUsers();
2022-10-12 14:52:10 -04:00
for (const auto &user: users) {
std::cout << std::left << std::setw(32) << user.GetUserName() <<
std::setw(30) << user.GetUserId() << std::setw(64) <<
user.GetArn() << std::setw(20) <<
2022-10-12 14:52:10 -04:00
user.GetCreateDate().ToGmtString(DATE_FORMAT.c_str())
<< std::endl;
}
2021-11-15 13:15:43 +00:00
2022-10-12 14:52:10 -04:00
if (outcome.GetResult().GetIsTruncated()) {
request.SetMarker(outcome.GetResult().GetMarker());
}
2022-10-12 14:52:10 -04:00
else {
done = true;
2021-11-15 13:15:43 +00:00
}
}
return true;
}
// snippet-end:[iam.cpp.list_users.code]
/*
*
* main function
*
* Usage: 'run_lists_users'
*
*/
#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::IAM::listUsers(clientConfig);
2021-11-15 13:15:43 +00:00
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD