Search in sources :

Example 16 with DatabaseInstance

use of com.google.api.services.sqladmin.model.DatabaseInstance in project cloudbreak by hortonworks.

the class GcpDatabaseServerLaunchServiceTest method testLaunchWhenDatabaseNOTAlreadyExistAndSharedProjectIdShouldCreate.

@Test
public void testLaunchWhenDatabaseNOTAlreadyExistAndSharedProjectIdShouldCreate() throws Exception {
    Network network = new Network(new Subnet("10.0.0.0/16"), Map.of("subnetId", "s-1", "availabilityZone", "a", "sharedProjectId", "sp1"));
    Map<String, Object> map = new HashMap<>();
    map.put("engineVersion", "1");
    DatabaseServer databaseServer = DatabaseServer.builder().connectionDriver("driver").serverId("driver").connectorJarUrl("driver").engine(DatabaseEngine.POSTGRESQL).location("location").port(99).storageSize(50L).rootUserName("rootUserName").rootPassword("rootPassword").flavor("flavor").useSslEnforcement(true).params(map).build();
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    PersistenceNotifier persistenceNotifier = mock(PersistenceNotifier.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    Compute compute = mock(Compute.class);
    DatabaseInstance databaseInstance = mock(DatabaseInstance.class);
    Compute.Subnetworks subnetworks = mock(Compute.Subnetworks.class);
    Compute.Subnetworks.Get subnetworksGet = mock(Compute.Subnetworks.Get.class);
    SQLAdmin.Users users = mock(SQLAdmin.Users.class);
    SQLAdmin.Users.Insert usersInsert = mock(SQLAdmin.Users.Insert.class);
    SQLAdmin.Instances.Get instancesGet = mock(SQLAdmin.Instances.Get.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
    Operation operation = mock(Operation.class);
    SQLAdmin.Instances.Insert sqlAdminInstancesInsert = mock(SQLAdmin.Instances.Insert.class);
    IpMapping ipMapping = new IpMapping();
    ipMapping.setIpAddress("10.0.0.0");
    ipMapping.setType("PRIVATE");
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseStack.getNetwork()).thenReturn(network);
    when(gcpLabelUtil.createLabelsFromTagsMap(anyMap())).thenReturn(new HashMap<>());
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpComputeFactory.buildCompute(any(CloudCredential.class))).thenReturn(compute);
    when(compute.subnetworks()).thenReturn(subnetworks);
    when(subnetworks.get(anyString(), anyString(), anyString())).thenReturn(subnetworksGet);
    when(subnetworksGet.execute()).thenReturn(new Subnetwork());
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
    when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
    when(sqlAdminInstancesList.execute()).thenReturn(instancesListResponse);
    when(instancesListResponse.getItems()).thenReturn(List.of());
    when(databaseInstance.getName()).thenReturn("name");
    when(sqlAdminInstances.get(anyString(), anyString())).thenReturn(instancesGet);
    when(instancesGet.execute()).thenReturn(databaseInstance);
    when(sqlAdminInstances.insert(anyString(), databaseInstanceArgumentCaptor.capture())).thenReturn(sqlAdminInstancesInsert);
    when(sqlAdminInstancesInsert.setPrettyPrint(anyBoolean())).thenReturn(sqlAdminInstancesInsert);
    when(sqlAdminInstancesInsert.execute()).thenReturn(operation);
    when(databaseInstance.getIpAddresses()).thenReturn(List.of(ipMapping));
    doNothing().when(databasePollerService).launchDatabasePoller(any(AuthenticatedContext.class), anyList());
    when(sqlAdmin.users()).thenReturn(users);
    when(users.insert(anyString(), anyString(), any(User.class))).thenReturn(usersInsert);
    when(usersInsert.execute()).thenReturn(operation);
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location(region("region"), availabilityZone("az1")));
    List<CloudResource> launch = underTest.launch(authenticatedContext, databaseStack, persistenceNotifier);
    assertNull(databaseInstanceArgumentCaptor.getValue().getDiskEncryptionConfiguration());
    Assert.assertEquals(1, launch.size());
}
Also used : User(com.google.api.services.sqladmin.model.User) HashMap(java.util.HashMap) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Operation(com.google.api.services.sqladmin.model.Operation) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) Network(com.sequenceiq.cloudbreak.cloud.model.Network) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) Subnetwork(com.google.api.services.compute.model.Subnetwork) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) IpMapping(com.google.api.services.sqladmin.model.IpMapping) Compute(com.google.api.services.compute.Compute) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Test(org.junit.jupiter.api.Test)

Example 17 with DatabaseInstance

use of com.google.api.services.sqladmin.model.DatabaseInstance in project cloudbreak by hortonworks.

the class GcpDatabaseServerTerminateService method terminate.

@Override
public List<CloudResource> terminate(AuthenticatedContext ac, DatabaseStack stack, PersistenceNotifier resourceNotifier) throws Exception {
    GcpDatabaseServerView databaseServerView = new GcpDatabaseServerView(stack.getDatabaseServer());
    String deploymentName = databaseServerView.getDbServerName();
    SQLAdmin sqlAdmin = gcpSQLAdminFactory.buildSQLAdmin(ac.getCloudCredential(), ac.getCloudCredential().getName());
    String projectId = gcpStackUtil.getProjectId(ac.getCloudCredential());
    List<CloudResource> buildableResource = List.of(new CloudResource.Builder().type(ResourceType.GCP_DATABASE).name(deploymentName).availabilityZone(ac.getCloudContext().getLocation().getAvailabilityZone().value()).build());
    try {
        InstancesListResponse list = sqlAdmin.instances().list(projectId).execute();
        Optional<DatabaseInstance> first = Optional.empty();
        if (!list.isEmpty()) {
            first = list.getItems().stream().filter(e -> e.getName().equals(deploymentName)).findFirst();
        }
        if (!first.isEmpty()) {
            SQLAdmin.Instances.Delete delete = sqlAdmin.instances().delete(projectId, deploymentName);
            delete.setPrettyPrint(true);
            try {
                Operation operation = delete.execute();
                verifyOperation(operation, buildableResource);
                CloudResource operationAwareCloudResource = createOperationAwareCloudResource(buildableResource.get(0), operation);
                databasePollerService.terminateDatabasePoller(ac, buildableResource);
                return Collections.singletonList(operationAwareCloudResource);
            } catch (GoogleJsonResponseException e) {
                throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
            }
        }
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
    }
    return List.of();
}
Also used : GcpDatabaseServerView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView) Operation(com.google.api.services.sqladmin.model.Operation) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 18 with DatabaseInstance

use of com.google.api.services.sqladmin.model.DatabaseInstance in project cloudbreak by hortonworks.

the class GcpDatabaseServerCheckService method check.

@Override
public ExternalDatabaseStatus check(AuthenticatedContext ac, DatabaseStack stack) {
    GcpDatabaseServerView databaseServerView = new GcpDatabaseServerView(stack.getDatabaseServer());
    String deploymentName = databaseServerView.getDbServerName();
    SQLAdmin sqlAdmin = gcpSQLAdminFactory.buildSQLAdmin(ac.getCloudCredential(), ac.getCloudCredential().getName());
    String projectId = gcpStackUtil.getProjectId(ac.getCloudCredential());
    try {
        InstancesListResponse list = sqlAdmin.instances().list(projectId).execute();
        Optional<DatabaseInstance> first = Optional.empty();
        if (!list.isEmpty()) {
            first = list.getItems().stream().filter(e -> e.getName().equals(deploymentName)).findFirst();
        }
        if (!first.isEmpty()) {
            switch(first.get().getState()) {
                case "RUNNABLE":
                    if ("ALWAYS".equals(first.get().getSettings().getActivationPolicy())) {
                        return ExternalDatabaseStatus.STARTED;
                    } else {
                        return ExternalDatabaseStatus.STOPPED;
                    }
                case "SUSPENDED":
                    return ExternalDatabaseStatus.STOPPED;
                case "UNKNOWN_STATE":
                case "FAILED":
                    return ExternalDatabaseStatus.DELETED;
                default:
                    return ExternalDatabaseStatus.UPDATE_IN_PROGRESS;
            }
        } else {
            return ExternalDatabaseStatus.DELETED;
        }
    } catch (TokenResponseException e) {
        throw gcpStackUtil.getMissingServiceAccountKeyError(e, projectId);
    } catch (IOException ex) {
        throw new CloudConnectorException(ex.getMessage(), ex);
    }
}
Also used : GcpDatabaseServerView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView) InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) IOException(java.io.IOException) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException)

Example 19 with DatabaseInstance

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

DatabaseInstance (com.google.api.services.sqladmin.model.DatabaseInstance)19 SQLAdmin (com.google.api.services.sqladmin.SQLAdmin)17 InstancesListResponse (com.google.api.services.sqladmin.model.InstancesListResponse)16 DatabaseServer (com.sequenceiq.cloudbreak.cloud.model.DatabaseServer)13 Test (org.junit.jupiter.api.Test)13 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)12 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)12 DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)12 Operation (com.google.api.services.sqladmin.model.Operation)8 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)8 Settings (com.google.api.services.sqladmin.model.Settings)6 ExternalDatabaseStatus (com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus)6 PersistenceNotifier (com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier)6 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)5 Compute (com.google.api.services.compute.Compute)5 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)5 GcpDatabaseServerView (com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView)5 Subnetwork (com.google.api.services.compute.model.Subnetwork)4 IpMapping (com.google.api.services.sqladmin.model.IpMapping)4 User (com.google.api.services.sqladmin.model.User)4