Search in sources :

Example 16 with InstancesListResponse

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();
}
Also used : GcpDatabaseServerView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView) 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) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 17 with InstancesListResponse

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);
    }
}
Also used : GcpDatabaseServerView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView) InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) CloudConnectorException(com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException) IOException(java.io.IOException) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) TokenResponseException(com.google.api.client.auth.oauth2.TokenResponseException)

Aggregations

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