Get the auto renewal terms 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 auto renewal terms of an agreement using an AWS SDK

The following code examples show how to get the auto renewal terms 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 static com.example.awsmarketplace.utils.ReferenceCodesConstants.*; 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.GetAgreementTermsRequest; import software.amazon.awssdk.services.marketplaceagreement.model.GetAgreementTermsResponse; public class GetAgreementAutoRenewal { /* * Obtain the auto-renewal status of the agreement */ public static void main(String[] args) { String agreementId = args.length > 0 ? args[0] : AGREEMENT_ID; String autoRenewal = getAutoRenewal(agreementId); System.out.println("Auto-Renewal status is " + autoRenewal); } public static String getAutoRenewal(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); String autoRenewal = "No Auto Renewal"; for (AcceptedTerm acceptedTerm : getAgreementTermsResponse.acceptedTerms()) { if (acceptedTerm.renewalTerm() != null && acceptedTerm.renewalTerm().configuration() != null && acceptedTerm.renewalTerm().configuration().enableAutoRenew() != null) { autoRenewal = String.valueOf(acceptedTerm.renewalTerm().configuration().enableAutoRenew().booleanValue()); break; } } return autoRenewal; } }
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 auto-renewal status of the agreement AG-15 """ import json import logging import os import utils.helpers as helper import boto3 from botocore.exceptions import ClientError logger = logging.getLogger(__name__) # agreement id AGREEMENT_ID = "agmt-11111111111111111111" # to use sample file or not USE_SAMPLE_FILE = False SAMPLE_FILE_NAME = "mockup_agreement_terms.json" # attribute name ROOT_ELEM = "acceptedTerms" TERM_NAME = "renewalTerm" CONFIG_ELEM = "configuration" ATTRIBUTE_NAME = "enableAutoRenew" 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) auto_renewal = "No Auto Renewal" for term in terms[ROOT_ELEM]: if TERM_NAME in term: if CONFIG_ELEM in term[TERM_NAME]: auto_renewal = term[TERM_NAME][CONFIG_ELEM][ATTRIBUTE_NAME] break return auto_renewal 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") agreement = get_agreement_information(mp_client, AGREEMENT_ID) if agreement is not None: print(f"Auto Renewal is {agreement}") else: print("Agreement with ID " + AGREEMENT_ID + " is not found") # 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.