Search in sources :

Example 1 with AttributeUpdate

use of com.amazonaws.services.dynamodbv2.document.AttributeUpdate in project spring-integration-aws by spring-projects.

the class DynamoDbMetaDataStore method putIfAbsent.

@Override
public String putIfAbsent(String key, String value) {
    Assert.hasText(key, "'key' must not be empty.");
    Assert.hasText(value, "'value' must not be empty.");
    awaitForActive();
    try {
        this.table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY, key).withAttributeUpdate(new AttributeUpdate(VALUE).put(value)).withExpected(new Expected(KEY).notExist()));
        return null;
    } catch (ConditionalCheckFailedException e) {
        return get(key);
    }
}
Also used : AttributeUpdate(com.amazonaws.services.dynamodbv2.document.AttributeUpdate) Expected(com.amazonaws.services.dynamodbv2.document.Expected) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 2 with AttributeUpdate

use of com.amazonaws.services.dynamodbv2.document.AttributeUpdate in project spring-integration-aws by spring-projects.

the class DynamoDbMetaDataStore method replace.

@Override
public boolean replace(String key, String oldValue, String newValue) {
    Assert.hasText(key, "'key' must not be empty.");
    Assert.hasText(oldValue, "'value' must not be empty.");
    Assert.hasText(newValue, "'newValue' must not be empty.");
    awaitForActive();
    try {
        return this.table.updateItem(new UpdateItemSpec().withPrimaryKey(KEY, key).withAttributeUpdate(new AttributeUpdate(VALUE).put(newValue)).withExpected(new Expected(VALUE).eq(oldValue)).withReturnValues(ReturnValue.UPDATED_NEW)).getItem() != null;
    } catch (ConditionalCheckFailedException e) {
        return false;
    }
}
Also used : AttributeUpdate(com.amazonaws.services.dynamodbv2.document.AttributeUpdate) Expected(com.amazonaws.services.dynamodbv2.document.Expected) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 3 with AttributeUpdate

use of com.amazonaws.services.dynamodbv2.document.AttributeUpdate in project athenz by yahoo.

the class DynamoDBSSHRecordStoreConnection method updateSSHCertRecord.

@Override
public boolean updateSSHCertRecord(SSHCertRecord certRecord) {
    final String primaryKey = getPrimaryKey(certRecord.getInstanceId(), certRecord.getService());
    try {
        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(KEY_PRIMARY, primaryKey).withAttributeUpdate(new AttributeUpdate(KEY_INSTANCE_ID).put(certRecord.getInstanceId()), new AttributeUpdate(KEY_SERVICE).put(certRecord.getService()), new AttributeUpdate(KEY_CLIENT_IP).put(certRecord.getClientIP()), new AttributeUpdate(KEY_PRINCIPALS).put(certRecord.getPrincipals()), new AttributeUpdate(KEY_PRIVATE_IP).put(certRecord.getPrivateIP()), new AttributeUpdate(KEY_TTL).put(System.currentTimeMillis() / 1000L + expiryTime));
        table.updateItem(updateItemSpec);
        return true;
    } catch (Exception ex) {
        LOGGER.error("DynamoDB Update Error for {}: {}/{}", primaryKey, ex.getClass(), ex.getMessage());
        return false;
    }
}
Also used : AttributeUpdate(com.amazonaws.services.dynamodbv2.document.AttributeUpdate) UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)

Example 4 with AttributeUpdate

use of com.amazonaws.services.dynamodbv2.document.AttributeUpdate in project athenz by yahoo.

the class DynamoDBCertRecordStoreConnection method updateX509CertRecord.

@Override
public boolean updateX509CertRecord(X509CertRecord certRecord) {
    final String primaryKey = getPrimaryKey(certRecord.getProvider(), certRecord.getInstanceId(), certRecord.getService());
    if (certRecord.getSvcDataUpdateTime() == null) {
        certRecord.setSvcDataUpdateTime(new Date());
    }
    String hostName = certRecord.getHostName();
    // Prevent inserting null values in hostName as the hostName-Index will not allow it
    if (StringUtil.isEmpty(hostName)) {
        hostName = primaryKey;
    }
    try {
        UpdateItemSpec updateItemSpec = new UpdateItemSpec().withPrimaryKey(KEY_PRIMARY, primaryKey).withAttributeUpdate(new AttributeUpdate(KEY_INSTANCE_ID).put(certRecord.getInstanceId()), new AttributeUpdate(KEY_PROVIDER).put(certRecord.getProvider()), new AttributeUpdate(KEY_SERVICE).put(certRecord.getService()), new AttributeUpdate(KEY_CURRENT_SERIAL).put(certRecord.getCurrentSerial()), new AttributeUpdate(KEY_CURRENT_IP).put(certRecord.getCurrentIP()), new AttributeUpdate(KEY_CURRENT_TIME).put(DynamoDBUtils.getLongFromDate(certRecord.getCurrentTime())), new AttributeUpdate(KEY_CURRENT_DATE).put(DynamoDBUtils.getIso8601FromDate(certRecord.getCurrentTime())), new AttributeUpdate(KEY_PREV_SERIAL).put(certRecord.getPrevSerial()), new AttributeUpdate(KEY_PREV_IP).put(certRecord.getPrevIP()), new AttributeUpdate(KEY_PREV_TIME).put(DynamoDBUtils.getLongFromDate(certRecord.getPrevTime())), new AttributeUpdate(KEY_CLIENT_CERT).put(certRecord.getClientCert()), new AttributeUpdate(KEY_TTL).put(certRecord.getCurrentTime().getTime() / 1000L + expiryTime), new AttributeUpdate(KEY_SVC_DATA_UPDATE_TIME).put(DynamoDBUtils.getLongFromDate(certRecord.getSvcDataUpdateTime())), new AttributeUpdate(KEY_EXPIRY_TIME).put(DynamoDBUtils.getLongFromDate(certRecord.getExpiryTime())), new AttributeUpdate(KEY_HOSTNAME).put(hostName));
        updateItemRetryDynamoDBCommand.run(() -> table.updateItem(updateItemSpec));
        return true;
    } catch (Exception ex) {
        LOGGER.error("DynamoDB Update Error for {}: {}/{}", primaryKey, ex.getClass(), ex.getMessage());
        return false;
    }
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 5 with AttributeUpdate

use of com.amazonaws.services.dynamodbv2.document.AttributeUpdate in project athenz by yahoo.

the class DynamoDBWorkloadRecordStoreConnectionTest method testUpdateWorkloadRecord.

@Test
public void testUpdateWorkloadRecord() {
    DynamoDBWorkloadRecordStoreConnection dbConn = getDBConnection();
    WorkloadRecord workloadRecord = new WorkloadRecord();
    workloadRecord.setProvider("openstack");
    long currTime = System.currentTimeMillis();
    Date currDate = new Date(currTime);
    workloadRecord.setUpdateTime(currDate);
    UpdateItemSpec item = new UpdateItemSpec().withPrimaryKey("primaryKey", "athenz.api#1234#10.0.0.1").withAttributeUpdate(new AttributeUpdate("provider").put(workloadRecord.getProvider()), new AttributeUpdate("updateTime").put(workloadRecord.getUpdateTime()));
    Mockito.doReturn(updateOutcome).when(table).updateItem(item);
    boolean requestSuccess = dbConn.updateWorkloadRecord(workloadRecord);
    Assert.assertTrue(requestSuccess);
    dbConn.close();
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)10 Test (org.testng.annotations.Test)5 AttributeUpdate (com.amazonaws.services.dynamodbv2.document.AttributeUpdate)3 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)3 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)3 Expected (com.amazonaws.services.dynamodbv2.document.Expected)2 SSHCertRecord (com.yahoo.athenz.common.server.ssh.SSHCertRecord)1 WorkloadRecord (com.yahoo.athenz.common.server.workload.WorkloadRecord)1 Date (java.util.Date)1