Get the dimensions purchased in an agreement using an AWS SDK - AWS SDK Code Examples

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

Get the dimensions purchased in an agreement using an AWS SDK

The following code examples show how to get the dimensions purchased in an agreement.

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 Marketplace API Reference Code Library repository.

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. // SPDX-License-Identifier: Apache-2.0 package com.example.awsmarketplace.agreementapi; import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider; import software.amazon.awssdk.http.apache.ApacheHttpClient; import software.amazon.awssdk.services.marketplaceagreement.MarketplaceAgreementClient; import software.amazon.awssdk.services.marketplaceagreement.model.AcceptedTerm; import software.amazon.awssdk.services.marketplaceagreement.model.Dimension; import software.amazon.awssdk.services.marketplaceagreement.model.GetAgreementTermsRequest; import software.amazon.awssdk.services.marketplaceagreement.model.GetAgreementTermsResponse; import java.util.ArrayList; import java.util.List; import static com.example.awsmarketplace.utils.ReferenceCodesConstants.AGREEMENT_ID; import com.example.awsmarketplace.utils.ReferenceCodesUtils; public class GetAgreementTermsDimensionPurchased { /* * Obtain the dimensions the buyer has purchased from me via the agreement */ public static void main(String[] args) { String agreementId = args.length > 0 ? args[0] : AGREEMENT_ID; List<String> dimensionKeys = getDimensionKeys(agreementId); ReferenceCodesUtils.formatOutput(dimensionKeys); } public static List<String> getDimensionKeys(String agreementId) { MarketplaceAgreementClient marketplaceAgreementClient = MarketplaceAgreementClient.builder() .httpClient(ApacheHttpClient.builder().build()) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); GetAgreementTermsRequest getAgreementTermsRequest = GetAgreementTermsRequest.builder().agreementId(agreementId) .build(); GetAgreementTermsResponse getAgreementTermsResponse = marketplaceAgreementClient.getAgreementTerms(getAgreementTermsRequest); List<String> dimensionKeys = new ArrayList<String>(); for (AcceptedTerm acceptedTerm : getAgreementTermsResponse.acceptedTerms()) { if (acceptedTerm.configurableUpfrontPricingTerm() != null) { if (acceptedTerm.configurableUpfrontPricingTerm().configuration().selectorValue() != null) { List<Dimension> dimensions = acceptedTerm.configurableUpfrontPricingTerm().configuration().dimensions(); for (Dimension dimension : dimensions) { dimensionKeys.add(dimension.dimensionKey()); } } } } return dimensionKeys; } }
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 Marketplace API Reference Code Library repository.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Obtain the dimensions the buyer has purchased from me via the agreement AG-28 """ import json import logging import os import boto3 import utils.helpers as helper from botocore.exceptions import ClientError logger = logging.getLogger(__name__) # agreement id AGREEMENT_ID = "agmt-1111111111111111111111111" # to use sample file or not USE_SAMPLE_FILE = False SAMPLE_FILE_NAME = "mockup_agreement_terms.json" # attribute name ROOT_ELEM = "acceptedTerms" TERM_NAME = "configurableUpfrontPricingTerm" CONFIG_ELEM = "configuration" ATTRIBUTE_NAME = "selectorValue" def get_agreement_information(mp_client, entity_id): """ Returns customer AWS Account id about a given agreement Args: entity_id str: Entity to return Returns: dict: Dictionary of agreement information """ try: if USE_SAMPLE_FILE: sample_file = os.path.join(os.path.dirname(__file__), SAMPLE_FILE_NAME) terms = open_json_file(sample_file) else: terms = mp_client.get_agreement_terms(agreementId=entity_id) dimensionKeys = [] for term in terms[ROOT_ELEM]: if TERM_NAME in term: if CONFIG_ELEM in term[TERM_NAME]: confParam = term[TERM_NAME][CONFIG_ELEM] if ATTRIBUTE_NAME in confParam: if "dimensions" in confParam: for dimension in confParam["dimensions"]: if "dimensionKey" in dimension: dimensionKey = dimension["dimensionKey"] print(f"Dimension Key: {dimensionKey}") dimensionKeys.append(dimensionKey) return dimensionKeys except ClientError as e: if e.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Agreement with ID %s not found.", entity_id) else: logger.error("Unexpected error: %s", e) def usage_demo(): logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") print("-" * 88) print("Looking for an agreement in the AWS Marketplace.") print("-" * 88) mp_client = boto3.client("marketplace-agreement") helper.pretty_print_datetime(get_agreement_information(mp_client, AGREEMENT_ID)) # open json file from path def open_json_file(filename): with open(filename, "r") as f: return json.load(f) if __name__ == "__main__": usage_demo()
  • For API details, see GetAgreementTerms in AWS SDK for Python (Boto3) API Reference.