Skip to content

Commit

Permalink
Changing code to be consistent and support tests
Browse files Browse the repository at this point in the history
  • Loading branch information
meyertst-aws committed Oct 6, 2022
1 parent 4a9dabf commit f82fbdf
Show file tree
Hide file tree
Showing 15 changed files with 555 additions and 374 deletions.
4 changes: 3 additions & 1 deletion cpp/example_code/iam/access_key_last_used.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
SPDX-License-Identifier: Apache-2.0
*/

//snippet-start:[iam.cpp.access_key_last_used.inc]
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
#include <aws/iam/model/GetAccessKeyLastUsedRequest.h>
#include <aws/iam/model/GetAccessKeyLastUsedResult.h>
#include <iostream>
#include "iam_samples.h"
//snippet-end:[iam.cpp.access_key_last_used.inc]

/**
* Before running this C++ code example, set up your development environment, including your credentials.
Expand Down Expand Up @@ -77,7 +79,7 @@ int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Usage: access_key_last_used <access_key_id>" <<
std::cout << "Usage: run_access_key_last_used <access_key_id>" <<
std::endl;
return 1;
}
Expand Down
5 changes: 3 additions & 2 deletions cpp/example_code/iam/attach_role_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
SPDX-License-Identifier: Apache-2.0
*/

//snippet-start:[iam.cpp.attach_role_policy.inc]
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
#include <aws/iam/model/AttachRolePolicyRequest.h>
Expand All @@ -11,7 +12,7 @@
#include <iostream>
#include <iomanip>
#include "iam_samples.h"

//snippet-end:[iam.cpp.attach_role_policy.inc]

/**
* Before running this C++ code example, set up your development environment, including your credentials.
Expand Down Expand Up @@ -102,7 +103,7 @@ static const char *SAMPLE_POLICY_ARN =
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
if (argc < 2 || argc >= 4) {
std::cout << "Usage: attach_role_policy <role_name> [policy_arn]" <<
std::cout << "Usage: run_attach_role_policy <role_name> [policy_arn]" <<
std::endl;
return 1;
}
Expand Down
4 changes: 3 additions & 1 deletion cpp/example_code/iam/create_access_key.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
SPDX-License-Identifier: Apache-2.0
*/

//snippet-start:[iam.cpp.create_access_key.inc]
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
#include <aws/iam/model/CreateAccessKeyRequest.h>
#include <aws/iam/model/CreateAccessKeyResult.h>
#include <iostream>
#include "iam_samples.h"
//snippet-end:[iam.cpp.create_access_key.inc]

/**
* Before running this C++ code example, set up your development environment, including your credentials.
Expand Down Expand Up @@ -73,7 +75,7 @@ int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Usage: create_access_key <user_name>" << std::endl;
std::cout << "Usage: run_create_access_key <user_name>" << std::endl;
return 1;
}

Expand Down
5 changes: 3 additions & 2 deletions cpp/example_code/iam/create_account_alias.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
SPDX-License-Identifier: Apache-2.0
*/

//snippet-start:[iam.cpp.create_account_alias.inc]
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
#include <aws/iam/model/CreateAccountAliasRequest.h>
#include <iostream>
#include "iam_samples.h"

//snippet-end:[iam.cpp.create_account_alias.inc]

/**
* Before running this C++ code example, set up your development environment, including your credentials.
Expand Down Expand Up @@ -67,7 +68,7 @@ int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Usage: create_account_alias <alias_name>" <<
std::cout << "Usage: run_create_account_alias <alias_name>" <<
std::endl;
return 1;
}
Expand Down
4 changes: 3 additions & 1 deletion cpp/example_code/iam/create_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
SPDX-License-Identifier: Apache-2.0
*/

//snippet-start:[iam.cpp.create_policy.inc]
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
#include <aws/iam/model/CreatePolicyRequest.h>
#include <aws/iam/model/CreatePolicyResult.h>
#include <iostream>
#include "iam_samples.h"
//snippet-end:[iam.cpp.create_policy.inc]

/**
* Before running this C++ code example, set up your development environment, including your credentials.
Expand Down Expand Up @@ -112,7 +114,7 @@ int main(int argc, char** argv)
{
if (argc != 3)
{
std::cout << "Usage: create_policy <policy_name> <resource_arn>" <<
std::cout << "Usage: run_create_policy <policy_name> <resource_arn>" <<
std::endl;
return 1;
}
Expand Down
31 changes: 17 additions & 14 deletions cpp/example_code/iam/create_user.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

//snippet-start:[iam.cpp.create_user.inc]
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
Expand All @@ -28,48 +29,48 @@
//! Creates an IAM user.
/*!
\sa createUser()
\param roleName: The name of the user.
\param clientConfig Aws client configuration.
\param userName: The user name.
\param clientConfig: Aws client configuration.
\return bool: Successful completion.
*/

bool AwsDoc::IAM::createUser(const Aws::String& user_name,
bool AwsDoc::IAM::createUser(const Aws::String& userName,
const Aws::Client::ClientConfiguration &clientConfig)
{
// snippet-start:[iam.cpp.create_user01.code]
Aws::IAM::IAMClient iam;
// snippet-end:[iam.cpp.create_user01.code]
// snippet-start:[iam.cpp.get_user.code]
Aws::IAM::Model::GetUserRequest get_request;
get_request.SetUserName(user_name);
get_request.SetUserName(userName);

auto get_outcome = iam.GetUser(get_request);
if (get_outcome.IsSuccess())
{
std::cout << "IAM user " << user_name << " already exists" << std::endl;
std::cout << "IAM user " << userName << " already exists" << std::endl;
return true;
}
else if (get_outcome.GetError().GetErrorType() !=
Aws::IAM::IAMErrors::NO_SUCH_ENTITY)
{
std::cerr << "Error checking existence of IAM user " << user_name << ":"
std::cerr << "Error checking existence of IAM user " << userName << ":"
<< get_outcome.GetError().GetMessage() << std::endl;
return false;
}
// snippet-end:[iam.cpp.get_user.code]

// snippet-start:[iam.cpp.create_user02.code]
Aws::IAM::Model::CreateUserRequest create_request;
create_request.SetUserName(user_name);
create_request.SetUserName(userName);

auto create_outcome = iam.CreateUser(create_request);
if (!create_outcome.IsSuccess())
{
std::cerr << "Error creating IAM user " << user_name << ":" <<
create_outcome.GetError().GetMessage() << std::endl;
std::cerr << "Error creating IAM user " << userName << ":" <<
create_outcome.GetError().GetMessage() << std::endl;
}
else{
std::cout << "Successfully created IAM user " << user_name << std::endl;
std::cout << "Successfully created IAM user " << userName << std::endl;
}

return create_outcome.IsSuccess();
Expand All @@ -89,20 +90,22 @@ int main(int argc, char** argv)
{
if (argc != 2)
{
std::cout << "Usage: create_user <user_name>" << std::endl;
std::cout << "Usage: run_create_user <user_name>" << std::endl;
return 1;
}

Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::String user_name(argv[1]);
Aws::String userName(argv[1]);

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::createUser(user_name, clientConfig);
}Aws::ShutdownAPI(options);
AwsDoc::IAM::createUser(userName, clientConfig);
}

Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD
114 changes: 67 additions & 47 deletions cpp/example_code/iam/delete_access_key.cpp
Original file line number Diff line number Diff line change
@@ -1,43 +1,80 @@

//snippet-sourcedescription:[delete_access_key.cpp demonstrates how to delete the AWS access key pair for an IAM user.]
//snippet-keyword:[C++]
//snippet-sourcesyntax:[cpp]
//snippet-keyword:[Code Sample]
//snippet-keyword:[AWS Identity and Access Management (IAM)]
//snippet-service:[iam]
//snippet-sourcetype:[full-example]
//snippet-sourcedate:[]
//snippet-sourceauthor:[AWS]


/*
Copyright 2010-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved.
This file is licensed under the Apache License, Version 2.0 (the "License").
You may not use this file except in compliance with the License. A copy of
the License is located at
http://aws.amazon.com/apache2.0/
This file is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
CONDITIONS OF ANY KIND, either express or implied. See the License for the
specific language governing permissions and limitations under the License.
Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
SPDX-License-Identifier: Apache-2.0
*/

//snippet-start:[iam.cpp.delete_access_key.inc]
#include <aws/core/Aws.h>
#include <aws/iam/IAMClient.h>
#include <aws/iam/model/DeleteAccessKeyRequest.h>
#include <iostream>
#include "iam_samples.h"
//snippet-end:[iam.cpp.delete_access_key.inc]

/**
* Deletes an access key from an IAM user, based on command line input
* 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
*
* Purpose
*
* Demonstrates deleting an access key from an IAM user.
*
*/

// snippet-start:[iam.cpp.delete_access_key.code]
//! Deletes an access key from an IAM user.
/*!
\sa deleteAccessKey()
\param userName: The user name.
\param accessKeyID: The access key name.
\param clientConfig: Aws client configuration.
\return bool: Successful completion.
*/
bool AwsDoc::IAM::deleteAccessKey(const Aws::String& userName,
const Aws::String& accessKeyID,
const Aws::Client::ClientConfiguration &clientConfig)
{
Aws::IAM::IAMClient iam(clientConfig);

Aws::IAM::Model::DeleteAccessKeyRequest request;
request.SetUserName(userName);
request.SetAccessKeyId(accessKeyID);

auto outcome = iam.DeleteAccessKey(request);

if (!outcome.IsSuccess())
{
std::cerr << "Error deleting access key " << accessKeyID << " from user "
<< userName << ": " << outcome.GetError().GetMessage() <<
std::endl;
}
else
{
std::cout << "Successfully deleted access key " << accessKeyID
<< " for IAM user " << userName << std::endl;
}

return outcome.IsSuccess();
}
// snippet-end:[iam.cpp.delete_access_key.code]

/*
*
* main function
*
* Usage: run_delete_access_key <user_name> <access_key_id>
*
*/

#ifndef TESTING_BUILD
int main(int argc, char** argv)
{
if (argc != 3)
{
std::cout << "Usage: delete_access_key <user_name> <access_key_id>"
std::cout << "Usage: run_delete_access_key <user_name> <access_key_id>"
<< std::endl;
return 1;
}
Expand All @@ -47,30 +84,13 @@ int main(int argc, char** argv)
{
Aws::String user_name(argv[1]);
Aws::String key_id(argv[2]);
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region in which the bucket was created (overrides config file).
// clientConfig.region = "us-east-1";

// snippet-start:[iam.cpp.delete_access_key.code]
Aws::IAM::IAMClient iam;

Aws::IAM::Model::DeleteAccessKeyRequest request;
request.SetUserName(user_name);
request.SetAccessKeyId(key_id);

auto outcome = iam.DeleteAccessKey(request);

if (!outcome.IsSuccess())
{
std::cout << "Error deleting access key " << key_id << " from user "
<< user_name << ": " << outcome.GetError().GetMessage() <<
std::endl;
}
else
{
std::cout << "Successfully deleted access key " << key_id
<< " for IAM user " << user_name << std::endl;
}
// snippet-end:[iam.cpp.delete_access_key.code]
AwsDoc::IAM::deleteAccessKey(user_name, key_id, clientConfig);
}
Aws::ShutdownAPI(options);
return 0;
}

#endif // TESTING_BUILD
Loading

0 comments on commit f82fbdf

Please sign in to comment.