View a markdown version of this page

Use ListUsageTotals with an AWS SDK - AWS SDK Code Examples

There are more AWS SDK examples available in the AWS Doc SDK Examples GitHub repo.

Use ListUsageTotals with an AWS SDK

The following code examples show how to use ListUsageTotals.

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:

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.

/** * Asynchronously lists Inspector2 usage totals using a paginator. * * @param accountIds optional list of account IDs * @param maxResults maximum results per page * @return CompletableFuture completed with formatted summary text */ public CompletableFuture<String> listUsageTotalsAsync( List<String> accountIds, int maxResults) { logger.info("Starting usage totals paginator…"); ListUsageTotalsRequest.Builder builder = ListUsageTotalsRequest.builder() .maxResults(maxResults); if (accountIds != null && !accountIds.isEmpty()) { builder.accountIds(accountIds); } ListUsageTotalsRequest request = builder.build(); ListUsageTotalsPublisher paginator = getAsyncClient().listUsageTotalsPaginator(request); StringBuilder summaryBuilder = new StringBuilder(); return paginator.subscribe(response -> { if (response.totals() != null && !response.totals().isEmpty()) { response.totals().forEach(total -> { if (total.usage() != null) { total.usage().forEach(usage -> { logger.info("Usage: {} = {}", usage.typeAsString(), usage.total()); summaryBuilder.append(usage.typeAsString()) .append(": ") .append(usage.total()) .append("\n"); }); } }); } else { logger.info("Page contained no usage totals."); } }).thenRun(() -> logger.info("Successfully listed usage totals.")) .thenApply(v -> { String summary = summaryBuilder.toString(); return summary.isEmpty() ? "No usage totals found." : summary; }).exceptionally(ex -> { Throwable cause = ex.getCause() != null ? ex.getCause() : ex; if (cause instanceof ValidationException ve) { throw new CompletionException( "Validation error listing usage totals: %s".formatted(ve.getMessage()), ve ); } throw new CompletionException("Failed to list usage totals", cause); }); }
  • For API details, see ListUsageTotals 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 Inspector2Wrapper: """Encapsulates Amazon Inspector (Inspector2) operations.""" def __init__(self, inspector2_client: BaseClient) -> None: """ :param inspector2_client: A Boto3 Amazon Inspector2 client. """ self.inspector2_client = inspector2_client @classmethod def from_client(cls) -> "Inspector2Wrapper": """ Creates an Inspector2Wrapper using a default Boto3 Inspector2 client. :return: An instance of Inspector2Wrapper. """ inspector2_client = boto3.client("inspector2") return cls(inspector2_client) def list_usage_totals(self, max_results: int = 10) -> list[dict[str, Any]]: """ Lists Amazon Inspector usage totals for the current account. :param max_results: The maximum number of items to return. :return: The usage totals. If a ValidationException occurs, it is logged and an empty list is returned. """ try: totals = list() paginator = self.inspector2_client.get_paginator("list_usage_totals") for page in paginator.paginate( PaginationConfig={"MaxItems": max_results, "PageSize": max_results} ): totals.extend(page.get("totals", list())) return totals except ClientError as err: if err.response["Error"]["Code"] == "ValidationException": logger.warning( "Validation error listing usage totals. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) return list() logger.error( "Couldn't list usage totals. Here's why: %s: %s", err.response["Error"]["Code"], err.response["Error"]["Message"], ) raise
  • For API details, see ListUsageTotals in AWS SDK for Python (Boto3) API Reference.