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

com.spotinst.sdkjava.model.SpotOceanAzureAksClusterClient 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.aks.*;
import com.spotinst.sdkjava.model.requests.ocean.aks.GetAksClusterNodesRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.List;

public class SpotOceanAzureAksClusterClient {
    
    private static final Logger LOGGER = LoggerFactory.getLogger(SpotOceanAzureAksClusterClient.class);
    private              String                                    authToken;
    private              String                                    account;
    private              ISpotOceanAzureAksClusterRepo             spotOceanAzureAksClusterRepo;


    public ISpotOceanAzureAksClusterRepo getSpotOceanAzureAksClusterRepo() {
        return spotOceanAzureAksClusterRepo;
    }

    public void setSpotOceanAzureAksClusterRepo() {
        this.spotOceanAzureAksClusterRepo = SpotinstRepoManager.getInstance().getSpotOceanAzureAksClusterRepo();
    }

    //Constructor
    public SpotOceanAzureAksClusterClient(String authToken, String account) {
        this.authToken = authToken;
        this.account = account;
        setSpotOceanAzureAksClusterRepo();
    }


    public OceanClusterAks createAksCluster(AksClusterCreationRequest oceanClusterCreationRequest) {

        OceanClusterAks retVal;
        OceanClusterAks clusterToCreate = oceanClusterCreationRequest.getCluster();

        RepoGenericResponse creationResponse = getSpotOceanAzureAksClusterRepo().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 cluster. Code: %s. Message: %s.",
                            httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }
        return retVal;
    }

    public Boolean updateAksCluster(AksClusterUpdateRequest aksClusterUpdateRequest, String clusterId) {
        Boolean retVal;

        OceanClusterAks clusterToUpdate = aksClusterUpdateRequest.getCluster();
        RepoGenericResponse updateResponse =
                getSpotOceanAzureAksClusterRepo().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 Boolean deleteAksCluster(AksClusterDeleteRequest clusterDeletionRequest) {
        Boolean                      retVal;
        String                       clusterIdToDelete       = clusterDeletionRequest.getClusterId();
        RepoGenericResponse clusterDeletionResponse =
                getSpotOceanAzureAksClusterRepo().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 cluster. Code: %s. Message: %s.",
                            httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        return retVal;
    }

    public OceanClusterAks getOceanAksCluster(AksClusterGetRequest aksClusterGetRequest) {
        OceanClusterAks retVal;
        String          clusterToGet = aksClusterGetRequest.getClusterId();
        RepoGenericResponse clusterRes =
                getSpotOceanAzureAksClusterRepo().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 cluster. Code: %s. Message: %s.",
                            httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        return retVal;
    }

    public List ListOceanAksCluster() {
        List retVal;

        ISpotOceanAzureAksClusterRepo repo         = getSpotOceanAzureAksClusterRepo();
        RepoGenericResponse> clustersRepoGenericResponse =
                repo.getAll(null, authToken, account);
        if (clustersRepoGenericResponse.isRequestSucceed()) {
            retVal = clustersRepoGenericResponse.getValue();
        }
        else {
            List httpExceptions = clustersRepoGenericResponse.getHttpExceptions();
            HttpError       httpException  = httpExceptions.get(0);
            LOGGER.error(
                    String.format("Error encountered while attempting to List all Ocean clusters. Code: %s. Message: %s.",
                            httpException.getCode(), httpException.getMessage()));
            throw new SpotinstHttpException(httpException.getMessage());
        }

        System.out.println("GET- List All Ocean clusters:");

        if (retVal.size() == 0){ // No groups for this account
            System.out.println("No cluster found for this account");
        }

        for (int i = 0; i < retVal.size(); i++) {
            System.out.println("Cluster number:" + i + "   " + retVal.get(i).getName() + "   " + retVal.get(i).getControllerClusterId());
        }

        return retVal;
    }

    public GetAzureAksClusterHeartBeatStatusResponse getAzureAksClusterHeartBeatStatus(String clusterId) {

        GetAzureAksClusterHeartBeatStatusResponse getK8sClusterHeartBeatStatus;

        RepoGenericResponse getK8sClusterHeartBeatStatusResponse =
                getSpotOceanAzureAksClusterRepo().getAzureAksClusterHeartBeatStatus(clusterId, authToken, account);

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

        return getK8sClusterHeartBeatStatus;

    }

    public List getClusterNodes(GetAksClusterNodesRequest getNodesRequest, String clusterId) {
        List retVal;
        RepoGenericResponse> getNodesResponse = getSpotOceanAzureAksClusterRepo().getClusterNodes(getNodesRequest, clusterId, authToken);

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

        return retVal;
    }

    public List detachVms(AksDetachInstances instances, String clusterId) {
        List retVal;
        RepoGenericResponse> detachInstancesResponse = getSpotOceanAzureAksClusterRepo().detachVms(instances, clusterId, authToken, account);

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

        return retVal;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy