Search in sources :

Example 1 with GcpDatabaseServerView

use of com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView in project cloudbreak by hortonworks.

the class AbstractGcpDatabaseServerStartStopService method startStop.

protected void startStop(AuthenticatedContext ac, DatabaseStack stack, DatabasePollerService databasePollerService, String policy) throws IOException {
    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> gcpDatabase = getGcpDatabase(stack, ac.getCloudContext().getLocation().getAvailabilityZone().value());
    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()) {
            try {
                Operation operation = sqlAdmin.instances().patch(projectId, deploymentName, getDatabaseInstance(policy)).execute();
                verifyOperation(operation, gcpDatabase);
                CloudResource operationAwareCloudResource = createOperationAwareCloudResource(gcpDatabase.get(0), operation);
                databasePollerService.startDatabasePoller(ac, List.of(operationAwareCloudResource));
            } catch (GoogleJsonResponseException e) {
                throw new GcpResourceException(checkException(e), resourceType(), gcpDatabase.get(0).getName());
            }
        } else {
            LOGGER.debug("Deployment does not exists: {}", deploymentName);
        }
    } catch (GoogleJsonResponseException e) {
        throw new GcpResourceException(checkException(e), resourceType(), gcpDatabase.get(0).getName());
    }
}
Also used : GcpDatabaseServerView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView) 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) Operation(com.google.api.services.sqladmin.model.Operation) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance)

Example 2 with GcpDatabaseServerView

use of com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView in project cloudbreak by hortonworks.

the class GcpDatabaseServerLaunchService method getDatabaseInstance.

private DatabaseInstance getDatabaseInstance(DatabaseStack stack, String deploymentName, Compute compute, String projectId) throws java.io.IOException {
    DatabaseServer databaseServer = stack.getDatabaseServer();
    GcpDatabaseServerView databaseServerView = new GcpDatabaseServerView(databaseServer);
    GcpDatabaseNetworkView databaseNetworkView = new GcpDatabaseNetworkView(stack.getNetwork());
    Subnetwork subnetworkForRedbeams;
    if (Strings.isNullOrEmpty(databaseNetworkView.getSharedProjectId())) {
        subnetworkForRedbeams = compute.subnetworks().get(projectId, databaseServerView.getLocation(), databaseNetworkView.getSubnetId()).execute();
    } else {
        subnetworkForRedbeams = compute.subnetworks().get(databaseNetworkView.getSharedProjectId(), databaseServerView.getLocation(), databaseNetworkView.getSubnetId()).execute();
    }
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setCurrentDiskSize(databaseServerView.getAllocatedStorageInMb());
    databaseInstance.setName(deploymentName);
    databaseInstance.setInstanceType("CLOUD_SQL_INSTANCE");
    databaseInstance.setBackendType("SECOND_GEN");
    databaseInstance.setRegion(databaseServerView.getLocation());
    databaseInstance.setRootPassword(databaseServerView.getAdminPassword());
    databaseInstance.setConnectionName(databaseServerView.getAdminLoginName());
    databaseInstance.setGceZone(databaseNetworkView.getAvailabilityZone());
    databaseInstance.setDatabaseVersion(databaseServerView.getDatabaseVersion());
    databaseInstance.setSettings(getSettings(stack, databaseServerView, subnetworkForRedbeams));
    String keyName = databaseServer.getStringParameter(VOLUME_ENCRYPTION_KEY_ID);
    if (keyName != null && !keyName.isEmpty()) {
        DiskEncryptionConfiguration diskEncryptionConfiguration = new DiskEncryptionConfiguration();
        diskEncryptionConfiguration.setKmsKeyName(keyName);
        databaseInstance.setDiskEncryptionConfiguration(diskEncryptionConfiguration);
    }
    return databaseInstance;
}
Also used : GcpDatabaseServerView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView) Subnetwork(com.google.api.services.compute.model.Subnetwork) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) DiskEncryptionConfiguration(com.google.api.services.sqladmin.model.DiskEncryptionConfiguration) GcpDatabaseNetworkView(com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseNetworkView) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance)

Example 3 with GcpDatabaseServerView

use of com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView in project cloudbreak by hortonworks.

the class GcpDatabaseServerLaunchService method launch.

public List<CloudResource> launch(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());
    Compute compute = gcpComputeFactory.buildCompute(ac.getCloudCredential());
    String projectId = gcpStackUtil.getProjectId(ac.getCloudCredential());
    List<CloudResource> buildableResource = new ArrayList<>();
    String availabilityZone = ac.getCloudContext().getLocation().getAvailabilityZone().value();
    buildableResource.add(getGcpDatabase(deploymentName, availabilityZone));
    buildableResource.add(getRdsPort(availabilityZone));
    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()) {
            DatabaseInstance databaseInstance = getDatabaseInstance(stack, deploymentName, compute, projectId);
            SQLAdmin.Instances.Insert insert = sqlAdmin.instances().insert(projectId, databaseInstance);
            insert.setPrettyPrint(Boolean.TRUE);
            try {
                Operation operation = insert.execute();
                verifyOperation(operation, buildableResource);
                CloudResource operationAwareCloudResource = createOperationAwareCloudResource(buildableResource.get(0), operation);
                databasePollerService.launchDatabasePoller(ac, List.of(operationAwareCloudResource));
                DatabaseInstance instance = sqlAdmin.instances().get(projectId, deploymentName).execute();
                if (instance != null) {
                    CloudResource.Builder rdsInstance = new CloudResource.Builder();
                    String instanceName = instance.getName();
                    buildableResource.add(getRdsHostName(instance, rdsInstance, instanceName, availabilityZone));
                    User rootUser = getRootUser(stack, projectId, instanceName);
                    operation = sqlAdmin.users().insert(projectId, instanceName, rootUser).execute();
                    verifyOperation(operation, buildableResource);
                    operationAwareCloudResource = createOperationAwareCloudResource(buildableResource.get(0), operation);
                    databasePollerService.insertUserPoller(ac, List.of(operationAwareCloudResource));
                }
                buildableResource.forEach(dbr -> resourceNotifier.notifyAllocation(dbr, ac.getCloudContext()));
                return Collections.singletonList(operationAwareCloudResource);
            } catch (GoogleJsonResponseException e) {
                throw new GcpResourceException(checkException(e), resourceType(), buildableResource.get(0).getName());
            }
        } else {
            LOGGER.debug("Deployment already exists: {}", deploymentName);
        }
    } 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) User(com.google.api.services.sqladmin.model.User) ArrayList(java.util.ArrayList) 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) Compute(com.google.api.services.compute.Compute) GcpResourceException(com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource)

Example 4 with GcpDatabaseServerView

use of com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView 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 5 with GcpDatabaseServerView

use of com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView 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

DatabaseInstance (com.google.api.services.sqladmin.model.DatabaseInstance)5 GcpDatabaseServerView (com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView)5 SQLAdmin (com.google.api.services.sqladmin.SQLAdmin)4 InstancesListResponse (com.google.api.services.sqladmin.model.InstancesListResponse)4 GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)3 Operation (com.google.api.services.sqladmin.model.Operation)3 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)3 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)3 TokenResponseException (com.google.api.client.auth.oauth2.TokenResponseException)1 Compute (com.google.api.services.compute.Compute)1 Subnetwork (com.google.api.services.compute.model.Subnetwork)1 DiskEncryptionConfiguration (com.google.api.services.sqladmin.model.DiskEncryptionConfiguration)1 User (com.google.api.services.sqladmin.model.User)1 CloudConnectorException (com.sequenceiq.cloudbreak.cloud.exception.CloudConnectorException)1 GcpDatabaseNetworkView (com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseNetworkView)1 DatabaseServer (com.sequenceiq.cloudbreak.cloud.model.DatabaseServer)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1