Search in sources :

Example 86 with Item

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

the class DynamoDBCertRecordStoreConnection method getUnrefreshedCertRecordsByDate.

private List<Item> getUnrefreshedCertRecordsByDate(String provider, long yesterday, String unrefreshedCertDate) {
    try {
        QuerySpec spec = new QuerySpec().withKeyConditionExpression("currentDate = :v_current_date").withFilterExpression("provider = :v_provider AND attribute_exists(hostName) AND (attribute_not_exists(lastNotifiedTime) OR lastNotifiedTime < :v_last_notified)").withValueMap(new ValueMap().withString(":v_current_date", unrefreshedCertDate).withNumber(":v_last_notified", yesterday).withString(":v_provider", provider));
        ItemCollection<QueryOutcome> outcome = itemCollectionRetryDynamoDBCommand.run(() -> currentTimeIndex.query(spec));
        List<Item> items = new ArrayList<>();
        for (Item item : outcome) {
            items.add(item);
        }
        return items;
    } catch (Exception ex) {
        LOGGER.error("DynamoDB getUnrefreshedCertRecordsByDate failed for provider: {}, date: {} error: {}", provider, unrefreshedCertDate, ex.getMessage());
    }
    return new ArrayList<>();
}
Also used : ValueMap(com.amazonaws.services.dynamodbv2.document.utils.ValueMap) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) ConditionalCheckFailedException(com.amazonaws.services.dynamodbv2.model.ConditionalCheckFailedException)

Example 87 with Item

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

the class ZTSTestUtils method generateAttributeValues.

public static Map<String, AttributeValue> generateAttributeValues(String service, String instanceId, String currentTime, String lastNotifiedTime, String lastNotifiedServer, String expiryTime, String hostName) {
    String provider = "provider";
    String primaryKey = provider + ":" + service + ":" + instanceId;
    Map<String, AttributeValue> item = new HashMap<>();
    item.put("primaryKey", new AttributeValue(primaryKey));
    item.put("service", new AttributeValue(service));
    item.put("provider", new AttributeValue(provider));
    item.put("instanceId", new AttributeValue(instanceId));
    item.put("currentSerial", new AttributeValue("currentSerial"));
    AttributeValue currentTimeVal = new AttributeValue();
    currentTimeVal.setN(currentTime);
    if (!StringUtil.isEmpty(currentTime)) {
        item.put("currentTime", currentTimeVal);
        item.put("prevTime", currentTimeVal);
    }
    item.put("currentIP", new AttributeValue("currentIP"));
    item.put("prevSerial", new AttributeValue("prevSerial"));
    item.put("prevIP", new AttributeValue("prevIP"));
    AttributeValue clientCertVal = new AttributeValue();
    clientCertVal.setBOOL(false);
    item.put("clientCert", clientCertVal);
    if (!StringUtil.isEmpty(lastNotifiedTime)) {
        AttributeValue lastNotifiedTimeVal = new AttributeValue();
        lastNotifiedTimeVal.setN(lastNotifiedTime);
        item.put("lastNotifiedTime", lastNotifiedTimeVal);
    }
    if (!StringUtil.isEmpty(lastNotifiedServer)) {
        item.put("lastNotifiedServer", new AttributeValue(lastNotifiedServer));
    }
    if (!StringUtil.isEmpty(expiryTime)) {
        AttributeValue expiryTimeVal = new AttributeValue();
        expiryTimeVal.setN(expiryTime);
        item.put("expiryTime", expiryTimeVal);
    }
    if (!StringUtil.isEmpty(hostName)) {
        item.put("hostName", new AttributeValue(hostName));
    }
    return item;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue)

Example 88 with Item

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

the class ZTSTestUtils method generateWorkloadAttributeValues.

public static Map<String, AttributeValue> generateWorkloadAttributeValues(String service, String instanceId, String provider, String ip, String hostname, String creationTime, String updateTime, String certExpiryTime) {
    String primaryKey = service + "#" + instanceId + "#" + ip;
    Map<String, AttributeValue> item = new HashMap<>();
    item.put("primaryKey", new AttributeValue(primaryKey));
    item.put("service", new AttributeValue(service));
    item.put("provider", new AttributeValue(provider));
    item.put("instanceId", new AttributeValue(instanceId));
    item.put("ip", new AttributeValue(ip));
    item.put("hostname", new AttributeValue(hostname));
    AttributeValue creationTimeVal = new AttributeValue();
    creationTimeVal.setN(creationTime);
    AttributeValue updateTimeVal = new AttributeValue();
    updateTimeVal.setN(updateTime);
    AttributeValue certExpiryTimeVal = new AttributeValue();
    certExpiryTimeVal.setN(certExpiryTime);
    item.put("creationTime", creationTimeVal);
    item.put("updateTime", updateTimeVal);
    item.put("certExpiryTime", certExpiryTimeVal);
    return item;
}
Also used : AttributeValue(com.amazonaws.services.dynamodbv2.model.AttributeValue)

Example 89 with Item

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

the class DynamoDBWorkloadRecordStoreConnectionTest method testGetWorkloadRecordsByServiceNotFoundException.

@Test
public void testGetWorkloadRecordsByServiceNotFoundException() {
    Mockito.doThrow(new AmazonDynamoDBException("item not found")).when(serviceIndex).query(Mockito.any(QuerySpec.class));
    DynamoDBWorkloadRecordStoreConnection dbConn = getDBConnection();
    List<WorkloadRecord> wlRecordList = dbConn.getWorkloadRecordsByService("athenz", "api");
    Assert.assertTrue(wlRecordList.isEmpty());
    dbConn.close();
}
Also used : AmazonDynamoDBException(com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException) QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Test(org.testng.annotations.Test)

Example 90 with Item

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

the class DynamoDBWorkloadRecordStoreConnectionTest method testGetWorkloadRecordsByIp.

@Test
public void testGetWorkloadRecordsByIp() {
    long currTime = System.currentTimeMillis();
    Mockito.doReturn("1234").when(item).getString("instanceId");
    Mockito.doReturn("openstack").when(item).getString("provider");
    Mockito.doReturn("athenz.api").when(item).getString("service");
    Mockito.doReturn("test-host").when(item).getString("hostname");
    Mockito.doReturn(currTime).when(item).get("creationTime");
    Mockito.doReturn(currTime).when(item).get("updateTime");
    Mockito.doReturn(currTime).when(item).get("certExpiryTime");
    Mockito.doReturn(currTime).when(item).getLong("creationTime");
    Mockito.doReturn(currTime).when(item).getLong("updateTime");
    Mockito.doReturn(currTime).when(item).getLong("certExpiryTime");
    ItemCollection<QueryOutcome> itemCollection = Mockito.mock(ItemCollection.class);
    IteratorSupport<Item, QueryOutcome> iteratorSupport = Mockito.mock(IteratorSupport.class);
    when(itemCollection.iterator()).thenReturn(iteratorSupport);
    when(iteratorSupport.hasNext()).thenReturn(true, false);
    when(iteratorSupport.next()).thenReturn(item);
    Mockito.doReturn(itemCollection).when(ipIndex).query(Mockito.any(QuerySpec.class));
    DynamoDBWorkloadRecordStoreConnection dbConn = getDBConnection();
    dbConn.setOperationTimeout(10);
    List<WorkloadRecord> wlRecordList = dbConn.getWorkloadRecordsByIp("10.0.0.1");
    Assert.assertNotNull(wlRecordList);
    Assert.assertEquals(wlRecordList.get(0).getInstanceId(), "1234");
    Assert.assertEquals(wlRecordList.get(0).getProvider(), "openstack");
    Assert.assertEquals(wlRecordList.get(0).getService(), "athenz.api");
    Assert.assertEquals(wlRecordList.get(0).getHostname(), "NA");
    Assert.assertEquals(wlRecordList.get(0).getUpdateTime(), new Date(currTime));
    Assert.assertEquals(wlRecordList.get(0).getCertExpiryTime(), new Date(0));
    dbConn.close();
}
Also used : QuerySpec(com.amazonaws.services.dynamodbv2.document.spec.QuerySpec) WorkloadRecord(com.yahoo.athenz.common.server.workload.WorkloadRecord) Date(java.util.Date) Test(org.testng.annotations.Test)

Aggregations

AttributeValue (com.amazonaws.services.dynamodbv2.model.AttributeValue)107 Item (com.amazonaws.services.dynamodbv2.document.Item)95 HashMap (java.util.HashMap)60 Table (com.amazonaws.services.dynamodbv2.document.Table)53 Test (org.testng.annotations.Test)49 QuerySpec (com.amazonaws.services.dynamodbv2.document.spec.QuerySpec)42 AmazonDynamoDB (com.amazonaws.services.dynamodbv2.AmazonDynamoDB)35 AmazonServiceException (com.amazonaws.AmazonServiceException)31 ValueMap (com.amazonaws.services.dynamodbv2.document.utils.ValueMap)27 UpdateItemSpec (com.amazonaws.services.dynamodbv2.document.spec.UpdateItemSpec)26 ArrayList (java.util.ArrayList)25 DynamoDB (com.amazonaws.services.dynamodbv2.document.DynamoDB)24 List (java.util.List)23 Test (org.junit.Test)23 QueryOutcome (com.amazonaws.services.dynamodbv2.document.QueryOutcome)22 IOException (java.io.IOException)21 X509CertRecord (com.yahoo.athenz.common.server.cert.X509CertRecord)20 AmazonDynamoDBException (com.amazonaws.services.dynamodbv2.model.AmazonDynamoDBException)18 Date (java.util.Date)18 Map (java.util.Map)17