Search in sources :

Example 1 with ReplicaUpdate

use of software.amazon.awssdk.services.dynamodb.model.ReplicaUpdate in project para by Erudika.

the class AWSDynamoUtils method deleteTable.

/**
 * Deletes the main table from AWS DynamoDB.
 * @param appid name of the {@link com.erudika.para.core.App}
 * @return true if deleted
 */
public static boolean deleteTable(String appid) {
    if (StringUtils.isBlank(appid) || !existsTable(appid)) {
        return false;
    }
    try {
        String table = getTableNameForAppid(appid);
        if (!getReplicaRegions().isEmpty() && !App.isRoot(appid)) {
            List<ReplicaUpdate> replicaUpdates = new LinkedList<>();
            getReplicaRegions().stream().forEach(region -> {
                logger.info("Removing replica from global table '{}' in region {}...", table, region);
                replicaUpdates.add(ReplicaUpdate.builder().delete(d -> d.regionName(region)).build());
            });
            try {
                // this only removes the replicas for each region - it DOES NOT delete the actual replica tables
                getClient().updateGlobalTable(b -> b.globalTableName(table).replicaUpdates(replicaUpdates));
                getReplicaRegions().stream().forEach(region -> {
                    DynamoDbAsyncClient asyncdb = DynamoDbAsyncClient.builder().region(Region.of(region)).build();
                    asyncdb.deleteTable(b -> b.tableName(table));
                    logger.info("Deleted DynamoDB table '{}' in region {}.", table, region);
                });
            } catch (Exception ex) {
                logger.error(null, ex);
            }
        } else {
            getClient().deleteTable(b -> b.tableName(table));
            logger.info("Deleted DynamoDB table '{}'.", table);
        }
    } catch (Exception e) {
        logger.error(null, e);
        return false;
    }
    return true;
}
Also used : DynamoDbAsyncClient(software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient) ReplicaUpdate(software.amazon.awssdk.services.dynamodb.model.ReplicaUpdate) LinkedList(java.util.LinkedList) ProvisionedThroughputExceededException(software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputExceededException)

Aggregations

LinkedList (java.util.LinkedList)1 DynamoDbAsyncClient (software.amazon.awssdk.services.dynamodb.DynamoDbAsyncClient)1 ProvisionedThroughputExceededException (software.amazon.awssdk.services.dynamodb.model.ProvisionedThroughputExceededException)1 ReplicaUpdate (software.amazon.awssdk.services.dynamodb.model.ReplicaUpdate)1