Search in sources :

Example 16 with UpdateItemSpec

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

the class DynamoDBCertRecordStoreConnectionTest method testUpdateX509Record.

@Test
public void testUpdateX509Record() {
    DynamoDBCertRecordStoreConnection dbConn = getDBConnection();
    Date now = new Date();
    X509CertRecord certRecord = getRecordNonNullableColumns(now);
    certRecord.setLastNotifiedTime(now);
    certRecord.setLastNotifiedServer("last-notified-server");
    certRecord.setExpiryTime(now);
    certRecord.setHostName("hostname");
    certRecord.setSvcDataUpdateTime(now);
    UpdateItemSpec item = new UpdateItemSpec().withPrimaryKey("primaryKey", "athenz.provider:cn:1234").withAttributeUpdate(new AttributeUpdate("instanceId").put(certRecord.getInstanceId()), new AttributeUpdate("provider").put(certRecord.getProvider()), new AttributeUpdate("service").put(certRecord.getService()), new AttributeUpdate("currentSerial").put(certRecord.getCurrentSerial()), new AttributeUpdate("currentIP").put(certRecord.getCurrentIP()), new AttributeUpdate("currentTime").put(certRecord.getCurrentTime().getTime()), new AttributeUpdate("currentDate").put(DynamoDBUtils.getIso8601FromDate(certRecord.getCurrentTime())), new AttributeUpdate("prevSerial").put(certRecord.getPrevSerial()), new AttributeUpdate("prevIP").put(certRecord.getPrevIP()), new AttributeUpdate("prevTime").put(certRecord.getPrevTime().getTime()), new AttributeUpdate("clientCert").put(certRecord.getClientCert()), new AttributeUpdate("ttl").put(certRecord.getCurrentTime().getTime() / 1000L + 3660 * 720), new AttributeUpdate("svcDataUpdateTime").put(certRecord.getSvcDataUpdateTime().getTime()), new AttributeUpdate("expiryTime").put(certRecord.getExpiryTime().getTime()), new AttributeUpdate("hostName").put(certRecord.getHostName()));
    Mockito.doReturn(updateOutcome).when(table).updateItem(item);
    boolean requestSuccess = dbConn.updateX509CertRecord(certRecord);
    assertTrue(requestSuccess);
    ArgumentCaptor<UpdateItemSpec> itemCaptor = ArgumentCaptor.forClass(UpdateItemSpec.class);
    Mockito.verify(table, times(1)).updateItem(itemCaptor.capture());
    List<UpdateItemSpec> allValues = itemCaptor.getAllValues();
    assertEquals(1, allValues.size());
    UpdateItemSpec capturedItem = allValues.get(0);
    assertEquals(capturedItem.getKeyComponents().toArray()[0].toString(), item.getKeyComponents().toArray()[0].toString());
    List<AttributeUpdate> capturedAttributes = capturedItem.getAttributeUpdate();
    List<AttributeUpdate> expectedAttributes = item.getAttributeUpdate();
    for (int i = 0; i < expectedAttributes.size(); ++i) {
        System.out.println("expected attr: " + expectedAttributes.get(i).getAttributeName() + ", value: " + expectedAttributes.get(i).getValue());
        assertEquals(capturedAttributes.get(i).getAttributeName(), expectedAttributes.get(i).getAttributeName());
        assertEquals(capturedAttributes.get(i).getValue(), expectedAttributes.get(i).getValue());
    }
    dbConn.close();
}
Also used : UpdateItemSpec(com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec) X509CertRecord(com.yahoo.athenz.common.server.cert.X509CertRecord) Test(org.testng.annotations.Test)

Example 17 with UpdateItemSpec

use of com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec 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

UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)17 Table (com.amazonaws.services.dynamodbv2.document.Table)6 UpdateItemOutcome (com.amazonaws.services.dynamodbv2.document.UpdateItemOutcome)6 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)6 Test (org.testng.annotations.Test)6 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)4 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)3 AttributeUpdate (com.amazonaws.services.dynamodbv2.document.AttributeUpdate)3 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)3 NameMap (com.amazonaws.services.dynamodbv2.document.utils.NameMap)3 ConditionalCheckFailedException (com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)3 IOException (java.io.IOException)3 Expected (com.amazonaws.services.dynamodbv2.document.Expected)2 PrimaryKey (com.amazonaws.services.dynamodbv2.document.PrimaryKey)1 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)1 AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)1 SSHCertRecord (com.yahoo.athenz.common.server.ssh.SSHCertRecord)1 WorkloadRecord (com.yahoo.athenz.common.server.workload.WorkloadRecord)1 Date (java.util.Date)1 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)1