use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project gora by apache.
the class DynamoDBUtils method waitForTableToBecomeAvailable.
/**
* Waits up to 6 minutes to confirm if a table has been created or not
*
* @param awsClient
* @param tableName
*/
public static void waitForTableToBecomeAvailable(AmazonDynamoDB awsClient, String tableName) {
LOG.debug("Waiting for {} to become available", tableName);
long startTime = System.currentTimeMillis();
long endTime = startTime + WAIT_TIME;
while (System.currentTimeMillis() < endTime) {
try {
Thread.sleep(SLEEP_TIME);
} catch (Exception e) {
}
try {
DescribeTableRequest request = new DescribeTableRequest().withTableName(tableName);
TableDescription tableDescription = awsClient.describeTable(request).getTable();
String tableStatus = tableDescription.getTableStatus();
LOG.debug("{} - current state: {}", tableName, tableStatus);
if (tableStatus.equals(TableStatus.ACTIVE.toString()))
return;
} catch (AmazonServiceException ase) {
if (ase.getErrorCode().equalsIgnoreCase("ResourceNotFoundException") == false)
throw ase;
}
}
throw new RuntimeException("Table " + tableName + " never became active");
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project gora by apache.
the class DynamoDBUtils method executeCreateTableRequest.
/**
* Executes a create table request using the DynamoDB client and waits the
* default time until it's been created.
*
* @param awsClient
* @param keySchema
* @param tableName
* @param proThrou
*/
public static void executeCreateTableRequest(AmazonDynamoDB awsClient, String tableName, ArrayList<KeySchemaElement> keySchema, Map<String, String> attrs, ProvisionedThroughput proThrou) {
CreateTableRequest createTableRequest = buildCreateTableRequest(tableName, keySchema, proThrou, attrs);
// use the client to perform the request
try {
awsClient.createTable(createTableRequest).getTableDescription();
// wait for table to become active
waitForTableToBecomeAvailable(awsClient, tableName);
} catch (ResourceInUseException ex) {
LOG.warn("Table '{}' already exists.", tableName);
} finally {
LOG.info("Table '{}' is available.", tableName);
}
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project athenz by yahoo.
the class DynamoDBClientFetcherImpl method getAuthenticatedDynamoDBClient.
private DynamoDBClientAndCredentials getAuthenticatedDynamoDBClient(DynamoDBClientSettings dynamoDBClientSettings, ZTSClientNotificationSender ztsClientNotificationSender) {
AWSCredentialsProviderImpl credentialsProvider = getCredentials(dynamoDBClientSettings, ztsClientNotificationSender);
AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().withCredentials(credentialsProvider).withRegion(dynamoDBClientSettings.getRegion()).build();
return new DynamoDBClientAndCredentials(client, credentialsProvider);
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project athenz by yahoo.
the class DynamoDBStatusChecker method check.
@Override
public void check() throws StatusCheckException {
DynamoDBClientAndCredentials clientAndCreds = null;
try {
// Get DynamoDB client and temp credentials (if required)
DynamoDBClientFetcher dynamoDBClientFetcher = getDynamoDBClientFetcher();
clientAndCreds = dynamoDBClientFetcher.getDynamoDBClient(null, keyStore);
AmazonDynamoDB amazonDynamoDB = clientAndCreds.getAmazonDynamoDB();
// Get list of tables and verify our table appears
boolean tableFound = amazonDynamoDB.listTables().getTableNames().stream().anyMatch(fetchedTableName -> fetchedTableName.equals(tableName));
if (!tableFound) {
throw new StatusCheckException(HttpStatus.SC_OK, "Table named " + tableName + " wasn't found in DynamoDB");
}
} catch (StatusCheckException ex) {
throw ex;
} catch (Throwable ex) {
throw new StatusCheckException(ex);
} finally {
// Close resources
if (clientAndCreds != null) {
try {
if (clientAndCreds.getAmazonDynamoDB() != null) {
clientAndCreds.getAmazonDynamoDB().shutdown();
}
if (clientAndCreds.getAwsCredentialsProvider() != null) {
clientAndCreds.getAwsCredentialsProvider().close();
}
} catch (IOException ignored) {
}
}
}
}
use of com.amazonaws.services.dynamodbv2.AmazonDynamoDB in project athenz by yahoo.
the class DynamoDBClientFetcherImplTest method testGetAuthenticatedClient.
@Test
public void testGetAuthenticatedClient() {
// public
String certPath = Resources.getResource("gdpr.aws.core.cert.pem").getPath();
// private
String keyPath = Resources.getResource("unit_test_gdpr.aws.core.key.pem").getPath();
System.setProperty(ZTS_PROP_DYNAMODB_KEY_PATH, keyPath);
System.setProperty(ZTS_PROP_DYNAMODB_CERT_PATH, certPath);
System.setProperty(ZTS_PROP_DYNAMODB_DOMAIN, "test.domain");
System.setProperty(ZTS_PROP_DYNAMODB_REGION, "test.region");
System.setProperty(ZTS_PROP_DYNAMODB_ROLE, "test.role");
System.setProperty(ZTS_PROP_DYNAMODB_TRUSTSTORE, "test.truststore");
System.setProperty(ZTS_PROP_DYNAMODB_TRUSTSTORE_PASSWORD, "test.truststore.password");
System.setProperty(ZTS_PROP_DYNAMODB_ZTS_URL, "https://dev.zts.athenzcompany.com:4443/zts/v1");
DynamoDBClientFetcherImpl dynamoDBClientFetcher = new DynamoDBClientFetcherImpl();
PrivateKeyStore keyStore = Mockito.mock(PrivateKeyStore.class);
when(keyStore.getApplicationSecret(Mockito.eq(""), Mockito.eq("test.truststore.password"))).thenReturn("mockPassword");
ZTSClientNotificationSender ztsClientNotificationSender = Mockito.mock(ZTSClientNotificationSender.class);
AmazonDynamoDB dynamoDBClient = dynamoDBClientFetcher.getDynamoDBClient(ztsClientNotificationSender, keyStore).getAmazonDynamoDB();
assertNotNull(dynamoDBClient);
// Also try with min and max expiry set
System.setProperty(ZTS_PROP_DYNAMODB_MIN_EXPIRY_TIME, "10");
System.setProperty(ZTS_PROP_DYNAMODB_MAX_EXPIRY_TIME, "100");
dynamoDBClient = dynamoDBClientFetcher.getDynamoDBClient(ztsClientNotificationSender, keyStore).getAmazonDynamoDB();
assertNotNull(dynamoDBClient);
System.clearProperty(ZTS_PROP_DYNAMODB_KEY_PATH);
System.clearProperty(ZTS_PROP_DYNAMODB_CERT_PATH);
System.clearProperty(ZTS_PROP_DYNAMODB_DOMAIN);
System.clearProperty(ZTS_PROP_DYNAMODB_REGION);
System.clearProperty(ZTS_PROP_DYNAMODB_ROLE);
System.clearProperty(ZTS_PROP_DYNAMODB_TRUSTSTORE);
System.clearProperty(ZTS_PROP_DYNAMODB_TRUSTSTORE_PASSWORD);
System.clearProperty(ZTS_PROP_DYNAMODB_ZTS_URL);
}
Aggregations