use of com.google.api.services.sqladmin.model.InstancesListResponse 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();
}
use of com.google.api.services.sqladmin.model.InstancesListResponse 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);
}
}
Aggregations