2021-11-15 13:15:43 +00:00
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
# include <aws/core/Aws.h>
# include <aws/core/utils/StringUtils.h>
# include <aws/codebuild/CodeBuildClient.h>
# include <aws/codebuild/model/ListBuildsRequest.h>
# include <aws/codebuild/model/ListBuildsResult.h>
# include <aws/codebuild/model/BatchGetBuildsRequest.h>
# include <iostream>
2024-05-01 11:03:07 -04:00
# include "codebuild_samples.h"
2021-11-15 13:15:43 +00:00
/**
2024-05-01 11:03:07 -04:00
* 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.
*
**/
// snippet-start:[cpp.example_code.codebuild.ListBuilds]
//! List the CodeBuild builds.
/*!
\param sortType: 'SortOrderType' type.
\param clientConfiguration: AWS client configuration.
\return bool: Function succeeded.
2021-11-15 13:15:43 +00:00
*/
2024-05-01 11:03:07 -04:00
bool AwsDoc : : CodeBuild : : listBuilds ( Aws : : CodeBuild : : Model : : SortOrderType sortType ,
const Aws : : Client : : ClientConfiguration & clientConfiguration ) {
Aws : : CodeBuild : : CodeBuildClient codeBuildClient ( clientConfiguration ) ;
Aws : : CodeBuild : : Model : : ListBuildsRequest listBuildsRequest ;
listBuildsRequest . SetSortOrder ( sortType ) ;
Aws : : String nextToken ; // Used for pagination.
do {
if ( ! nextToken . empty ( ) ) {
listBuildsRequest . SetNextToken ( nextToken ) ;
}
Aws : : CodeBuild : : Model : : ListBuildsOutcome listBuildsOutcome = codeBuildClient . ListBuilds (
listBuildsRequest ) ;
if ( listBuildsOutcome . IsSuccess ( ) ) {
2024-07-10 15:20:33 -04:00
const Aws : : Vector < Aws : : String > & ids = listBuildsOutcome . GetResult ( ) . GetIds ( ) ;
if ( ! ids . empty ( ) ) {
std : : cout < < " Information about each build: " < < std : : endl ;
Aws : : CodeBuild : : Model : : BatchGetBuildsRequest getBuildsRequest ;
getBuildsRequest . SetIds ( listBuildsOutcome . GetResult ( ) . GetIds ( ) ) ;
Aws : : CodeBuild : : Model : : BatchGetBuildsOutcome getBuildsOutcome = codeBuildClient . BatchGetBuilds (
getBuildsRequest ) ;
if ( getBuildsOutcome . IsSuccess ( ) ) {
const Aws : : Vector < Aws : : CodeBuild : : Model : : Build > & builds = getBuildsOutcome . GetResult ( ) . GetBuilds ( ) ;
std : : cout < < builds . size ( ) < < " build(s) found. " < < std : : endl ;
for ( auto val : builds ) {
std : : cout < < val . GetId ( ) < < std : : endl ;
}
} else {
std : : cerr < < " Error getting builds "
< < getBuildsOutcome . GetError ( ) . GetMessage ( ) < < std : : endl ;
return false ;
2024-05-01 11:03:07 -04:00
}
2024-07-10 15:20:33 -04:00
} else {
std : : cout < < " No builds found. " < < std : : endl ;
2024-05-01 11:03:07 -04:00
}
2024-07-10 15:20:33 -04:00
// Get the next token for pagination.
nextToken = listBuildsOutcome . GetResult ( ) . GetNextToken ( ) ;
} else {
2024-05-01 11:03:07 -04:00
std : : cerr < < " Error listing builds "
< < listBuildsOutcome . GetError ( ) . GetMessage ( )
< < std : : endl ;
return false ;
}
2024-07-10 15:20:33 -04:00
} while ( ! nextToken .
empty ( )
) ;
2024-05-01 11:03:07 -04:00
return true ;
}
// snippet-end:[cpp.example_code.codebuild.ListBuilds]
/*
*
* main function
*
* Usage: 'Usage: run_list_builds <ASCENDING | DESCENDING>'
*
*/
# ifndef TESTING_BUILD
2021-11-15 13:15:43 +00:00
2024-04-24 14:30:38 -04:00
int main ( int argc , char * * argv ) {
if ( argc ! = 2 ) {
2024-05-01 11:03:07 -04:00
std : : cout < < " Usage: run_list_builds <ASCENDING | DESCENDING> " ;
2024-04-24 14:30:38 -04:00
return 1 ;
2021-11-15 13:15:43 +00:00
}
2024-05-01 11:03:07 -04:00
2024-04-24 14:30:38 -04:00
Aws : : SDKOptions options ;
Aws : : InitAPI ( options ) ;
2021-11-15 13:15:43 +00:00
{
2024-05-01 11:03:07 -04:00
Aws : : String sortOrderType = argv [ 1 ] ;
2021-11-15 13:15:43 +00:00
2024-05-01 11:03:07 -04:00
Aws : : CodeBuild : : Model : : SortOrderType sortType = Aws : : CodeBuild : : Model : : SortOrderType : : NOT_SET ;
2024-04-24 14:30:38 -04:00
if ( Aws : : Utils : : StringUtils : : CaselessCompare ( argv [ 1 ] , " ASCENDING " ) ) {
2024-05-01 11:03:07 -04:00
sortType = Aws : : CodeBuild : : Model : : SortOrderType : : ASCENDING ;
2024-07-10 15:20:33 -04:00
} else if ( Aws : : Utils : : StringUtils : : CaselessCompare ( argv [ 1 ] , " DESCENDING " ) ) {
2024-05-01 11:03:07 -04:00
sortType = Aws : : CodeBuild : : Model : : SortOrderType : : DESCENDING ;
2024-07-10 15:20:33 -04:00
} else {
2024-05-01 11:03:07 -04:00
std : : cout < < " Invalid sort order type. " < < std : : endl ;
2021-11-15 13:15:43 +00:00
}
2024-05-01 11:03:07 -04:00
Aws : : Client : : ClientConfiguration clientConfig ;
// Optional: Set to the AWS Region (overrides config file).
// clientConfig.region = "us-east-1";
2021-11-15 13:15:43 +00:00
2024-05-01 11:03:07 -04:00
AwsDoc : : CodeBuild : : listBuilds ( sortType , clientConfig ) ;
2021-11-15 13:15:43 +00:00
}
2024-04-24 14:30:38 -04:00
Aws : : ShutdownAPI ( options ) ;
return 0 ;
2021-11-15 13:15:43 +00:00
}
2024-05-01 11:03:07 -04:00
# endif // TESTING_BUILD