in src/main/java/com/amazonaws/partners/saasfactory/metering/common/TenantConfiguration.java [90:154]
public static List<TenantConfiguration> getTenantConfigurations(TableConfiguration tableConfig, DynamoDbClient ddb, Logger logger) {
// Add support for the invoice end field here
// https://stripe.com/docs/api/invoices/upcoming
// When the application retrieves the tenant configuration, check for this attribute.
// if it exists, move on. If it doesn't, retrieve it from Stripe, add it to the returned
// tenant configuration and put it back into DDB. The retrieval part should be part of
// the Lambda function that integrates Stripe
List<TenantConfiguration> tenantIDs = new ArrayList<>();
HashMap<String,String> expressionNames = new HashMap<>();
expressionNames.put(CONFIG_EXPRESSION_NAME, SORT_KEY_NAME);
HashMap<String,AttributeValue> expressionValues = new HashMap<>();
AttributeValue sortKeyValue = AttributeValue.builder()
.s(CONFIG_SORT_KEY_VALUE)
.build();
expressionValues.put(CONFIG_EXPRESSION_VALUE, sortKeyValue);
QueryResponse result = null;
do {
QueryRequest request = QueryRequest.builder()
.tableName(tableConfig.getTableName())
.indexName(tableConfig.getIndexName())
.keyConditionExpression(String.format("%s = %s", CONFIG_EXPRESSION_NAME, CONFIG_EXPRESSION_VALUE))
.expressionAttributeNames(expressionNames)
.expressionAttributeValues(expressionValues)
.build();
if (result != null && !result.lastEvaluatedKey().isEmpty()) {
request = request.toBuilder()
.exclusiveStartKey(result.lastEvaluatedKey())
.build();
}
try {
result = ddb.query(request);
} catch (ResourceNotFoundException e) {
logger.error("Table {} does not exist", tableConfig.getTableName());
return new ArrayList<>();
} catch (InternalServerErrorException e) {
logger.error(e.getMessage());
return new ArrayList<>();
}
for (Map<String, AttributeValue> item : result.items()) {
String tenantID = item.get(PRIMARY_KEY_NAME).s();
String externalProductCode = item.get(EXTERNAL_SUBSCRIPTION_IDENTIFIER_ATTRIBUTE_NAME).s();
String invoiceClosingTime = item.getOrDefault(
CLOSING_INVOICE_TIME_ATTRIBUTE_NAME,
AttributeValue.builder()
.nul(true)
.build())
.s();
TenantConfiguration tenant;
try {
tenant = new TenantConfiguration(
tenantID,
externalProductCode,
invoiceClosingTime);
} catch (DateTimeParseException e) {
logger.error("Could not parse the invoice closing date for tenant {}", tenantID);
continue;
}
logger.info("Found tenant ID {}", tenantID);
tenantIDs.add(tenant);
}
} while (!result.lastEvaluatedKey().isEmpty());
return tenantIDs;
}