Search in sources :

Example 11 with DatabaseServer

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseServer in project cloudbreak by hortonworks.

the class GcpDatabaseServerTerminateServiceTest method testTerminateWhenDatabaseIsPresentedAndListThrowExceptionShouldDeleteDatabaseFails.

@Test
public void testTerminateWhenDatabaseIsPresentedAndListThrowExceptionShouldDeleteDatabaseFails() 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);
    GoogleJsonResponseException googleJsonResponseException = mock(GoogleJsonResponseException.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn("server-1");
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location(region("region"), availabilityZone("az1")));
    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);
    GoogleJsonError googleJsonError = mock(GoogleJsonError.class);
    when(googleJsonResponseException.getDetails()).thenReturn(googleJsonError);
    when(googleJsonError.getMessage()).thenReturn("error");
    when(sqlAdminInstancesList.execute()).thenThrow(googleJsonResponseException);
    GcpResourceException gcpResourceException = assertThrows(GcpResourceException.class, () -> underTest.terminate(authenticatedContext, databaseStack, persistenceNotifier));
    Assert.assertEquals("error: [ resourceType: GCP_DATABASE,  resourceName: server-1 ]", gcpResourceException.getMessage());
}
Also used : GoogleJsonResponseException(com.google.api.client.googleapis.json.GoogleJsonResponseException) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) Test(org.junit.jupiter.api.Test)

Example 12 with DatabaseServer

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseServer 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 13 with DatabaseServer

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseServer 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 14 with DatabaseServer

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseServer in project cloudbreak by hortonworks.

the class AwsRdsLaunchServiceTest method createDatabaseStack.

private DatabaseStack createDatabaseStack(boolean useSslEnforcement, boolean sslCertificateIdentifierDefined) {
    Subnet subnet = new Subnet(SUBNET_CIDR);
    Network network = new Network(subnet, List.of(NETWORK_CIDR), OutboundInternetTraffic.ENABLED);
    network.putParameter("subnetId", SUBNET_ID);
    network.putParameter("vpcCidr", NETWORK_CIDR);
    Security security = new Security(Collections.emptyList(), List.of(SECURITY_GROUP_ID));
    DatabaseServer databaseServer = DatabaseServer.builder().useSslEnforcement(useSslEnforcement).params(sslCertificateIdentifierDefined ? Map.of(DatabaseServer.SSL_CERTIFICATE_IDENTIFIER, SSL_CERTIFICATE_IDENTIFIER) : Map.of()).security(security).build();
    return new DatabaseStack(network, databaseServer, Collections.emptyMap(), null);
}
Also used : Network(com.sequenceiq.cloudbreak.cloud.model.Network) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Security(com.sequenceiq.cloudbreak.cloud.model.Security)

Example 15 with DatabaseServer

use of com.sequenceiq.cloudbreak.cloud.model.DatabaseServer in project cloudbreak by hortonworks.

the class AwsResourceConnectorTest method terminateDatabaseServerWithDeleteProtectionTest.

@Test
void terminateDatabaseServerWithDeleteProtectionTest() throws Exception {
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    when(dbStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn(DB_INSTANCE_IDENTIFIER);
    PersistenceNotifier persistenceNotifier = mock(PersistenceNotifier.class);
    DescribeDBInstancesResult describeDBInstancesResult = mock(DescribeDBInstancesResult.class);
    when(awsRdsStatusLookupService.getDescribeDBInstancesResultForDeleteProtection(any(), any())).thenReturn(describeDBInstancesResult);
    when(awsRdsStatusLookupService.isDeleteProtectionEnabled(describeDBInstancesResult)).thenReturn(true);
    underTest.terminateDatabaseServer(authenticatedContext, dbStack, Collections.emptyList(), persistenceNotifier, true);
    verify(awsRdsModifyService, times(1)).disableDeleteProtection(any(), any());
    verify(awsRdsTerminateService, times(1)).terminate(authenticatedContext, dbStack, true, persistenceNotifier, Collections.emptyList());
}
Also used : PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) DescribeDBInstancesResult(com.amazonaws.services.rds.model.DescribeDBInstancesResult) Test(org.junit.jupiter.api.Test)

Aggregations

DatabaseServer (com.sequenceiq.cloudbreak.cloud.model.DatabaseServer)36 Test (org.junit.jupiter.api.Test)27 DatabaseStack (com.sequenceiq.cloudbreak.cloud.model.DatabaseStack)23 AuthenticatedContext (com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext)18 CloudCredential (com.sequenceiq.cloudbreak.cloud.model.CloudCredential)17 SQLAdmin (com.google.api.services.sqladmin.SQLAdmin)16 DatabaseInstance (com.google.api.services.sqladmin.model.DatabaseInstance)13 InstancesListResponse (com.google.api.services.sqladmin.model.InstancesListResponse)13 PersistenceNotifier (com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 Network (com.sequenceiq.cloudbreak.cloud.model.Network)8 ExternalDatabaseStatus (com.sequenceiq.cloudbreak.cloud.model.ExternalDatabaseStatus)7 Subnet (com.sequenceiq.cloudbreak.cloud.model.Subnet)7 HashMap (java.util.HashMap)7 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)6 Settings (com.google.api.services.sqladmin.model.Settings)5 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)5 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)4 Compute (com.google.api.services.compute.Compute)4