Ottieni i dettagli del prodotto e dell'offerta da un contratto utilizzando un AWS SDK - Esempi di codice dell'AWS SDK

Ci sono altri AWS SDK esempi disponibili nel repository AWS Doc SDK Examples GitHub .

Le traduzioni sono generate tramite traduzione automatica. In caso di conflitto tra il contenuto di una traduzione e la versione originale in Inglese, quest'ultima prevarrà.

Ottieni i dettagli del prodotto e dell'offerta da un contratto utilizzando un AWS SDK

I seguenti esempi di codice mostrano come ottenere dettagli su prodotti e offerte da un contratto.

Java
SDKper Java 2.x
Nota

C'è altro su. GitHub Trova l'esempio completo e scopri come configurarlo ed eseguirlo nel repository Marketplace AWS APIReference Code Library.

// 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 java.util.ArrayList; import java.util.List; import static com.example.awsmarketplace.utils.ReferenceCodesConstants.*; import com.example.awsmarketplace.utils.ReferenceCodesUtils; import software.amazon.awssdk.services.marketplacecatalog.MarketplaceCatalogClient; import software.amazon.awssdk.services.marketplacecatalog.model.DescribeEntityRequest; import software.amazon.awssdk.services.marketplacecatalog.model.DescribeEntityResponse; public class GetProductAndOfferDetailFromAgreement { public static void main(String[] args) { // call Agreement API to get offer and product information for the agreement String agreementId = args.length > 0 ? args[0] : AGREEMENT_ID; List<DescribeEntityResponse> entityResponseList = getEntities(agreementId); for (DescribeEntityResponse response : entityResponseList) { ReferenceCodesUtils.formatOutput(response); } } public static List<DescribeEntityResponse> getEntities(String agreementId) { List<DescribeEntityResponse> entityResponseList = new ArrayList<DescribeEntityResponse> (); MarketplaceAgreementClient marketplaceAgreementClient = MarketplaceAgreementClient.builder() .httpClient(ApacheHttpClient.builder().build()) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); DescribeAgreementRequest describeAgreementRequest = DescribeAgreementRequest.builder() .agreementId(agreementId) .build(); DescribeAgreementResponse describeAgreementResponse = marketplaceAgreementClient.describeAgreement(describeAgreementRequest); // get offer id for the given agreement String offerId = describeAgreementResponse.proposalSummary().offerId(); // get all the product ids for this agreement List<String> productIds = new ArrayList<String>(); for (Resource resource : describeAgreementResponse.proposalSummary().resources()) { productIds.add(resource.id()); } // call Catalog API to get the details of the offer and products MarketplaceCatalogClient marketplaceCatalogClient = MarketplaceCatalogClient.builder() .httpClient(ApacheHttpClient.builder().build()) .credentialsProvider(ProfileCredentialsProvider.create()) .build(); DescribeEntityRequest describeEntityRequest = DescribeEntityRequest.builder() .catalog(AWS_MP_CATALOG) .entityId(offerId).build(); DescribeEntityResponse describeEntityResponse = marketplaceCatalogClient.describeEntity(describeEntityRequest); entityResponseList.add(describeEntityResponse); for (String productId : productIds) { describeEntityRequest = DescribeEntityRequest.builder() .catalog(AWS_MP_CATALOG) .entityId(productId).build(); describeEntityResponse = marketplaceCatalogClient.describeEntity(describeEntityRequest); System.out.println("Print details for product " + productId); entityResponseList.add(describeEntityResponse); } return entityResponseList; } }
Python
SDKper Python (Boto3)
Nota

C'è di più su. GitHub Trova l'esempio completo e scopri come configurarlo ed eseguirlo nel repository Marketplace AWS APIReference Code Library.

# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. # SPDX-License-Identifier: Apache-2.0 """ Purpose Shows how to use the AWS SDK for Python (Boto3) to get product and offer details in a given agreement AG-10 """ import argparse import logging import boto3 import utils.helpers as helper from botocore.exceptions import ClientError mpa_client = boto3.client("marketplace-agreement") mpc_client = boto3.client("marketplace-catalog") logger = logging.getLogger(__name__) def get_agreement_information(agreement_id): """ Returns information about a given agreement Args: agreement_id str: Entity to return Returns: dict: Dictionary of agreement information """ try: agreement = mpa_client.describe_agreement(agreementId=agreement_id) return agreement except ClientError as e: if e.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Agreement with ID %s not found.", agreement_id) else: logger.error("Unexpected error: %s", e) def get_entity_information(entity_id): """ Returns information about a given entity Args: entity_id str: Entity to return Returns: dict: Dictionary of entity information """ try: response = mpc_client.describe_entity( Catalog="AWSMarketplace", EntityId=entity_id, ) return response except ClientError as e: if e.response["Error"]["Code"] == "ResourceNotFoundException": logger.error("Entity with ID %s not found.", entity_id) else: logger.error("Unexpected error: %s", e) def get_agreement_components(agreement_id): agreement_component_list = [] agreement = get_agreement_information(agreement_id) if agreement is not None: productIds = [] for resource in agreement["proposalSummary"]["resources"]: productIds.append(resource["id"]) for product_id in productIds: product_document = get_entity_information(product_id) product_document_dict = {} product_document_dict["product_id"] = product_id product_document_dict["document"] = product_document agreement_component_list.append(product_document_dict) offerId = agreement["proposalSummary"]["offerId"] offer_document = get_entity_information(offerId) offer_document_dict = {} offer_document_dict["offer_id"] = offerId offer_document_dict["document"] = offer_document agreement_component_list.append(offer_document_dict) return agreement_component_list else: print("Agreement with ID " + args.agreement_id + " is not found") if __name__ == "__main__": logging.basicConfig(level=logging.INFO, format="%(levelname)s: %(message)s") parser = argparse.ArgumentParser() parser.add_argument( "--agreement_id", "-aid", help="Provide agreement ID to search for product and offer detail", required=True, ) args = parser.parse_args() product_offer_detail = get_agreement_components(agreement_id=args.agreement_id) helper.pretty_print_datetime(product_offer_detail)