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 1 Java
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
/**
* 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.
*
**/
2021-11-15 13:15:43 +00:00
// snippet-start:[ec2.cpp.describe_security_groups.inc]
#include <aws/ec2/EC2Client.h>
#include <aws/ec2/model/DescribeSecurityGroupsRequest.h>
#include <aws/ec2/model/DescribeSecurityGroupsResponse.h>
#include <iomanip>
#include <iostream>
// snippet-end:[ec2.cpp.describe_security_groups.inc]
#include "ec2_samples.h"
2021-11-15 13:15:43 +00:00
2024-08-02 10:24:50 -04:00
// snippet-start:[cpp.example_code.ec2.DescribeSecurityGroups]
//! Describe all Amazon Elastic Compute Cloud (Amazon EC2) security groups, or a specific group.
/*!
\param groupID: A group ID, ignored if empty.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
2021-11-15 13:15:43 +00:00
*/
2024-08-02 10:24:50 -04:00
bool AwsDoc::EC2::describeSecurityGroups(const Aws::String &groupID,
const Aws::Client::ClientConfiguration &clientConfiguration) {
// snippet-start:[ec2.cpp.describe_security_groups.code]
Aws::EC2::EC2Client ec2Client(clientConfiguration);
Aws::EC2::Model::DescribeSecurityGroupsRequest request;
2021-11-15 13:15:43 +00:00
if (!groupID.empty()) {
request.AddGroupIds(groupID);
}
2021-11-15 13:15:43 +00:00
Aws::String nextToken;
do {
if (!nextToken.empty()) {
request.SetNextToken(nextToken);
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
Aws::EC2::Model::DescribeSecurityGroupsOutcome outcome = ec2Client.DescribeSecurityGroups(request);
if (outcome.IsSuccess()) {
2021-11-15 13:15:43 +00:00
std::cout << std::left <<
std::setw(32) << "Name" <<
std::setw(30) << "GroupId" <<
std::setw(30) << "VpcId" <<
std::setw(64) << "Description" << std::endl;
2021-11-15 13:15:43 +00:00
const std::vector<Aws::EC2::Model::SecurityGroup> &securityGroups =
outcome.GetResult().GetSecurityGroups();
2021-11-15 13:15:43 +00:00
for (const auto &securityGroup: securityGroups) {
2021-11-15 13:15:43 +00:00
std::cout << std::left <<
std::setw(32) << securityGroup.GetGroupName() <<
std::setw(30) << securityGroup.GetGroupId() <<
std::setw(30) << securityGroup.GetVpcId() <<
std::setw(64) << securityGroup.GetDescription() <<
std::endl;
2021-11-15 13:15:43 +00:00
}
2024-08-02 10:24:50 -04:00
} else {
std::cerr << "Failed to describe security groups:" <<
outcome.GetError().GetMessage() << std::endl;
return false;
2021-11-15 13:15:43 +00:00
}
nextToken = outcome.GetResult().GetNextToken();
} while (!nextToken.empty());
// snippet-end:[ec2.cpp.describe_security_groups.code]
return true;
}
2024-08-02 10:24:50 -04:00
// snippet-end:[cpp.example_code.ec2.DescribeSecurityGroups]
/*
*
* main function
*
* Usage: 'run_describe_security_groups [group_id]'
*
*/
#ifndef TESTING_BUILD
int main(int argc, char **argv) {
if (argc > 2) {
std::cout << "Usage: run_describe_security_groups [group_id]" <<
std::endl;
return 1;
}
Aws::SDKOptions options;
Aws::InitAPI(options);
{
Aws::String groupID;
if (argc == 2) {
groupID = argv[1];
}
Aws::Client::ClientConfiguration clientConfig;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
2024-08-02 10:24:50 -04:00
AwsDoc::EC2::describeSecurityGroups(groupID, clientConfig);
2021-11-15 13:15:43 +00:00
}
Aws::ShutdownAPI(options);
return 0;
}
#endif // TESTING_BUILD