All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.sap.cloud.mt.subscription.InstanceLifecycleManagerImpl Maven / Gradle / Ivy

There is a newer version: 3.3.3
Show newest version
/******************************************************************************
 * © 2020 SAP SE or an SAP affiliate company. All rights reserved.            *
 ******************************************************************************/
package com.sap.cloud.mt.subscription;

import com.sap.cloud.mt.subscription.exceptions.InternalError;
import com.sap.cloud.mt.subscription.exceptions.UnknownTenant;
import com.sap.xsa.core.instancemanager.client.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;


public class InstanceLifecycleManagerImpl implements InstanceLifecycleManager {
    private static final Logger logger = LoggerFactory.getLogger(InstanceLifecycleManagerImpl.class);
    private final InstanceManagerClient instanceManagerClient;
    private final int timeout;

    public InstanceLifecycleManagerImpl(InstanceManagerClient instanceManagerClient, int timeout) {
        this.instanceManagerClient = instanceManagerClient;
        this.timeout = timeout;
    }

    @Override
    public void createNewInstance(String tenantId, InstanceCreationOptions instanceCreationOptions) throws InternalError {
        try {
            instanceManagerClient.createManagedInstance(tenantId, instanceCreationOptions, timeout);
        } catch (ImClientException e) {
            logger.error("Could not create HDI container. Error is: {}", e.getMessage());
            throw new InternalError(e);
        }
    }

    @Override
    public void deleteInstance(String tenantId) throws InternalError, UnknownTenant {
        checkThatTenantExists(tenantId);
        try {
            instanceManagerClient.deleteManagedInstance(tenantId, timeout);
        } catch (ImClientException e) {
            logger.error("Could not delete HDI container. Error is: {}", e.getMessage());
            throw new InternalError(e);
        }
    }

    @Override
    public DataSourceInfo getDataSourceInfo(String tenantId, boolean forceCacheUpdate) throws InternalError, UnknownTenant {
        ManagedServiceInstance instance = null;
        try {
            instance = instanceManagerClient.getManagedInstance(tenantId, forceCacheUpdate);
            if (instance == null) throw new UnknownTenant("Tenant" + tenantId + "is not known");
        } catch (ImClientException e) {
            logger.error("Could not get HDI container information. Error is: {}", e.getMessage());
            throw new UnknownTenant("Tenant" + tenantId + "is not known");
        }
        if (getContainerStatus(instance.getStatus()) != ContainerStatus.OK) {
            logger.error("Database for tenant {} has a wrong status {}", tenantId, instance.getStatus());
            throw new InternalError("Database for tenant " + tenantId + " has a wrong status " + instance.getStatus());
        }
        DataSourceInfo dataSourceInfo = new DataSourceInfo();
        dataSourceInfo.setHost((String) instance.getCredentials().get("host"));
        dataSourceInfo.setPort((String) instance.getCredentials().get("port"));
        dataSourceInfo.setDriver((String) instance.getCredentials().get("driver"));
        dataSourceInfo.setUrl((String) instance.getCredentials().get("url"));
        dataSourceInfo.setSchema((String) instance.getCredentials().get("schema"));
        dataSourceInfo.setHdiUser((String) instance.getCredentials().get("hdi_user"));
        dataSourceInfo.setHdiPassword((String) instance.getCredentials().get("hdi_password"));
        dataSourceInfo.setUser((String) instance.getCredentials().get("user"));
        dataSourceInfo.setPassword((String) instance.getCredentials().get("password"));
        dataSourceInfo.setCertificate((String) instance.getCredentials().get("certificate"));
        dataSourceInfo.setTenantId(tenantId);
        return dataSourceInfo;
    }

    @Override
    public ContainerStatus getContainerStatus(String tenantId) throws InternalError {
        try {
            ManagedServiceInstance instance = instanceManagerClient.getManagedInstance(tenantId, true);
            if (instance == null) return ContainerStatus.DOES_NOT_EXIST;
            OperationStatus status = instance.getStatus();
            return getContainerStatus(status);
        } catch (ImClientException e) {
            throw new InternalError(e);
        }
    }

    private ContainerStatus getContainerStatus(OperationStatus status) throws InternalError {
        switch (status) {
            case CREATION_SUCCEEDED:
            case UPDATE_SUCCEEDED:
            case UPDATE_FAILED:
                return ContainerStatus.OK;
            case CREATION_IN_PROGRESS:
            case UPDATE_IN_PROGRESS:
            case DELETION_IN_PROGRESS:
                return ContainerStatus.IN_PROGRESS;
            case DELETION_FAILED:
                return ContainerStatus.ERRONEOUS;
            case CREATION_FAILED:
                return ContainerStatus.CREATION_ERROR;
            default:
                logger.error("Undefined status {}", status);
                throw new InternalError("Unknown status");
        }
    }

    @Override
    public Set getAllTenants() throws InternalError {
        List instances = null;
        try {
            instances = instanceManagerClient.getManagedInstances();
        } catch (ImClientException e) {
            logger.error("Could not get list of HDI containers. Error is: {}", e.getMessage());
            throw new InternalError(e);
        }
        return instances.stream().filter(i -> i.getStatus() == OperationStatus.CREATION_SUCCEEDED)
                .map(i -> i.getId()).filter(FilterTenants.externalTenants()).collect(Collectors.toSet());
    }

    @Override
    public void checkThatTenantExists(String tenantId) throws UnknownTenant {
        try {
            ManagedServiceInstance instance = instanceManagerClient.getManagedInstance(tenantId);
            if (instance == null) throw new UnknownTenant("Tenant " + tenantId + " is not known");
        } catch (ImClientException e) {
            throw new UnknownTenant(e, "Tenant " + tenantId + " is not known");
        }

    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy