Search in sources :

Example 26 with PrimaryKey

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);
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue) Table(com.amazonaws.services.dynamodbv2.document.Table) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) UpdateItemOutcome(com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome) Test(org.testng.annotations.Test)

Example 27 with PrimaryKey

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();
}
Also used : DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec) Test(org.testng.annotations.Test)

Example 28 with PrimaryKey

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;
    }
}
Also used : DeleteItemSpec(com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)

Example 29 with PrimaryKey

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;
    }
}
Also used : Item(com.amazonaws.services.dynamodbv2.document.Item) SSHCertRecord(com.yahoo.athenz.common.server.ssh.SSHCertRecord)

Example 30 with PrimaryKey

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;
    }
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)

Aggregations

Test (org.testng.annotations.Test)15 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)11 Item (com.amazonaws.services.dynamodbv2.document.Item)9 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)8 Table (com.amazonaws.services.dynamodbv2.document.Table)7 DeleteItemSpec (com.amazonaws.services.dynamodbv2.document.spec.DeleteItemSpec)5 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)5 UpdateItemOutcome (com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome)4 AmazonServiceException (com.amazonaws.AmazonServiceException)3 SSHCertRecord (com.yahoo.athenz.common.server.ssh.SSHCertRecord)3 Date (java.util.Date)3 AmazonClientException (com.amazonaws.AmazonClientException)2 ClientConfiguration (com.amazonaws.ClientConfiguration)2 PropertiesCredentials (com.amazonaws.auth.PropertiesCredentials)2 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)2 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)2 PrimaryKey (com.amazonaws.services.dynamodbv2.document.PrimaryKey)2 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)2 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)2 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)2