There are more AWS SDK examples available in the AWS Doc SDK Examples
Use ListBaselines with an AWS SDK
The following code examples show how to use ListBaselines.
Action examples are code excerpts from larger programs and must be run in context. You can see this action in context in the following code example:
- .NET
-
- SDK for .NET (v4)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /// <summary> /// List all baselines. /// </summary> /// <returns>A list of baseline summaries.</returns> public async Task<List<BaselineSummary>> ListBaselinesAsync() { try { var baselines = new List<BaselineSummary>(); var baselinesPaginator = _controlTowerService.Paginators.ListBaselines(new ListBaselinesRequest()); await foreach (var response in baselinesPaginator.Responses) { baselines.AddRange(response.Baselines); } return baselines; } catch (AmazonControlTowerException ex) { Console.WriteLine($"Couldn't list baselines. Here's why: {ex.ErrorCode}: {ex.Message}"); throw; } }-
For API details, see ListBaselines in AWS SDK for .NET API Reference.
-
- Java
-
- SDK for Java 2.x
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. /** * Lists all available baselines using pagination to retrieve complete results. * * @return a list of all baselines * @throws ControlTowerException if a service-specific error occurs * @throws SdkException if an SDK error occurs */ public CompletableFuture<List<BaselineSummary>> listBaselinesAsync() { System.out.println("Starting list baselines paginator…"); ListBaselinesRequest request = ListBaselinesRequest.builder().build(); ListBaselinesPublisher paginator = getAsyncClient().listBaselinesPaginator(request); List<BaselineSummary> baselines = new ArrayList<>(); return paginator.subscribe(response -> { if (response.baselines() != null && !response.baselines().isEmpty()) { response.baselines().forEach(baseline -> { baselines.add(baseline); }); } else { System.out.println("Page contained no baselines."); } }) .thenRun(() -> System.out.println("Successfully listed baselines. Total: " + baselines.size()) ) .thenApply(v -> baselines) .exceptionally(ex -> { Throwable cause = ex.getCause() != null ? ex.getCause() : ex; if (cause instanceof ControlTowerException e) { String errorCode = e.awsErrorDetails().errorCode(); if ("AccessDeniedException".equals(errorCode)) { throw new CompletionException( "Access denied when listing baselines: %s".formatted(e.getMessage()), e ); } throw new CompletionException( "Error listing baselines: %s".formatted(e.getMessage()), e ); } if (cause instanceof SdkException) { throw new CompletionException( "SDK error listing baselines: %s".formatted(cause.getMessage()), cause ); } throw new CompletionException("Failed to list baselines", cause); }); }-
For API details, see ListBaselines in AWS SDK for Java 2.x API Reference.
-
- Python
-
- SDK for Python (Boto3)
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. class ControlTowerWrapper: """Encapsulates AWS Control Tower and Control Catalog functionality.""" def __init__( self, controltower_client: boto3.client, controlcatalog_client: boto3.client ): """ :param controltower_client: A Boto3 Amazon ControlTower client. :param controlcatalog_client: A Boto3 Amazon ControlCatalog client. """ self.controltower_client = controltower_client self.controlcatalog_client = controlcatalog_client @classmethod def from_client(cls): controltower_client = boto3.client("controltower") controlcatalog_client = boto3.client("controlcatalog") return cls(controltower_client, controlcatalog_client) def list_baselines(self): """ Lists all baselines. :return: List of baselines. :raises ClientError: If the listing operation fails. """ try: paginator = self.controltower_client.get_paginator("list_baselines") baselines = [] for page in paginator.paginate(): baselines.extend(page["baselines"]) return baselines except ClientError as err: if err.response["Error"]["Code"] == "AccessDeniedException": logger.error( "Access denied. Please ensure you have the necessary permissions." ) else: logger.error( "Couldn't list baselines. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise-
For API details, see ListBaselines in AWS SDK for Python (Boto3) API Reference.
-
- SAP ABAP
-
- SDK for SAP ABAP
-
Note
There's more on GitHub. Find the complete example and learn how to set up and run in the AWS Code Examples Repository
. DATA lt_baselines TYPE /aws1/cl_cttbaselinesummary=>tt_baselines. DATA lv_nexttoken TYPE /aws1/cttstring. " List all baselines using pagination DO. DATA(lo_output) = io_ctt->listbaselines( iv_nexttoken = lv_nexttoken ). APPEND LINES OF lo_output->get_baselines( ) TO lt_baselines. lv_nexttoken = lo_output->get_nexttoken( ). IF lv_nexttoken IS INITIAL. EXIT. ENDIF. ENDDO. ot_baselines = lt_baselines. MESSAGE 'Listed baselines successfully.' TYPE 'I'.-
For API details, see ListBaselines in AWS SDK for SAP ABAP API reference.
-