Search in sources :

Example 1 with DescribeDBInstancesResult

use of com.amazonaws.services.rds.model.DescribeDBInstancesResult in project Synapse-Stack-Builder by Sage-Bionetworks.

the class MySqlDatabaseSetup method describeResources.

public void describeResources() {
    DescribeDBInstancesRequest req;
    DescribeDBInstancesResult res;
    req = buildIdGeneratorDescribeDBInstanceRequest();
    res = client.describeDBInstances(req);
    if ((res.getDBInstances() != null) && (res.getDBInstances().size() == 1)) {
        resources.setIdGeneratorDatabase(res.getDBInstances().get(0));
    }
    req = buildStackInstanceDescribeDBInstanceRequest();
    res = client.describeDBInstances(req);
    if ((res.getDBInstances() != null) && (res.getDBInstances().size() == 1)) {
        resources.setStackInstancesDatabase(res.getDBInstances().get(0));
    }
    // TODO: Describe table DB instances
    List<DBInstance> descDbInstResults = new ArrayList<DBInstance>();
    int numTableInstances = Integer.parseInt(this.config.getNumberTableInstances());
    for (int i = 0; i < numTableInstances; i++) {
        req = buildStackTableDBInstanceDescribeDBInstanceRequest(i);
        res = client.describeDBInstances(req);
        if ((res.getDBInstances() != null) && (res.getDBInstances().size() == 1)) {
            descDbInstResults.add(res.getDBInstances().get(0));
        }
    }
    resources.setStackInstanceTablesDatabases(descDbInstResults);
}
Also used : DBInstance(com.amazonaws.services.rds.model.DBInstance) ArrayList(java.util.ArrayList) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest)

Example 2 with DescribeDBInstancesResult

use of com.amazonaws.services.rds.model.DescribeDBInstancesResult in project Synapse-Stack-Builder by Sage-Bionetworks.

the class MySqlDatabaseSetupTest method testCreateOrGetDatabaseInstanceDoesExist.

@Test
public void testCreateOrGetDatabaseInstanceDoesExist() {
    // For this case the database already exists so it should not be created again.
    String dbIdentifier = "id-123";
    CreateDBInstanceRequest request = MySqlDatabaseSetup.getDefaultCreateDBInstanceRequest();
    request.setDBInstanceIdentifier(dbIdentifier);
    DescribeDBInstancesRequest describeRequest = new DescribeDBInstancesRequest().withDBInstanceIdentifier(dbIdentifier);
    DBInstance instance = new DBInstance();
    instance.setDBInstanceIdentifier(dbIdentifier);
    DescribeDBInstancesResult describeResult = new DescribeDBInstancesResult();
    describeResult.withDBInstances(instance);
    when(mockClient.describeDBInstances(describeRequest)).thenReturn(describeResult);
    DBInstance instanceResult = databaseSetup.createOrGetDatabaseInstance(request);
    assertEquals(instance, instanceResult);
    // Describe once
    verify(mockClient, times(1)).describeDBInstances(describeRequest);
    // Create should not be called this time.
    verify(mockClient, times(0)).createDBInstance(request);
}
Also used : CreateDBInstanceRequest(com.amazonaws.services.rds.model.CreateDBInstanceRequest) DBInstance(com.amazonaws.services.rds.model.DBInstance) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest) Test(org.junit.Test)

Example 3 with DescribeDBInstancesResult

use of com.amazonaws.services.rds.model.DescribeDBInstancesResult in project Synapse-Stack-Builder by Sage-Bionetworks.

the class MySqlDatabaseSetup method createOrGetDatabaseInstance.

/**
 * If this database instances does not already exist it will be created, otherwise the instance information will be returned.
 *
 * @param request
 * @return
 */
DBInstance createOrGetDatabaseInstance(CreateDBInstanceRequest request) {
    // First query for the instance
    try {
        DescribeDBInstancesResult result = client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier(request.getDBInstanceIdentifier()));
        if (result.getDBInstances() == null || result.getDBInstances().size() != 1)
            throw new IllegalStateException("Did not find exactly one database instances with the identifier: " + request.getDBInstanceIdentifier());
        log.debug("Database: " + request.getDBInstanceIdentifier() + " already exists");
        return result.getDBInstances().get(0);
    } catch (DBInstanceNotFoundException e) {
        // This database does not exist to create it
        // Create the database.
        log.debug("Creating database...");
        log.debug(request);
        return client.createDBInstance(request);
    }
}
Also used : DBInstanceNotFoundException(com.amazonaws.services.rds.model.DBInstanceNotFoundException) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest)

Example 4 with DescribeDBInstancesResult

use of com.amazonaws.services.rds.model.DescribeDBInstancesResult in project Synapse-Stack-Builder by Sage-Bionetworks.

the class MySqlDatabaseSetup method waitForDatabase.

/**
 * Wait for a database to be available
 * @param stackInstance
 */
public DBInstance waitForDatabase(DBInstance stackInstance) throws InterruptedException {
    String status = null;
    DBInstance instance = null;
    // Try to minimize risk of bouncing available status
    boolean available = false;
    for (int i = 0; i < 10; i++) {
        sleeper.sleep(30000);
        DescribeDBInstancesResult result = client.describeDBInstances(new DescribeDBInstancesRequest().withDBInstanceIdentifier(stackInstance.getDBInstanceIdentifier()));
        if ((result != null) && (result.getDBInstances() != null) && (result.getDBInstances().size() >= 1)) {
            instance = result.getDBInstances().get(0);
            status = instance.getDBInstanceStatus();
            log.info(String.format("Waiting for database: instance: %1$s status: %2$s ", stackInstance.getDBInstanceIdentifier(), status));
            if (available = "available".equals(status.toLowerCase())) {
                break;
            }
        }
    }
    if (available) {
        return instance;
    } else {
        return null;
    }
}
Also used : DBInstance(com.amazonaws.services.rds.model.DBInstance) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) DescribeDBInstancesRequest(com.amazonaws.services.rds.model.DescribeDBInstancesRequest)

Aggregations

DescribeDBInstancesRequest (com.amazonaws.services.rds.model.DescribeDBInstancesRequest)4 DescribeDBInstancesResult (com.amazonaws.services.rds.model.DescribeDBInstancesResult)4 DBInstance (com.amazonaws.services.rds.model.DBInstance)3 CreateDBInstanceRequest (com.amazonaws.services.rds.model.CreateDBInstanceRequest)1 DBInstanceNotFoundException (com.amazonaws.services.rds.model.DBInstanceNotFoundException)1 ArrayList (java.util.ArrayList)1 Test (org.junit.Test)1