Search in sources :

Example 16 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project cas by apereo.

the class DynamoDbServiceRegistryFacilitator method buildTableAttributeValuesMapFromService.

/**
 * Build table attribute values from map.
 *
 * @param service the service
 * @return the map
 */
public Map<String, AttributeValue> buildTableAttributeValuesMapFromService(final RegisteredService service) {
    final Map<String, AttributeValue> values = new HashMap<>();
    values.put(ColumnNames.ID.getColumnName(), new AttributeValue(String.valueOf(service.getId())));
    values.put(ColumnNames.NAME.getColumnName(), new AttributeValue(service.getName()));
    values.put(ColumnNames.DESCRIPTION.getColumnName(), new AttributeValue(service.getDescription()));
    values.put(ColumnNames.SERVICE_ID.getColumnName(), new AttributeValue(service.getServiceId()));
    final ByteArrayOutputStream out = new ByteArrayOutputStream();
    jsonSerializer.to(out, service);
    values.put(ColumnNames.ENCODED.getColumnName(), new AttributeValue().withB(ByteBuffer.wrap(out.toByteArray())));
    LOGGER.debug("Created attribute values [{}] based on provided service [{}]", values, service);
    return values;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) HashMap(java.util.HashMap) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 17 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project cas by apereo.

the class DynamoDbTicketRegistryFacilitator method createTicketTables.

/**
 * Create ticket tables.
 *
 * @param deleteTables the delete tables
 */
public void createTicketTables(final boolean deleteTables) {
    final Collection<TicketDefinition> metadata = this.ticketCatalog.findAll();
    metadata.forEach(Unchecked.consumer(r -> {
        final CreateTableRequest request = new CreateTableRequest().withAttributeDefinitions(new AttributeDefinition(ColumnNames.ID.getColumnName(), ScalarAttributeType.S)).withKeySchema(new KeySchemaElement(ColumnNames.ID.getColumnName(), KeyType.HASH)).withProvisionedThroughput(new ProvisionedThroughput(dynamoDbProperties.getReadCapacity(), dynamoDbProperties.getWriteCapacity())).withTableName(r.getProperties().getStorageName());
        if (deleteTables) {
            final DeleteTableRequest delete = new DeleteTableRequest(r.getProperties().getStorageName());
            LOGGER.debug("Sending delete request [{}] to remove table if necessary", delete);
            TableUtils.deleteTableIfExists(amazonDynamoDBClient, delete);
        }
        LOGGER.debug("Sending delete request [{}] to create table", request);
        TableUtils.createTableIfNotExists(amazonDynamoDBClient, request);
        LOGGER.debug("Waiting until table [{}] becomes active...", request.getTableName());
        TableUtils.waitUntilActive(amazonDynamoDBClient, request.getTableName());
        final DescribeTableRequest describeTableRequest = new DescribeTableRequest().withTableName(request.getTableName());
        LOGGER.debug("Sending request [{}] to obtain table description...", describeTableRequest);
        final TableDescription tableDescription = amazonDynamoDBClient.describeTable(describeTableRequest).getTable();
        LOGGER.debug("Located newly created table with description: [{}]", tableDescription);
    }));
}
Also used : DeleteTableRequest(com.amazonaws.services.dynamodbv2.model.DeleteTableRequest) PutItemResult(com.amazonaws.services.dynamodbv2.model.PutItemResult) Getter(lombok.Getter) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement) KeyType(com.amazonaws.services.dynamodbv2.model.KeyType) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) SerializationUtils(org.apache.commons.lang3.SerializationUtils) HashMap(java.util.HashMap) ByteBuffer(java.nio.ByteBuffer) ArrayList(java.util.ArrayList) DeleteItemResult(com.amazonaws.services.dynamodbv2.model.DeleteItemResult) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Map(java.util.Map) CollectionUtils(org.apereo.cas.util.CollectionUtils) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) TicketCatalog(org.apereo.cas.ticket.TicketCatalog) Unchecked(org.jooq.lambda.Unchecked) GetItemRequest(com.amazonaws.services.dynamodbv2.model.GetItemRequest) ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) Collection(java.util.Collection) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) Collectors(java.util.stream.Collectors) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) DeleteItemRequest(com.amazonaws.services.dynamodbv2.model.DeleteItemRequest) TableUtils(com.amazonaws.services.dynamodbv2.util.TableUtils) PutItemRequest(com.amazonaws.services.dynamodbv2.model.PutItemRequest) ScalarAttributeType(com.amazonaws.services.dynamodbv2.model.ScalarAttributeType) TicketDefinition(org.apereo.cas.ticket.TicketDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) Slf4j(lombok.extern.slf4j.Slf4j) DynamoDbTicketRegistryProperties(org.apereo.cas.configuration.model.support.dynamodb.DynamoDbTicketRegistryProperties) AllArgsConstructor(lombok.AllArgsConstructor) Ticket(org.apereo.cas.ticket.Ticket) DeleteTableRequest(com.amazonaws.services.dynamodbv2.model.DeleteTableRequest) TicketDefinition(org.apereo.cas.ticket.TicketDefinition) AttributeDefinition(com.amazonaws.services.dynamodbv2.model.AttributeDefinition) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) DescribeTableRequest(com.amazonaws.services.dynamodbv2.model.DescribeTableRequest) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest) TableDescription(com.amazonaws.services.dynamodbv2.model.TableDescription) KeySchemaElement(com.amazonaws.services.dynamodbv2.model.KeySchemaElement)

Example 18 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project cas by apereo.

the class DynamoDbCloudConfigBootstrapConfiguration method locate.

@Override
public PropertySource<?> locate(final Environment environment) {
    final AmazonDynamoDB amazonDynamoDBClient = getAmazonDynamoDbClient(environment);
    createSettingsTable(amazonDynamoDBClient, false);
    final ScanRequest scan = new ScanRequest(TABLE_NAME);
    LOGGER.debug("Scanning table with request [{}]", scan);
    final ScanResult result = amazonDynamoDBClient.scan(scan);
    LOGGER.debug("Scanned table with result [{}]", scan);
    final Properties props = new Properties();
    result.getItems().stream().map(DynamoDbCloudConfigBootstrapConfiguration::retrieveSetting).forEach(p -> props.put(p.getKey(), p.getValue()));
    return new PropertiesPropertySource(getClass().getSimpleName(), props);
}
Also used : ScanRequest(com.amazonaws.services.dynamodbv2.model.ScanRequest) ScanResult(com.amazonaws.services.dynamodbv2.model.ScanResult) PropertiesPropertySource(org.springframework.core.env.PropertiesPropertySource) AmazonDynamoDB(com.amazonaws.services.dynamodbv2.AmazonDynamoDB) Properties(java.util.Properties)

Example 19 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project amos-ss17-alexa by c-i-ber.

the class DynamoDbMapper method dropTable.

/**
 * drops table by a pojo class
 *
 * @param cl class which will be mapped to drop a table
 */
public void dropTable(Class cl) {
    DeleteTableRequest tableRequest = mapper.generateDeleteTableRequest(cl);
    Table table = dynamoDB.getTable(tableRequest.getTableName());
    try {
        dynamoDbClient.deleteTable(tableRequest);
        table.waitForDelete();
    } catch (ResourceNotFoundException e) {
    // Table does not exist
    } catch (InterruptedException e) {
        e.printStackTrace();
    }
}
Also used : DeleteTableRequest(com.amazonaws.services.dynamodbv2.model.DeleteTableRequest) Table(com.amazonaws.services.dynamodbv2.document.Table) ResourceNotFoundException(com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)

Example 20 with Table

use of com.amazonaws.services.dynamodbv2.document.Table in project amos-ss17-alexa by c-i-ber.

the class DynamoDbMapper method createTable.

/**
 * creates a new table by a pojo class
 *
 * @param cl class which will be mapped to the new table
 */
public void createTable(Class cl) throws InterruptedException {
    CreateTableRequest tableRequest = mapper.generateCreateTableRequest(cl);
    tableRequest.setProvisionedThroughput(new ProvisionedThroughput(1L, 1L));
    Table table = dynamoDB.createTable(tableRequest);
    table.waitForActive();
}
Also used : Table(com.amazonaws.services.dynamodbv2.document.Table) ProvisionedThroughput(com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput) CreateTableRequest(com.amazonaws.services.dynamodbv2.model.CreateTableRequest)

Aggregations

AmazonServiceException (com.amazonaws.AmazonServiceException)16 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)12 ResourceNotFoundException (com.amazonaws.services.dynamodbv2.model.ResourceNotFoundException)12 TableDescription (com.amazonaws.services.dynamodbv2.model.TableDescription)11 HashMap (java.util.HashMap)10 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)9 DescribeTableRequest (com.amazonaws.services.dynamodbv2.model.DescribeTableRequest)9 ProvisionedThroughput (com.amazonaws.services.dynamodbv2.model.ProvisionedThroughput)9 CreateTableRequest (com.amazonaws.services.dynamodbv2.model.CreateTableRequest)8 KeySchemaElement (com.amazonaws.services.dynamodbv2.model.KeySchemaElement)8 ScanRequest (com.amazonaws.services.dynamodbv2.model.ScanRequest)8 ScanResult (com.amazonaws.services.dynamodbv2.model.ScanResult)7 AttributeDefinition (com.amazonaws.services.dynamodbv2.model.AttributeDefinition)6 IOException (java.io.IOException)6 ArrayList (java.util.ArrayList)6 DeleteTableRequest (com.amazonaws.services.dynamodbv2.model.DeleteTableRequest)5 GoraException (org.apache.gora.util.GoraException)4 AmazonClientException (com.amazonaws.AmazonClientException)3 DynamoDBMapper (com.amazonaws.services.dynamodbv2.datamodeling.DynamoDBMapper)2 Table (com.amazonaws.services.dynamodbv2.document.Table)2