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

com.spotinst.sdkjava.model.SpotOceanGkeClusterClient Maven / Gradle / Ivy

There is a newer version: 1.0.121
Show newest version
package com.spotinst.sdkjava.model;

import com.spotinst.sdkjava.exception.HttpError;
import com.spotinst.sdkjava.exception.SpotinstHttpException;
import com.spotinst.sdkjava.model.bl.ocean.gke.LaunchSpecSpecification;
import com.spotinst.sdkjava.model.bl.ocean.gke.OceanGkeCluster;
import com.spotinst.sdkjava.model.requests.ocean.gke.LaunchSpecRequest;
import com.spotinst.sdkjava.model.requests.ocean.gke.OceanGkeClusterRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

public class SpotOceanGkeClusterClient {

    private static final Logger                      LOGGER =
            LoggerFactory.getLogger(SpotOceanGkeClusterClient.class);
    //region Members
    private              String                      authToken;
    private              String                      account;
    private              ISpotOceanGKELaunchSpecRepo spotOceanGkeClusterLaunchSpecRepo;
    private              ISpotOceanGkeClusterRepo    spotOceanGkeClusterRepo;

    public SpotOceanGkeClusterClient(String authToken, String account) {

        this.authToken = authToken;
        this.account = account;
        this.spotOceanGkeClusterLaunchSpecRepo = SpotinstRepoManager.getInstance().getSpotinstOceanGKELaunchSpecRepoGcp();
        this.spotOceanGkeClusterRepo = SpotinstRepoManager.getInstance().getSpotinstOceanGkeClusterRepo();
    }

    private ISpotOceanGkeClusterRepo getSpotOceanGkeClusterRepo() {
        return spotOceanGkeClusterRepo;
    }

    public LaunchSpecSpecification createLaunchSpec(LaunchSpecRequest launchSpecRequest){
        LaunchSpecSpecification               retVal;
        LaunchSpecSpecification
                                              oceanGKEUpdateLaunchSpecRes = launchSpecRequest
                .getOceanGKECreateLaunchSpecRes();
        RepoGenericResponse creationResponse    =
                spotOceanGkeClusterLaunchSpecRepo.create(oceanGKEUpdateLaunchSpecRes, authToken, account);

        if (creationResponse.isRequestSucceed()) {
            retVal = creationResponse.getValue();
        }
        else {
            List httpExceptions = creationResponse.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(
                    String.format("Error encountered while attempting to create a launch spec. Code: %s. Message: %s.",
                                  httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        return retVal;

    }

    public List getAllLaunchSpec(String oceanId){
        List     retVal;
        RepoGenericResponse> getAllResponse    =
                spotOceanGkeClusterLaunchSpecRepo.getAllStatus(authToken,account,oceanId);

        if (getAllResponse.isRequestSucceed()) {
            retVal = getAllResponse.getValue();
        }
        else {
            List httpExceptions = getAllResponse.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(
                    String.format("Error encountered while attempting to list all Ocean GKE launch spec. Code: %s. Message: %s.",
                                  httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }
        return retVal;
    }

    public LaunchSpecSpecification getLaunchSpec(LaunchSpecRequest launchSpecRequest){
        LaunchSpecSpecification     retVal;
        String                     launchSpecId = launchSpecRequest.getLaunchSpecId();
        RepoGenericResponse getResponse    =
                spotOceanGkeClusterLaunchSpecRepo.get(launchSpecId,authToken,account);
        if (getResponse.isRequestSucceed()) {
            retVal = getResponse.getValue();
        }
        else {
            List httpExceptions = getResponse.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(
                    String.format("Error encountered while attempting to fetch Ocean GKE  launch spec. Code: %s. Message: %s.",
                                  httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }
        return retVal;
    }

    public Boolean updateLaunchSpec(LaunchSpecRequest updateLaunchSpecRequest, String launchSpecId){

        Boolean                               retVal;
        LaunchSpecSpecification oceanGKEUpdateLaunchSpecRes = updateLaunchSpecRequest.getOceanGKECreateLaunchSpecRes();
        RepoGenericResponse updateResponse    =
                spotOceanGkeClusterLaunchSpecRepo.update(launchSpecId,oceanGKEUpdateLaunchSpecRes, authToken, account);

        if (updateResponse.isRequestSucceed()) {
            retVal = updateResponse.getValue();
        }
        else {
            List httpExceptions = updateResponse.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(
                    String.format("Error encountered while attempting to update a launch Spec. Code: %s. Message: %s.",
                                  httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        return retVal;
    }

    public Boolean deleteALaunchSpec(LaunchSpecRequest launchSpecDeleteRequest){

        Boolean retVal;
        String  launchSpecId = launchSpecDeleteRequest.getLaunchSpecId();
        RepoGenericResponse deleteResponse =
                spotOceanGkeClusterLaunchSpecRepo.delete(launchSpecId, authToken, account);

        if (deleteResponse.isRequestSucceed()) {
            retVal = deleteResponse.getValue();
        }
        else {
            List httpExceptions = deleteResponse.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(
                    String.format("Error encountered while attempting to delete a launch Spec. Code: %s. Message: %s.",
                                  httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        return retVal;
    }

    public  OceanGkeCluster createOceanGkeCluster(OceanGkeClusterRequest oceanEcsClusterCreationRequest){
        OceanGkeCluster retVal;
        OceanGkeCluster clusterToCreate = oceanEcsClusterCreationRequest.getCluster();

        RepoGenericResponse creationResponse = getSpotOceanGkeClusterRepo().create(clusterToCreate, authToken, account);

        if (creationResponse.isRequestSucceed()) {
            retVal = creationResponse.getValue();
        }
        else {
            List httpExceptions = creationResponse.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(String.format("Error encountered while attempting to create ocean gke cluster. Code: %s. Message: %s.",
                                       httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        return retVal;
    }

    public Boolean updateGkeCluster(OceanGkeClusterRequest EcsClusterUpdateRequest, String clusterId) {
        Boolean retVal;

        OceanGkeCluster clusterToUpdate = EcsClusterUpdateRequest.getCluster();
        RepoGenericResponse updateResponse =
                getSpotOceanGkeClusterRepo().update(clusterId, clusterToUpdate, authToken, account);

        if (updateResponse.isRequestSucceed()) {
            retVal = updateResponse.getValue();
        }
        else {
            List httpExceptions = updateResponse.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(String.format("Error encountered while attempting to update cluster. Code: %s. Message: %s.",
                                       httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        return retVal;
    }

    public OceanGkeCluster getOceanGkeCluster(OceanGkeClusterRequest oceanEcsClusterGetRequest) {
        OceanGkeCluster retVal;

        String          clusterToGet = oceanEcsClusterGetRequest.getClusterId();
        RepoGenericResponse clusterRes =
                getSpotOceanGkeClusterRepo().get(clusterToGet, authToken, account);

        if (clusterRes.isRequestSucceed()) {
            retVal = clusterRes.getValue();
        }
        else {
            List httpExceptions = clusterRes.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(
                    String.format("Error encountered while attempting to get ocean gke cluster. Code: %s. Message: %s.",
                                  httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        return retVal;
    }

    public Boolean deleteGkeCluster(OceanGkeClusterRequest clusterDeletionRequest) {
        Boolean                      retVal;
        String                       clusterIdToDelete       = clusterDeletionRequest.getClusterId();
        RepoGenericResponse clusterDeletionResponse =
                getSpotOceanGkeClusterRepo().delete(clusterIdToDelete, authToken, account);

        if (clusterDeletionResponse.isRequestSucceed()) {
            retVal = clusterDeletionResponse.getValue();
        }
        else {
            List httpExceptions = clusterDeletionResponse.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(String.format("Error encountered while attempting to delete ocean gke cluster. Code: %s. Message: %s.",
                                       httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        return retVal;
    }

    public List getAllOceanGkeClusters() {
        List retVal;

        RepoGenericResponse> clusterRes = getSpotOceanGkeClusterRepo().getAll(null, authToken, account);

        if (clusterRes.isRequestSucceed()) {
            retVal = clusterRes.getValue();
        }
        else {
            List httpExceptions = clusterRes.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(
                    String.format("Error encountered while attempting to get ocean cluster. Code: %s. Message: %s.",
                                  httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        return retVal;
    }

}





© 2015 - 2024 Weber Informatics LLC | Privacy Policy