Search in sources :

Example 6 with Settings

use of com.google.api.services.sqladmin.model.Settings in project core by z1lc.

the class MasterEtl method getSessionFactory.

private SessionFactory getSessionFactory(Hbm2ddlType hbm2ddlType, ConnectionType connectionType) throws HibernateException, IOException, GeneralSecurityException, InterruptedException {
    try {
        config.setProperty("hibernate.hbm2ddl.auto", hbm2ddlType.getText());
        String username;
        String password;
        String instanceName;
        if (connectionType.equals(ConnectionType.RSANEK)) {
            username = secretProvider.getSecret(GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_USERNAME);
            password = secretProvider.getSecret(GOOGLE_CLOUD_SQL_RSANEK_POSTGRES_PASSWORD);
            instanceName = "rsanek-db";
        } else if (connectionType.equals(ConnectionType.CRONUS)) {
            username = secretProvider.getSecret(GOOGLE_CLOUD_SQL_CRONUS_POSTGRES_USERNAME);
            password = secretProvider.getSecret(GOOGLE_CLOUD_SQL_CRONUS_POSTGRES_USERNAME);
            instanceName = "cronus-pg-primary";
        } else {
            throw new RuntimeException(String.format("Don't know configuration for connection type %s.", connectionType));
        }
        config.setProperty("hibernate.connection.username", username);
        config.setProperty("hibernate.connection.password", password);
        config.setProperty("connection.url", "jdbc:postgresql://google/postgres?socketFactory=com.google.cloud.sql.postgres.SocketFactory&cloudSqlInstance=arctic-rite-143002:us-west1:" + instanceName);
        return config.buildSessionFactory();
    } catch (ServiceException e) {
        log.info("Failed to connect to Cloud SQL Instance. Assuming instance is stopped, attempting to start...");
        String project = "arctic-rite-143002";
        String instance = "rsanek-db";
        DatabaseInstance requestBody = new DatabaseInstance();
        requestBody.setSettings(new Settings().setActivationPolicy("ALWAYS"));
        SQLAdmin sqlAdminService = createSqlAdminService();
        SQLAdmin.Instances.Patch request = sqlAdminService.instances().patch(project, instance, requestBody);
        Operation response = request.execute();
        log.info(response.toPrettyString());
        log.info("Starting instance successful. Waiting for %s seconds to let instance start.", INSTANCE_STARTUP_WAIT_TIME.toMinutes());
        Thread.sleep(INSTANCE_STARTUP_WAIT_TIME.toMillis());
        return config.buildSessionFactory();
    }
}
Also used : ServiceException(org.hibernate.service.spi.ServiceException) Operation(com.google.api.services.sqladmin.model.Operation) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) Settings(com.google.api.services.sqladmin.model.Settings)

Aggregations

SQLAdmin (com.google.api.services.sqladmin.SQLAdmin)6 DatabaseInstance (com.google.api.services.sqladmin.model.DatabaseInstance)6 Settings (com.google.api.services.sqladmin.model.Settings)6 InstancesListResponse (com.google.api.services.sqladmin.model.InstancesListResponse)5 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)5 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)5 DatabaseServer (com.sequenceiq.cloudbreak.cloud.model.DatabaseServer)5 DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)5 Test (org.junit.jupiter.api.Test)5 PersistenceNotifier (com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier)3 Operation (com.google.api.services.sqladmin.model.Operation)2 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)2 ExternalDatabaseStatus (com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus)2 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)1 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 Compute (com.google.api.services.compute.Compute)1 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)1 ServiceException (org.hibernate.service.spi.ServiceException)1