use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project athenz by yahoo.
the class DynamoDBNotificationsHelperTest method testUpdateLastNotifiedItem.
@Test
public void testUpdateLastNotifiedItem() throws TimeoutException, InterruptedException {
Date now = new Date(1591706189000L);
long lastNotifiedTime = now.getTime();
long yesterday = lastNotifiedTime - TimeUnit.DAYS.toMillis(1);
long fiveDaysAgo = lastNotifiedTime - 5 * 24 * 60 * 60 * 1000;
DynamoDBNotificationsHelper dynamoDBNotificationsHelper = new DynamoDBNotificationsHelper();
Map<String, AttributeValue> reNotified = ZTSTestUtils.generateAttributeValues("home.test.service3", "reNotified", Long.toString(fiveDaysAgo), Long.toString(fiveDaysAgo), "testServer", null, "testHost2");
Item item = ItemUtils.toItem(reNotified);
UpdateItemOutcome updateItemOutcome1 = Mockito.mock(UpdateItemOutcome.class);
when(updateItemOutcome1.getItem()).thenReturn(item);
Table table = Mockito.mock(Table.class);
Mockito.when(table.updateItem(any(UpdateItemSpec.class))).thenReturn(updateItemOutcome1);
Item updatedItem = dynamoDBNotificationsHelper.updateLastNotifiedItem("lastNotifiedServer", lastNotifiedTime, yesterday, item, "primaryKey", table);
assertEquals(updatedItem, item);
}
use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project athenz by yahoo.
the class DynamoDBSSHRecordStoreConnectionTest method testDeleteSSHRecord.
@Test
public void testDeleteSSHRecord() {
DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey("primaryKey", "cn:1234");
Mockito.doReturn(deleteOutcome).when(table).deleteItem(deleteItemSpec);
DynamoDBSSHRecordStoreConnection dbConn = new DynamoDBSSHRecordStoreConnection(dynamoDB, tableName);
boolean requestSuccess = dbConn.deleteSSHCertRecord("12345", "cn");
assertTrue(requestSuccess);
dbConn.close();
}
use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project athenz by yahoo.
the class DynamoDBSSHRecordStoreConnection method deleteSSHCertRecord.
@Override
public boolean deleteSSHCertRecord(String instanceId, String service) {
final String primaryKey = getPrimaryKey(instanceId, service);
try {
DeleteItemSpec deleteItemSpec = new DeleteItemSpec().withPrimaryKey(KEY_PRIMARY, primaryKey);
table.deleteItem(deleteItemSpec);
return true;
} catch (Exception ex) {
LOGGER.error("DynamoDB Delete Error for {}: {}/{}", primaryKey, ex.getClass(), ex.getMessage());
return false;
}
}
use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project athenz by yahoo.
the class DynamoDBSSHRecordStoreConnection method getSSHCertRecord.
@Override
public SSHCertRecord getSSHCertRecord(String instanceId, String service) {
final String primaryKey = getPrimaryKey(instanceId, service);
try {
Item item = table.getItem(KEY_PRIMARY, primaryKey);
if (item == null) {
LOGGER.error("DynamoDB Get Error for {}: item not found", primaryKey);
return null;
}
SSHCertRecord certRecord = new SSHCertRecord();
certRecord.setInstanceId(instanceId);
certRecord.setService(service);
certRecord.setPrincipals(item.getString(KEY_PRINCIPALS));
certRecord.setClientIP(item.getString(KEY_CLIENT_IP));
certRecord.setPrivateIP(item.getString(KEY_PRIVATE_IP));
return certRecord;
} catch (Exception ex) {
LOGGER.error("DynamoDB Get Error for {}: {}/{}", primaryKey, ex.getClass(), ex.getMessage());
return null;
}
}
use of com.amazonaws.services.dynamodbv2.document.PrimaryKey in project athenz by yahoo.
the class DynamoDBWorkloadRecordStoreConnection method updateWorkloadRecord.
@Override
public boolean updateWorkloadRecord(WorkloadRecord workloadRecord) {
// updateItem does not fail on absence of primaryKey, and behaves as insert. So we should set all attributes with update too.
try {
UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(KEY_PRIMARY, getPrimaryKey(workloadRecord.getService(), workloadRecord.getInstanceId(), workloadRecord.getIp())).withAttributeUpdate(new AttributeUpdate(KEY_SERVICE).put(workloadRecord.getService()), new AttributeUpdate(KEY_PROVIDER).put(workloadRecord.getProvider()), new AttributeUpdate(KEY_IP).put(workloadRecord.getIp()), new AttributeUpdate(KEY_INSTANCE_ID).put(workloadRecord.getInstanceId()), new AttributeUpdate(KEY_CREATION_TIME).put(DynamoDBUtils.getLongFromDate(workloadRecord.getCreationTime())), new AttributeUpdate(KEY_UPDATE_TIME).put(DynamoDBUtils.getLongFromDate(workloadRecord.getUpdateTime())), new AttributeUpdate(KEY_EXPIRY_TIME).put(DynamoDBUtils.getLongFromDate(workloadRecord.getCertExpiryTime())), new AttributeUpdate(KEY_HOSTNAME).put(workloadRecord.getHostname()), new AttributeUpdate(KEY_TTL).put(workloadRecord.getUpdateTime().getTime() / 1000L + expiryTime));
updateItemRetryDynamoDBCommand.run(() -> table.updateItem(updateItemSpec));
return true;
} catch (Exception ex) {
LOGGER.error("DynamoDB Workload update Error={}: {}/{}", workloadRecord, ex.getClass(), ex.getMessage());
return false;
}
}
Aggregations