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
*
* Demonstrates updating the status (active/inactive) of an IAM user's access key.
*
*/
2021-11-15 13:15:43 +00:00
// snippet-start:[iam.cpp.update_access_key.inc]
# include <aws/core/Aws.h>
# include <aws/iam/IAMClient.h>
# include <aws/iam/model/UpdateAccessKeyRequest.h>
# 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.update_access_key.inc]
2022-10-07 15:58:01 -04:00
static void printUsage ( ) ;
//! Updates the status (active/inactive) of an IAM user's access key.
/*!
\sa updateAccessKey()
\param userName: The user's name.
\param accessKeyID: The access key ID.
\param status: The access key status.
\param clientConfig: Aws client configuration.
\return bool: Successful completion.
*/
// snippet-start:[iam.cpp.update_access_key.code]
bool AwsDoc : : IAM : : updateAccessKey ( const Aws : : String & userName ,
const Aws : : String & accessKeyID ,
Aws : : IAM : : Model : : StatusType status ,
const Aws : : Client : : ClientConfiguration & clientConfig ) {
Aws : : IAM : : IAMClient iam ( clientConfig ) ;
Aws : : IAM : : Model : : UpdateAccessKeyRequest request ;
request . SetUserName ( userName ) ;
request . SetAccessKeyId ( accessKeyID ) ;
request . SetStatus ( status ) ;
auto outcome = iam . UpdateAccessKey ( request ) ;
2022-10-12 14:52:10 -04:00
if ( outcome . IsSuccess ( ) ) {
2022-10-07 15:58:01 -04:00
std : : cout < < " Successfully updated status of access key "
2022-10-12 14:52:10 -04:00
< < accessKeyID < < " for user " < < userName < < std : : endl ;
2022-10-07 15:58:01 -04:00
}
2022-10-12 14:52:10 -04:00
else {
2022-10-07 15:58:01 -04:00
std : : cerr < < " Error updated status of access key " < < accessKeyID < <
" for user " < < userName < < " : " < <
outcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
}
return outcome . IsSuccess ( ) ;
2021-11-15 13:15:43 +00:00
}
2022-10-07 15:58:01 -04:00
// snippet-end:[iam.cpp.update_access_key.code]
2021-11-15 13:15:43 +00:00
2022-10-07 15:58:01 -04:00
/*
*
* main function
*
* Prerequisites: Existing access key.
*
* Usage: 'run_update_access_key <user_name> <access_key_id> <Active|Inactive>'
*
2021-11-15 13:15:43 +00:00
*/
2022-10-07 15:58:01 -04:00
# ifndef TESTING_BUILD
2021-11-15 13:15:43 +00:00
int main ( int argc , char * * argv )
{
if ( argc ! = 4 )
{
2022-10-07 15:58:01 -04:00
printUsage ( ) ;
2021-11-15 13:15:43 +00:00
return 1 ;
}
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
{
2022-10-07 15:58:01 -04:00
Aws : : String userName ( argv [ 1 ] ) ;
2021-11-15 13:15:43 +00:00
Aws : : String accessKeyId ( argv [ 2 ] ) ;
2022-10-07 15:58:01 -04:00
Aws : : String keyStatus ( argv [ 3 ] ) ;
2021-11-15 13:15:43 +00:00
2022-10-07 15:58:01 -04:00
Aws : : IAM : : Model : : StatusType status =
2021-11-15 13:15:43 +00:00
Aws : : IAM : : Model : : StatusTypeMapper : : GetStatusTypeForName ( argv [ 3 ] ) ;
if ( status = = Aws : : IAM : : Model : : StatusType : : NOT_SET )
{
2022-10-07 15:58:01 -04:00
printUsage ( ) ;
2021-11-15 13:15:43 +00:00
return 1 ;
}
2022-10-07 15:58:01 -04:00
Aws : : Client : : ClientConfiguration clientConfig ;
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// clientConfig.region = "us-east-1";
AwsDoc : : IAM : : updateAccessKey ( userName , accessKeyId , status , clientConfig ) ;
2021-11-15 13:15:43 +00:00
}
Aws : : ShutdownAPI ( options ) ;
return 0 ;
}
2022-10-07 15:58:01 -04:00
# endif
2021-11-15 13:15:43 +00:00
2022-10-12 14:52:10 -04:00
void printUsage ( ) {
2022-10-07 15:58:01 -04:00
std : : cout < <
" Usage: run_update_access_key <user_name> <access_key_id> <Active|Inactive> "
< < std : : endl ;
}