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());
}
}
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;
}
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();
}
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();
}
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);
}
}
Aggregations