Search in sources :

Example 1 with Operation

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

use of com.google.api.services.sqladmin.model.Operation 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 3 with Operation

use of com.google.api.services.sqladmin.model.Operation in project cloudbreak by hortonworks.

the class GcpDatabaseServerLaunchServiceTest method testLaunchWhenDatabaseNOTAlreadyExistAndNOTSharedProjectIdShouldCreate.

@Test
public void testLaunchWhenDatabaseNOTAlreadyExistAndNOTSharedProjectIdShouldCreate() throws Exception {
    Network network = new Network(new Subnet("10.0.0.0/16"), Map.of("subnetId", "s-1", "availabilityZone", "a"));
    Map<String, Object> map = new HashMap<>();
    map.put("engineVersion", "1");
    map.put("key", "value");
    DatabaseServer databaseServer = DatabaseServer.builder().connectionDriver("driver").serverId("driver").connectorJarUrl("driver").engine(DatabaseEngine.POSTGRESQL).location("location").port(99).storageSize(50L).rootUserName("rootUserName").rootPassword("rootPassword").flavor("flavor").useSslEnforcement(true).params(map).build();
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    PersistenceNotifier persistenceNotifier = mock(PersistenceNotifier.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    Compute compute = mock(Compute.class);
    DatabaseInstance databaseInstance = mock(DatabaseInstance.class);
    Compute.Subnetworks subnetworks = mock(Compute.Subnetworks.class);
    Compute.Subnetworks.Get subnetworksGet = mock(Compute.Subnetworks.Get.class);
    SQLAdmin.Users users = mock(SQLAdmin.Users.class);
    SQLAdmin.Users.Insert usersInsert = mock(SQLAdmin.Users.Insert.class);
    SQLAdmin.Instances.Get instancesGet = mock(SQLAdmin.Instances.Get.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
    Operation operation = mock(Operation.class);
    SQLAdmin.Instances.Insert sqlAdminInstancesInsert = mock(SQLAdmin.Instances.Insert.class);
    IpMapping ipMapping = new IpMapping();
    ipMapping.setIpAddress("10.0.0.0");
    ipMapping.setType("PRIVATE");
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseStack.getNetwork()).thenReturn(network);
    when(gcpLabelUtil.createLabelsFromTagsMap(anyMap())).thenReturn(new HashMap<>());
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpComputeFactory.buildCompute(any(CloudCredential.class))).thenReturn(compute);
    when(compute.subnetworks()).thenReturn(subnetworks);
    when(subnetworks.get(anyString(), anyString(), anyString())).thenReturn(subnetworksGet);
    when(subnetworksGet.execute()).thenReturn(new Subnetwork());
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
    when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
    when(sqlAdminInstancesList.execute()).thenReturn(instancesListResponse);
    when(instancesListResponse.getItems()).thenReturn(List.of());
    when(databaseInstance.getName()).thenReturn("name");
    when(sqlAdminInstances.get(anyString(), anyString())).thenReturn(instancesGet);
    when(instancesGet.execute()).thenReturn(databaseInstance);
    when(sqlAdminInstances.insert(anyString(), databaseInstanceArgumentCaptor.capture())).thenReturn(sqlAdminInstancesInsert);
    when(sqlAdminInstancesInsert.setPrettyPrint(anyBoolean())).thenReturn(sqlAdminInstancesInsert);
    when(sqlAdminInstancesInsert.execute()).thenReturn(operation);
    when(databaseInstance.getIpAddresses()).thenReturn(List.of(ipMapping));
    doNothing().when(databasePollerService).launchDatabasePoller(any(AuthenticatedContext.class), anyList());
    when(sqlAdmin.users()).thenReturn(users);
    when(users.insert(anyString(), anyString(), any(User.class))).thenReturn(usersInsert);
    when(usersInsert.execute()).thenReturn(operation);
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location(region("region"), availabilityZone("az1")));
    List<CloudResource> launch = underTest.launch(authenticatedContext, databaseStack, persistenceNotifier);
    Assert.assertEquals("value", databaseInstanceArgumentCaptor.getValue().getDiskEncryptionConfiguration().getKmsKeyName());
    Assert.assertEquals(1, launch.size());
}
Also used : User(com.google.api.services.sqladmin.model.User) HashMap(java.util.HashMap) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Operation(com.google.api.services.sqladmin.model.Operation) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) Network(com.sequenceiq.cloudbreak.cloud.model.Network) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) Subnetwork(com.google.api.services.compute.model.Subnetwork) CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) IpMapping(com.google.api.services.sqladmin.model.IpMapping) Compute(com.google.api.services.compute.Compute) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Subnet(com.sequenceiq.cloudbreak.cloud.model.Subnet) Test(org.junit.jupiter.api.Test)

Example 4 with Operation

use of com.google.api.services.sqladmin.model.Operation in project cloudbreak by hortonworks.

the class GcpDatabaseResourceCheckerTest method testCheckWhenSdkCallSucceedsShouldReturnWithTheOperation.

@Test
void testCheckWhenSdkCallSucceedsShouldReturnWithTheOperation() throws IOException {
    Operation operation = new Operation();
    when(sqlAdminGetOperation.execute()).thenReturn(operation);
    Operation result = underTest.check(sqlAdmin, authenticatedContext, "anOperationId");
    Assertions.assertEquals(operation, result);
}
Also used : Operation(com.google.api.services.sqladmin.model.Operation) Test(org.junit.jupiter.api.Test)

Example 5 with Operation

use of com.google.api.services.sqladmin.model.Operation in project cloudbreak by hortonworks.

the class GcpDatabaseServerTerminateServiceTest method testTerminateWhenDatabaseIsPresentedShouldDeleteDatabase.

@Test
public void testTerminateWhenDatabaseIsPresentedShouldDeleteDatabase() throws Exception {
    AuthenticatedContext authenticatedContext = mock(AuthenticatedContext.class);
    CloudCredential cloudCredential = mock(CloudCredential.class);
    DatabaseStack databaseStack = mock(DatabaseStack.class);
    DatabaseServer databaseServer = mock(DatabaseServer.class);
    PersistenceNotifier persistenceNotifier = mock(PersistenceNotifier.class);
    SQLAdmin sqlAdmin = mock(SQLAdmin.class);
    SQLAdmin.Instances sqlAdminInstances = mock(SQLAdmin.Instances.class);
    SQLAdmin.Instances.List sqlAdminInstancesList = mock(SQLAdmin.Instances.List.class);
    InstancesListResponse instancesListResponse = mock(InstancesListResponse.class);
    when(authenticatedContext.getCloudCredential()).thenReturn(cloudCredential);
    when(cloudCredential.getName()).thenReturn("credential");
    when(databaseStack.getDatabaseServer()).thenReturn(databaseServer);
    when(databaseServer.getServerId()).thenReturn("server-1");
    when(gcpSQLAdminFactory.buildSQLAdmin(any(CloudCredential.class), anyString())).thenReturn(sqlAdmin);
    when(gcpStackUtil.getProjectId(any(CloudCredential.class))).thenReturn("project-id");
    when(sqlAdmin.instances()).thenReturn(sqlAdminInstances);
    when(sqlAdminInstances.list(anyString())).thenReturn(sqlAdminInstancesList);
    when(sqlAdminInstancesList.execute()).thenReturn(instancesListResponse);
    DatabaseInstance databaseInstance = new DatabaseInstance();
    databaseInstance.setName("server-1");
    databaseInstance.setState("RUNNABLE");
    Settings settings = new Settings();
    settings.setActivationPolicy("ALWAYS");
    databaseInstance.setSettings(settings);
    when(instancesListResponse.getItems()).thenReturn(List.of(databaseInstance));
    SQLAdmin.Instances.Delete delete = mock(SQLAdmin.Instances.Delete.class);
    Operation operation = mock(Operation.class);
    when(sqlAdminInstances.delete(anyString(), anyString())).thenReturn(delete);
    when(delete.execute()).thenReturn(operation);
    when(delete.setPrettyPrint(anyBoolean())).thenReturn(delete);
    when(operation.getError()).thenReturn(null);
    when(authenticatedContext.getCloudContext()).thenReturn(cloudContext);
    when(cloudContext.getLocation()).thenReturn(location(region("region"), availabilityZone("az1")));
    doNothing().when(databasePollerService).terminateDatabasePoller(any(AuthenticatedContext.class), anyList());
    List<CloudResource> terminate = underTest.terminate(authenticatedContext, databaseStack, persistenceNotifier);
    Assert.assertEquals(1, terminate.size());
    Assert.assertEquals("server-1", terminate.get(0).getName());
}
Also used : CloudCredential(com.sequenceiq.cloudbreak.cloud.model.CloudCredential) DatabaseStack(com.sequenceiq.cloudbreak.cloud.model.DatabaseStack) AuthenticatedContext(com.sequenceiq.cloudbreak.cloud.context.AuthenticatedContext) Operation(com.google.api.services.sqladmin.model.Operation) SQLAdmin(com.google.api.services.sqladmin.SQLAdmin) DatabaseInstance(com.google.api.services.sqladmin.model.DatabaseInstance) InstancesListResponse(com.google.api.services.sqladmin.model.InstancesListResponse) PersistenceNotifier(com.sequenceiq.cloudbreak.cloud.notification.PersistenceNotifier) DatabaseServer(com.sequenceiq.cloudbreak.cloud.model.DatabaseServer) CloudResource(com.sequenceiq.cloudbreak.cloud.model.CloudResource) Settings(com.google.api.services.sqladmin.model.Settings) Test(org.junit.jupiter.api.Test)

Aggregations

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