SaaS 產品整合的程式碼範例 - AWS Marketplace

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

SaaS 產品整合的程式碼範例

下列程式碼範例可協助您整合軟體即服務 (SaaS) 產品與發佈和維護產品所需的AWS Marketplace API。

ResolveCustomer程式碼範例

下列程式碼範例適用於所有定價模式。Python 範例會將x-amzn-marketplace-token權杖交換為CustomerIdentifierProductCode、和CustomerAWSAccountIdCustomerAWSAccountId是與訂閱相關聯的AWS 帳戶識別碼。此代碼在您註冊網站上的應用程序中運行,當您從AWS Marketplace 管理入口網站. 重定向是包含令牌的 POST 請求。

如需有關的詳細資訊ResolveCustomer,請參閱AWS Marketplace計量服務 API 參考ResolveCustomer中的。

# Import AWS Python SDK and urllib.parse import boto3 import urllib.parse as urlparse # Resolving Customer Registration Token formFields = urlparse.parse_qs(postBody) regToken = formFields['x-amzn-marketplace-token'][0] # If regToken present in POST request, exchange for customerID if (regToken): marketplaceClient = boto3.client('meteringmarketplace') customerData = marketplaceClient.resolve_customer(RegistrationToken=regToken) productCode = customerData['ProductCode'] customerID = customerData['CustomerIdentifier'] customerAWSAccountId = customerData['CustomerAWSAccountId'] # TODO: Store customer information # TODO: Validate no other accounts share the same customerID

回應範例

{ 'CustomerIdentifier': 'string', 'CustomerAWSAccountId':'string', 'ProductCode': 'string' }

GetEntitlement程式碼範例

以下代碼示例與具有消費定價模型的合同和 SaaS 合同的 SaaS 產品相關。Python 範例會驗證客戶是否具有作用中的權益。

如需有關的詳細資訊GetEntitlement,請參閱AWS Marketplace權益服務 API 參考GetEntitlement中的。

# Import AWS Python SDK import boto3 marketplaceClient = boto3.client('marketplace-entitlement', region_name='us-east-1') # Filter entitlements for a specific customerID # # productCode is supplied after the AWS Marketplace Ops team has published # the product to limited # # customerID is obtained from the ResolveCustomer response entitlement = marketplaceClient.get_entitlements({ 'ProductCode': 'productCode', 'Filter' : { 'CUSTOMER_IDENTIFIER': [ 'customerID', ] }, 'NextToken' : 'string', 'MaxResults': 123 }) # TODO: Verify the dimension a customer is subscribed to and the quantity, # if applicable

回應範例

傳回的值對應於在中建立產品時所建立的維度AWS Marketplace 管理入口網站。

{ "Entitlements": [ { "CustomerIdentifier": "string", "Dimension": "string", "ExpirationDate": number, "ProductCode": "string", "Value": { "BooleanValue": boolean, "DoubleValue": number, "IntegerValue": number, "StringValue": "string" } } ], "NextToken": "string" }

BatchMeterUsage程式碼範例

下列程式碼範例適用於 SaaS 訂閱與消費定價模式的合約,但不適用於未消費的 SaaS 合約產品。Python 範例會傳送計量記錄,向AWS Marketplace您的客戶收取 pay-as-you-go 費用。

# NOTE: Your application will need to aggregate usage for the # customer for the hour and set the quantity as seen below. # AWS Marketplace can only accept records for up to an hour in the past. # # productCode is supplied after the AWS Marketplace Ops team has # published the product to limited # # customerID is obtained from the ResolveCustomer response # Import AWS Python SDK import boto3 usageRecord = [ { 'Timestamp': datetime(2015, 1, 1), 'CustomerIdentifier': 'customerID', 'Dimension': 'string', 'Quantity': 123 } ] marketplaceClient = boto3.client('meteringmarketplace') response = marketplaceClient.batch_meter_usage(usageRecord, productCode)

如需有關的詳細資訊BatchMeterUsage,請參閱AWS Marketplace計量服務 API 參考BatchMeterUsage中的。

回應範例

{ 'Results': [ { 'UsageRecord': { 'Timestamp': datetime(2015, 1, 1), 'CustomerIdentifier': 'string', 'Dimension': 'string', 'Quantity': 123 }, 'MeteringRecordId': 'string', 'Status': 'Success' | 'CustomerNotSubscribed' | 'DuplicateRecord' }, ], 'UnprocessedRecords': [ { 'Timestamp': datetime(2015, 1, 1), 'CustomerIdentifier': 'string', 'Dimension': 'string', 'Quantity': 123 } ] }

BatchMeterUsage與使用分配標記代碼示例(可選)

下列程式碼範例適用於 SaaS 訂閱和含消費定價模式的合約,但不適用於未消費的 SaaS 合約產品。Python 範例會傳送含有適當使用量分配標籤的計量記錄,AWS Marketplace以向您的客戶收取 pay-as-you-go 費用。

# NOTE: Your application will need to aggregate usage for the # customer for the hour and set the quantity as seen below. # AWS Marketplace can only accept records for up to an hour in the past. # # productCode is supplied after the AWS Marketplace Ops team has # published the product to limited # # customerID is obtained from the ResolveCustomer response # Import AWS Python SDK import boto3 import time usageRecords = [ { "Timestamp": int(time.time()), "CustomerIdentifier": "customerID", "Dimension": "Dimension1", "Quantity":3, "UsageAllocations": [ { "AllocatedUsageQuantity": 2, "Tags": [ { "Key": "BusinessUnit", "Value": "IT" }, { "Key": "AccountId", "Value": "123456789" }, ] }, { "AllocatedUsageQuantity": 1, "Tags": [ { "Key": "BusinessUnit", "Value": "Finance" }, { "Key": "AccountId", "Value": "987654321" }, ] }, ] } ] marketplaceClient = boto3.client('meteringmarketplace') response = marketplaceClient.batch_meter_usage(UsageRecords=usageRecords, ProductCode="testProduct")

如需有關的詳細資訊BatchMeterUsage,請BatchMeterUsage參閱 AWS Marketplace Metering ServiceAPI 參考中的。

回應範例

{ "Results": [ { "Timestamp": "1634691015", "CustomerIdentifier": "customerID", "Dimension": "Dimension1", "Quantity":3, "UsageAllocations": [ { "AllocatedUsageQuantity": 2, "Tags": [ { "Key": "BusinessUnit", "Value": "IT" }, { "Key": "AccountId", "Value": "123456789" }, ] }, { "AllocatedUsageQuantity": 1, "Tags": [ { "Key": "BusinessUnit", "Value": "Finance" }, { "Key": "AccountId", "Value": "987654321" }, ] }, ] }, "MeteringRecordId": "8fjef98ejf", "Status": "Success" }, ], "UnprocessedRecords": [ { "Timestamp": "1634691015", "CustomerIdentifier": "customerID", "Dimension": "Dimension1", "Quantity":3, "UsageAllocations": [] } ] }