Get the product type of 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 product type of an agreement using an AWS SDK

The following code examples show how to get the product type of 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.DescribeAgreementRequest; import software.amazon.awssdk.services.marketplaceagreement.model.DescribeAgreementResponse; import software.amazon.awssdk.services.marketplaceagreement.model.Resource; import static com.example.awsmarketplace.utils.ReferenceCodesConstants.*; import java.util.ArrayList; import java.util.List; import com.example.awsmarketplace.utils.ReferenceCodesUtils; public class GetAgreementProductType { /* * Obtain the Product Type of the product the agreement was created on */ public static void main(String[] args) { String agreementId = args.length > 0 ? args[0] : AGREEMENT_ID; List<String> productIds = getProducts(agreementId); ReferenceCodesUtils.formatOutput(productIds); } public static List<String> getProducts(String agreementId) { MarketplaceAgreementClient marketplaceAgreementClient = MarketplaceAgreementClient.builder() .httpClient(ApacheHttpClient.builder().build()) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); DescribeAgreementRequest describeAgreementRequest = DescribeAgreementRequest.builder() .agreementId(agreementId) .build(); DescribeAgreementResponse describeAgreementResponse = marketplaceAgreementClient.describeAgreement(describeAgreementRequest); List<String> productIds = new ArrayList<String>(); for (Resource resource : describeAgreementResponse.proposalSummary().resources()) { productIds.add(resource.id() + ":" + resource.type()); } return productIds; } }
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 Product Type of the product the agreement was created on AG-11 """ import logging import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) # agreement id AGREEMENT_ID = "agmt-1111111111111111111111111" def get_agreement_information(mp_client, entity_id): """ Returns information about a given agreement Args: entity_id str: Entity to return Returns: dict: Dictionary of agreement information """ try: agreement = mp_client.describe_agreement(agreementId=entity_id) return agreement 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 offer and product details in a given agreement by agreement id.") print("-" * 88) mp_client = boto3.client("marketplace-agreement") agreement = get_agreement_information(mp_client, AGREEMENT_ID) if agreement is not None: productHash = {} for resource in agreement["resourceSummaries"]: productHash[resource["resourceId"]] = resource["resourceType"] for key, value in productHash.items(): print(f"Product ID: {key} | Product Type: {value}") else: print("Agreement with ID " + AGREEMENT_ID + " is not found") if __name__ == "__main__": usage_demo()
  • For API details, see DescribeAgreement in AWS SDK for Python (Boto3) API Reference.