2022-10-07 15:58:01 -04:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
2021-11-15 13:15:43 +00:00
2022-10-07 15:58:01 -04: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-07 15:58:01 -04:00
*
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.
*
2022-10-07 15:58:01 -04:00
* Purpose
*
2022-10-12 14:52:10 -04:00
* Demonstrates listing all IAM users.
2022-10-07 15:58:01 -04:00
*
*/
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>
2022-10-07 15:58:01 -04:00
# 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.
2022-10-07 15:58:01 -04:00
/*!
\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]
2022-10-07 15:58:01 -04:00
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
2022-10-07 15:58:01 -04:00
bool done = false ;
bool header = false ;
2022-10-12 14:52:10 -04:00
while ( ! done ) {
2022-10-07 15:58:01 -04:00
auto outcome = iam . ListUsers ( request ) ;
2022-10-12 14:52:10 -04:00
if ( ! outcome . IsSuccess ( ) ) {
2022-10-07 15:58:01 -04:00
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 ) {
2022-10-07 15:58:01 -04:00
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
2022-10-07 15:58:01 -04:00
const auto & users = outcome . GetResult ( ) . GetUsers ( ) ;
2022-10-12 14:52:10 -04:00
for ( const auto & user : users ) {
2022-10-07 15:58:01 -04:00
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 ;
2022-10-07 15:58:01 -04:00
}
2021-11-15 13:15:43 +00:00
2022-10-12 14:52:10 -04:00
if ( outcome . GetResult ( ) . GetIsTruncated ( ) ) {
2022-10-07 15:58:01 -04:00
request . SetMarker ( outcome . GetResult ( ) . GetMarker ( ) ) ;
}
2022-10-12 14:52:10 -04:00
else {
2022-10-07 15:58:01 -04:00
done = true ;
2021-11-15 13:15:43 +00:00
}
2022-10-07 15:58:01 -04: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 ;
2023-01-06 13:30:37 -05:00
// Optional: Set to the AWS Region (overrides config file).
2022-10-07 15:58:01 -04:00
// clientConfig.region = "us-east-1";
AwsDoc : : IAM : : listUsers ( clientConfig ) ;
2021-11-15 13:15:43 +00:00
}
Aws : : ShutdownAPI ( options ) ;
return 0 ;
}
2023-01-06 13:30:37 -05:00
# endif // TESTING_BUILD