Search in sources :

Example 1 with SQLAdmin

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

the class AbstractGcpDatabaseServerStartStopService method startStop.

protected void startStop(AuthenticatedContext ac, DatabaseStack stack, DatabasePollerService databasePollerService, String policy) throws IOException {
    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> gcpDatabase = getGcpDatabase(stack, ac.getCloudContext().getLocation().getAvailabilityZone().value());
    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()) {
            try {
                Operation operation = sqlAdmin.instances().patch(projectId, deploymentName, getDatabaseInstance(policy)).execute();
                verifyOperation(operation, gcpDatabase);
                CloudResource operationAwareCloudResource = createOperationAwareCloudResource(gcpDatabase.get(0), operation);
                databasePollerService.startDatabasePoller(ac, List.of(operationAwareCloudResource));
            } catch (GoogleJsonResponseException e) {
                throw new GcpResourceException(checkException(e), resourceType(), gcpDatabase.get(0).getName());
            }
        } else {
            LOGGER.debug("Deployment does not exists: {}", deploymentName);
        }
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), gcpDatabase.get(0).getName());
    }
}
Also used : GcpDatabaseServerView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView) 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) Operation(com.google.api.services.sqladmin.model.Operation) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance)

Example 2 with SQLAdmin

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

the class GcpDatabaseServerLaunchService method launch.

public List<CloudResource> launch(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());
    Compute compute = gcpComputeFactory.buildCompute(ac.getCloudCredential());
    String projectId = gcpStackUtil.getProjectId(ac.getCloudCredential());
    List<CloudResource> buildableResource = new ArrayList<>();
    String availabilityZone = ac.getCloudContext().getLocation().getAvailabilityZone().value();
    buildableResource.add(getGcpDatabase(deploymentName, availabilityZone));
    buildableResource.add(getRdsPort(availabilityZone));
    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()) {
            DatabaseInstance databaseInstance = getDatabaseInstance(stack, deploymentName, compute, projectId);
            SQLAdmin.Instances.Insert insert = sqlAdmin.instances().insert(projectId, databaseInstance);
            insert.setPrettyPrint(Boolean.TRUE);
            try {
                Operation operation = insert.execute();
                verifyOperation(operation, buildableResource);
                CloudResource operationAwareCloudResource = createOperationAwareCloudResource(buildableResource.get(0), operation);
                databasePollerService.launchDatabasePoller(ac, List.of(operationAwareCloudResource));
                DatabaseInstance instance = sqlAdmin.instances().get(projectId, deploymentName).execute();
                if (instance != null) {
                    CloudResource.Builder rdsInstance = new CloudResource.Builder();
                    String instanceName = instance.getName();
                    buildableResource.add(getRdsHostName(instance, rdsInstance, instanceName, availabilityZone));
                    User rootUser = getRootUser(stack, projectId, instanceName);
                    operation = sqlAdmin.users().insert(projectId, instanceName, rootUser).execute();
                    verifyOperation(operation, buildableResource);
                    operationAwareCloudResource = createOperationAwareCloudResource(buildableResource.get(0), operation);
                    databasePollerService.insertUserPoller(ac, List.of(operationAwareCloudResource));
                }
                buildableResource.forEach(dbr -> resourceNotifier.notifyAllocation(dbr, ac.getCloudContext()));
                return Collections.singletonList(operationAwareCloudResource);
            } catch (GoogleJsonResponseException e) {
                throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
            }
        } else {
            LOGGER.debug("Deployment already exists: {}", deploymentName);
        }
    } 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) User(com.google.api.services.sqladmin.model.User) ArrayList(java.util.ArrayList) 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) Compute(com.google.api.services.compute.Compute) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 3 with SQLAdmin

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

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceIsRunnableNOTAlwaysShouldReturnStarted.

@Test
public void testCheckWhenDbInstanceIsRunnableNOTAlwaysShouldReturnStarted() throws IOException {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn("test");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    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.isEmpty()).thenReturn(false);
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setName("test");
    databaseInstance.setState("RUNNABLE");
    Settings settings = new Settings();
    settings.setActivationPolicy("NOT_ALWAYS");
    databaseInstance.setSettings(settings);
    when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
    ExternalDatabaseStatus check = underTest.check(authenticatedContext, databaseStack);
    Assert.assertEquals(ExternalDatabaseStatus.STOPPED, check);
}
Also used : InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) Settings(com.google.api.services.sqladmin.model.Settings) ExternalDatabaseStatus(com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus) Test(org.junit.jupiter.api.Test)

Example 4 with SQLAdmin

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

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceNotAvailableShouldReturnDeleted.

@Test
public void testCheckWhenDbInstanceNotAvailableShouldReturnDeleted() throws IOException {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn("test");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    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.isEmpty()).thenReturn(true);
    ExternalDatabaseStatus check = underTest.check(authenticatedContext, databaseStack);
    Assert.assertEquals(ExternalDatabaseStatus.DELETED, check);
}
Also used : InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) ExternalDatabaseStatus(com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus) Test(org.junit.jupiter.api.Test)

Example 5 with SQLAdmin

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

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceDropGcpResourceExceptionShouldReturnGcpResourceException.

@Test
public void testCheckWhenDbInstanceDropGcpResourceExceptionShouldReturnGcpResourceException() throws IOException {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    TokenResponseException tokenResponseException = mock(TokenResponseException.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn("test");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
    when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
    when(sqlAdminInstancesList.execute()).thenThrow(tokenResponseException);
    when(gcpStackUtil.getMissingServiceAccountKeyError(any(TokenResponseException.class), anyString())).thenReturn(new GcpResourceException("error"));
    GcpResourceException gcpResourceException = assertThrows(GcpResourceException.class, () -> underTest.check(authenticatedContext, databaseStack));
    Assert.assertEquals("error", gcpResourceException.getMessage());
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException) Test(org.junit.jupiter.api.Test)

Aggregations

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