use of com.google.api.services.sqladmin.model.Operation in project cloudbreak by hortonworks.
the class GcpDatabaseServerBaseServiceTest method testVerifyOperation.
@Test
public void testVerifyOperation() {
Operation operation = mock(Operation.class);
OperationErrors operationErrors = mock(OperationErrors.class);
OperationError operationError = mock(OperationError.class);
CloudResource cloudResource = mock(CloudResource.class);
when(operation.getError()).thenReturn(operationErrors);
when(cloudResource.getName()).thenReturn("google-resource");
when(operationErrors.isEmpty()).thenReturn(false);
when(operationError.getMessage()).thenReturn("error1");
when(operationErrors.getErrors()).thenReturn(List.of(operationError));
GcpResourceException gcpResourceException = assertThrows(GcpResourceException.class, () -> underTest.verifyOperation(operation, List.of(cloudResource)));
Assert.assertEquals("Failed to create Database: error1,: [ resourceType: GCP_DATABASE, resourceName: google-resource ]", gcpResourceException.getMessage());
}
use of com.google.api.services.sqladmin.model.Operation in project cloudbreak by hortonworks.
the class GcpDatabaseServerLaunchServiceTest method testLaunchWhenDatabaseNOTAlreadyExistAndUserCreateThrowExceptionShouldThrowGcpResourceException.
@Test
public void testLaunchWhenDatabaseNOTAlreadyExistAndUserCreateThrowExceptionShouldThrowGcpResourceException() throws Exception {
Network network = new Network(new Subnet("10.0.0.0/16"), Map.of("subnetId", "s-1", "availabilityZone", "a"));
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");
GoogleJsonResponseException googleJsonResponseException = mock(GoogleJsonResponseException.class);
GoogleJsonError googleJsonError = mock(GoogleJsonError.class);
when(googleJsonResponseException.getDetails()).thenReturn(googleJsonError);
when(googleJsonError.getMessage()).thenReturn("error");
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(), any(DatabaseInstance.class))).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()).thenThrow(googleJsonResponseException);
when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
when(cloudContext.getLocation()).thenReturn(location(region("region"), availabilityZone("az1")));
GcpResourceException gcpResourceException = assertThrows(GcpResourceException.class, () -> underTest.launch(authenticatedContext, databaseStack, persistenceNotifier));
Assert.assertEquals("error: [ resourceType: GCP_DATABASE, resourceName: driver ]", gcpResourceException.getMessage());
}
use of com.google.api.services.sqladmin.model.Operation 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());
}
use of com.google.api.services.sqladmin.model.Operation in project cloudbreak by hortonworks.
the class GcpDatabaseResourceCheckerTest method testCheckWhenOperationSdkCallReturnWithOperationThatContainsErrorShouldThrowRetryableException.
@Test
void testCheckWhenOperationSdkCallReturnWithOperationThatContainsErrorShouldThrowRetryableException() throws IOException {
Operation operation = new Operation();
OperationError operationError = new OperationError();
operationError.setCode("UnExpectedErrorCode");
operationError.setMessage("An unexpected error happened on our API...");
operation.setError(new OperationErrors().setErrors(List.of(operationError)));
when(sqlAdminGetOperation.execute()).thenReturn(operation);
Assertions.assertThrows(CloudConnectorException.class, () -> underTest.check(sqlAdmin, authenticatedContext, "anOperationId"));
}
use of com.google.api.services.sqladmin.model.Operation 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();
}
Aggregations