Search in sources :

Example 1 with User

use of com.google.api.services.sqladmin.model.User 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)

Aggregations

GoogleJsonResponseException (com.google.api.client.googleapis.json.GoogleJsonResponseException)1 Compute (com.google.api.services.compute.Compute)1 SQLAdmin (com.google.api.services.sqladmin.SQLAdmin)1 DatabaseInstance (com.google.api.services.sqladmin.model.DatabaseInstance)1 InstancesListResponse (com.google.api.services.sqladmin.model.InstancesListResponse)1 Operation (com.google.api.services.sqladmin.model.Operation)1 User (com.google.api.services.sqladmin.model.User)1 GcpResourceException (com.sequenceiq.cloudbreak.cloud.gcp.GcpResourceException)1 GcpDatabaseServerView (com.sequenceiq.cloudbreak.cloud.gcp.view.GcpDatabaseServerView)1 CloudResource (com.sequenceiq.cloudbreak.cloud.model.CloudResource)1 ArrayList (java.util.ArrayList)1