Search in sources :

Example 1 with Settings

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

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

the class GcpDatabaseServerTerminateServiceTest method testTerminateWhenDatabaseIsPresentedAndDeleteThrowExceptionShouldDeleteDatabaseFails.

@Test
public void testTerminateWhenDatabaseIsPresentedAndDeleteThrowExceptionShouldDeleteDatabaseFails() throws Exception {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    PersistenceNotifier persistenceNotifier = mock(PersistenceNotifier.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("server-1");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location(region("region"), availabilityZone("az1")));
    when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
    when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
    when(sqlAdminInstancesList.execute()).thenReturn(instancesListResponse);
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setName("server-1");
    databaseInstance.setState("RUNNABLE");
    Settings settings = new Settings();
    settings.setActivationPolicy("ALWAYS");
    databaseInstance.setSettings(settings);
    when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
    SQLAdmin.Instances.Delete delete = mock(SQLAdmin.Instances.Delete.class);
    GoogleJsonResponseException googleJsonResponseException = mock(GoogleJsonResponseException.class);
    when(sqlAdminInstances.delete(anyString(), anyString())).thenReturn(delete);
    when(delete.execute()).thenThrow(googleJsonResponseException);
    when(delete.setPrettyPrint(anyBoolean())).thenReturn(delete);
    GoogleJsonError googleJsonError = mock(GoogleJsonError.class);
    when(googleJsonResponseException.getDetails()).thenReturn(googleJsonError);
    when(googleJsonError.getMessage()).thenReturn("error");
    GcpResourceException gcpResourceException = assertThrows(GcpResourceException.class, () -> underTest.terminate(authenticatedContext, databaseStack, persistenceNotifier));
    Assert.assertEquals("error: [ resourceType: GCP_DATABASE,  resourceName: server-1 ]", gcpResourceException.getMessage());
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) 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) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) Settings(com.google.api.services.sqladmin.model.Settings) Test(org.junit.jupiter.api.Test)

Example 3 with Settings

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

the class GcpDatabaseServerTerminateServiceTest method testTerminateWhenDatabaseIsPresentedShouldDeleteDatabase.

@Test
public void testTerminateWhenDatabaseIsPresentedShouldDeleteDatabase() throws Exception {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    PersistenceNotifier persistenceNotifier = mock(PersistenceNotifier.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("server-1");
    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);
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setName("server-1");
    databaseInstance.setState("RUNNABLE");
    Settings settings = new Settings();
    settings.setActivationPolicy("ALWAYS");
    databaseInstance.setSettings(settings);
    when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
    SQLAdmin.Instances.Delete delete = mock(SQLAdmin.Instances.Delete.class);
    Operation operation = mock(Operation.class);
    when(sqlAdminInstances.delete(anyString(), anyString())).thenReturn(delete);
    when(delete.execute()).thenReturn(operation);
    when(delete.setPrettyPrint(anyBoolean())).thenReturn(delete);
    when(operation.getError()).thenReturn(null);
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location(region("region"), availabilityZone("az1")));
    doNothing().when(databasePollerService).terminateDatabasePoller(any(AuthenticatedContext.class), anyList());
    List<CloudResource> terminate = underTest.terminate(authenticatedContext, databaseStack, persistenceNotifier);
    Assert.assertEquals(1, terminate.size());
    Assert.assertEquals("server-1", terminate.get(0).getName());
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) 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) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Settings(com.google.api.services.sqladmin.model.Settings) Test(org.junit.jupiter.api.Test)

Example 4 with Settings

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

the class GcpDatabaseServerCheckServiceTest method testCheckWhenDbInstanceIsRunnableAlwaysShouldReturnStarted.

@Test
public void testCheckWhenDbInstanceIsRunnableAlwaysShouldReturnStarted() 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("ALWAYS");
    databaseInstance.setSettings(settings);
    when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
    ExternalDatabaseStatus check = underTest.check(authenticatedContext, databaseStack);
    Assert.assertEquals(ExternalDatabaseStatus.STARTED, 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 5 with Settings

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

the class GcpDatabaseServerLaunchServiceTest method testLaunchWhenDatabaseAlreadyExistShouldNotCreateAgain.

@Test
public void testLaunchWhenDatabaseAlreadyExistShouldNotCreateAgain() throws Exception {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    PersistenceNotifier persistenceNotifier = mock(PersistenceNotifier.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    Compute compute = mock(Compute.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("server-1");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpComputeFactory.buildCompute(any(CloudCredential.class))).thenReturn(compute);
    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);
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setName("server-1");
    databaseInstance.setState("RUNNABLE");
    Settings settings = new Settings();
    settings.setActivationPolicy("ALWAYS");
    databaseInstance.setSettings(settings);
    when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location(region("region"), availabilityZone("az1")));
    List<CloudResource> launch = underTest.launch(authenticatedContext, databaseStack, persistenceNotifier);
    Assert.assertEquals(0, launch.size());
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) Compute(com.google.api.services.compute.Compute) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Settings(com.google.api.services.sqladmin.model.Settings) Test(org.junit.jupiter.api.Test)

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