Search in sources :

Example 11 with Operation

use of com.google.api.services.sqladmin.model.Operation 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

Operation (com.google.api.services.sqladmin.model.Operation)11 SQLAdmin (com.google.api.services.sqladmin.SQLAdmin)8 DatabaseInstance (com.google.api.services.sqladmin.model.DatabaseInstance)8 InstancesListResponse (com.google.api.services.sqladmin.model.InstancesListResponse)7 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)7 Test (org.junit.jupiter.api.Test)7 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)5 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)4 Compute (com.google.api.services.compute.Compute)4 User (com.google.api.services.sqladmin.model.User)4 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)4 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)4 DatabaseServer (com.sequenceiq.cloudbreak.cloud.model.DatabaseServer)4 DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)4 PersistenceNotifier (com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier)4 Subnetwork (com.google.api.services.compute.model.Subnetwork)3 IpMapping (com.google.api.services.sqladmin.model.IpMapping)3 GcpDatabaseServerView (com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView)3 Network (com.sequenceiq.cloudbreak.cloud.model.Network)3 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)3